| 1 |
import os |
| 2 |
import warnings |
| 3 |
from datetime import datetime, timezone |
| 4 |
from urllib.parse import urljoin |
| 5 |
|
| 6 |
from django.conf import settings |
| 7 |
from django.core.files import File, locks |
| 8 |
from django.core.files.move import file_move_safe |
| 9 |
from django.core.signals import setting_changed |
| 10 |
from django.utils._os import safe_join, safe_makedirs |
| 11 |
from django.utils.deconstruct import deconstructible |
| 12 |
from django.utils.deprecation import RemovedInDjango60Warning |
| 13 |
from django.utils.encoding import filepath_to_uri |
| 14 |
from django.utils.functional import cached_property |
| 15 |
|
| 16 |
from .base import Storage |
| 17 |
from .mixins import StorageSettingsMixin |
| 18 |
|
| 19 |
|
| 20 |
@deconstructible(path="django.core.files.storage.FileSystemStorage") |
| 21 |
class FileSystemStorage(Storage, StorageSettingsMixin): |
| 22 |
""" |
| 23 |
Standard filesystem storage |
| 24 |
""" |
| 25 |
|
| 26 |
# RemovedInDjango60Warning: remove OS_OPEN_FLAGS. |
| 27 |
OS_OPEN_FLAGS = os.O_WRONLY | os.O_CREAT | os.O_EXCL | getattr(os, "O_BINARY", 0) |
| 28 |
|
| 29 |
def __init__( |
| 30 |
self, |
| 31 |
location=None, |
| 32 |
base_url=None, |
| 33 |
file_permissions_mode=None, |
| 34 |
directory_permissions_mode=None, |
| 35 |
allow_overwrite=False, |
| 36 |
): |
| 37 |
self._location = location |
| 38 |
self._base_url = base_url |
| 39 |
self._file_permissions_mode = file_permissions_mode |
| 40 |
self._directory_permissions_mode = directory_permissions_mode |
| 41 |
self._allow_overwrite = allow_overwrite |
| 42 |
setting_changed.connect(self._clear_cached_properties) |
| 43 |
# RemovedInDjango60Warning: remove this warning. |
| 44 |
if self.OS_OPEN_FLAGS != os.O_WRONLY | os.O_CREAT | os.O_EXCL | getattr( |
| 45 |
os, "O_BINARY", 0 |
| 46 |
): |
| 47 |
warnings.warn( |
| 48 |
"Overriding OS_OPEN_FLAGS is deprecated. Use " |
| 49 |
"the allow_overwrite parameter instead.", |
| 50 |
RemovedInDjango60Warning, |
| 51 |
stacklevel=2, |
| 52 |
) |
| 53 |
|
| 54 |
@cached_property |
| 55 |
def base_location(self): |
| 56 |
return self._value_or_setting(self._location, settings.MEDIA_ROOT) |
| 57 |
|
| 58 |
@cached_property |
| 59 |
def location(self): |
| 60 |
return os.path.abspath(self.base_location) |
| 61 |
|
| 62 |
@cached_property |
| 63 |
def base_url(self): |
| 64 |
if self._base_url is not None and not self._base_url.endswith("/"): |
| 65 |
self._base_url += "/" |
| 66 |
return self._value_or_setting(self._base_url, settings.MEDIA_URL) |
| 67 |
|
| 68 |
@cached_property |
| 69 |
def file_permissions_mode(self): |
| 70 |
return self._value_or_setting( |
| 71 |
self._file_permissions_mode, settings.FILE_UPLOAD_PERMISSIONS |
| 72 |
) |
| 73 |
|
| 74 |
@cached_property |
| 75 |
def directory_permissions_mode(self): |
| 76 |
return self._value_or_setting( |
| 77 |
self._directory_permissions_mode, settings.FILE_UPLOAD_DIRECTORY_PERMISSIONS |
| 78 |
) |
| 79 |
|
| 80 |
def _open(self, name, mode="rb"): |
| 81 |
return File(open(self.path(name), mode)) |
| 82 |
|
| 83 |
def _save(self, name, content): |
| 84 |
full_path = self.path(name) |
| 85 |
|
| 86 |
# Create any intermediate directories that do not exist. |
| 87 |
directory = os.path.dirname(full_path) |
| 88 |
try: |
| 89 |
if self.directory_permissions_mode is not None: |
| 90 |
# Workaround because os.makedirs() doesn't apply the "mode" |
| 91 |
# argument to intermediate-level directories. |
| 92 |
# https://github.com/python/cpython/issues/86533 |
| 93 |
safe_makedirs(directory, self.directory_permissions_mode, exist_ok=True) |
| 94 |
else: |
| 95 |
os.makedirs(directory, exist_ok=True) |
| 96 |
except FileExistsError: |
| 97 |
raise FileExistsError("%s exists and is not a directory." % directory) |
| 98 |
|
| 99 |
# There's a potential race condition between get_available_name and |
| 100 |
# saving the file; it's possible that two threads might return the |
| 101 |
# same name, at which point all sorts of fun happens. So we need to |
| 102 |
# try to create the file, but if it already exists we have to go back |
| 103 |
# to get_available_name() and try again. |
| 104 |
|
| 105 |
while True: |
| 106 |
try: |
| 107 |
# This file has a file path that we can move. |
| 108 |
if hasattr(content, "temporary_file_path"): |
| 109 |
file_move_safe( |
| 110 |
content.temporary_file_path(), |
| 111 |
full_path, |
| 112 |
allow_overwrite=self._allow_overwrite, |
| 113 |
) |
| 114 |
|
| 115 |
# This is a normal uploadedfile that we can stream. |
| 116 |
else: |
| 117 |
# The combination of O_CREAT and O_EXCL makes os.open() raises an |
| 118 |
# OSError if the file already exists before it's opened. |
| 119 |
open_flags = ( |
| 120 |
os.O_WRONLY |
| 121 |
| os.O_CREAT |
| 122 |
| os.O_EXCL |
| 123 |
| getattr(os, "O_BINARY", 0) |
| 124 |
) |
| 125 |
# RemovedInDjango60Warning: when the deprecation ends, replace with: |
| 126 |
# if self._allow_overwrite: |
| 127 |
# open_flags = open_flags & ~os.O_EXCL | os.O_TRUNC |
| 128 |
if self.OS_OPEN_FLAGS != open_flags: |
| 129 |
open_flags = self.OS_OPEN_FLAGS |
| 130 |
elif self._allow_overwrite: |
| 131 |
open_flags = open_flags & ~os.O_EXCL | os.O_TRUNC |
| 132 |
fd = os.open(full_path, open_flags, 0o666) |
| 133 |
_file = None |
| 134 |
try: |
| 135 |
locks.lock(fd, locks.LOCK_EX) |
| 136 |
for chunk in content.chunks(): |
| 137 |
if _file is None: |
| 138 |
mode = "wb" if isinstance(chunk, bytes) else "wt" |
| 139 |
_file = os.fdopen(fd, mode) |
| 140 |
_file.write(chunk) |
| 141 |
finally: |
| 142 |
locks.unlock(fd) |
| 143 |
if _file is not None: |
| 144 |
_file.close() |
| 145 |
else: |
| 146 |
os.close(fd) |
| 147 |
except FileExistsError: |
| 148 |
# A new name is needed if the file exists. |
| 149 |
name = self.get_available_name(name) |
| 150 |
full_path = self.path(name) |
| 151 |
else: |
| 152 |
# OK, the file save worked. Break out of the loop. |
| 153 |
break |
| 154 |
|
| 155 |
if self.file_permissions_mode is not None: |
| 156 |
os.chmod(full_path, self.file_permissions_mode) |
| 157 |
|
| 158 |
# Ensure the saved path is always relative to the storage root. |
| 159 |
name = os.path.relpath(full_path, self.location) |
| 160 |
# Ensure the moved file has the same gid as the storage root. |
| 161 |
self._ensure_location_group_id(full_path) |
| 162 |
# Store filenames with forward slashes, even on Windows. |
| 163 |
return str(name).replace("\\", "/") |
| 164 |
|
| 165 |
def _ensure_location_group_id(self, full_path): |
| 166 |
if os.name == "posix": |
| 167 |
file_gid = os.stat(full_path).st_gid |
| 168 |
location_gid = os.stat(self.location).st_gid |
| 169 |
if file_gid != location_gid: |
| 170 |
try: |
| 171 |
os.chown(full_path, uid=-1, gid=location_gid) |
| 172 |
except PermissionError: |
| 173 |
pass |
| 174 |
|
| 175 |
def delete(self, name): |
| 176 |
if not name: |
| 177 |
raise ValueError("The name must be given to delete().") |
| 178 |
name = self.path(name) |
| 179 |
# If the file or directory exists, delete it from the filesystem. |
| 180 |
try: |
| 181 |
if os.path.isdir(name): |
| 182 |
os.rmdir(name) |
| 183 |
else: |
| 184 |
os.remove(name) |
| 185 |
except FileNotFoundError: |
| 186 |
# FileNotFoundError is raised if the file or directory was removed |
| 187 |
# concurrently. |
| 188 |
pass |
| 189 |
|
| 190 |
def is_name_available(self, name, max_length=None): |
| 191 |
if self._allow_overwrite: |
| 192 |
return not (max_length and len(name) > max_length) |
| 193 |
return super().is_name_available(name, max_length=max_length) |
| 194 |
|
| 195 |
def get_alternative_name(self, file_root, file_ext): |
| 196 |
if self._allow_overwrite: |
| 197 |
return f"{file_root}{file_ext}" |
| 198 |
return super().get_alternative_name(file_root, file_ext) |
| 199 |
|
| 200 |
def exists(self, name): |
| 201 |
return os.path.lexists(self.path(name)) |
| 202 |
|
| 203 |
def listdir(self, path): |
| 204 |
path = self.path(path) |
| 205 |
directories, files = [], [] |
| 206 |
with os.scandir(path) as entries: |
| 207 |
for entry in entries: |
| 208 |
if entry.is_dir(): |
| 209 |
directories.append(entry.name) |
| 210 |
else: |
| 211 |
files.append(entry.name) |
| 212 |
return directories, files |
| 213 |
|
| 214 |
def path(self, name): |
| 215 |
return safe_join(self.location, name) |
| 216 |
|
| 217 |
def size(self, name): |
| 218 |
return os.path.getsize(self.path(name)) |
| 219 |
|
| 220 |
def url(self, name): |
| 221 |
if self.base_url is None: |
| 222 |
raise ValueError("This file is not accessible via a URL.") |
| 223 |
url = filepath_to_uri(name) |
| 224 |
if url is not None: |
| 225 |
url = url.lstrip("/") |
| 226 |
return urljoin(self.base_url, url) |
| 227 |
|
| 228 |
def _datetime_from_timestamp(self, ts): |
| 229 |
""" |
| 230 |
If timezone support is enabled, make an aware datetime object in UTC; |
| 231 |
otherwise make a naive one in the local timezone. |
| 232 |
""" |
| 233 |
tz = timezone.utc if settings.USE_TZ else None |
| 234 |
return datetime.fromtimestamp(ts, tz=tz) |
| 235 |
|
| 236 |
def get_accessed_time(self, name): |
| 237 |
return self._datetime_from_timestamp(os.path.getatime(self.path(name))) |
| 238 |
|
| 239 |
def get_created_time(self, name): |
| 240 |
return self._datetime_from_timestamp(os.path.getctime(self.path(name))) |
| 241 |
|
| 242 |
def get_modified_time(self, name): |
| 243 |
return self._datetime_from_timestamp(os.path.getmtime(self.path(name))) |