diff --git a/gallery/static/style.css b/gallery/static/style.css index b8f2d6f..74ec1ce 100644 --- a/gallery/static/style.css +++ b/gallery/static/style.css @@ -1,27 +1,14 @@ +.nav ul { padding: 0; } .nav li { display: inline; } .flex_container { display: flex; - flex-wrap: wrap; + flex-flow: row wrap; width: 100%; + gap: 0 4px; } -.infocard { - margin: 0.5em; - background-color: lightgrey; - font-size: 0.7em; -} -.infocard img { border: 1px solid grey; } -.infocard ul { - list-style-type: none; - margin: 0; - padding: 0; -} -.infocard li { - width: 160px; - white-space: nowrap; - overflow: hidden; - text-overflow: ellipsis; -} +.infocard { background-color: lightgrey; } +.infocard ul { padding: 0; list-style-type: none; } diff --git a/gallery/templates/album.j2 b/gallery/templates/album.j2 index 1945fdc..b2f7423 100644 --- a/gallery/templates/album.j2 +++ b/gallery/templates/album.j2 @@ -8,29 +8,25 @@ +

Gallery

+
{% for image in images %}
- + -
{% endfor %}
diff --git a/gallery/templates/index.j2 b/gallery/templates/index.j2 index 32e31d9..dd3e076 100644 --- a/gallery/templates/index.j2 +++ b/gallery/templates/index.j2 @@ -8,13 +8,38 @@ -
+

Gallery

+ + +

Albums

+
+ {% for album in albums %} +
+ + {% if album.count > 0 %} + + {% else %} + + {% endif %} + +
    +
  • {{ album.name }}
  • +
  • items: {{ album.count }}
  • +
+
+ {% endfor %} +
+ diff --git a/gallery/views/default_routes.py b/gallery/views/default_routes.py index aca60e5..0c3113f 100644 --- a/gallery/views/default_routes.py +++ b/gallery/views/default_routes.py @@ -4,12 +4,13 @@ import re from PIL import Image -from flask import Blueprint, current_app, redirect, render_template, url_for -from flask import send_from_directory, Response +from flask import (Blueprint, current_app, redirect, render_template, url_for, + send_from_directory, Response) bp = Blueprint('default_routes', __name__) VALID_CHARS = re.compile(r"^[a-zA-Z0-9_\-\.]+$") +IMAGE_DIR = 'images' # # Routes @@ -17,20 +18,18 @@ VALID_CHARS = re.compile(r"^[a-zA-Z0-9_\-\.]+$") @bp.route('/') def index() -> Response: - album_dir = os.path.join(current_app.instance_path, 'images') - album_dirs = [] + image_dir = getImageDir() + albums = [] + entries = sorted(os.listdir(image_dir)) - if os.path.isdir(album_dir): - entries = os.listdir(album_dir) - for e in entries: - if os.path.isdir(os.path.join(album_dir, e)): - album_dirs.append(e) + for e in entries: + albums.append(getAlbumDetails(entries, e)) - return render_template('index.j2', albums = sorted(album_dirs)) + return render_template('index.j2', albums = albums) @bp.route('/album/') def album(path:str = '') -> Response: - image_dir = joinPath(current_app.instance_path, 'images') + image_dir = getImageDir() album_dir = joinPath(image_dir, path) images = [] @@ -40,24 +39,23 @@ def album(path:str = '') -> Response: for entry in os.scandir(album_dir): if entry.is_file(): createThumbnail(entry, path) - images.append(getImageDetails(entry)) + images.append(entry.name) return render_template('album.j2', img_root = f'/image/{path}/', album = path, - images = images + images = sorted(images) ) @bp.route('/image//') def image(path:str, img:str) -> Response: - base_dir = os.path.join(current_app.instance_path, 'images') - album_dir = os.path.join(base_dir, os.path.basename(path)) + album_dir = joinPath(getImageDir(), path) return send_from_directory(album_dir, img) @bp.route('/thumbs//') def thumbs(path:str, img:str) -> Response: - base_dir = os.path.join(current_app.instance_path, 'thumbnails') - album_dir = os.path.join(base_dir, os.path.basename(path)) + base_dir = joinPath(current_app.instance_path, 'thumbnails') + album_dir = joinPath(base_dir, path) return send_from_directory(album_dir, img) @@ -65,6 +63,9 @@ def thumbs(path:str, img:str) -> Response: # Helpers # +def getImageDir() -> os.PathLike: + return joinPath(current_app.instance_path, IMAGE_DIR) + def joinPath(base:os.PathLike, path:str) -> os.PathLike: return os.path.join(base, os.path.basename(path)) @@ -83,6 +84,27 @@ def validateFile(base:os.PathLike, path:str) -> bool: return False +def getAlbumDetails(dir_list:list, album:str) -> dict: + image_dir = getImageDir() + + if not validateDir(image_dir, album): + return None + + album_dir = joinPath(image_dir, album) + entries = os.scandir(album_dir) + entries = sorted(os.scandir(album_dir), + key=lambda e: e.name) + + if len(entries) > 0: + image = entries[0].name + createThumbnail(entries[0], album) + else: + image = None + + return { 'name': album, 'image': image, 'count': len(entries) } + + + def getImageDetails(entry:os.PathLike) -> dict: return { 'name': entry.name, @@ -104,7 +126,6 @@ def createThumbnail(image:os.DirEntry, album:str) -> None: except OSError: pass with Image.open(image.path) as im: - im.thumbnail([160, 160]) + im.thumbnail([300, 300]) im.save(joinPath(thumb_path, image.name), "JPEG") -