page load optimization and pagination changes

This commit is contained in:
Bryan Van Deusen
2025-08-03 11:40:32 -04:00
parent 63918363c1
commit 0dc944ef25
11 changed files with 115 additions and 84 deletions
+21 -16
View File
@@ -11,12 +11,10 @@ import uuid
from app import db
from app.models import ImageRecord
THUMB_DIR = "/images/thumbs"
THUMB_SIZE = (400, 400)
def import_images_task(source_dir, dest_dir):
imported = []
os.makedirs(THUMB_DIR, exist_ok=True)
for artist_dir in os.listdir(source_dir):
artist_path = os.path.join(source_dir, artist_dir)
@@ -48,11 +46,9 @@ def import_images_task(source_dir, dest_dir):
dest_path = os.path.join(target_dir, filename)
shutil.copy2(full_path, dest_path)
# Generate thumbnail
thumb_filename = f"{uuid.uuid4().hex}.jpg"
thumb_path = os.path.join(THUMB_DIR, thumb_filename)
# Generate thumbnail AFTER copying to /images
try:
generate_thumbnail(full_path, thumb_path)
thumb_path = generate_thumbnail(dest_path)
except Exception as e:
print(f"[WARN] Failed to generate thumbnail: {e}")
thumb_path = None
@@ -75,26 +71,35 @@ def import_images_task(source_dir, dest_dir):
db.session.add(record)
imported.append(filename)
# Add delay between each image
db.session.commit()
time.sleep(1)
return f"Imported {len(imported)} images."
def generate_thumbnail(image_path, size=(400, 400), overwrite=False):
thumbnail_dir = THUMB_DIR # update this accordingly
basename = os.path.basename(image_path)
thumb_path = os.path.join(thumbnail_dir, basename)
if not overwrite and os.path.exists(thumb_path):
return thumb_path
def generate_thumbnail(image_path, size=(400, 400), overwrite=False):
from pathlib import Path
images_root = Path("/images").resolve()
image_path = Path(image_path).resolve()
try:
rel_path = image_path.relative_to(images_root)
except ValueError:
raise ValueError(f"{image_path} is not under {images_root}")
thumb_path = images_root / "thumbs" / rel_path
thumb_path.parent.mkdir(parents=True, exist_ok=True)
if not overwrite and thumb_path.exists():
return str(thumb_path)
with Image.open(image_path) as img:
img.thumbnail(size)
img.save(thumb_path)
return thumb_path
return str(thumb_path)
def calculate_hash(file_path):
hash_func = hashlib.sha256()
@@ -155,4 +160,4 @@ def extract_metadata(file_path):
except Exception as e:
print(f"[WARN] Failed to read EXIF data: {file_path} {e}")
return metadata
return metadata