storage.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. import os
  2. import errno
  3. from datetime import datetime
  4. from django.conf import settings
  5. from django.core.exceptions import SuspiciousFileOperation
  6. from django.core.files import locks, File
  7. from django.core.files.move import file_move_safe
  8. from django.utils.crypto import get_random_string
  9. from django.utils.encoding import force_text, filepath_to_uri
  10. from django.utils.functional import LazyObject
  11. from django.utils.module_loading import import_string
  12. from django.utils.six.moves.urllib.parse import urljoin
  13. from django.utils.text import get_valid_filename
  14. from django.utils._os import safe_join, abspathu
  15. from django.utils.deconstruct import deconstructible
  16. __all__ = ('Storage', 'FileSystemStorage', 'DefaultStorage', 'default_storage')
  17. class Storage(object):
  18. """
  19. A base storage class, providing some default behaviors that all other
  20. storage systems can inherit or override, as necessary.
  21. """
  22. # The following methods represent a public interface to private methods.
  23. # These shouldn't be overridden by subclasses unless absolutely necessary.
  24. def open(self, name, mode='rb'):
  25. """
  26. Retrieves the specified file from storage.
  27. """
  28. return self._open(name, mode)
  29. def save(self, name, content):
  30. """
  31. Saves new content to the file specified by name. The content should be
  32. a proper File object or any python file-like object, ready to be read
  33. from the beginning.
  34. """
  35. # Get the proper name for the file, as it will actually be saved.
  36. if name is None:
  37. name = content.name
  38. if not hasattr(content, 'chunks'):
  39. content = File(content)
  40. name = self.get_available_name(name)
  41. name = self._save(name, content)
  42. # Store filenames with forward slashes, even on Windows
  43. return force_text(name.replace('\\', '/'))
  44. # These methods are part of the public API, with default implementations.
  45. def get_valid_name(self, name):
  46. """
  47. Returns a filename, based on the provided filename, that's suitable for
  48. use in the target storage system.
  49. """
  50. return get_valid_filename(name)
  51. def get_available_name(self, name):
  52. """
  53. Returns a filename that's free on the target storage system, and
  54. available for new content to be written to.
  55. """
  56. dir_name, file_name = os.path.split(name)
  57. file_root, file_ext = os.path.splitext(file_name)
  58. # If the filename already exists, add an underscore and a random 7
  59. # character alphanumeric string (before the file extension, if one
  60. # exists) to the filename until the generated filename doesn't exist.
  61. while self.exists(name):
  62. # file_ext includes the dot.
  63. name = os.path.join(dir_name, "%s_%s%s" % (file_root, get_random_string(7), file_ext))
  64. return name
  65. def path(self, name):
  66. """
  67. Returns a local filesystem path where the file can be retrieved using
  68. Python's built-in open() function. Storage systems that can't be
  69. accessed using open() should *not* implement this method.
  70. """
  71. raise NotImplementedError("This backend doesn't support absolute paths.")
  72. # The following methods form the public API for storage systems, but with
  73. # no default implementations. Subclasses must implement *all* of these.
  74. def delete(self, name):
  75. """
  76. Deletes the specified file from the storage system.
  77. """
  78. raise NotImplementedError('subclasses of Storage must provide a delete() method')
  79. def exists(self, name):
  80. """
  81. Returns True if a file referenced by the given name already exists in the
  82. storage system, or False if the name is available for a new file.
  83. """
  84. raise NotImplementedError('subclasses of Storage must provide an exists() method')
  85. def listdir(self, path):
  86. """
  87. Lists the contents of the specified path, returning a 2-tuple of lists;
  88. the first item being directories, the second item being files.
  89. """
  90. raise NotImplementedError('subclasses of Storage must provide a listdir() method')
  91. def size(self, name):
  92. """
  93. Returns the total size, in bytes, of the file specified by name.
  94. """
  95. raise NotImplementedError('subclasses of Storage must provide a size() method')
  96. def url(self, name):
  97. """
  98. Returns an absolute URL where the file's contents can be accessed
  99. directly by a Web browser.
  100. """
  101. raise NotImplementedError('subclasses of Storage must provide a url() method')
  102. def accessed_time(self, name):
  103. """
  104. Returns the last accessed time (as datetime object) of the file
  105. specified by name.
  106. """
  107. raise NotImplementedError('subclasses of Storage must provide an accessed_time() method')
  108. def created_time(self, name):
  109. """
  110. Returns the creation time (as datetime object) of the file
  111. specified by name.
  112. """
  113. raise NotImplementedError('subclasses of Storage must provide a created_time() method')
  114. def modified_time(self, name):
  115. """
  116. Returns the last modified time (as datetime object) of the file
  117. specified by name.
  118. """
  119. raise NotImplementedError('subclasses of Storage must provide a modified_time() method')
  120. @deconstructible
  121. class FileSystemStorage(Storage):
  122. """
  123. Standard filesystem storage
  124. """
  125. def __init__(self, location=None, base_url=None, file_permissions_mode=None,
  126. directory_permissions_mode=None):
  127. if location is None:
  128. location = settings.MEDIA_ROOT
  129. self.base_location = location
  130. self.location = abspathu(self.base_location)
  131. if base_url is None:
  132. base_url = settings.MEDIA_URL
  133. elif not base_url.endswith('/'):
  134. base_url += '/'
  135. self.base_url = base_url
  136. self.file_permissions_mode = (
  137. file_permissions_mode if file_permissions_mode is not None
  138. else settings.FILE_UPLOAD_PERMISSIONS
  139. )
  140. self.directory_permissions_mode = (
  141. directory_permissions_mode if directory_permissions_mode is not None
  142. else settings.FILE_UPLOAD_DIRECTORY_PERMISSIONS
  143. )
  144. def _open(self, name, mode='rb'):
  145. return File(open(self.path(name), mode))
  146. def _save(self, name, content):
  147. full_path = self.path(name)
  148. # Create any intermediate directories that do not exist.
  149. # Note that there is a race between os.path.exists and os.makedirs:
  150. # if os.makedirs fails with EEXIST, the directory was created
  151. # concurrently, and we can continue normally. Refs #16082.
  152. directory = os.path.dirname(full_path)
  153. if not os.path.exists(directory):
  154. try:
  155. if self.directory_permissions_mode is not None:
  156. # os.makedirs applies the global umask, so we reset it,
  157. # for consistency with file_permissions_mode behavior.
  158. old_umask = os.umask(0)
  159. try:
  160. os.makedirs(directory, self.directory_permissions_mode)
  161. finally:
  162. os.umask(old_umask)
  163. else:
  164. os.makedirs(directory)
  165. except OSError as e:
  166. if e.errno != errno.EEXIST:
  167. raise
  168. if not os.path.isdir(directory):
  169. raise IOError("%s exists and is not a directory." % directory)
  170. # There's a potential race condition between get_available_name and
  171. # saving the file; it's possible that two threads might return the
  172. # same name, at which point all sorts of fun happens. So we need to
  173. # try to create the file, but if it already exists we have to go back
  174. # to get_available_name() and try again.
  175. while True:
  176. try:
  177. # This file has a file path that we can move.
  178. if hasattr(content, 'temporary_file_path'):
  179. file_move_safe(content.temporary_file_path(), full_path)
  180. # This is a normal uploadedfile that we can stream.
  181. else:
  182. # This fun binary flag incantation makes os.open throw an
  183. # OSError if the file already exists before we open it.
  184. flags = (os.O_WRONLY | os.O_CREAT | os.O_EXCL |
  185. getattr(os, 'O_BINARY', 0))
  186. # The current umask value is masked out by os.open!
  187. fd = os.open(full_path, flags, 0o666)
  188. _file = None
  189. try:
  190. locks.lock(fd, locks.LOCK_EX)
  191. for chunk in content.chunks():
  192. if _file is None:
  193. mode = 'wb' if isinstance(chunk, bytes) else 'wt'
  194. _file = os.fdopen(fd, mode)
  195. _file.write(chunk)
  196. finally:
  197. locks.unlock(fd)
  198. if _file is not None:
  199. _file.close()
  200. else:
  201. os.close(fd)
  202. except OSError as e:
  203. if e.errno == errno.EEXIST:
  204. # Ooops, the file exists. We need a new file name.
  205. name = self.get_available_name(name)
  206. full_path = self.path(name)
  207. else:
  208. raise
  209. else:
  210. # OK, the file save worked. Break out of the loop.
  211. break
  212. if self.file_permissions_mode is not None:
  213. os.chmod(full_path, self.file_permissions_mode)
  214. return name
  215. def delete(self, name):
  216. assert name, "The name argument is not allowed to be empty."
  217. name = self.path(name)
  218. # If the file exists, delete it from the filesystem.
  219. # Note that there is a race between os.path.exists and os.remove:
  220. # if os.remove fails with ENOENT, the file was removed
  221. # concurrently, and we can continue normally.
  222. if os.path.exists(name):
  223. try:
  224. os.remove(name)
  225. except OSError as e:
  226. if e.errno != errno.ENOENT:
  227. raise
  228. def exists(self, name):
  229. return os.path.exists(self.path(name))
  230. def listdir(self, path):
  231. path = self.path(path)
  232. directories, files = [], []
  233. for entry in os.listdir(path):
  234. if os.path.isdir(os.path.join(path, entry)):
  235. directories.append(entry)
  236. else:
  237. files.append(entry)
  238. return directories, files
  239. def path(self, name):
  240. try:
  241. path = safe_join(self.location, name)
  242. except ValueError:
  243. raise SuspiciousFileOperation("Attempted access to '%s' denied." % name)
  244. return os.path.normpath(path)
  245. def size(self, name):
  246. return os.path.getsize(self.path(name))
  247. def url(self, name):
  248. if self.base_url is None:
  249. raise ValueError("This file is not accessible via a URL.")
  250. return urljoin(self.base_url, filepath_to_uri(name))
  251. def accessed_time(self, name):
  252. return datetime.fromtimestamp(os.path.getatime(self.path(name)))
  253. def created_time(self, name):
  254. return datetime.fromtimestamp(os.path.getctime(self.path(name)))
  255. def modified_time(self, name):
  256. return datetime.fromtimestamp(os.path.getmtime(self.path(name)))
  257. def get_storage_class(import_path=None):
  258. return import_string(import_path or settings.DEFAULT_FILE_STORAGE)
  259. class DefaultStorage(LazyObject):
  260. def _setup(self):
  261. self._wrapped = get_storage_class()()
  262. default_storage = DefaultStorage()