From dd7986fba884701a055c7b8a8d628e8bd5ab082b Mon Sep 17 00:00:00 2001 From: Chris Benninger Date: Fri, 26 Jun 2020 19:14:56 -0700 Subject: [PATCH] Be default, build only newly found and missing photos. --- .env.sample | 5 +++++ fussel.py | 9 ++++++++- generator/generate.py | 32 +++++++++++++++++++++----------- 3 files changed, 34 insertions(+), 12 deletions(-) diff --git a/.env.sample b/.env.sample index 888d083..44c0232 100644 --- a/.env.sample +++ b/.env.sample @@ -19,6 +19,10 @@ # Include trailing slash HTTP_ROOT="/" +# Setting to true will force a full rebuild of the gallery photos. +# Setting to false will only generate files which are missing or added +OVERWRITE="false" + # Enable face tag detection. # Adds a faces button and virtual albums for detected people PEOPLE_ENABLE="true" @@ -44,3 +48,4 @@ RECURSIVE_ALBUMS="true" # - {parent_album} will be replaced with the parent album # - {album} will be replaced with the album name RECURSIVE_ALBUMS_NAME_PATTERN="{parent_album} > {album}" + diff --git a/fussel.py b/fussel.py index e904258..1056bb9 100755 --- a/fussel.py +++ b/fussel.py @@ -44,6 +44,12 @@ recursive_albums_name_pattern = os.getenv("RECURSIVE_ALBUMS_NAME_PATTERN") +overwrite = os.getenv("OVERWRITE") +if overwrite == 'true': + overwrite = True +else: + overwrite = False + filenames = [os.path.join(os.path.dirname(os.path.realpath(__file__)), "web", "package.json")] massedit.edit_files(filenames, ["re.sub(r'^.*\"homepage\":.*$', ' \"homepage\": \""+http_root+"\",', line)"], dry_run=False) @@ -62,6 +68,7 @@ watermark_path, watermark_ratio, recursive_albums, - recursive_albums_name_pattern + recursive_albums_name_pattern, + overwrite ) generator.generate_site(output_photos_path, output_data_path, http_root) diff --git a/generator/generate.py b/generator/generate.py index 134402b..0539975 100755 --- a/generator/generate.py +++ b/generator/generate.py @@ -12,7 +12,7 @@ class SiteGenerator: def __init__(self, site_name, input_photos_dir, people_enabled, watermark_enabled, watermark_path, watermark_ratio, - recursive_albums, recursive_albums_name_pattern): + recursive_albums, recursive_albums_name_pattern, overwrite): self.input_photos_dir = input_photos_dir self.people_enabled = people_enabled self.watermark_enabled = watermark_enabled @@ -20,6 +20,7 @@ def __init__(self, site_name, input_photos_dir, people_enabled, watermark_enable self.watermark_ratio = watermark_ratio self.recursive_albums = recursive_albums self.recursive_albums_name_pattern = recursive_albums_name_pattern + self.overwrite = overwrite self.people_data = {} self.albums_data = {} self.site_data = { @@ -28,8 +29,11 @@ def __init__(self, site_name, input_photos_dir, people_enabled, watermark_enable def process_photo(self, external_path, photo, output_dir): new_original_photo = os.path.join(output_dir, "original_%s" % os.path.basename(photo)) - print(" ----> Copying to '%s'" % new_original_photo) - shutil.copyfile(photo, new_original_photo) + + # Only copy if overwrite explicitly asked for or if doesn't exist + if self.overwrite or not os.path.exists(new_original_photo): + print(" ----> Copying to '%s'" % new_original_photo) + shutil.copyfile(photo, new_original_photo) with Image.open(new_original_photo) as im: original_size = im.size @@ -55,16 +59,21 @@ def process_photo(self, external_path, photo, output_dir): largest_src = new_sub_photo if smallest_src is None: smallest_src = new_sub_photo - print(" ------> Generating photo size... '%s'" % new_sub_photo) - with Image.open(new_original_photo) as im: - im.thumbnail(new_size) - im.save(new_sub_photo) - data['srcSet'] += ["%s/%s %sw" % (external_path, os.path.basename(new_sub_photo), new_size[0])] + + # Only generate if overwrite explicitly asked for or if doesn't exist + if self.overwrite or not os.path.exists(new_sub_photo): + print(" ------> Generating photo size... '%s'" % new_sub_photo) + with Image.open(new_original_photo) as im: + im.thumbnail(new_size) + im.save(new_sub_photo) + data['srcSet'] += ["%s/%s %sw" % (external_path, os.path.basename(new_sub_photo), new_size[0])] data['src'] = "%s/%s" % (external_path, os.path.basename(largest_src)) data['_thumb'] = "%s/%s" % (external_path, os.path.basename(smallest_src)) - if self.watermark_enabled: + + # Only copy if overwrite explicitly asked for or if doesn't exist + if self.watermark_enabled and (self.overwrite or not os.path.exists(new_original_photo)): with Image.open(self.watermark_path) as watermark_im: print(" ------> Adding watermark ... '%s'" % largest_src) self.apply_watermark(largest_src, watermark_im) @@ -276,7 +285,7 @@ def process_album(self, album_dir, album_name, output_albums_photos_path, extern # TODO externalize this? external_path = external_root + "static/_gallery/albums/" + album_name_folder #external_path = external_root + "static/_gallery/albums/" + album_name - os.makedirs(album_folder) + os.makedirs(album_folder, exist_ok=True) entries = list(map(lambda e: os.path.join(album_dir, e), os.listdir(album_dir))) dirs = list(filter(lambda e: self.is_supported_album(e), entries)) @@ -310,7 +319,8 @@ def generate_site(self, output_photos_path, output_data_path, external_root): output_albums_photos_path = os.path.join(output_photos_path, "albums") # Cleanup and prep of deploy space - shutil.rmtree(output_photos_path, ignore_errors=True) + if self.overwrite: + shutil.rmtree(output_photos_path, ignore_errors=True) os.makedirs(output_photos_path, exist_ok=True) shutil.rmtree(output_data_path, ignore_errors=True) os.makedirs(output_data_path, exist_ok=True)