2008-07-01 23:10:51 +08:00
|
|
|
"""
|
|
|
|
Classes representing uploaded files.
|
|
|
|
"""
|
|
|
|
|
2013-11-03 03:55:36 +08:00
|
|
|
import errno
|
2008-07-01 23:10:51 +08:00
|
|
|
import os
|
2012-05-06 01:47:03 +08:00
|
|
|
from io import BytesIO
|
2008-07-01 23:10:51 +08:00
|
|
|
|
2008-07-08 07:16:00 +08:00
|
|
|
from django.conf import settings
|
2008-07-27 06:48:51 +08:00
|
|
|
from django.core.files import temp as tempfile
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.core.files.base import File
|
2012-08-30 04:40:51 +08:00
|
|
|
from django.utils.encoding import force_str
|
2008-07-27 06:48:51 +08:00
|
|
|
|
Fixed #7830 -- Removed all of the remaining, deprecated, non-oldforms features:
* Support for representing files as strings was removed. Use `django.core.files.base.ContentFile` instead.
* Support for representing uploaded files as dictionaries was removed. Use `django.core.files.uploadedfile.SimpleUploadedFile` instead.
* The `filename`, `file_name`, `file_size`, and `chuck` properties of `UploadedFile` were removed. Use the `name`, `name`, `size`, and `chunks` properties instead, respectively.
* The `get_FIELD_filename`, `get_FIELD_url`, `get_FIELD_size`, and `save_FIELD_file` methods for Models with `FileField` fields were removed. Instead, use the `path`, `url`, and `size` attributes and `save` method on the field itself, respectively.
* The `get_FIELD_width` and `get_FIELD_height` methods for Models with `ImageField` fields were removed. Use the `width` and `height` attributes on the field itself instead.
* The dispatcher `connect`, `disconnect`, `send`, and `sendExact` functions were removed. Use the signal object's own `connect`, `disconnect`, `send`, and `send` methods instead, respectively.
* The `form_for_model` and `form_for_instance` functions were removed. Use a `ModelForm` subclass instead.
* Support for importing `django.newforms` was removed. Use `django.forms` instead.
* Support for importing `django.utils.images` was removed. Use `django.core.files.images` instead.
* Support for the `follow` argument in the `create_object` and `update_object` generic views was removed. Use the `django.forms` package and the new `form_class` argument instead.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@8291 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-08-11 05:10:47 +08:00
|
|
|
__all__ = ('UploadedFile', 'TemporaryUploadedFile', 'InMemoryUploadedFile',
|
|
|
|
'SimpleUploadedFile')
|
2008-07-01 23:10:51 +08:00
|
|
|
|
2013-11-03 03:55:36 +08:00
|
|
|
|
2008-08-09 04:59:02 +08:00
|
|
|
class UploadedFile(File):
|
2008-07-01 23:10:51 +08:00
|
|
|
"""
|
2008-07-03 14:04:17 +08:00
|
|
|
A abstract uploaded file (``TemporaryUploadedFile`` and
|
2008-07-01 23:10:51 +08:00
|
|
|
``InMemoryUploadedFile`` are the built-in concrete subclasses).
|
|
|
|
|
|
|
|
An ``UploadedFile`` object behaves somewhat like a file object and
|
|
|
|
represents some file data that the user submitted with a form.
|
|
|
|
"""
|
2013-11-04 02:08:55 +08:00
|
|
|
DEFAULT_CHUNK_SIZE = 64 * 2 ** 10
|
2008-07-01 23:10:51 +08:00
|
|
|
|
2013-04-20 01:20:23 +08:00
|
|
|
def __init__(self, file=None, name=None, content_type=None, size=None, charset=None, content_type_extra=None):
|
2009-05-08 23:08:09 +08:00
|
|
|
super(UploadedFile, self).__init__(file, name)
|
2008-07-08 07:16:00 +08:00
|
|
|
self.size = size
|
2008-07-01 23:10:51 +08:00
|
|
|
self.content_type = content_type
|
|
|
|
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 __repr__(self):
|
2012-08-30 04:40:51 +08:00
|
|
|
return force_str("<%s: %s (%s)>" % (
|
2012-05-19 23:43:34 +08:00
|
|
|
self.__class__.__name__, self.name, self.content_type))
|
2008-07-01 23:10:51 +08:00
|
|
|
|
2008-07-08 07:16:00 +08:00
|
|
|
def _get_name(self):
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
def _set_name(self, name):
|
2008-07-01 23:10:51 +08:00
|
|
|
# Sanitize the file name so that it can't be dangerous.
|
|
|
|
if name is not None:
|
|
|
|
# Just use the basename of the file -- anything else is dangerous.
|
|
|
|
name = os.path.basename(name)
|
2008-07-08 07:16:00 +08:00
|
|
|
|
2008-07-01 23:10:51 +08:00
|
|
|
# File names longer than 255 characters can cause problems on older OSes.
|
|
|
|
if len(name) > 255:
|
|
|
|
name, ext = os.path.splitext(name)
|
2013-09-05 00:08:13 +08:00
|
|
|
ext = ext[:255]
|
2008-07-01 23:10:51 +08:00
|
|
|
name = name[:255 - len(ext)] + ext
|
|
|
|
|
2008-07-08 07:16:00 +08:00
|
|
|
self._name = name
|
|
|
|
|
|
|
|
name = property(_get_name, _set_name)
|
|
|
|
|
2013-11-03 03:55:36 +08:00
|
|
|
|
2008-07-01 23:10:51 +08:00
|
|
|
class TemporaryUploadedFile(UploadedFile):
|
|
|
|
"""
|
|
|
|
A file uploaded to a temporary location (i.e. stream-to-disk).
|
|
|
|
"""
|
2014-02-18 19:25:39 +08:00
|
|
|
def __init__(self, name, content_type, size, charset, content_type_extra=None):
|
2008-07-08 07:16:00 +08:00
|
|
|
if settings.FILE_UPLOAD_TEMP_DIR:
|
2016-03-29 06:33:29 +08:00
|
|
|
file = tempfile.NamedTemporaryFile(suffix='.upload', dir=settings.FILE_UPLOAD_TEMP_DIR)
|
2008-07-08 07:16:00 +08:00
|
|
|
else:
|
2009-05-08 23:08:09 +08:00
|
|
|
file = tempfile.NamedTemporaryFile(suffix='.upload')
|
2013-04-20 01:20:23 +08:00
|
|
|
super(TemporaryUploadedFile, self).__init__(file, name, content_type, size, charset, content_type_extra)
|
2008-07-01 23:10:51 +08:00
|
|
|
|
|
|
|
def temporary_file_path(self):
|
|
|
|
"""
|
|
|
|
Returns the full path of this file.
|
|
|
|
"""
|
2009-05-08 23:08:09 +08:00
|
|
|
return self.file.name
|
|
|
|
|
2008-08-06 01:43:05 +08:00
|
|
|
def close(self):
|
|
|
|
try:
|
2009-05-11 17:57:19 +08:00
|
|
|
return self.file.close()
|
2012-04-29 00:09:37 +08:00
|
|
|
except OSError as e:
|
2013-11-03 03:55:36 +08:00
|
|
|
if e.errno != errno.ENOENT:
|
2009-05-11 17:57:19 +08:00
|
|
|
# Means the file was moved or deleted before the tempfile
|
|
|
|
# could unlink it. Still sets self.file.close_called and
|
|
|
|
# calls self.file.file.close() before the exception
|
|
|
|
raise
|
Fixed #7830 -- Removed all of the remaining, deprecated, non-oldforms features:
* Support for representing files as strings was removed. Use `django.core.files.base.ContentFile` instead.
* Support for representing uploaded files as dictionaries was removed. Use `django.core.files.uploadedfile.SimpleUploadedFile` instead.
* The `filename`, `file_name`, `file_size`, and `chuck` properties of `UploadedFile` were removed. Use the `name`, `name`, `size`, and `chunks` properties instead, respectively.
* The `get_FIELD_filename`, `get_FIELD_url`, `get_FIELD_size`, and `save_FIELD_file` methods for Models with `FileField` fields were removed. Instead, use the `path`, `url`, and `size` attributes and `save` method on the field itself, respectively.
* The `get_FIELD_width` and `get_FIELD_height` methods for Models with `ImageField` fields were removed. Use the `width` and `height` attributes on the field itself instead.
* The dispatcher `connect`, `disconnect`, `send`, and `sendExact` functions were removed. Use the signal object's own `connect`, `disconnect`, `send`, and `send` methods instead, respectively.
* The `form_for_model` and `form_for_instance` functions were removed. Use a `ModelForm` subclass instead.
* Support for importing `django.newforms` was removed. Use `django.forms` instead.
* Support for importing `django.utils.images` was removed. Use `django.core.files.images` instead.
* Support for the `follow` argument in the `create_object` and `update_object` generic views was removed. Use the `django.forms` package and the new `form_class` argument instead.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@8291 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-08-11 05:10:47 +08:00
|
|
|
|
2013-11-03 03:55:36 +08:00
|
|
|
|
2008-07-01 23:10:51 +08:00
|
|
|
class InMemoryUploadedFile(UploadedFile):
|
|
|
|
"""
|
|
|
|
A file uploaded into memory (i.e. stream-to-memory).
|
|
|
|
"""
|
2014-02-18 19:25:39 +08:00
|
|
|
def __init__(self, file, field_name, name, content_type, size, charset, content_type_extra=None):
|
2013-04-20 01:20:23 +08:00
|
|
|
super(InMemoryUploadedFile, self).__init__(file, name, content_type, size, charset, content_type_extra)
|
2008-07-01 23:10:51 +08:00
|
|
|
self.field_name = field_name
|
|
|
|
|
2009-05-11 17:57:19 +08:00
|
|
|
def open(self, mode=None):
|
2009-05-08 23:08:09 +08:00
|
|
|
self.file.seek(0)
|
2008-07-01 23:10:51 +08:00
|
|
|
|
2008-07-08 07:16:00 +08:00
|
|
|
def chunks(self, chunk_size=None):
|
2009-05-08 23:08:09 +08:00
|
|
|
self.file.seek(0)
|
2008-07-01 23:10:51 +08:00
|
|
|
yield self.read()
|
|
|
|
|
|
|
|
def multiple_chunks(self, chunk_size=None):
|
|
|
|
# Since it's in memory, we'll never have multiple chunks.
|
|
|
|
return False
|
|
|
|
|
2008-08-28 04:29:45 +08:00
|
|
|
|
2008-07-01 23:10:51 +08:00
|
|
|
class SimpleUploadedFile(InMemoryUploadedFile):
|
|
|
|
"""
|
|
|
|
A simple representation of a file, which just has content, size, and a name.
|
|
|
|
"""
|
|
|
|
def __init__(self, name, content, content_type='text/plain'):
|
2012-05-06 01:47:03 +08:00
|
|
|
content = content or b''
|
|
|
|
super(SimpleUploadedFile, self).__init__(BytesIO(content), None, name,
|
2013-04-20 01:20:23 +08:00
|
|
|
content_type, len(content), None, None)
|
2008-07-01 23:10:51 +08:00
|
|
|
|
2013-11-20 10:02:30 +08:00
|
|
|
@classmethod
|
2008-07-01 23:10:51 +08:00
|
|
|
def from_dict(cls, file_dict):
|
|
|
|
"""
|
|
|
|
Creates a SimpleUploadedFile object from
|
|
|
|
a dictionary object with the following keys:
|
|
|
|
- filename
|
|
|
|
- content-type
|
|
|
|
- content
|
|
|
|
"""
|
|
|
|
return cls(file_dict['filename'],
|
|
|
|
file_dict['content'],
|
|
|
|
file_dict.get('content-type', 'text/plain'))
|