Mostly works! Image loading and caching seems slow
This commit is contained in:
116
src/rendering/helpers.py
Normal file
116
src/rendering/helpers.py
Normal file
@ -0,0 +1,116 @@
|
||||
from dataclasses import dataclass
|
||||
from src.config.config import Configuration
|
||||
from src.rendering import GENERIC_FILE_MAPPING
|
||||
from enum import Enum
|
||||
|
||||
from thumbhash import image_to_thumbhash
|
||||
from PIL import Image
|
||||
|
||||
|
||||
@dataclass
|
||||
class ImageMetadata:
|
||||
path: str
|
||||
width: int
|
||||
height: int
|
||||
alt: str
|
||||
thumbhash: str
|
||||
# exif attributes
|
||||
exif: dict
|
||||
|
||||
|
||||
@dataclass
|
||||
class FileMetadata:
|
||||
path: str
|
||||
first_hundred_chars: str
|
||||
|
||||
|
||||
@dataclass
|
||||
class TemplateFile:
|
||||
name: str
|
||||
extension: str
|
||||
categories: list[str]
|
||||
date_modified: str
|
||||
date_created: str
|
||||
size_kb: int
|
||||
metadata: ImageMetadata | FileMetadata | None
|
||||
dir_item_count: int
|
||||
is_dir: bool
|
||||
|
||||
|
||||
class TemplateHelpers:
|
||||
def __init__(self, config: Configuration):
|
||||
self.config: Configuration = config
|
||||
|
||||
def get_folder_contents(self, path: str = ""):
|
||||
"""Returns the contents of a folder as a list of TemplateFile objects
|
||||
|
||||
The metadata field is populated with the appropriate metadata object
|
||||
"""
|
||||
search_contnet_path = self.config.content_dir / path
|
||||
files = search_contnet_path.glob("*")
|
||||
ret = []
|
||||
for f in files:
|
||||
t = TemplateFile(
|
||||
name=f.name,
|
||||
extension=f.suffix.lower(),
|
||||
categories=[],
|
||||
date_modified=f.stat().st_mtime,
|
||||
date_created=f.stat().st_ctime,
|
||||
size_kb=f.stat().st_size / 1024,
|
||||
metadata=None,
|
||||
dir_item_count=len(list(f.glob("*"))) if f.is_dir() else 0,
|
||||
is_dir=f.is_dir(),
|
||||
)
|
||||
if f.is_file():
|
||||
# Build metadata depending on the mapping in GENERIC_FILE_MAPPING
|
||||
for k, v in GENERIC_FILE_MAPPING.items():
|
||||
if f.suffix[1:].lower() in v:
|
||||
t.categories.append(k)
|
||||
if k == "image":
|
||||
img = Image.open(f)
|
||||
exif = img._getexif()
|
||||
orientation = exif.get(274, 1) if exif else 1
|
||||
width, height = img.width, img.height
|
||||
if orientation in [5, 6, 7, 8]:
|
||||
width, height = height, width
|
||||
t.metadata = ImageMetadata(
|
||||
path=str(f.relative_to(self.config.content_dir)),
|
||||
width=width,
|
||||
height=height,
|
||||
alt=f.name,
|
||||
thumbhash=image_to_thumbhash(img),
|
||||
exif=img.info,
|
||||
)
|
||||
elif k == "document":
|
||||
with open(f, "r") as fdoc:
|
||||
t.metadata = FileMetadata(
|
||||
path=str(f.relative_to(self.config.content_dir)),
|
||||
first_hundred_chars=fdoc.read(100),
|
||||
)
|
||||
ret.append(t)
|
||||
return ret
|
||||
|
||||
|
||||
def get_sibling_content_files(self, path: str = ""):
|
||||
search_contnet_path = self.config.content_dir / path
|
||||
files = search_contnet_path.glob("*")
|
||||
return [
|
||||
(file.name, str(file.relative_to(self.config.content_dir)))
|
||||
for file in files
|
||||
if file.is_file()
|
||||
]
|
||||
|
||||
def get_text_document_preview(self, path: str):
|
||||
file_path = self.config.content_dir / path
|
||||
with open(file_path, "r") as f:
|
||||
content = f.read(100)
|
||||
return content
|
||||
|
||||
def get_sibling_content_folders(self, path: str = ""):
|
||||
search_contnet_path = self.config.content_dir / path
|
||||
files = search_contnet_path.glob("*")
|
||||
return [
|
||||
(file.name, str(file.relative_to(self.config.content_dir)))
|
||||
for file in files
|
||||
if file.is_dir()
|
||||
]
|
Reference in New Issue
Block a user