Fix attachment de-dup stripping real digits from the filename

The collision-rename loop in email_download_attachment used
`target.stem.rstrip('-0123456789')`, which strips every trailing digit and
dash, not just a previously-appended `-N` suffix. So saving a second
`invoice_2024.pdf` produced `invoice_-1.pdf`, `IMG_20240115.jpg` became
`IMG_-1.jpg`, and Outlook's `image001.png` became `image-1.png`.

Strip only a trailing `-<number>` suffix with a regex, preserving the
original digits (and still incrementing correctly on repeated collisions).
This commit is contained in:
Osamaali313
2026-07-26 23:40:00 +03:00
parent db93d75bf6
commit 330e12131e
+1 -1
View File
@@ -555,7 +555,7 @@ def make_email_tools(
while target.exists():
target = (
scratch.path
/ f"{target.stem.rstrip('-0123456789') or 'attachment'}-{counter}{target.suffix}"
/ f"{re.sub(r'-[0-9]+$', '', target.stem) or 'attachment'}-{counter}{target.suffix}"
)
counter += 1
target.write_bytes(payload)