2014-10-15 15:42:06 +08:00
|
|
|
import os
|
2015-01-28 20:35:27 +08:00
|
|
|
from datetime import datetime
|
2017-01-07 19:11:46 +08:00
|
|
|
from urllib.parse import urljoin
|
2008-08-09 04:59:02 +08:00
|
|
|
|
|
|
|
from django.conf import settings
|
2014-10-15 15:42:06 +08:00
|
|
|
from django.core.exceptions import SuspiciousFileOperation
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.core.files import File, locks
|
2009-03-02 12:48:47 +08:00
|
|
|
from django.core.files.move import file_move_safe
|
2016-01-07 12:34:55 +08:00
|
|
|
from django.core.signals import setting_changed
|
2016-02-09 23:00:14 +08:00
|
|
|
from django.utils import timezone
|
2017-01-20 21:01:02 +08:00
|
|
|
from django.utils._os import safe_join
|
2014-08-08 22:20:08 +08:00
|
|
|
from django.utils.crypto import get_random_string
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.utils.deconstruct import deconstructible
|
2017-03-04 22:47:49 +08:00
|
|
|
from django.utils.encoding import filepath_to_uri
|
2016-01-07 12:34:55 +08:00
|
|
|
from django.utils.functional import LazyObject, cached_property
|
2014-01-21 04:15:14 +08:00
|
|
|
from django.utils.module_loading import import_string
|
2008-08-09 23:16:47 +08:00
|
|
|
from django.utils.text import get_valid_filename
|
2008-08-09 04:59:02 +08:00
|
|
|
|
|
|
|
__all__ = ('Storage', 'FileSystemStorage', 'DefaultStorage', 'default_storage')
|
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class Storage:
|
2008-08-09 04:59:02 +08:00
|
|
|
"""
|
|
|
|
A base storage class, providing some default behaviors that all other
|
|
|
|
storage systems can inherit or override, as necessary.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# The following methods represent a public interface to private methods.
|
|
|
|
# These shouldn't be overridden by subclasses unless absolutely necessary.
|
|
|
|
|
2011-09-13 23:10:49 +08:00
|
|
|
def open(self, name, mode='rb'):
|
2017-01-26 03:02:33 +08:00
|
|
|
"""Retrieve the specified file from storage."""
|
2011-09-13 23:10:49 +08:00
|
|
|
return self._open(name, mode)
|
2008-08-09 04:59:02 +08:00
|
|
|
|
2014-10-15 15:42:06 +08:00
|
|
|
def save(self, name, content, max_length=None):
|
2008-08-09 04:59:02 +08:00
|
|
|
"""
|
2017-01-26 03:02:33 +08:00
|
|
|
Save new content to the file specified by name. The content should be
|
2013-02-23 20:42:04 +08:00
|
|
|
a proper File object or any python file-like object, ready to be read
|
|
|
|
from the beginning.
|
2008-08-09 04:59:02 +08:00
|
|
|
"""
|
|
|
|
# Get the proper name for the file, as it will actually be saved.
|
|
|
|
if name is None:
|
|
|
|
name = content.name
|
2009-03-02 12:48:47 +08:00
|
|
|
|
2013-02-23 20:42:04 +08:00
|
|
|
if not hasattr(content, 'chunks'):
|
2016-04-13 13:38:56 +08:00
|
|
|
content = File(content, name)
|
2013-02-23 20:42:04 +08:00
|
|
|
|
2015-08-20 21:43:07 +08:00
|
|
|
name = self.get_available_name(name, max_length=max_length)
|
2016-03-21 09:51:17 +08:00
|
|
|
return self._save(name, content)
|
2008-08-09 04:59:02 +08:00
|
|
|
|
|
|
|
# These methods are part of the public API, with default implementations.
|
|
|
|
|
|
|
|
def get_valid_name(self, name):
|
|
|
|
"""
|
2017-01-26 03:02:33 +08:00
|
|
|
Return a filename, based on the provided filename, that's suitable for
|
2008-08-09 04:59:02 +08:00
|
|
|
use in the target storage system.
|
|
|
|
"""
|
|
|
|
return get_valid_filename(name)
|
|
|
|
|
2014-10-15 15:42:06 +08:00
|
|
|
def get_available_name(self, name, max_length=None):
|
2008-08-09 04:59:02 +08:00
|
|
|
"""
|
2017-01-26 03:02:33 +08:00
|
|
|
Return a filename that's free on the target storage system and
|
2008-08-09 04:59:02 +08:00
|
|
|
available for new content to be written to.
|
|
|
|
"""
|
2009-05-08 13:50:31 +08:00
|
|
|
dir_name, file_name = os.path.split(name)
|
|
|
|
file_root, file_ext = os.path.splitext(file_name)
|
2014-08-08 22:20:08 +08:00
|
|
|
# If the filename already exists, add an underscore and a random 7
|
|
|
|
# character alphanumeric string (before the file extension, if one
|
|
|
|
# exists) to the filename until the generated filename doesn't exist.
|
2014-10-15 15:42:06 +08:00
|
|
|
# Truncate original name if required, so the new filename does not
|
|
|
|
# exceed the max_length.
|
|
|
|
while self.exists(name) or (max_length and len(name) > max_length):
|
2009-05-08 13:50:31 +08:00
|
|
|
# file_ext includes the dot.
|
2014-08-08 22:20:08 +08:00
|
|
|
name = os.path.join(dir_name, "%s_%s%s" % (file_root, get_random_string(7), file_ext))
|
2014-10-15 15:42:06 +08:00
|
|
|
if max_length is None:
|
|
|
|
continue
|
|
|
|
# Truncate file_root if max_length exceeded.
|
|
|
|
truncation = len(name) - max_length
|
|
|
|
if truncation > 0:
|
|
|
|
file_root = file_root[:-truncation]
|
|
|
|
# Entire file_root was truncated in attempt to find an available filename.
|
|
|
|
if not file_root:
|
|
|
|
raise SuspiciousFileOperation(
|
|
|
|
'Storage can not find an available filename for "%s". '
|
|
|
|
'Please make sure that the corresponding file field '
|
|
|
|
'allows sufficient "max_length".' % name
|
|
|
|
)
|
|
|
|
name = os.path.join(dir_name, "%s_%s%s" % (file_root, get_random_string(7), file_ext))
|
2008-08-09 04:59:02 +08:00
|
|
|
return name
|
|
|
|
|
2016-03-21 09:51:17 +08:00
|
|
|
def generate_filename(self, filename):
|
|
|
|
"""
|
|
|
|
Validate the filename by calling get_valid_name() and return a filename
|
|
|
|
to be passed to the save() method.
|
|
|
|
"""
|
|
|
|
# `filename` may include a path as returned by FileField.upload_to.
|
|
|
|
dirname, filename = os.path.split(filename)
|
|
|
|
return os.path.normpath(os.path.join(dirname, self.get_valid_name(filename)))
|
|
|
|
|
2008-08-09 04:59:02 +08:00
|
|
|
def path(self, name):
|
|
|
|
"""
|
2017-01-26 03:02:33 +08:00
|
|
|
Return a local filesystem path where the file can be retrieved using
|
2008-08-09 04:59:02 +08:00
|
|
|
Python's built-in open() function. Storage systems that can't be
|
|
|
|
accessed using open() should *not* implement this method.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError("This backend doesn't support absolute paths.")
|
|
|
|
|
|
|
|
# The following methods form the public API for storage systems, but with
|
|
|
|
# no default implementations. Subclasses must implement *all* of these.
|
|
|
|
|
|
|
|
def delete(self, name):
|
|
|
|
"""
|
2017-01-26 03:02:33 +08:00
|
|
|
Delete the specified file from the storage system.
|
2008-08-09 04:59:02 +08:00
|
|
|
"""
|
2013-09-07 02:24:52 +08:00
|
|
|
raise NotImplementedError('subclasses of Storage must provide a delete() method')
|
2008-08-09 04:59:02 +08:00
|
|
|
|
|
|
|
def exists(self, name):
|
|
|
|
"""
|
2017-01-26 03:02:33 +08:00
|
|
|
Return True if a file referenced by the given name already exists in the
|
2008-08-09 04:59:02 +08:00
|
|
|
storage system, or False if the name is available for a new file.
|
|
|
|
"""
|
2014-05-29 08:39:14 +08:00
|
|
|
raise NotImplementedError('subclasses of Storage must provide an exists() method')
|
2008-08-09 04:59:02 +08:00
|
|
|
|
|
|
|
def listdir(self, path):
|
|
|
|
"""
|
2017-01-26 03:02:33 +08:00
|
|
|
List the contents of the specified path. Return a 2-tuple of lists:
|
2008-08-09 04:59:02 +08:00
|
|
|
the first item being directories, the second item being files.
|
|
|
|
"""
|
2013-09-07 02:24:52 +08:00
|
|
|
raise NotImplementedError('subclasses of Storage must provide a listdir() method')
|
2008-08-09 04:59:02 +08:00
|
|
|
|
|
|
|
def size(self, name):
|
|
|
|
"""
|
2017-01-26 03:02:33 +08:00
|
|
|
Return the total size, in bytes, of the file specified by name.
|
2008-08-09 04:59:02 +08:00
|
|
|
"""
|
2013-09-07 02:24:52 +08:00
|
|
|
raise NotImplementedError('subclasses of Storage must provide a size() method')
|
2008-08-09 04:59:02 +08:00
|
|
|
|
|
|
|
def url(self, name):
|
|
|
|
"""
|
2017-01-26 03:02:33 +08:00
|
|
|
Return an absolute URL where the file's contents can be accessed
|
2010-10-09 16:12:50 +08:00
|
|
|
directly by a Web browser.
|
2008-08-09 04:59:02 +08:00
|
|
|
"""
|
2013-09-07 02:24:52 +08:00
|
|
|
raise NotImplementedError('subclasses of Storage must provide a url() method')
|
2008-08-09 04:59:02 +08:00
|
|
|
|
2016-02-09 23:00:14 +08:00
|
|
|
def get_accessed_time(self, name):
|
|
|
|
"""
|
|
|
|
Return the last accessed time (as a datetime) of the file specified by
|
|
|
|
name. The datetime will be timezone-aware if USE_TZ=True.
|
|
|
|
"""
|
2017-01-01 00:11:04 +08:00
|
|
|
raise NotImplementedError('subclasses of Storage must provide a get_accessed_time() method')
|
2016-02-09 23:00:14 +08:00
|
|
|
|
|
|
|
def get_created_time(self, name):
|
|
|
|
"""
|
|
|
|
Return the creation time (as a datetime) of the file specified by name.
|
|
|
|
The datetime will be timezone-aware if USE_TZ=True.
|
|
|
|
"""
|
2017-01-01 00:11:04 +08:00
|
|
|
raise NotImplementedError('subclasses of Storage must provide a get_created_time() method')
|
2016-02-09 23:00:14 +08:00
|
|
|
|
|
|
|
def get_modified_time(self, name):
|
|
|
|
"""
|
|
|
|
Return the last modified time (as a datetime) of the file specified by
|
|
|
|
name. The datetime will be timezone-aware if USE_TZ=True.
|
|
|
|
"""
|
2017-01-01 00:11:04 +08:00
|
|
|
raise NotImplementedError('subclasses of Storage must provide a get_modified_time() method')
|
2016-02-09 23:00:14 +08:00
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2014-05-07 13:23:23 +08:00
|
|
|
@deconstructible
|
2008-08-09 04:59:02 +08:00
|
|
|
class FileSystemStorage(Storage):
|
|
|
|
"""
|
|
|
|
Standard filesystem storage
|
|
|
|
"""
|
|
|
|
|
2013-11-05 18:02:54 +08:00
|
|
|
def __init__(self, location=None, base_url=None, file_permissions_mode=None,
|
2016-01-07 12:34:55 +08:00
|
|
|
directory_permissions_mode=None):
|
|
|
|
self._location = location
|
|
|
|
self._base_url = base_url
|
|
|
|
self._file_permissions_mode = file_permissions_mode
|
|
|
|
self._directory_permissions_mode = directory_permissions_mode
|
|
|
|
setting_changed.connect(self._clear_cached_properties)
|
|
|
|
|
|
|
|
def _clear_cached_properties(self, setting, **kwargs):
|
|
|
|
"""Reset setting based property values."""
|
|
|
|
if setting == 'MEDIA_ROOT':
|
|
|
|
self.__dict__.pop('base_location', None)
|
|
|
|
self.__dict__.pop('location', None)
|
|
|
|
elif setting == 'MEDIA_URL':
|
|
|
|
self.__dict__.pop('base_url', None)
|
|
|
|
elif setting == 'FILE_UPLOAD_PERMISSIONS':
|
|
|
|
self.__dict__.pop('file_permissions_mode', None)
|
|
|
|
elif setting == 'FILE_UPLOAD_DIRECTORY_PERMISSIONS':
|
|
|
|
self.__dict__.pop('directory_permissions_mode', None)
|
|
|
|
|
|
|
|
def _value_or_setting(self, value, setting):
|
|
|
|
return setting if value is None else value
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def base_location(self):
|
|
|
|
return self._value_or_setting(self._location, settings.MEDIA_ROOT)
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def location(self):
|
2017-01-20 21:01:02 +08:00
|
|
|
return os.path.abspath(self.base_location)
|
2016-01-07 12:34:55 +08:00
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def base_url(self):
|
2016-07-28 19:37:07 +08:00
|
|
|
if self._base_url is not None and not self._base_url.endswith('/'):
|
|
|
|
self._base_url += '/'
|
2016-01-07 12:34:55 +08:00
|
|
|
return self._value_or_setting(self._base_url, settings.MEDIA_URL)
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def file_permissions_mode(self):
|
|
|
|
return self._value_or_setting(self._file_permissions_mode, settings.FILE_UPLOAD_PERMISSIONS)
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def directory_permissions_mode(self):
|
|
|
|
return self._value_or_setting(self._directory_permissions_mode, settings.FILE_UPLOAD_DIRECTORY_PERMISSIONS)
|
2008-08-09 04:59:02 +08:00
|
|
|
|
|
|
|
def _open(self, name, mode='rb'):
|
|
|
|
return File(open(self.path(name), mode))
|
|
|
|
|
|
|
|
def _save(self, name, content):
|
|
|
|
full_path = self.path(name)
|
|
|
|
|
2011-05-26 16:21:35 +08:00
|
|
|
# Create any intermediate directories that do not exist.
|
2008-08-09 04:59:02 +08:00
|
|
|
directory = os.path.dirname(full_path)
|
|
|
|
if not os.path.exists(directory):
|
2017-09-07 20:16:21 +08:00
|
|
|
try:
|
2013-11-05 18:02:54 +08:00
|
|
|
if self.directory_permissions_mode is not None:
|
2013-05-13 19:38:53 +08:00
|
|
|
# os.makedirs applies the global umask, so we reset it,
|
2013-11-05 18:02:54 +08:00
|
|
|
# for consistency with file_permissions_mode behavior.
|
2013-05-13 19:38:53 +08:00
|
|
|
old_umask = os.umask(0)
|
|
|
|
try:
|
2013-11-05 18:02:54 +08:00
|
|
|
os.makedirs(directory, self.directory_permissions_mode)
|
2013-05-13 19:38:53 +08:00
|
|
|
finally:
|
|
|
|
os.umask(old_umask)
|
|
|
|
else:
|
|
|
|
os.makedirs(directory)
|
2017-09-07 20:16:21 +08:00
|
|
|
except FileNotFoundError:
|
|
|
|
# There's a race between os.path.exists() and os.makedirs().
|
|
|
|
# If os.makedirs() fails with FileNotFoundError, the directory
|
|
|
|
# was created concurrently.
|
|
|
|
pass
|
2011-05-26 16:21:35 +08:00
|
|
|
if not os.path.isdir(directory):
|
2008-08-09 04:59:02 +08:00
|
|
|
raise IOError("%s exists and is not a directory." % directory)
|
2008-08-12 00:51:18 +08:00
|
|
|
|
|
|
|
# There's a potential race condition between get_available_name and
|
|
|
|
# saving the file; it's possible that two threads might return the
|
|
|
|
# same name, at which point all sorts of fun happens. So we need to
|
|
|
|
# try to create the file, but if it already exists we have to go back
|
|
|
|
# to get_available_name() and try again.
|
|
|
|
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
# This file has a file path that we can move.
|
|
|
|
if hasattr(content, 'temporary_file_path'):
|
|
|
|
file_move_safe(content.temporary_file_path(), full_path)
|
|
|
|
|
|
|
|
# This is a normal uploadedfile that we can stream.
|
|
|
|
else:
|
|
|
|
# This fun binary flag incantation makes os.open throw an
|
|
|
|
# OSError if the file already exists before we open it.
|
2012-09-05 23:05:28 +08:00
|
|
|
flags = (os.O_WRONLY | os.O_CREAT | os.O_EXCL |
|
|
|
|
getattr(os, 'O_BINARY', 0))
|
|
|
|
# The current umask value is masked out by os.open!
|
|
|
|
fd = os.open(full_path, flags, 0o666)
|
2013-05-18 00:29:41 +08:00
|
|
|
_file = None
|
2008-08-12 00:51:18 +08:00
|
|
|
try:
|
|
|
|
locks.lock(fd, locks.LOCK_EX)
|
|
|
|
for chunk in content.chunks():
|
2012-08-29 21:13:20 +08:00
|
|
|
if _file is None:
|
|
|
|
mode = 'wb' if isinstance(chunk, bytes) else 'wt'
|
|
|
|
_file = os.fdopen(fd, mode)
|
|
|
|
_file.write(chunk)
|
2008-08-12 00:51:18 +08:00
|
|
|
finally:
|
|
|
|
locks.unlock(fd)
|
2012-08-29 21:13:20 +08:00
|
|
|
if _file is not None:
|
|
|
|
_file.close()
|
|
|
|
else:
|
|
|
|
os.close(fd)
|
2017-01-25 23:13:08 +08:00
|
|
|
except FileExistsError:
|
|
|
|
# A new name is needed if the file exists.
|
|
|
|
name = self.get_available_name(name)
|
|
|
|
full_path = self.path(name)
|
2008-08-12 00:51:18 +08:00
|
|
|
else:
|
|
|
|
# OK, the file save worked. Break out of the loop.
|
|
|
|
break
|
2009-03-02 12:48:47 +08:00
|
|
|
|
2013-10-19 20:40:12 +08:00
|
|
|
if self.file_permissions_mode is not None:
|
|
|
|
os.chmod(full_path, self.file_permissions_mode)
|
2009-03-02 12:48:47 +08:00
|
|
|
|
2016-03-21 09:51:17 +08:00
|
|
|
# Store filenames with forward slashes, even on Windows.
|
2017-03-04 22:47:49 +08:00
|
|
|
return name.replace('\\', '/')
|
2008-08-09 04:59:02 +08:00
|
|
|
|
|
|
|
def delete(self, name):
|
2013-06-27 16:59:30 +08:00
|
|
|
assert name, "The name argument is not allowed to be empty."
|
2008-08-09 04:59:02 +08:00
|
|
|
name = self.path(name)
|
2017-02-22 22:44:25 +08:00
|
|
|
# If the file or directory exists, delete it from the filesystem.
|
2017-09-07 20:16:21 +08:00
|
|
|
try:
|
2017-02-22 22:44:25 +08:00
|
|
|
if os.path.isdir(name):
|
|
|
|
os.rmdir(name)
|
|
|
|
else:
|
|
|
|
os.remove(name)
|
2017-09-07 20:16:21 +08:00
|
|
|
except FileNotFoundError:
|
|
|
|
# FileNotFoundError is raised if the file or directory was removed
|
|
|
|
# concurrently.
|
|
|
|
pass
|
2008-08-09 04:59:02 +08:00
|
|
|
|
|
|
|
def exists(self, name):
|
|
|
|
return os.path.exists(self.path(name))
|
|
|
|
|
|
|
|
def listdir(self, path):
|
|
|
|
path = self.path(path)
|
|
|
|
directories, files = [], []
|
|
|
|
for entry in os.listdir(path):
|
|
|
|
if os.path.isdir(os.path.join(path, entry)):
|
|
|
|
directories.append(entry)
|
|
|
|
else:
|
|
|
|
files.append(entry)
|
|
|
|
return directories, files
|
|
|
|
|
|
|
|
def path(self, name):
|
2014-11-12 01:59:49 +08:00
|
|
|
return safe_join(self.location, name)
|
2008-08-09 04:59:02 +08:00
|
|
|
|
|
|
|
def size(self, name):
|
|
|
|
return os.path.getsize(self.path(name))
|
|
|
|
|
|
|
|
def url(self, name):
|
|
|
|
if self.base_url is None:
|
|
|
|
raise ValueError("This file is not accessible via a URL.")
|
2016-04-03 23:21:56 +08:00
|
|
|
url = filepath_to_uri(name)
|
|
|
|
if url is not None:
|
|
|
|
url = url.lstrip('/')
|
|
|
|
return urljoin(self.base_url, url)
|
2008-08-09 04:59:02 +08:00
|
|
|
|
2016-02-09 23:00:14 +08:00
|
|
|
def _datetime_from_timestamp(self, ts):
|
|
|
|
"""
|
|
|
|
If timezone support is enabled, make an aware datetime object in UTC;
|
|
|
|
otherwise make a naive one in the local timezone.
|
|
|
|
"""
|
2018-06-28 21:26:41 +08:00
|
|
|
if settings.USE_TZ:
|
|
|
|
# Safe to use .replace() because UTC doesn't have DST
|
|
|
|
return datetime.utcfromtimestamp(ts).replace(tzinfo=timezone.utc)
|
|
|
|
else:
|
|
|
|
return datetime.fromtimestamp(ts)
|
2016-02-09 23:00:14 +08:00
|
|
|
|
|
|
|
def get_accessed_time(self, name):
|
|
|
|
return self._datetime_from_timestamp(os.path.getatime(self.path(name)))
|
|
|
|
|
|
|
|
def get_created_time(self, name):
|
|
|
|
return self._datetime_from_timestamp(os.path.getctime(self.path(name)))
|
|
|
|
|
|
|
|
def get_modified_time(self, name):
|
|
|
|
return self._datetime_from_timestamp(os.path.getmtime(self.path(name)))
|
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2009-03-02 12:48:47 +08:00
|
|
|
def get_storage_class(import_path=None):
|
2014-01-21 04:15:14 +08:00
|
|
|
return import_string(import_path or settings.DEFAULT_FILE_STORAGE)
|
2008-08-09 04:59:02 +08:00
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2009-03-02 12:48:47 +08:00
|
|
|
class DefaultStorage(LazyObject):
|
|
|
|
def _setup(self):
|
|
|
|
self._wrapped = get_storage_class()()
|
|
|
|
|
2016-11-13 01:11:23 +08:00
|
|
|
|
2008-08-09 04:59:02 +08:00
|
|
|
default_storage = DefaultStorage()
|