Ticket #13721: content_type_extra.diff

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

patch (with unit tests and docs)

  • django/core/files/uploadhandler.py

    diff -r 9b24e08becc2 django/core/files/uploadhandler.py
    a b  
    8484        """
    8585        pass
    8686
    87     def new_file(self, field_name, file_name, content_type, content_length, charset=None):
     87    def new_file(self, field_name, file_name, content_type, content_length,
     88            charset=None, content_type_extra=None):
    8889        """
    8990        Signal that a new file has been started.
    9091
     
    9697        self.content_type = content_type
    9798        self.content_length = content_length
    9899        self.charset = charset
     100        if content_type_extra is None:
     101            content_type_extra = {}
     102        self.content_type_extra = content_type_extra
    99103
    100104    def receive_data_chunk(self, raw_data, start):
    101105        """
  • django/http/multipartparser.py

    diff -r 9b24e08becc2 django/http/multipartparser.py
    a b  
    169169                    file_name = self.IE_sanitize(unescape_entities(file_name))
    170170
    171171                    content_type = meta_data.get('content-type', ('',))[0].strip()
     172                    content_type_extra = meta_data.get('content-type', (0,{}))[1]
     173                    if content_type_extra is None:
     174                        content_type_extra = {}
    172175                    try:
    173                         charset = meta_data.get('content-type', (0,{}))[1].get('charset', None)
     176                        charset = content_type_extra.get('charset', None)
    174177                    except:
    175178                        charset = None
    176179
     
    185188                            try:
    186189                                handler.new_file(field_name, file_name,
    187190                                                 content_type, content_length,
    188                                                  charset)
     191                                                 charset, content_type_extra.copy())
    189192                            except StopFutureHandlers:
    190193                                break
    191194
  • django/test/client.py

    diff -r 9b24e08becc2 django/test/client.py
    a b  
    142142
    143143def encode_file(boundary, key, file):
    144144    to_str = lambda s: smart_str(s, settings.DEFAULT_CHARSET)
    145     content_type = mimetypes.guess_type(file.name)[0]
     145    if hasattr(file, 'content_type'):
     146        content_type = file.content_type
     147    else:
     148        content_type = mimetypes.guess_type(file.name)[0]
    146149    if content_type is None:
    147150        content_type = 'application/octet-stream'
    148151    return [
  • docs/topics/http/file-uploads.txt

    diff -r 9b24e08becc2 docs/topics/http/file-uploads.txt
    a b  
    195195        For ``text/*`` content-types, the character set (i.e. ``utf8``) supplied
    196196        by the browser. Again, "trust but verify" is the best policy here.
    197197
     198    ``UploadedFile.content_type_extra``
     199        A dict containing the extra parameters that were passed to the
     200        content-type header.
     201
    198202    ``UploadedFile.temporary_file_path()``
    199203        Only files uploaded onto disk will have this method; it returns the full
    200204        path to the temporary uploaded file.
     
    351355
    352356        The default is 64*2\ :sup:`10` bytes, or 64 KB.
    353357
    354     ``FileUploadHandler.new_file(self, field_name, file_name, content_type, content_length, charset)``
     358    ``FileUploadHandler.new_file(self, field_name, file_name, content_type, content_length, charset, content_type_extra)``
    355359        Callback signaling that a new file upload is starting. This is called
    356360        before any data has been fed to any upload handlers.
    357361
     
    369373        ``charset`` is the character set (i.e. ``utf8``) given by the browser.
    370374        Like ``content_length``, this sometimes won't be provided.
    371375
     376        ``content_type_extra`` is a dict containing the extra parameters that
     377        were passed to the content-type header.
     378
    372379        This method may raise a ``StopFutureHandlers`` exception to prevent
    373380        future handlers from handling this file.
    374381
  • tests/regressiontests/file_uploads/tests.py

    diff -r 9b24e08becc2 tests/regressiontests/file_uploads/tests.py
    a b  
    170170        got = simplejson.loads(response.content)
    171171        self.assert_('f' not in got)
    172172
     173    def test_extra_content_type(self):
     174        f = tempfile.NamedTemporaryFile()
     175        f.write('a' * (2 ** 21))
     176        f.seek(0)
     177        f.content_type = 'text/plain; blob-key=upload blob key; other=test'
     178
     179        response = self.client.post("/file_uploads/content_type_extra/", {'f': f})
     180        got = simplejson.loads(response.content)
     181        self.assertEqual(got['f'], 'upload blob key')
     182
    173183    def test_broken_custom_upload_handler(self):
    174184        f = tempfile.NamedTemporaryFile()
    175185        f.write('a' * (2 ** 21))
  • tests/regressiontests/file_uploads/uploadhandler.py

    diff -r 9b24e08becc2 tests/regressiontests/file_uploads/uploadhandler.py
    a b  
    22Upload handlers to test the upload API.
    33"""
    44
    5 from django.core.files.uploadhandler import FileUploadHandler, StopUpload
     5from django.core.files.uploadedfile import InMemoryUploadedFile
     6from django.core.files.uploadhandler import (FileUploadHandler, StopUpload,
     7    StopFutureHandlers)
     8from StringIO import StringIO
    69
    710class QuotaUploadHandler(FileUploadHandler):
    811    """
     
    3235    """A handler that raises an exception."""
    3336    def receive_data_chunk(self, raw_data, start):
    3437        raise CustomUploadError("Oops!")
     38
     39class ContentTypeExtraUploadHandler(FileUploadHandler):
     40    """
     41    File upload handler that handles content_type_extra
     42    """
     43
     44    def new_file(self, *args, **kwargs):
     45        super(ContentTypeExtraUploadHandler, self).new_file(*args, **kwargs)
     46        self.blobkey = self.content_type_extra.get('blob-key', '')
     47        self.file = StringIO()
     48        self.file.write(self.blobkey)
     49        self.active = self.blobkey is not None
     50        if self.active:
     51            raise StopFutureHandlers()
     52
     53    def receive_data_chunk(self, raw_data, start):
     54        """
     55        Add the data to the StringIO file.
     56        """
     57        if not self.active:
     58            return raw_data
     59
     60    def file_complete(self, file_size):
     61        if not self.active:
     62            return
     63
     64        self.file.seek(0)
     65        return InMemoryUploadedFile(
     66            file = self.file,
     67            field_name = self.field_name,
     68            name = self.file_name,
     69            content_type = self.content_type,
     70            size = file_size,
     71            charset = self.charset
     72        )
  • tests/regressiontests/file_uploads/urls.py

    diff -r 9b24e08becc2 tests/regressiontests/file_uploads/urls.py
    a b  
    22import views
    33
    44urlpatterns = patterns('',
    5     (r'^upload/$',          views.file_upload_view),
    6     (r'^verify/$',          views.file_upload_view_verify),
    7     (r'^unicode_name/$',    views.file_upload_unicode_name),
    8     (r'^echo/$',            views.file_upload_echo),
    9     (r'^quota/$',           views.file_upload_quota),
    10     (r'^quota/broken/$',    views.file_upload_quota_broken),
    11     (r'^getlist_count/$',   views.file_upload_getlist_count),
    12     (r'^upload_errors/$',   views.file_upload_errors),
     5    (r'^upload/$',              views.file_upload_view),
     6    (r'^verify/$',              views.file_upload_view_verify),
     7    (r'^unicode_name/$',        views.file_upload_unicode_name),
     8    (r'^echo/$',                views.file_upload_echo),
     9    (r'^quota/$',               views.file_upload_quota),
     10    (r'^quota/broken/$',        views.file_upload_quota_broken),
     11    (r'^getlist_count/$',       views.file_upload_getlist_count),
     12    (r'^upload_errors/$',       views.file_upload_errors),
     13    (r'^content_type_extra/$',  views.file_upload_content_type_extra),
    1314)
  • tests/regressiontests/file_uploads/views.py

    diff -r 9b24e08becc2 tests/regressiontests/file_uploads/views.py
    a b  
    33from django.http import HttpResponse, HttpResponseServerError
    44from django.utils import simplejson
    55from models import FileModel, UPLOAD_TO
    6 from uploadhandler import QuotaUploadHandler, ErroringUploadHandler
     6from uploadhandler import (QuotaUploadHandler, ErroringUploadHandler,
     7    ContentTypeExtraUploadHandler)
    78from django.utils.hashcompat import sha_constructor
    89from tests import UNICODE_FILENAME
    910
     
    112113def file_upload_errors(request):
    113114    request.upload_handlers.insert(0, ErroringUploadHandler())
    114115    return file_upload_echo(request)
     116
     117def file_upload_content_type_extra(request):
     118    request.upload_handlers.insert(0, ContentTypeExtraUploadHandler())
     119    r = dict([(k, f.read()) for k, f in request.FILES.items()])
     120    return HttpResponse(simplejson.dumps(r))
Back to Top