1
0
mirror of https://github.com/enpaul/kodak.git synced 2024-11-14 10:36:55 +00:00

Fix link name generation logic to support nested directories

This commit is contained in:
Ethan Paul 2021-11-14 19:11:02 -05:00
parent dc81600d0c
commit bbb7dec2b3
No known key found for this signature in database
GPG Key ID: D0E2CBF1245E92BF
3 changed files with 18 additions and 9 deletions

View File

@ -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

View File

@ -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()),

View File

@ -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")