From 8f4f50d5c45e9eab669dde3164c1ed70cab6e333 Mon Sep 17 00:00:00 2001 From: Henrik Levkowetz Date: Tue, 13 Mar 2018 13:56:00 +0000 Subject: [PATCH] Added a new argument encoding= to handle_upload_file() in order to be able to deal better with various upload encodings. - Legacy-Id: 14780 --- ietf/secr/proceedings/utils.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/ietf/secr/proceedings/utils.py b/ietf/secr/proceedings/utils.py index cb3506c19..707bcb075 100644 --- a/ietf/secr/proceedings/utils.py +++ b/ietf/secr/proceedings/utils.py @@ -10,7 +10,7 @@ import debug # pyflakes:ignore from ietf.utils.html import sanitize_document -def handle_upload_file(file,filename,meeting,subdir, request=None): +def handle_upload_file(file,filename,meeting,subdir, request=None, encoding=None): ''' This function takes a file object, a filename and a meeting object and subdir as string. It saves the file to the appropriate directory, get_materials_path() + subdir. @@ -37,7 +37,16 @@ def handle_upload_file(file,filename,meeting,subdir, request=None): destination = open(os.path.join(path,filename), 'wb+') if extension in settings.MEETING_VALID_MIME_TYPE_EXTENSIONS['text/html']: file.open() - text = smart_text(file.read()) + text = file.read() + if encoding: + text = text.decode(encoding) + else: + try: + text = smart_text(text) + except UnicodeDecodeError as e: + msg = "Failure trying to save '%s': %s..." % (filename, str(e)[:120]) + return msg + # Whole file sanitization; add back what's missing from a complete # document (sanitize will remove these). clean = sanitize_document(text) @@ -56,3 +65,4 @@ def handle_upload_file(file,filename,meeting,subdir, request=None): os.chdir(path) os.system('unzip %s' % filename) + return None