moveFileToItsResolutionFolder method

int moveFileToItsResolutionFolder(
  1. FileSystemEntity entity
)

Move a file to the target folder according to its resolution indicator

Implementation

int moveFileToItsResolutionFolder(FileSystemEntity entity) {
  var fileNameRegExp = config.fileNameRegExp;
  int nAffectedFile = 0;
  final fileNameMatch = fileNameRegExp.firstMatch(p.basename(entity.path));
  if (fileNameMatch != null) {
    // Extract a filename and its resolution
    String fileNameWithExt =
        '${fileNameMatch.group(1)}${fileNameMatch.group(4)}${fileNameMatch.group(5)}';
    double resolution = double.parse('${fileNameMatch.group(3)}');

    String resolutionDirectoryName = '${resolution.toStringAsFixed(1)}x';
    String toDirectory = '${entity.parent.path}/$resolutionDirectoryName/';
    Directory(toDirectory).createSync(recursive: true);
    String toPath = '$toDirectory$fileNameWithExt';

    if (config.allowOverwrite ||
        FileSystemEntity.typeSync(toPath) == FileSystemEntityType.notFound) {
      entity.renameSync('$toDirectory$fileNameWithExt');
      print('Moved ${entity.path} -> $toDirectory$fileNameWithExt');
      nAffectedFile = 1;
    } else {
      print(
          '$toDirectory$fileNameWithExt has already existed. Try changing the file name.');
    }
  }
  return nAffectedFile;
}