findDuplicates function

Stream<FileLog> findDuplicates(
  1. List<Directory> dirs, {
  2. File? file,
  3. bool filter(
    1. String
    )?,
})

Stream the channel that transmits (FileLog), where the first is the original file and second is a duplicate.

For multiple directory searches, the Directory list is passed in the parameters.

Function filter takes the file path and returned bool type.

Implementation

Stream<FileLog> findDuplicates(
  List<Directory> dirs, {
  File? file,
  bool Function(String)? filter,
}) async* {
  if (dirs.isEmpty) return;
  Map<Digest, File> files = {};

  if (file != null) {
    files[await generateHashFile(file)] = file;
  }
  await for (final entitieFile
      in StreamGroup.merge([for (final dir in dirs) recListFile(dir)])) {
    if (file != null && file.path == entitieFile.path) continue;
    if (filter != null && !filter(entitieFile.path)) continue;

    var hash = await generateHashFile(entitieFile);

    if (files[hash] != null) {
      yield FileLog(
        file1: files[hash]!,
        file2: entitieFile,
        action: Action.compare,
      );
    } else {
      if (file == null) {
        files[hash] = entitieFile;
      }
    }
  }
}