Skip to content

Commit f3dc37b

Browse files
authored
fix:cleanFilename #606
* cleanFilename: make badchars regex less strict cleanFilename was unnecessarily strict, also removing the legal characters '+[] Also, " appeared twice in the regex. And it was missing checks for control characters. Finally, /\ should be counted among the illegal characters, not as whitespace. See https://cs.android.com/android/platform/superproject/+/master:frameworks/base/core/java/android/os/FileUtils.java;l=972?q=isValidFatFilenameChar * cleanFilename: make whitespace regex configurable As requested in #606, make it possible to choose how to "clean" whitespace. Potential options: - w => '_' - encodeURIComponent - w => w (i.e. do nothing -- whitespace isn't forbidden) Also, simplify the regex -- \s already contains \t and \n, see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions/Character_classes
1 parent cc6b82d commit f3dc37b

1 file changed

Lines changed: 4 additions & 2 deletions

File tree

app/utils/utils.common.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ export function omit<T extends object, U extends keyof T>(object: T, ...props: U
1414
.reduce((newObj, key) => Object.assign(newObj, { [key]: object[key] }), {} as any);
1515
}
1616

17-
export function cleanFilename(str) {
18-
return str.replace(/[|?*<\":>+\[\]'"]+/g, '').replace(/[\\\s\t\n\/]+/g, '_');
17+
export function cleanFilename(str, wsRep=(w => '_')) {
18+
return str
19+
.replace(/[\x00-\x1f\x7f"*\/:<>?\\|\0x7f]+/g, encodeURIComponent)
20+
.replace(/\s/g, wsRep);
1921
}
2022
export function getFormatedDateForFilename(value?: number, dateFormat = ApplicationSettings.getString(SETTINGS_FILE_NAME_FORMAT, FILENAME_DATE_FORMAT), clean = true) {
2123
const now = dayjs(value);

0 commit comments

Comments
 (0)