2
0

storage.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. import os
  2. import pathlib
  3. from datetime import datetime
  4. from urllib.parse import urljoin
  5. from django.conf import settings
  6. from django.core.exceptions import SuspiciousFileOperation
  7. from django.core.files import File, locks
  8. from django.core.files.move import file_move_safe
  9. from django.core.files.utils import validate_file_name
  10. from django.core.signals import setting_changed
  11. from django.utils import timezone
  12. from django.utils._os import safe_join
  13. from django.utils.crypto import get_random_string
  14. from django.utils.deconstruct import deconstructible
  15. from django.utils.encoding import filepath_to_uri
  16. from django.utils.functional import LazyObject, cached_property
  17. from django.utils.module_loading import import_string
  18. from django.utils.text import get_valid_filename
  19. __all__ = (
  20. "Storage",
  21. "FileSystemStorage",
  22. "DefaultStorage",
  23. "default_storage",
  24. "get_storage_class",
  25. )
  26. class Storage:
  27. """
  28. A base storage class, providing some default behaviors that all other
  29. storage systems can inherit or override, as necessary.
  30. """
  31. # The following methods represent a public interface to private methods.
  32. # These shouldn't be overridden by subclasses unless absolutely necessary.
  33. def open(self, name, mode="rb"):
  34. """Retrieve the specified file from storage."""
  35. return self._open(name, mode)
  36. def save(self, name, content, max_length=None):
  37. """
  38. Save new content to the file specified by name. The content should be
  39. a proper File object or any Python file-like object, ready to be read
  40. from the beginning.
  41. """
  42. # Get the proper name for the file, as it will actually be saved.
  43. if name is None:
  44. name = content.name
  45. if not hasattr(content, "chunks"):
  46. content = File(content, name)
  47. name = self.get_available_name(name, max_length=max_length)
  48. name = self._save(name, content)
  49. # Ensure that the name returned from the storage system is still valid.
  50. validate_file_name(name, allow_relative_path=True)
  51. return name
  52. # These methods are part of the public API, with default implementations.
  53. def get_valid_name(self, name):
  54. """
  55. Return a filename, based on the provided filename, that's suitable for
  56. use in the target storage system.
  57. """
  58. return get_valid_filename(name)
  59. def get_alternative_name(self, file_root, file_ext):
  60. """
  61. Return an alternative filename, by adding an underscore and a random 7
  62. character alphanumeric string (before the file extension, if one
  63. exists) to the filename.
  64. """
  65. return "%s_%s%s" % (file_root, get_random_string(7), file_ext)
  66. def get_available_name(self, name, max_length=None):
  67. """
  68. Return a filename that's free on the target storage system and
  69. available for new content to be written to.
  70. """
  71. name = str(name).replace("\\", "/")
  72. dir_name, file_name = os.path.split(name)
  73. if ".." in pathlib.PurePath(dir_name).parts:
  74. raise SuspiciousFileOperation(
  75. "Detected path traversal attempt in '%s'" % dir_name
  76. )
  77. validate_file_name(file_name)
  78. file_root, file_ext = os.path.splitext(file_name)
  79. # If the filename already exists, generate an alternative filename
  80. # until it doesn't exist.
  81. # Truncate original name if required, so the new filename does not
  82. # exceed the max_length.
  83. while self.exists(name) or (max_length and len(name) > max_length):
  84. # file_ext includes the dot.
  85. name = os.path.join(
  86. dir_name, self.get_alternative_name(file_root, file_ext)
  87. )
  88. if max_length is None:
  89. continue
  90. # Truncate file_root if max_length exceeded.
  91. truncation = len(name) - max_length
  92. if truncation > 0:
  93. file_root = file_root[:-truncation]
  94. # Entire file_root was truncated in attempt to find an
  95. # available filename.
  96. if not file_root:
  97. raise SuspiciousFileOperation(
  98. 'Storage can not find an available filename for "%s". '
  99. "Please make sure that the corresponding file field "
  100. 'allows sufficient "max_length".' % name
  101. )
  102. name = os.path.join(
  103. dir_name, self.get_alternative_name(file_root, file_ext)
  104. )
  105. return name
  106. def generate_filename(self, filename):
  107. """
  108. Validate the filename by calling get_valid_name() and return a filename
  109. to be passed to the save() method.
  110. """
  111. filename = str(filename).replace("\\", "/")
  112. # `filename` may include a path as returned by FileField.upload_to.
  113. dirname, filename = os.path.split(filename)
  114. if ".." in pathlib.PurePath(dirname).parts:
  115. raise SuspiciousFileOperation(
  116. "Detected path traversal attempt in '%s'" % dirname
  117. )
  118. return os.path.normpath(os.path.join(dirname, self.get_valid_name(filename)))
  119. def path(self, name):
  120. """
  121. Return a local filesystem path where the file can be retrieved using
  122. Python's built-in open() function. Storage systems that can't be
  123. accessed using open() should *not* implement this method.
  124. """
  125. raise NotImplementedError("This backend doesn't support absolute paths.")
  126. # The following methods form the public API for storage systems, but with
  127. # no default implementations. Subclasses must implement *all* of these.
  128. def delete(self, name):
  129. """
  130. Delete the specified file from the storage system.
  131. """
  132. raise NotImplementedError(
  133. "subclasses of Storage must provide a delete() method"
  134. )
  135. def exists(self, name):
  136. """
  137. Return True if a file referenced by the given name already exists in the
  138. storage system, or False if the name is available for a new file.
  139. """
  140. raise NotImplementedError(
  141. "subclasses of Storage must provide an exists() method"
  142. )
  143. def listdir(self, path):
  144. """
  145. List the contents of the specified path. Return a 2-tuple of lists:
  146. the first item being directories, the second item being files.
  147. """
  148. raise NotImplementedError(
  149. "subclasses of Storage must provide a listdir() method"
  150. )
  151. def size(self, name):
  152. """
  153. Return the total size, in bytes, of the file specified by name.
  154. """
  155. raise NotImplementedError("subclasses of Storage must provide a size() method")
  156. def url(self, name):
  157. """
  158. Return an absolute URL where the file's contents can be accessed
  159. directly by a web browser.
  160. """
  161. raise NotImplementedError("subclasses of Storage must provide a url() method")
  162. def get_accessed_time(self, name):
  163. """
  164. Return the last accessed time (as a datetime) of the file specified by
  165. name. The datetime will be timezone-aware if USE_TZ=True.
  166. """
  167. raise NotImplementedError(
  168. "subclasses of Storage must provide a get_accessed_time() method"
  169. )
  170. def get_created_time(self, name):
  171. """
  172. Return the creation time (as a datetime) of the file specified by name.
  173. The datetime will be timezone-aware if USE_TZ=True.
  174. """
  175. raise NotImplementedError(
  176. "subclasses of Storage must provide a get_created_time() method"
  177. )
  178. def get_modified_time(self, name):
  179. """
  180. Return the last modified time (as a datetime) of the file specified by
  181. name. The datetime will be timezone-aware if USE_TZ=True.
  182. """
  183. raise NotImplementedError(
  184. "subclasses of Storage must provide a get_modified_time() method"
  185. )
  186. @deconstructible
  187. class FileSystemStorage(Storage):
  188. """
  189. Standard filesystem storage
  190. """
  191. # The combination of O_CREAT and O_EXCL makes os.open() raise OSError if
  192. # the file already exists before it's opened.
  193. OS_OPEN_FLAGS = os.O_WRONLY | os.O_CREAT | os.O_EXCL | getattr(os, "O_BINARY", 0)
  194. def __init__(
  195. self,
  196. location=None,
  197. base_url=None,
  198. file_permissions_mode=None,
  199. directory_permissions_mode=None,
  200. ):
  201. self._location = location
  202. self._base_url = base_url
  203. self._file_permissions_mode = file_permissions_mode
  204. self._directory_permissions_mode = directory_permissions_mode
  205. setting_changed.connect(self._clear_cached_properties)
  206. def _clear_cached_properties(self, setting, **kwargs):
  207. """Reset setting based property values."""
  208. if setting == "MEDIA_ROOT":
  209. self.__dict__.pop("base_location", None)
  210. self.__dict__.pop("location", None)
  211. elif setting == "MEDIA_URL":
  212. self.__dict__.pop("base_url", None)
  213. elif setting == "FILE_UPLOAD_PERMISSIONS":
  214. self.__dict__.pop("file_permissions_mode", None)
  215. elif setting == "FILE_UPLOAD_DIRECTORY_PERMISSIONS":
  216. self.__dict__.pop("directory_permissions_mode", None)
  217. def _value_or_setting(self, value, setting):
  218. return setting if value is None else value
  219. @cached_property
  220. def base_location(self):
  221. return self._value_or_setting(self._location, settings.MEDIA_ROOT)
  222. @cached_property
  223. def location(self):
  224. return os.path.abspath(self.base_location)
  225. @cached_property
  226. def base_url(self):
  227. if self._base_url is not None and not self._base_url.endswith("/"):
  228. self._base_url += "/"
  229. return self._value_or_setting(self._base_url, settings.MEDIA_URL)
  230. @cached_property
  231. def file_permissions_mode(self):
  232. return self._value_or_setting(
  233. self._file_permissions_mode, settings.FILE_UPLOAD_PERMISSIONS
  234. )
  235. @cached_property
  236. def directory_permissions_mode(self):
  237. return self._value_or_setting(
  238. self._directory_permissions_mode, settings.FILE_UPLOAD_DIRECTORY_PERMISSIONS
  239. )
  240. def _open(self, name, mode="rb"):
  241. return File(open(self.path(name), mode))
  242. def _save(self, name, content):
  243. full_path = self.path(name)
  244. # Create any intermediate directories that do not exist.
  245. directory = os.path.dirname(full_path)
  246. try:
  247. if self.directory_permissions_mode is not None:
  248. # Set the umask because os.makedirs() doesn't apply the "mode"
  249. # argument to intermediate-level directories.
  250. old_umask = os.umask(0o777 & ~self.directory_permissions_mode)
  251. try:
  252. os.makedirs(
  253. directory, self.directory_permissions_mode, exist_ok=True
  254. )
  255. finally:
  256. os.umask(old_umask)
  257. else:
  258. os.makedirs(directory, exist_ok=True)
  259. except FileExistsError:
  260. raise FileExistsError("%s exists and is not a directory." % directory)
  261. # There's a potential race condition between get_available_name and
  262. # saving the file; it's possible that two threads might return the
  263. # same name, at which point all sorts of fun happens. So we need to
  264. # try to create the file, but if it already exists we have to go back
  265. # to get_available_name() and try again.
  266. while True:
  267. try:
  268. # This file has a file path that we can move.
  269. if hasattr(content, "temporary_file_path"):
  270. file_move_safe(content.temporary_file_path(), full_path)
  271. # This is a normal uploadedfile that we can stream.
  272. else:
  273. # The current umask value is masked out by os.open!
  274. fd = os.open(full_path, self.OS_OPEN_FLAGS, 0o666)
  275. _file = None
  276. try:
  277. locks.lock(fd, locks.LOCK_EX)
  278. for chunk in content.chunks():
  279. if _file is None:
  280. mode = "wb" if isinstance(chunk, bytes) else "wt"
  281. _file = os.fdopen(fd, mode)
  282. _file.write(chunk)
  283. finally:
  284. locks.unlock(fd)
  285. if _file is not None:
  286. _file.close()
  287. else:
  288. os.close(fd)
  289. except FileExistsError:
  290. # A new name is needed if the file exists.
  291. name = self.get_available_name(name)
  292. full_path = self.path(name)
  293. else:
  294. # OK, the file save worked. Break out of the loop.
  295. break
  296. if self.file_permissions_mode is not None:
  297. os.chmod(full_path, self.file_permissions_mode)
  298. # Ensure the saved path is always relative to the storage root.
  299. name = os.path.relpath(full_path, self.location)
  300. # Store filenames with forward slashes, even on Windows.
  301. return str(name).replace("\\", "/")
  302. def delete(self, name):
  303. if not name:
  304. raise ValueError("The name must be given to delete().")
  305. name = self.path(name)
  306. # If the file or directory exists, delete it from the filesystem.
  307. try:
  308. if os.path.isdir(name):
  309. os.rmdir(name)
  310. else:
  311. os.remove(name)
  312. except FileNotFoundError:
  313. # FileNotFoundError is raised if the file or directory was removed
  314. # concurrently.
  315. pass
  316. def exists(self, name):
  317. return os.path.lexists(self.path(name))
  318. def listdir(self, path):
  319. path = self.path(path)
  320. directories, files = [], []
  321. with os.scandir(path) as entries:
  322. for entry in entries:
  323. if entry.is_dir():
  324. directories.append(entry.name)
  325. else:
  326. files.append(entry.name)
  327. return directories, files
  328. def path(self, name):
  329. return safe_join(self.location, name)
  330. def size(self, name):
  331. return os.path.getsize(self.path(name))
  332. def url(self, name):
  333. if self.base_url is None:
  334. raise ValueError("This file is not accessible via a URL.")
  335. url = filepath_to_uri(name)
  336. if url is not None:
  337. url = url.lstrip("/")
  338. return urljoin(self.base_url, url)
  339. def _datetime_from_timestamp(self, ts):
  340. """
  341. If timezone support is enabled, make an aware datetime object in UTC;
  342. otherwise make a naive one in the local timezone.
  343. """
  344. tz = timezone.utc if settings.USE_TZ else None
  345. return datetime.fromtimestamp(ts, tz=tz)
  346. def get_accessed_time(self, name):
  347. return self._datetime_from_timestamp(os.path.getatime(self.path(name)))
  348. def get_created_time(self, name):
  349. return self._datetime_from_timestamp(os.path.getctime(self.path(name)))
  350. def get_modified_time(self, name):
  351. return self._datetime_from_timestamp(os.path.getmtime(self.path(name)))
  352. def get_storage_class(import_path=None):
  353. return import_string(import_path or settings.DEFAULT_FILE_STORAGE)
  354. class DefaultStorage(LazyObject):
  355. def _setup(self):
  356. self._wrapped = get_storage_class()()
  357. default_storage = DefaultStorage()