31 lines
918 B
Python
31 lines
918 B
Python
from jsonfile import PROJECT_ROOT
|
|
|
|
STATIC_DIR = PROJECT_ROOT / "static"
|
|
IMAGE_EXTENSIONS = {".png", ".jpg", ".jpeg", ".gif", ".webp", ".svg", ".avif", ".ico"}
|
|
|
|
|
|
def site_path(file_path) -> str:
|
|
return "/" + file_path.relative_to(STATIC_DIR).as_posix()
|
|
|
|
|
|
def list_images() -> list[dict]:
|
|
if not STATIC_DIR.exists():
|
|
return []
|
|
images = []
|
|
for file_path in sorted(STATIC_DIR.rglob("*")):
|
|
if file_path.is_file() and file_path.suffix.lower() in IMAGE_EXTENSIONS:
|
|
images.append({"path": site_path(file_path), "size": file_path.stat().st_size})
|
|
return images
|
|
|
|
|
|
def image_exists(path: str) -> bool:
|
|
return (STATIC_DIR / path.lstrip("/")).is_file()
|
|
|
|
|
|
def format_size(size: int) -> str:
|
|
for unit in ["B", "KB", "MB"]:
|
|
if size < 1024:
|
|
return f"{size:.0f} {unit}" if unit == "B" else f"{size:.1f} {unit}"
|
|
size /= 1024
|
|
return f"{size:.1f} GB"
|