diff --git a/kodak/database/image.py b/kodak/database/image.py index cc28fb3..0c3c512 100644 --- a/kodak/database/image.py +++ b/kodak/database/image.py @@ -29,22 +29,24 @@ class ImageRecord(KodakModel): :param path: Full path to the image file to process. The file path provided is expected to already be absolute, with all symlinks and aliases resolved. """ - name = path.stem extension = path.suffix for item in constants.ImageFormat: - if extension.lower()[1:] in item.value: + if extension.replace(".", "") in item.value: format_ = item break else: raise RuntimeError - name = name.replace(str(config.source_dir), "").replace( + name = str(path.relative_to(config.source_dir)).replace( os.sep, constants.IMAGE_PATH_NAME_SEPARATOR - ) + )[: -len(extension)] return cls( - name=name, source=path, format_=format_, checksum=Checksum.from_path(path) + name=name, + source=path.relative_to(config.source_dir), + format_=format_, + checksum=Checksum.from_path(path), ) def create_link(self, config: configuration.KodakConfig) -> Path: @@ -56,7 +58,7 @@ class ImageRecord(KodakModel): Path(config.content_dir, self.name).mkdir(exist_ok=True) link = Path(config.content_dir, self.name, "original") try: - link.symlink_to(self.source) + link.symlink_to(config.source_dir / self.source) except FileExistsError: pass return link diff --git a/kodak/resources/image.py b/kodak/resources/image.py index d047e04..fd0fdec 100644 --- a/kodak/resources/image.py +++ b/kodak/resources/image.py @@ -19,6 +19,9 @@ class Image(KodakResource): with database.interface.atomic(): image = database.ImageRecord.get(database.ImageRecord.name == image_name) + # Note that this sends the original source file directly, rather than the symlink named + # "original". This is because flask will serve the symlink file itself, not the linked file, + # to the browser. resp = flask.send_file( image.source, cache_timeout=int(datetime.timedelta(days=365).total_seconds()), diff --git a/kodak/tools/index.py b/kodak/tools/index.py index 3431aea..67ed4e8 100644 --- a/kodak/tools/index.py +++ b/kodak/tools/index.py @@ -1,4 +1,5 @@ import logging +import shutil from pathlib import Path from typing import List from typing import Optional @@ -117,11 +118,14 @@ def build(config: Optional[configuration.KodakConfig] = None) -> None: ), ) - logger.info(f"Removing source links to {len(removed_images)} removed image files") + logger.info( + f"Removing generated assets for {len(removed_images)} removed image files" + ) for image in removed_images: - logger.debug(f"Removing link to removed source image {image.source}") - image.remove_link(config) + content = config.content_dir / image.name + logger.debug(f"Removing content directory {content}") + shutil.rmtree(str(content)) logger.info("Processing source links")