From 13d38291bd07401bd16b6e6805edc72bf0029b2f Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Sat, 17 Aug 2019 15:17:02 +0200 Subject: Fetch and store thumbnails via storage modules This will allow us to remotely store thumbnails in case of S3. For S3 the thumb bucket is configurable to allow these to be stored separately. The S3 key for thumbnails does not conflict with files, so these can be stored in the same bucket if needed. --- fbin/fbin.py | 52 ++++++++++++++++++++++++++++------------------------ 1 file changed, 28 insertions(+), 24 deletions(-) (limited to 'fbin/fbin.py') diff --git a/fbin/fbin.py b/fbin/fbin.py index 983c1aa..d1dff76 100755 --- a/fbin/fbin.py +++ b/fbin/fbin.py @@ -359,33 +359,37 @@ def videos(): @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) - if f.is_image(): - try: - #im = Image.open(f.get_path()) + f = get_file(hash, update_accessed=False) + response = storage.get_thumbnail(f) + if not response: + with tempfile.NamedTemporaryFile(suffix='.jpg') as ttf: # temporary thumb file + if f.is_image(): + try: + with storage.temp_file(f) as tf: + im = Image.open(tf) + # 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(ttf) + except IOError: + # We can't generate a thumbnail for this file, just say it doesn't exist. + abort(404) + elif f.is_video(): with storage.temp_file(f) as tf: - im = Image.open(tf) - # 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) - except IOError: - # We can't generate a thumbnail for this file, just say it doesn't exist. + p = subprocess.run(['ffmpegthumbnailer', '-i', '-', '-o', ttf.name], stdin=tf) + if p.returncode != 0: + abort(404) + else: abort(404) - elif f.is_video(): - #p = subprocess.run(['ffmpegthumbnailer', '-i', f.get_path(), '-o', thumbfile]) - with storage.temp_file(f) as tf: - p = subprocess.run(['ffmpegthumbnailer', '-i', '-', '-o', thumbfile], stdin=tf) - if p.returncode != 0: - if os.path.exists(thumbfile): - os.unlink(thumbfile) + ttf.seek(0) + if not os.path.getsize(ttf.name): abort(404) - else: - abort(404) - return send_file(thumbfile) + storage.store_thumbnail(f, ttf) + response = storage.get_thumbnail(f) + if isinstance(response, Response): + return response + return send_file(response, attachment_filename='thumb.jpg') @app.route('/h') @app.route('/help') -- cgit v1.2.3