From 48567e94329add0c5b44416dae0de36273ef1305 Mon Sep 17 00:00:00 2001 From: Tomek Dubrownik Date: Thu, 21 Feb 2013 02:21:49 +0100 Subject: [PATCH] initial --- gallery.cfg.dist | 7 +++++ gallery.py | 67 +++++++++++++++++++++++++++++++++++++++++++++ templates/main.html | 24 ++++++++++++++++ 3 files changed, 98 insertions(+) create mode 100644 gallery.cfg.dist create mode 100644 gallery.py create mode 100644 templates/main.html diff --git a/gallery.cfg.dist b/gallery.cfg.dist new file mode 100644 index 0000000..15e7a4d --- /dev/null +++ b/gallery.cfg.dist @@ -0,0 +1,7 @@ +CHARSET = 'utf-8' + +DIRECTORY = '' +MIME = r'image/.*' + +THUMB_DIR = './thumb' +THUMB_SIZE = (100,100) diff --git a/gallery.py b/gallery.py new file mode 100644 index 0000000..2fd4a66 --- /dev/null +++ b/gallery.py @@ -0,0 +1,67 @@ +import os +import re +import mimetypes +from os.path import isdir, abspath, exists, split +from flask import Flask, render_template, abort, send_file, safe_join +from PIL import Image +from jinja2 import contextfilter + +app = Flask('gallery') +app.config.from_pyfile('gallery.cfg') + +picmime = re.compile(app.config['MIME']) +base = app.config['DIRECTORY'] +thumb = app.config['THUMB_DIR'] +thsize = app.config['THUMB_SIZE'] + +def is_image(path): + return picmime.match(mimetypes.guess_type(path)[0] or 'NOPE') + +@app.template_filter('relurl') +@contextfilter +def relurl(ctx, tgt): + return safe_join(ctx['path'], tgt) + +@app.route('/pictures/') +def send_pic(tgt): + return send_file(safe_join(base, tgt)) + +def ensure_dir(path): + try: + os.makedirs(split(path)[0]) + except OSError as e: + if e.errno == 17: + pass + else: + raise + +@app.route('/thumb/') +def send_thumb(tgt): + orig_path = safe_join(base, tgt) + thumb_path = safe_join(thumb, tgt) + if not exists(thumb_path) or \ + os.stat(thumb_path).st_mtime < os.stat(orig_path).st_mtime: + ensure_dir(thumb_path) + image = Image.open(orig_path) + sz = image.size + scale = min(float(b) / a for a,b in zip(sz, thsize)) + scale = min(scale, 1) + image.resize(map(int, (scale * sz[0], scale * sz[1])), Image.ANTIALIAS).save(thumb_path) + return send_file(thumb_path) + +@app.route('/') +def view(tgt): + try: + path = abspath(safe_join(base, tgt)) + names = map(lambda s: s.decode(app.config['CHARSET']), os.listdir(path)) + dirs = filter(lambda f: isdir(safe_join(path,f)), names) + pics = filter(is_image, names) + return render_template('main.html', dirs=dirs, pics=pics, path=tgt) + except OSError as e: + if e.errno == 2: + abort(404) + +app.route('/')(lambda: view('')) + +if __name__ == '__main__': + app.run('::', 8080, debug=True) diff --git a/templates/main.html b/templates/main.html new file mode 100644 index 0000000..68024a8 --- /dev/null +++ b/templates/main.html @@ -0,0 +1,24 @@ + + + + + +

Subgalleries

+
    + {% for d in dirs %} +
  • {{d}}
  • + {% endfor %} +
+

Pictures

+ + +