Fix: dont re-render path template when checking collisions (#13718)

This commit is contained in:
shamoon
2026-08-18 07:19:51 -07:00
committed by GitHub
parent 2865a095b0
commit 643d0205bc
2 changed files with 44 additions and 10 deletions
+12 -10
View File
@@ -62,18 +62,17 @@ def generate_unique_filename(doc, *, archive_filename=False) -> Path:
old_filename = Path(doc.filename) if doc.filename else None
root = settings.ORIGINALS_DIR
base_filename = generate_filename(doc, archive_filename=archive_filename)
# If generating archive filenames, try to make a name that is similar to
# the original filename first.
if archive_filename and doc.filename:
# Generate the full path using the same logic as generate_filename
base_generated = generate_filename(doc, archive_filename=archive_filename)
# Try to create a simple PDF version based on the original filename
# but preserve any directory structure from the template
if str(base_generated.parent) != ".":
if str(base_filename.parent) != ".":
# Has directory structure, preserve it
simple_pdf_name = base_generated.parent / (Path(doc.filename).stem + ".pdf")
simple_pdf_name = base_filename.parent / (Path(doc.filename).stem + ".pdf")
else:
# No directory structure
simple_pdf_name = Path(Path(doc.filename).stem + ".pdf")
@@ -81,14 +80,17 @@ def generate_unique_filename(doc, *, archive_filename=False) -> Path:
if simple_pdf_name == old_filename or not (root / simple_pdf_name).exists():
return simple_pdf_name
file_extension = ".pdf" if archive_filename else doc.file_type
filename_stem = base_filename.name.removesuffix(file_extension)
counter = 0
while True:
new_filename = generate_filename(
doc,
counter=counter,
archive_filename=archive_filename,
)
new_filename = base_filename
if counter:
new_filename = base_filename.with_name(
f"{filename_stem}_{counter:02}{file_extension}",
)
if new_filename == old_filename:
# still the same as before.
return new_filename