summaryrefslogtreecommitdiff
path: root/app/services/images.py
blob: 4938014e2a55091f59a95c5e1935679fbbbddbd8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
from pathlib import Path
from flask import Flask, current_app
import random

image_cache = set()

def cache_images(app: Flask):
    photos_dir = Path(f"{app.static_folder}/photos")
    for file_path in photos_dir.rglob("*"):
        if file_path.is_file():
            img_path = file_path.relative_to(app.static_folder)
            image_cache.add(str(img_path))

def get_all_images(current_app: Flask):
    # if not image_cache:
    cache_images(current_app)
    print(f"Returning {len(image_cache)} images")
    shuffled = random.sample(list(image_cache), k=len(image_cache))
    return shuffled