legalizeFATFilename function
Sanitizes the filename for FAT systems.
replacement is used to replace illegal characters.
If the filename is empty after sanitization, it will be replaced with placeholder.
It is recommended to not use an empty replacement and an empty placeholder as it may result in an empty filename.
Implementation
String legalizeFATFilename(String filename, {String replacement = '_', String placeholder = 'untitled'}) {
var result = filename;
result = result.replaceAll(controlCharacterRegex, replacement);
// Replace illegal characters
result = result.replaceAll(illegalFATCharactersRegex, replacement);
// If the filename is empty, replace it with a placeholder
if (result.isEmpty || isRelativePath(result)) {
result = placeholder;
}
// Check length
if (result.length > 255) {
result = result.substring(0, 255);
}
return result;
}