-
Notifications
You must be signed in to change notification settings - Fork 34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Exporting data items with forward slashes results in wrong filename #591
Comments
My initial thought is that the nionswift/nion/swift/DocumentController.py Line 774 in 6dd9301
|
I've dug a bit through this one. Addressing the big question of if there is an easy cross-platform way to sanitize paths, the answer is no. Though there are some attempts at resolving this, ex: the first answer of https://stackoverflow.com/questions/9532499/check-whether-a-path-is-valid-in-python-without-creating-a-file-at-the-paths-ta, not only does it note it has issues, but it also misses issues such as restricted file names, ex: "NUL", along with not even considering UNC paths on the windows side. (As an additional note, most OSs will prevent or attempt to prevent invalid or disliked file names within their graphical and text shells for the most part. Windows for example will not let you use a file with "/" in it when utilizing the explorer selection window.) That said, there are some mentioned libraries, along with the suggestion that one could just attempt to create the file and see what happens. I dislike the first option as I have no clue how well maintained another library might be going into the future. The second option seems reasonable enough though, especially since we're trying to write a file at that destination anyways. If that fails I could then have swift present the user with a notification saying the export process failed due to X lower-level python file error. As for the slashes, python does have a way to resolve a path, and expand it to a full-length path, which would be how I solve the slashes in prefix problem (which also converts slashes to the required slash for the host OS). This would effectively make any path with a slash spawn an additional sub-directory (or fail to spawn one) depending on desired behavior. |
I think the best approach is to start with something restrictive and add exceptions/restrictions as necessary. And write a unit test (hopefully just one easy to understand test function) that passes on all platforms.
|
Quick question on this one @cmeyer - is there a use case where having a special character in a file name is needed? For instance MS Word just brings up a simple error message if you try to name a file with a special character and makes you redo it, could/should we just do the same? |
Just replace illegal characters with _ when creating the default filename from the image title.
It’s tedious to go through and fix little O/S proclivities.
Niklas
***@***.******@***.***>
Chief Scientist 11511 NE 118St
Electron Microscopy Kirkland,WA 98034, USA
Bruker AXS LLC
…-Bruker Confidential-
From: James ***@***.***>
Sent: Friday, May 17, 2024 7:25 AM
To: nion-software/nionswift ***@***.***>
Cc: Subscribed ***@***.***>
Subject: Re: [nion-software/nionswift] Exporting data items with forward slashes results in wrong filename (#591)
**EXTERNAL EMAIL**
Quick question on this one @cmeyer<https://github.com/cmeyer> - is there a use case where having a special character in a file name is needed? For instance MS Word just brings up a simple error message if you try to name a file with a special character and makes you redo it, could/should we just do the same?
image.png (view on web)<https://github.com/nion-software/nionswift/assets/166606203/60f9c23a-6b9f-4b3d-8d06-d8577d7efa36>
—
Reply to this email directly, view it on GitHub<#591 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/ALBPXDZXK4TUVEY2TMJ7ZP3ZCYHMLAVCNFSM4WFNHYT2U5DIOJSWCZC7NNSXTN2JONZXKZKDN5WW2ZLOOQ5TEMJRG43TEOBYGMZQ>.
You are receiving this because you are subscribed to this thread.Message ID: ***@***.***>
|
@jamesrussell216 This already occurs when using the file dialog. The batch export functionality does not use the file dialog and batch export presents complications because some files may succeed and some may fail. For instance, if batch export is using the title of the data item in its template and some data item titles contain a slash. |
What are "illegal characters"? That's the crux of the issue. See task list at the top of this bug, other comments, and abandoned implementation in #874 for more info and suggestions. |
@cmeyer illegal characters will be anything that causes a file save error - should be easy enough to define surely? I see the complication - it's potentially more complicated to catch all the places that an illegal character could come in from, thus probably better to make the code hand illegal characters safely rather than trying to block all the places they could come in. |
Hmm not sure why this got auto-closed. Opening it again. It's not fixed with #1050. |
Forward slashes aren't allowed in Windows filenames.
Exporting a single file with forward slashes works on macOS. It is not allowed (some versions of Qt?) on Windows.
Exporting a batch with forward slashes in the prefix silently fails on both macOS and Windows.
Also, how are backslashes and other disallowed characters handled?
Is there any way to escape a filename and remove disallowed characters that works cross platform?
Edit 2024-05-14: This issue only applies to batch exports or possibly the default file name used when exporting a single file. In those places, we should ensure that if we generate a filename from a title, it is sanitized for use as a filename. See #874 for some initial, abandoned work. We can assume that a filename the user chooses a filename in a save file dialog is sanitized; it may still fail but will be reported to the user.
Tasks
The text was updated successfully, but these errors were encountered: