1
0
mirror of https://github.com/django/django.git synced 2024-12-23 01:25:58 +00:00

Refs #35818 - Added hook to make it easier to customize file extensions parsing

This commit is contained in:
Bruno Alla 2024-10-08 12:21:57 -03:00
parent 519615aecc
commit 73bc1dc911
No known key found for this signature in database
2 changed files with 16 additions and 12 deletions

View File

@ -84,15 +84,7 @@ class Storage:
"Detected path traversal attempt in '%s'" % dir_name "Detected path traversal attempt in '%s'" % dir_name
) )
validate_file_name(file_name) validate_file_name(file_name)
# Extract file extensions (.txt, .jpeg, .tar.gz) from the filename file_ext = self.get_filename_extensions(file_name)
suffixes = pathlib.PurePath(file_name).suffixes
file_ext = ""
# Iterate backwards from the last suffix. Consider suffixes
# shorter than 5 characters to be part of the extension
for index, suffix in enumerate(suffixes[::-1]):
if index > 0 and len(suffix) >= 5:
break
file_ext = suffix + file_ext
file_root = file_name.removesuffix(file_ext) file_root = file_name.removesuffix(file_ext)
# If the filename is not available, generate an alternative # If the filename is not available, generate an alternative
# filename until one is available. # filename until one is available.
@ -122,6 +114,10 @@ class Storage:
) )
return name return name
def get_filename_extensions(self, filename):
"""Extract all extensions from the filename."""
return "".join(pathlib.PurePath(filename).suffixes)
def generate_filename(self, filename): def generate_filename(self, filename):
""" """
Validate the filename by calling get_valid_name() and return a filename Validate the filename by calling get_valid_name() and return a filename

View File

@ -738,11 +738,19 @@ class OverwritingStorageTests(FileStorageTests):
self.assertEqual(stored_name, "test_l.txt") self.assertEqual(stored_name, "test_l.txt")
self.assertEqual(len(stored_name), 10) self.assertEqual(len(stored_name), 10)
def test_file_name_truncation_with_dotted_name(self): def test_file_name_truncation_with_dotted_name_custom_strategy(self):
class DottedFileNameStorage(FileSystemStorage):
def get_filename_extensions(self, filename):
ext = filename.split(".")[-1]
return f".{ext}"
storage = DottedFileNameStorage(
location=self.temp_dir, base_url="/test_media_url/", allow_overwrite=True
)
name = "test.long.dotted.name.txt" name = "test.long.dotted.name.txt"
file = ContentFile(b"content") file = ContentFile(b"content")
stored_name = self.storage.save(name, file, max_length=10) stored_name = storage.save(name, file, max_length=10)
self.addCleanup(self.storage.delete, stored_name) self.addCleanup(storage.delete, stored_name)
self.assertEqual(stored_name, "test.l.txt") self.assertEqual(stored_name, "test.l.txt")
self.assertEqual(len(stored_name), 10) self.assertEqual(len(stored_name), 10)