Closed DropSnorz closed 3 years ago
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
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.
Fix released in OwlPlug 0.15.0
Spaces (and only
" "
) should be allowed in filenames. It's not the case in FileUtils.sanitizeFileNamehttps://github.com/DropSnorz/OwlPlug/blob/2e43c1ff4f7bd9406c1ff744dcda748cc267f57e/owlplug-client/src/main/java/com/owlplug/core/utils/FileUtils.java#L58
If spaces are allowed, we should trim the String to avoid trailing and duplicates space character.