1
0
mirror of https://github.com/django/django.git synced 2025-05-04 14:14:37 +00:00

Fixed #23759 -- Preserved all file extensions in Storage.get_available_name().

This commit is contained in:
Adam Zapletal 2024-02-18 21:56:26 -06:00 committed by Mariusz Felisiak
parent 3426a5c33c
commit eb2d49b734
3 changed files with 21 additions and 13 deletions

View File

@ -21,6 +21,7 @@ answer newbie questions, and generally made Django that much better:
Adam Johnson <https://github.com/adamchainz> Adam Johnson <https://github.com/adamchainz>
Adam Malinowski <https://adammalinowski.co.uk/> Adam Malinowski <https://adammalinowski.co.uk/>
Adam Vandenberg Adam Vandenberg
Adam Zapletal <https://adamzap.com/>
Ade Lee <alee@redhat.com> Ade Lee <alee@redhat.com>
Adiyat Mubarak <adiyatmubarak@gmail.com> Adiyat Mubarak <adiyatmubarak@gmail.com>
Adnan Umer <u.adnan@outlook.com> Adnan Umer <u.adnan@outlook.com>

View File

@ -69,7 +69,8 @@ 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)
file_root, file_ext = os.path.splitext(file_name) file_ext = "".join(pathlib.PurePath(file_name).suffixes)
file_root = file_name.removesuffix(file_ext)
# If the filename already exists, generate an alternative filename # If the filename already exists, generate an alternative filename
# until it doesn't exist. # until it doesn't exist.
# Truncate original name if required, so the new filename does not # Truncate original name if required, so the new filename does not

View File

@ -769,18 +769,24 @@ class FileFieldStorageTests(TestCase):
def test_duplicate_filename(self): def test_duplicate_filename(self):
# Multiple files with the same name get _(7 random chars) appended to them. # Multiple files with the same name get _(7 random chars) appended to them.
objs = [Storage() for i in range(2)] tests = [
for o in objs: ("multiple_files", "txt"),
o.normal.save("multiple_files.txt", ContentFile("Same Content")) ("multiple_files_many_extensions", "tar.gz"),
try: ]
names = [o.normal.name for o in objs] for filename, extension in tests:
self.assertEqual(names[0], "tests/multiple_files.txt") with self.subTest(filename=filename):
self.assertRegex( objs = [Storage() for i in range(2)]
names[1], "tests/multiple_files_%s.txt" % FILE_SUFFIX_REGEX for o in objs:
) o.normal.save(f"{filename}.{extension}", ContentFile("Content"))
finally: try:
for o in objs: names = [o.normal.name for o in objs]
o.delete() self.assertEqual(names[0], f"tests/{filename}.{extension}")
self.assertRegex(
names[1], f"tests/{filename}_{FILE_SUFFIX_REGEX}.{extension}"
)
finally:
for o in objs:
o.delete()
def test_file_truncation(self): def test_file_truncation(self):
# Given the max_length is limited, when multiple files get uploaded # Given the max_length is limited, when multiple files get uploaded