From a27a916a1fcb9f62a9f075d1316346ff2e6424b2 Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Mon, 6 Aug 2018 17:05:33 +0200 Subject: Add video list and video thumbnails Add a separate video page for listing uploaded videos. The thumbnail endpoint now supports generating video thumbnails using ffmpegthumbnailer. --- fbin/db.py | 3 +++ fbin/fbin.py | 38 +++++++++++++++++++++++++++++--------- fbin/templates/base.html | 1 + fbin/templates/images.html | 4 ++-- 4 files changed, 35 insertions(+), 11 deletions(-) diff --git a/fbin/db.py b/fbin/db.py index 5323134..58bf8ab 100644 --- a/fbin/db.py +++ b/fbin/db.py @@ -98,6 +98,9 @@ class File(Base): def is_image(self): return self.get_mime_type().startswith('image') + def is_video(self): + return self.get_mime_type().startswith('video') + @property def ext(self): return os.path.splitext(self.filename)[1] diff --git a/fbin/fbin.py b/fbin/fbin.py index ffc6a5b..af35bdb 100755 --- a/fbin/fbin.py +++ b/fbin/fbin.py @@ -358,22 +358,42 @@ def images(): } return render_template('images.html', **context) +@app.route('/v') +@app.route('/videos') +@login_required +def videos(): + files = [f for f in get_files(current_user.user) if f.is_video()] + context = { + 'title': 'Videos', + 'fullwidth': True, + 'files': files, + 'total_size': db.File.pretty_size(sum(f.get_size() for f in files if f.exists)), + } + return render_template('images.html', **context) + @app.route('/t/') @app.route('/thumb/') def thumb(hash): thumbfile = os.path.join(current_app.config['THUMB_DIRECTORY'], hash + '.jpg') if not os.access(thumbfile, os.F_OK): f = get_file(hash, update_accessed = False) - try: - im = Image.open(f.get_path()) - except IOError: - # We can't generate a thumbnail for this file, just say it doesn't exist. + if f.is_image(): + try: + im = Image.open(f.get_path()) + except IOError: + # We can't generate a thumbnail for this file, just say it doesn't exist. + abort(404) + # Check for valid JPEG modes. + if im.mode not in ('1', 'L', 'RGB', 'RGBX', 'CMYK', 'YCbCr'): + im = im.convert('RGB') + im.thumbnail(current_app.config.get('THUMB_SIZE', (128, 128)), Image.ANTIALIAS) + im.save(thumbfile) + elif f.is_video(): + p = subprocess.run(['ffmpegthumbnailer', '-i', f.get_path(), '-o', thumbfile]) + if p.returncode != 0: + abort(404) + else: abort(404) - # Check for valid JPEG modes. - if im.mode not in ('1', 'L', 'RGB', 'RGBX', 'CMYK', 'YCbCr'): - im = im.convert('RGB') - im.thumbnail(current_app.config.get('THUMB_SIZE', (128, 128)), Image.ANTIALIAS) - im.save(thumbfile) return send_file(thumbfile) @app.route('/h') diff --git a/fbin/templates/base.html b/fbin/templates/base.html index dfd6d06..beb1e17 100644 --- a/fbin/templates/base.html +++ b/fbin/templates/base.html @@ -28,6 +28,7 @@ {% if current_user.is_authenticated %} {{ nav_html('.files', 'Files') }} {{ nav_html('.images', 'Images') }} + {{ nav_html('.videos', 'Videos') }} {{ nav_html('.account', 'Account') }} {{ nav_html('.api', 'API') }} {{ nav_html('.logout', 'Logout') }} diff --git a/fbin/templates/images.html b/fbin/templates/images.html index c4bfd8f..e6c1ad0 100644 --- a/fbin/templates/images.html +++ b/fbin/templates/images.html @@ -1,7 +1,7 @@ {% extends "base.html" %} {% block content %} -

You have {{ files|length() }} uploaded images totaling {{ total_size }}.

{% if files %} +

You have {{ files|length() }} uploaded {{ title|lower }} totaling {{ total_size }}.

{% for file in files %}
@@ -13,7 +13,7 @@ {% endfor %}
{% else %} -
(No image uploads yet.)
+
(No {{ title|lower }} uploaded yet.)
{% endif %} {% endblock %} {% block scripts %} -- cgit v1.2.3