From 466ebbf11f958279f20bca9aacd2a2deeaaab2bf Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Tue, 28 Sep 2021 17:23:49 +0200 Subject: Make MIMETYPE_BLACKLIST work with the S3 module --- fbin/fbin.py | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/fbin/fbin.py b/fbin/fbin.py index 04811c0..148c0dd 100755 --- a/fbin/fbin.py +++ b/fbin/fbin.py @@ -160,6 +160,21 @@ def uploaded(hash): return render_template('uploaded.html', **context) +def _get_mimetype(f, path): + if isinstance(path, Response): + mimetype = path.content_type + else: + mimetype = f.get_mime_type() + # Serve blacklisted mimetypes as either text/plain or application/octet-stream + if mimetype in current_app.config['MIMETYPE_BLACKLIST'] and (f.user is None + or f.user.username not in current_app.config['MIMETYPE_USER_WHITELIST']): + if mimetype.startswith('text/'): + mimetype = 'text/plain' + else: + mimetype = 'application/octet-stream' + return mimetype + + @app.route('/f/') @app.route('/f/') @app.route('/f//') @@ -173,18 +188,12 @@ def _file(hash, ext=None, filename=None): for scan in f.blocked_reason['scans'].values()))): abort(404) path = storage.get_file(f) + mimetype = _get_mimetype(f, path) if isinstance(path, Response): + path.content_type = mimetype return path if not path or not os.path.exists(path): abort(404) - mimetype = f.get_mime_type() - # Serve blacklisted mimetypes as either text/plain or application/octet-stream - if mimetype in current_app.config['MIMETYPE_BLACKLIST'] and (f.user is None - or f.user.username not in current_app.config['MIMETYPE_USER_WHITELIST']): - if mimetype.startswith('text/'): - mimetype = 'text/plain' - else: - mimetype = 'application/octet-stream' return send_file(path, mimetype=mimetype, attachment_filename=f.filename) -- cgit v1.2.3