1
0
mirror of https://github.com/django/django.git synced 2024-12-22 09:05:43 +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 Malinowski <https://adammalinowski.co.uk/>
Adam Vandenberg
Adam Zapletal <https://adamzap.com/>
Ade Lee <alee@redhat.com>
Adiyat Mubarak <adiyatmubarak@gmail.com>
Adnan Umer <u.adnan@outlook.com>

View File

@ -69,7 +69,8 @@ class Storage:
"Detected path traversal attempt in '%s'" % dir_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
# until it doesn't exist.
# 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):
# Multiple files with the same name get _(7 random chars) appended to them.
objs = [Storage() for i in range(2)]
for o in objs:
o.normal.save("multiple_files.txt", ContentFile("Same Content"))
try:
names = [o.normal.name for o in objs]
self.assertEqual(names[0], "tests/multiple_files.txt")
self.assertRegex(
names[1], "tests/multiple_files_%s.txt" % FILE_SUFFIX_REGEX
)
finally:
for o in objs:
o.delete()
tests = [
("multiple_files", "txt"),
("multiple_files_many_extensions", "tar.gz"),
]
for filename, extension in tests:
with self.subTest(filename=filename):
objs = [Storage() for i in range(2)]
for o in objs:
o.normal.save(f"{filename}.{extension}", ContentFile("Content"))
try:
names = [o.normal.name for o in objs]
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):
# Given the max_length is limited, when multiple files get uploaded