Don't double file extension in clean_filename (#3942)

* Don't double file extension in clean_filename

* Don't use display_name as ClearableBasenameFileInput.FakeFile.name

Reason: it's used as the thumbnail source and therefore needs to be a valid file name and not some display name
This commit is contained in:
Mira
2024-03-01 09:58:17 +01:00
committed by GitHub
parent 50b5f760bb
commit 6e6b75d55e
2 changed files with 7 additions and 4 deletions

View File

@@ -123,8 +123,6 @@ class ClearableBasenameFileInput(forms.ClearableFileInput):
@property
def name(self):
if hasattr(self.file, 'display_name'):
return self.file.display_name
return self.file.name
@property

View File

@@ -32,9 +32,14 @@ def clean_filename(fname):
"Terms.pdf""Terms.pdf.OybgvyAH.22c0583727d5bc.pdf"
This function reverses this operation:
This function reverses this operation (leaving names without doubled extension as-is):
"Terms.pdf.OybgvyAH.22c0583727d5bc.pdf""Terms.pdf"
"Terms.pdf""Terms.pdf"
"""
ext = '.' + fname.split('.')[-1]
return fname.rsplit(ext + ".", 1)[0] + ext
parts = fname.rsplit(ext + ".", 1)
if len(parts) == 1:
return parts[0]
else:
return parts[0] + ext