2008-07-01 23:10:51 +08:00
|
|
|
"""
|
|
|
|
Base file upload handler classes, and the built-in concrete subclasses
|
|
|
|
"""
|
2008-07-22 11:24:09 +08:00
|
|
|
|
2012-05-06 01:47:03 +08:00
|
|
|
from io import BytesIO
|
2008-07-01 23:10:51 +08:00
|
|
|
|
|
|
|
from django.conf import settings
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.core.files.uploadedfile import (
|
|
|
|
InMemoryUploadedFile, TemporaryUploadedFile,
|
|
|
|
)
|
2014-01-21 04:15:14 +08:00
|
|
|
from django.utils.module_loading import import_string
|
2008-07-01 23:10:51 +08:00
|
|
|
|
2013-10-25 01:30:03 +08:00
|
|
|
__all__ = [
|
|
|
|
'UploadFileException', 'StopUpload', 'SkipFile', 'FileUploadHandler',
|
|
|
|
'TemporaryFileUploadHandler', 'MemoryFileUploadHandler', 'load_handler',
|
|
|
|
'StopFutureHandlers'
|
|
|
|
]
|
2008-07-01 23:10:51 +08:00
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2008-07-01 23:10:51 +08:00
|
|
|
class UploadFileException(Exception):
|
|
|
|
"""
|
|
|
|
Any error having to do with uploading files.
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2008-07-01 23:10:51 +08:00
|
|
|
class StopUpload(UploadFileException):
|
|
|
|
"""
|
|
|
|
This exception is raised when an upload must abort.
|
|
|
|
"""
|
|
|
|
def __init__(self, connection_reset=False):
|
|
|
|
"""
|
|
|
|
If ``connection_reset`` is ``True``, Django knows will halt the upload
|
|
|
|
without consuming the rest of the upload. This will cause the browser to
|
|
|
|
show a "connection reset" error.
|
|
|
|
"""
|
|
|
|
self.connection_reset = connection_reset
|
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2008-07-01 23:10:51 +08:00
|
|
|
if self.connection_reset:
|
2012-06-08 00:08:47 +08:00
|
|
|
return 'StopUpload: Halt current upload.'
|
2008-07-01 23:10:51 +08:00
|
|
|
else:
|
2012-06-08 00:08:47 +08:00
|
|
|
return 'StopUpload: Consume request data, then halt.'
|
2008-07-01 23:10:51 +08:00
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2008-07-01 23:10:51 +08:00
|
|
|
class SkipFile(UploadFileException):
|
|
|
|
"""
|
|
|
|
This exception is raised by an upload handler that wants to skip a given file.
|
|
|
|
"""
|
|
|
|
pass
|
2009-03-08 17:31:30 +08:00
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2008-07-01 23:10:51 +08:00
|
|
|
class StopFutureHandlers(UploadFileException):
|
|
|
|
"""
|
2019-08-05 23:47:50 +08:00
|
|
|
Upload handlers that have handled a file and do not want future handlers to
|
2008-07-01 23:10:51 +08:00
|
|
|
run should raise this exception instead of returning None.
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class FileUploadHandler:
|
2008-07-01 23:10:51 +08:00
|
|
|
"""
|
|
|
|
Base class for streaming upload handlers.
|
|
|
|
"""
|
2013-11-03 05:02:56 +08:00
|
|
|
chunk_size = 64 * 2 ** 10 # : The default chunk size is 64 KB.
|
2008-07-01 23:10:51 +08:00
|
|
|
|
|
|
|
def __init__(self, request=None):
|
|
|
|
self.file_name = None
|
|
|
|
self.content_type = None
|
|
|
|
self.content_length = None
|
|
|
|
self.charset = None
|
2013-04-20 01:20:23 +08:00
|
|
|
self.content_type_extra = None
|
2008-07-01 23:10:51 +08:00
|
|
|
self.request = request
|
|
|
|
|
|
|
|
def handle_raw_input(self, input_data, META, content_length, boundary, encoding=None):
|
|
|
|
"""
|
|
|
|
Handle the raw input from the client.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
|
|
|
:input_data:
|
|
|
|
An object that supports reading via .read().
|
|
|
|
:META:
|
|
|
|
``request.META``.
|
|
|
|
:content_length:
|
|
|
|
The (integer) value of the Content-Length header from the
|
|
|
|
client.
|
|
|
|
:boundary: The boundary from the Content-Type header. Be sure to
|
|
|
|
prepend two '--'.
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
2013-04-20 01:20:23 +08:00
|
|
|
def new_file(self, field_name, file_name, content_type, content_length, charset=None, content_type_extra=None):
|
2008-07-01 23:10:51 +08:00
|
|
|
"""
|
|
|
|
Signal that a new file has been started.
|
|
|
|
|
|
|
|
Warning: As with any data from the client, you should not trust
|
|
|
|
content_length (and sometimes won't even get it).
|
|
|
|
"""
|
|
|
|
self.field_name = field_name
|
|
|
|
self.file_name = file_name
|
|
|
|
self.content_type = content_type
|
|
|
|
self.content_length = content_length
|
|
|
|
self.charset = charset
|
2013-04-20 01:20:23 +08:00
|
|
|
self.content_type_extra = content_type_extra
|
2008-07-01 23:10:51 +08:00
|
|
|
|
|
|
|
def receive_data_chunk(self, raw_data, start):
|
|
|
|
"""
|
|
|
|
Receive data from the streamed upload parser. ``start`` is the position
|
|
|
|
in the file of the chunk.
|
|
|
|
"""
|
2014-03-02 22:25:53 +08:00
|
|
|
raise NotImplementedError('subclasses of FileUploadHandler must provide a receive_data_chunk() method')
|
2008-07-01 23:10:51 +08:00
|
|
|
|
|
|
|
def file_complete(self, file_size):
|
|
|
|
"""
|
|
|
|
Signal that a file has completed. File size corresponds to the actual
|
|
|
|
size accumulated by all the chunks.
|
|
|
|
|
2010-10-28 04:08:20 +08:00
|
|
|
Subclasses should return a valid ``UploadedFile`` object.
|
2008-07-01 23:10:51 +08:00
|
|
|
"""
|
2013-09-07 02:24:52 +08:00
|
|
|
raise NotImplementedError('subclasses of FileUploadHandler must provide a file_complete() method')
|
2008-07-01 23:10:51 +08:00
|
|
|
|
|
|
|
def upload_complete(self):
|
|
|
|
"""
|
|
|
|
Signal that the upload is complete. Subclasses should perform cleanup
|
|
|
|
that is necessary for this handler.
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2008-07-01 23:10:51 +08:00
|
|
|
class TemporaryFileUploadHandler(FileUploadHandler):
|
|
|
|
"""
|
|
|
|
Upload handler that streams data into a temporary file.
|
|
|
|
"""
|
2015-04-20 17:59:30 +08:00
|
|
|
def new_file(self, *args, **kwargs):
|
2008-07-01 23:10:51 +08:00
|
|
|
"""
|
|
|
|
Create the file object to append to as data is coming in.
|
|
|
|
"""
|
2017-01-21 21:13:44 +08:00
|
|
|
super().new_file(*args, **kwargs)
|
2013-04-20 01:20:23 +08:00
|
|
|
self.file = TemporaryUploadedFile(self.file_name, self.content_type, 0, self.charset, self.content_type_extra)
|
2008-07-01 23:10:51 +08:00
|
|
|
|
|
|
|
def receive_data_chunk(self, raw_data, start):
|
2008-07-08 07:16:00 +08:00
|
|
|
self.file.write(raw_data)
|
2008-07-01 23:10:51 +08:00
|
|
|
|
|
|
|
def file_complete(self, file_size):
|
|
|
|
self.file.seek(0)
|
2008-07-08 07:16:00 +08:00
|
|
|
self.file.size = file_size
|
|
|
|
return self.file
|
2008-07-01 23:10:51 +08:00
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2008-07-01 23:10:51 +08:00
|
|
|
class MemoryFileUploadHandler(FileUploadHandler):
|
|
|
|
"""
|
|
|
|
File upload handler to stream uploads into memory (used for small files).
|
|
|
|
"""
|
|
|
|
|
|
|
|
def handle_raw_input(self, input_data, META, content_length, boundary, encoding=None):
|
|
|
|
"""
|
2017-01-26 03:02:33 +08:00
|
|
|
Use the content_length to signal whether or not this handler should be
|
|
|
|
used.
|
2008-07-01 23:10:51 +08:00
|
|
|
"""
|
|
|
|
# Check the content-length header to see if we should
|
2009-05-18 00:45:28 +08:00
|
|
|
# If the post is too large, we cannot use the Memory handler.
|
2018-01-12 22:05:16 +08:00
|
|
|
self.activated = content_length <= settings.FILE_UPLOAD_MAX_MEMORY_SIZE
|
2008-07-01 23:10:51 +08:00
|
|
|
|
|
|
|
def new_file(self, *args, **kwargs):
|
2017-01-21 21:13:44 +08:00
|
|
|
super().new_file(*args, **kwargs)
|
2008-07-01 23:10:51 +08:00
|
|
|
if self.activated:
|
2012-05-06 01:47:03 +08:00
|
|
|
self.file = BytesIO()
|
2008-07-01 23:10:51 +08:00
|
|
|
raise StopFutureHandlers()
|
|
|
|
|
|
|
|
def receive_data_chunk(self, raw_data, start):
|
2017-01-26 03:02:33 +08:00
|
|
|
"""Add the data to the BytesIO file."""
|
2008-07-01 23:10:51 +08:00
|
|
|
if self.activated:
|
|
|
|
self.file.write(raw_data)
|
|
|
|
else:
|
|
|
|
return raw_data
|
|
|
|
|
|
|
|
def file_complete(self, file_size):
|
2017-01-26 03:02:33 +08:00
|
|
|
"""Return a file object if this handler is activated."""
|
2008-07-01 23:10:51 +08:00
|
|
|
if not self.activated:
|
|
|
|
return
|
|
|
|
|
2009-05-11 18:13:43 +08:00
|
|
|
self.file.seek(0)
|
2008-07-02 02:47:46 +08:00
|
|
|
return InMemoryUploadedFile(
|
2013-11-03 17:22:11 +08:00
|
|
|
file=self.file,
|
|
|
|
field_name=self.field_name,
|
|
|
|
name=self.file_name,
|
|
|
|
content_type=self.content_type,
|
|
|
|
size=file_size,
|
|
|
|
charset=self.charset,
|
|
|
|
content_type_extra=self.content_type_extra
|
2008-07-02 02:47:46 +08:00
|
|
|
)
|
2008-07-01 23:10:51 +08:00
|
|
|
|
|
|
|
|
|
|
|
def load_handler(path, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Given a path to a handler, return an instance of that handler.
|
|
|
|
|
|
|
|
E.g.::
|
2013-10-11 19:25:14 +08:00
|
|
|
>>> from django.http import HttpRequest
|
|
|
|
>>> request = HttpRequest()
|
2008-07-01 23:10:51 +08:00
|
|
|
>>> load_handler('django.core.files.uploadhandler.TemporaryFileUploadHandler', request)
|
|
|
|
<TemporaryFileUploadHandler object at 0x...>
|
|
|
|
"""
|
2014-01-21 04:15:14 +08:00
|
|
|
return import_string(path)(*args, **kwargs)
|