From 3f7fad2e5a1dd2cccaff97d662745ac24cc18efa Mon Sep 17 00:00:00 2001 From: Jason Bittel Date: Fri, 5 Sep 2014 16:27:55 -0700 Subject: [PATCH] Refactor disposition unpacking for clarity --- rest_framework/parsers.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/rest_framework/parsers.py b/rest_framework/parsers.py index 161bc8090..c287908dc 100644 --- a/rest_framework/parsers.py +++ b/rest_framework/parsers.py @@ -289,22 +289,23 @@ class FileUploadParser(BaseParser): try: meta = parser_context['request'].META - disposition = parse_header(meta['HTTP_CONTENT_DISPOSITION'].encode('utf-8'))[1] - if 'filename*' in disposition: - return self.get_encoded_filename(disposition) - return force_text(disposition['filename']) + disposition = parse_header(meta['HTTP_CONTENT_DISPOSITION'].encode('utf-8')) + filename_parm = disposition[1] + if 'filename*' in filename_parm: + return self.get_encoded_filename(filename_parm) + return force_text(filename_parm['filename']) except (AttributeError, KeyError): pass - def get_encoded_filename(self, disposition): + def get_encoded_filename(self, filename_parm): """ Handle encoded filenames per RFC6266. See also: http://tools.ietf.org/html/rfc2231#section-4 """ - encoded_filename = force_text(disposition['filename*']) + encoded_filename = force_text(filename_parm['filename*']) try: charset, lang, filename = encoded_filename.split('\'', 2) filename = urlparse.unquote(filename) except (ValueError, LookupError): - filename = force_text(disposition['filename']) + filename = force_text(filename_parm['filename']) return filename