From eb2d49b73408f1e60b8e1fa7fee822816945e1cd Mon Sep 17 00:00:00 2001 From: Adam Zapletal Date: Sun, 18 Feb 2024 21:56:26 -0600 Subject: [PATCH] Fixed #23759 -- Preserved all file extensions in Storage.get_available_name(). --- AUTHORS | 1 + django/core/files/storage/base.py | 3 ++- tests/file_storage/tests.py | 30 ++++++++++++++++++------------ 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/AUTHORS b/AUTHORS index 0b571ea9ce..1041e2a631 100644 --- a/AUTHORS +++ b/AUTHORS @@ -21,6 +21,7 @@ answer newbie questions, and generally made Django that much better: Adam Johnson Adam Malinowski Adam Vandenberg + Adam Zapletal Ade Lee Adiyat Mubarak Adnan Umer diff --git a/django/core/files/storage/base.py b/django/core/files/storage/base.py index 16ac22f70a..6ce4ab2535 100644 --- a/django/core/files/storage/base.py +++ b/django/core/files/storage/base.py @@ -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 diff --git a/tests/file_storage/tests.py b/tests/file_storage/tests.py index 637de0a3c9..420314573d 100644 --- a/tests/file_storage/tests.py +++ b/tests/file_storage/tests.py @@ -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