defaultFilesystemSanitiser function

FilesystemSanitiserResult defaultFilesystemSanitiser(
  1. String input
)

Implementation

FilesystemSanitiserResult defaultFilesystemSanitiser(String input) {
  final List<String> errorMessages = [];
  String validOutput = input;

  // Apply other character rules with general RegExp
  validOutput = validOutput.replaceAll(RegExp(r'[\\\\/\:\*\?\"\<\>\|]'), '_');
  if (validOutput != input) {
    errorMessages
        .add('The name cannot contain invalid characters: \'[NUL]\\/:*?"<>|\'');
  }

  // Trim
  validOutput = validOutput.trim();
  if (validOutput != input) {
    errorMessages.add('The name cannot contain leading and/or trailing spaces');
  }

  // Ensure is not empty
  if (validOutput.isEmpty) {
    errorMessages.add('The name cannot be empty');
    validOutput = '_';
  }

  // Ensure is not just '.'
  if (validOutput.replaceAll('.', '').isEmpty) {
    errorMessages.add('The name cannot consist of only periods (.)');
    validOutput = validOutput.replaceAll('.', '_');
  }

  // Reduce string to under 255 chars (keeps end)
  if (validOutput.length > 255) {
    validOutput = validOutput.substring(validOutput.length - 255);
    if (validOutput != input) {
      errorMessages.add('The name cannot contain more than 255 characters');
    }
  }

  return FilesystemSanitiserResult(
    validOutput: validOutput,
    errorMessages: errorMessages,
  );
}