Code

Ticket #13721: content_type_extra.diff

File content_type_extra.diff, 9.2 KB (added by wkornewald, 4 years ago)

patch (with unit tests and docs)

Line 
1diff -r 9b24e08becc2 django/core/files/uploadhandler.py
2--- a/django/core/files/uploadhandler.py        Sat Nov 13 18:43:13 2010 +0000
3+++ b/django/core/files/uploadhandler.py        Wed Dec 08 12:55:51 2010 +0100
4@@ -84,7 +84,8 @@
5         """
6         pass
7 
8-    def new_file(self, field_name, file_name, content_type, content_length, charset=None):
9+    def new_file(self, field_name, file_name, content_type, content_length,
10+            charset=None, content_type_extra=None):
11         """
12         Signal that a new file has been started.
13 
14@@ -96,6 +97,9 @@
15         self.content_type = content_type
16         self.content_length = content_length
17         self.charset = charset
18+        if content_type_extra is None:
19+            content_type_extra = {}
20+        self.content_type_extra = content_type_extra
21 
22     def receive_data_chunk(self, raw_data, start):
23         """
24diff -r 9b24e08becc2 django/http/multipartparser.py
25--- a/django/http/multipartparser.py    Sat Nov 13 18:43:13 2010 +0000
26+++ b/django/http/multipartparser.py    Wed Dec 08 12:55:51 2010 +0100
27@@ -169,8 +169,11 @@
28                     file_name = self.IE_sanitize(unescape_entities(file_name))
29 
30                     content_type = meta_data.get('content-type', ('',))[0].strip()
31+                    content_type_extra = meta_data.get('content-type', (0,{}))[1]
32+                    if content_type_extra is None:
33+                        content_type_extra = {}
34                     try:
35-                        charset = meta_data.get('content-type', (0,{}))[1].get('charset', None)
36+                        charset = content_type_extra.get('charset', None)
37                     except:
38                         charset = None
39 
40@@ -185,7 +188,7 @@
41                             try:
42                                 handler.new_file(field_name, file_name,
43                                                  content_type, content_length,
44-                                                 charset)
45+                                                 charset, content_type_extra.copy())
46                             except StopFutureHandlers:
47                                 break
48 
49diff -r 9b24e08becc2 django/test/client.py
50--- a/django/test/client.py     Sat Nov 13 18:43:13 2010 +0000
51+++ b/django/test/client.py     Wed Dec 08 12:55:51 2010 +0100
52@@ -142,7 +142,10 @@
53 
54 def encode_file(boundary, key, file):
55     to_str = lambda s: smart_str(s, settings.DEFAULT_CHARSET)
56-    content_type = mimetypes.guess_type(file.name)[0]
57+    if hasattr(file, 'content_type'):
58+        content_type = file.content_type
59+    else:
60+        content_type = mimetypes.guess_type(file.name)[0]
61     if content_type is None:
62         content_type = 'application/octet-stream'
63     return [
64diff -r 9b24e08becc2 docs/topics/http/file-uploads.txt
65--- a/docs/topics/http/file-uploads.txt Sat Nov 13 18:43:13 2010 +0000
66+++ b/docs/topics/http/file-uploads.txt Wed Dec 08 12:55:51 2010 +0100
67@@ -195,6 +195,10 @@
68         For ``text/*`` content-types, the character set (i.e. ``utf8``) supplied
69         by the browser. Again, "trust but verify" is the best policy here.
70 
71+    ``UploadedFile.content_type_extra``
72+        A dict containing the extra parameters that were passed to the
73+        content-type header.
74+
75     ``UploadedFile.temporary_file_path()``
76         Only files uploaded onto disk will have this method; it returns the full
77         path to the temporary uploaded file.
78@@ -351,7 +355,7 @@
79 
80         The default is 64*2\ :sup:`10` bytes, or 64 KB.
81 
82-    ``FileUploadHandler.new_file(self, field_name, file_name, content_type, content_length, charset)``
83+    ``FileUploadHandler.new_file(self, field_name, file_name, content_type, content_length, charset, content_type_extra)``
84         Callback signaling that a new file upload is starting. This is called
85         before any data has been fed to any upload handlers.
86 
87@@ -369,6 +373,9 @@
88         ``charset`` is the character set (i.e. ``utf8``) given by the browser.
89         Like ``content_length``, this sometimes won't be provided.
90 
91+        ``content_type_extra`` is a dict containing the extra parameters that
92+        were passed to the content-type header.
93+
94         This method may raise a ``StopFutureHandlers`` exception to prevent
95         future handlers from handling this file.
96 
97diff -r 9b24e08becc2 tests/regressiontests/file_uploads/tests.py
98--- a/tests/regressiontests/file_uploads/tests.py       Sat Nov 13 18:43:13 2010 +0000
99+++ b/tests/regressiontests/file_uploads/tests.py       Wed Dec 08 12:55:51 2010 +0100
100@@ -170,6 +170,16 @@
101         got = simplejson.loads(response.content)
102         self.assert_('f' not in got)
103 
104+    def test_extra_content_type(self):
105+        f = tempfile.NamedTemporaryFile()
106+        f.write('a' * (2 ** 21))
107+        f.seek(0)
108+        f.content_type = 'text/plain; blob-key=upload blob key; other=test'
109+
110+        response = self.client.post("/file_uploads/content_type_extra/", {'f': f})
111+        got = simplejson.loads(response.content)
112+        self.assertEqual(got['f'], 'upload blob key')
113+
114     def test_broken_custom_upload_handler(self):
115         f = tempfile.NamedTemporaryFile()
116         f.write('a' * (2 ** 21))
117diff -r 9b24e08becc2 tests/regressiontests/file_uploads/uploadhandler.py
118--- a/tests/regressiontests/file_uploads/uploadhandler.py       Sat Nov 13 18:43:13 2010 +0000
119+++ b/tests/regressiontests/file_uploads/uploadhandler.py       Wed Dec 08 12:55:51 2010 +0100
120@@ -2,7 +2,10 @@
121 Upload handlers to test the upload API.
122 """
123 
124-from django.core.files.uploadhandler import FileUploadHandler, StopUpload
125+from django.core.files.uploadedfile import InMemoryUploadedFile
126+from django.core.files.uploadhandler import (FileUploadHandler, StopUpload,
127+    StopFutureHandlers)
128+from StringIO import StringIO
129 
130 class QuotaUploadHandler(FileUploadHandler):
131     """
132@@ -32,3 +35,38 @@
133     """A handler that raises an exception."""
134     def receive_data_chunk(self, raw_data, start):
135         raise CustomUploadError("Oops!")
136+
137+class ContentTypeExtraUploadHandler(FileUploadHandler):
138+    """
139+    File upload handler that handles content_type_extra
140+    """
141+
142+    def new_file(self, *args, **kwargs):
143+        super(ContentTypeExtraUploadHandler, self).new_file(*args, **kwargs)
144+        self.blobkey = self.content_type_extra.get('blob-key', '')
145+        self.file = StringIO()
146+        self.file.write(self.blobkey)
147+        self.active = self.blobkey is not None
148+        if self.active:
149+            raise StopFutureHandlers()
150+
151+    def receive_data_chunk(self, raw_data, start):
152+        """
153+        Add the data to the StringIO file.
154+        """
155+        if not self.active:
156+            return raw_data
157+
158+    def file_complete(self, file_size):
159+        if not self.active:
160+            return
161+
162+        self.file.seek(0)
163+        return InMemoryUploadedFile(
164+            file = self.file,
165+            field_name = self.field_name,
166+            name = self.file_name,
167+            content_type = self.content_type,
168+            size = file_size,
169+            charset = self.charset
170+        )
171diff -r 9b24e08becc2 tests/regressiontests/file_uploads/urls.py
172--- a/tests/regressiontests/file_uploads/urls.py        Sat Nov 13 18:43:13 2010 +0000
173+++ b/tests/regressiontests/file_uploads/urls.py        Wed Dec 08 12:55:51 2010 +0100
174@@ -2,12 +2,13 @@
175 import views
176 
177 urlpatterns = patterns('',
178-    (r'^upload/$',          views.file_upload_view),
179-    (r'^verify/$',          views.file_upload_view_verify),
180-    (r'^unicode_name/$',    views.file_upload_unicode_name),
181-    (r'^echo/$',            views.file_upload_echo),
182-    (r'^quota/$',           views.file_upload_quota),
183-    (r'^quota/broken/$',    views.file_upload_quota_broken),
184-    (r'^getlist_count/$',   views.file_upload_getlist_count),
185-    (r'^upload_errors/$',   views.file_upload_errors),
186+    (r'^upload/$',              views.file_upload_view),
187+    (r'^verify/$',              views.file_upload_view_verify),
188+    (r'^unicode_name/$',        views.file_upload_unicode_name),
189+    (r'^echo/$',                views.file_upload_echo),
190+    (r'^quota/$',               views.file_upload_quota),
191+    (r'^quota/broken/$',        views.file_upload_quota_broken),
192+    (r'^getlist_count/$',       views.file_upload_getlist_count),
193+    (r'^upload_errors/$',       views.file_upload_errors),
194+    (r'^content_type_extra/$',  views.file_upload_content_type_extra),
195 )
196diff -r 9b24e08becc2 tests/regressiontests/file_uploads/views.py
197--- a/tests/regressiontests/file_uploads/views.py       Sat Nov 13 18:43:13 2010 +0000
198+++ b/tests/regressiontests/file_uploads/views.py       Wed Dec 08 12:55:51 2010 +0100
199@@ -3,7 +3,8 @@
200 from django.http import HttpResponse, HttpResponseServerError
201 from django.utils import simplejson
202 from models import FileModel, UPLOAD_TO
203-from uploadhandler import QuotaUploadHandler, ErroringUploadHandler
204+from uploadhandler import (QuotaUploadHandler, ErroringUploadHandler,
205+    ContentTypeExtraUploadHandler)
206 from django.utils.hashcompat import sha_constructor
207 from tests import UNICODE_FILENAME
208 
209@@ -112,3 +113,8 @@
210 def file_upload_errors(request):
211     request.upload_handlers.insert(0, ErroringUploadHandler())
212     return file_upload_echo(request)
213+
214+def file_upload_content_type_extra(request):
215+    request.upload_handlers.insert(0, ContentTypeExtraUploadHandler())
216+    r = dict([(k, f.read()) for k, f in request.FILES.items()])
217+    return HttpResponse(simplejson.dumps(r))