Skip to content

media: use the override suffix when finding an available file name - #3700

Merged
pbek merged 2 commits into
pbek:mainfrom
VXNCXNX:fix/find-available-file-name-override-suffix
Aug 17, 2026
Merged

media: use the override suffix when finding an available file name#3700
pbek merged 2 commits into
pbek:mainfrom
VXNCXNX:fix/find-available-file-name-override-suffix

Conversation

@VXNCXNX

@VXNCXNX VXNCXNX commented Aug 17, 2026

Copy link
Copy Markdown
Contributor

Summary

Utils::Misc::findAvailableFileName() declares an overrideSuffix parameter (misc.h:151) but never reads it, so the returned name always keeps the suffix of the input path.

Both call sites in src/entities/note.cpp pass a suffix they detected from the file's mime type, precisely because the input is a temporary file whose name has no suffix or the wrong one. So pasted or imported media can be saved as qownnotes-media-AbCdEf with no extension, or keep a .jpeg the mime type says should be .jpg.

Repro

before:
findAvailableFileName("qownnotes-media-AbCdEf", dir, "png") -> "qownnotes-media-AbCdEf"
findAvailableFileName("screenshot.jpeg", dir, "jpg")        -> "screenshot.jpeg"

after:
                                                            -> "qownnotes-media-AbCdEf.png"
                                                            -> "screenshot.jpg"

The fix

Use overrideSuffix when it is not empty, otherwise the suffix of the path. This matches makeFileNameRandom(), which has the same parameter and already honours it.

Verification

testFindAvailableFileNameOverrideSuffix covers the two cases above, the unchanged behaviour when no override is given, and the collision counter keeping the override (image plus an existing image.png gives image-1.png). Reverting newSuffix to fileInfo.suffix() fails it on the first comparison.

Full unit test target passes: 45, 3, 3, 5, 9, 2, 8 and 36 across the eight test classes, [Tests PASS].

Checklist

  • I read the code contribution guidelines
  • This is a small, self-contained fix
  • Created against main, from a separate branch
  • Followed the repository's commit message style
  • No translation files edited

Written with AI assistance (Claude Code). I built the test target against Qt 5.15.2 and ran the suite and the mutation check myself.

findAvailableFileName declared an overrideSuffix parameter but never
read it, so the name always kept the suffix of the input path. Both
call sites pass a mime-detected suffix precisely because the input is
a temporary file, so pasted media could be saved with no extension.
@pbek

pbek commented Aug 17, 2026

Copy link
Copy Markdown
Owner

Is there an open issue (which I can reproduce) this PR is attempting to fix? If not, can you please open one?

@VXNCXNX

VXNCXNX commented Aug 17, 2026

Copy link
Copy Markdown
Contributor Author

No open issue, so I opened #3701 with the repro. It boils down to findAvailableFileName() taking an overrideSuffix it never reads, while both callers in note.cpp pass a mime-detected one.

Signed-off-by: Patrizio Bekerle <patrizio@bekerle.com>
@pbek

pbek commented Aug 17, 2026

Copy link
Copy Markdown
Owner

Thank you!

@pbek
pbek merged commit 3594d34 into pbek:main Aug 17, 2026
39 of 40 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants