Skip to content
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

Allow spaces in sanitizeFileName #59

Closed
DropSnorz opened this issue Jan 9, 2021 · 3 comments
Closed

Allow spaces in sanitizeFileName #59

DropSnorz opened this issue Jan 9, 2021 · 3 comments
Assignees

Comments

@DropSnorz
Copy link
Owner

Spaces (and only " ") should be allowed in filenames. It's not the case in FileUtils.sanitizeFileName

return fileName.replaceAll("[^-_.A-Za-z0-9]", "");

If spaces are allowed, we should trim the String to avoid trailing and duplicates space character.

@DropSnorz DropSnorz changed the title Allow spaces in filenames Allow spaces in sanitizeFileName Jan 9, 2021
@the-simian
Copy link
Contributor

the-simian commented Jan 10, 2021

How about fileName.replaceAll("[\\\\/:*?\"<>|]", "") This actually just targets the illegal characters, so it works subtractively rather than only allowing the chars here. It would allow spaces but also disallow certain characters. Is this too permissive?

the fix to keep the existing pattern would be:
filename.replaceAll("/[^a-zA-Z0-9 ]/", ""). Looks similar but there's a space after the 9, but that wouldn't trim leading and trailing spaces, so I think Java has a .trim() method for that? so like:
filename.replaceAll("/[^a-zA-Z0-9 ]/", "").trim()

As for repeated spaces... I know it would be slick to do it all in one regex but if we added .replaceAll("\\s+"," ") to the end that would remove any runs of more than one space, too.

Not 100% sure how to write that all as a single regex, but
return filename.replaceAll("/[^a-zA-Z0-9 ]/", "").trim().replaceAll("\\s+"," ");
...i think would be ok

@DropSnorz DropSnorz self-assigned this Jan 10, 2021
@DropSnorz
Copy link
Owner Author

Thank you for your suggestions. I've updated the function to allow spaces in filenames.
I've also added a few unit tests to ensure the behaviour is correct.

@DropSnorz
Copy link
Owner Author

Fix released in OwlPlug 0.15.0

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

No branches or pull requests

2 participants