From e15662bf20ee4853b8cbd0fb605199e0569f89a3 Mon Sep 17 00:00:00 2001 From: Parthiv Patel Date: Tue, 20 Sep 2022 12:37:47 +0000 Subject: [PATCH] [FIX] mimetype --- flectra/tools/mimetypes.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/flectra/tools/mimetypes.py b/flectra/tools/mimetypes.py index b18a734726..f999899216 100644 --- a/flectra/tools/mimetypes.py +++ b/flectra/tools/mimetypes.py @@ -179,7 +179,7 @@ else: _guesser = ms.buffer def guess_mimetype(bin_data, default=None): - mimetype = _guesser(bin_data) + mimetype = _guesser(bin_data[:1024]) # upgrade incorrect mimetype to official one, fixed upstream # https://github.com/file/file/commit/1a08bb5c235700ba623ffa6f3c95938fe295b262 if mimetype == 'image/svg': @@ -191,3 +191,14 @@ def neuter_mimetype(mimetype, user): if wrong_type and not user._is_system(): return 'text/plain' return mimetype + + +def get_extension(filename): + """ Return the extension the current filename based on the heuristic that + ext is less than or equal to 10 chars and is alphanumeric. + + :param str filename: filename to try and guess a extension for + :returns: detected extension or `` + """ + ext = '.' in filename and filename.split('.')[-1] + return ext and len(ext) <= 10 and ext.isalnum() and '.' + ext.lower() or ''