nc-photos/lib/use_case/scan_dir.dart

58 lines
1.9 KiB
Dart
Raw Normal View History

2021-04-10 06:28:12 +02:00
import 'package:logging/logging.dart';
import 'package:nc_photos/account.dart';
2021-09-04 14:35:04 +02:00
import 'package:nc_photos/debug_util.dart';
2021-04-10 06:28:12 +02:00
import 'package:nc_photos/entity/file.dart';
2021-04-30 17:25:15 +02:00
import 'package:nc_photos/exception.dart';
2021-10-26 16:44:33 +02:00
import 'package:nc_photos/exception_event.dart';
import 'package:nc_photos/remote_storage_util.dart' as remote_storage_util;
2021-04-10 06:28:12 +02:00
import 'package:nc_photos/use_case/ls.dart';
class ScanDir {
ScanDir(this.fileRepo);
/// List all files under a dir recursively
///
/// Dirs with a .nomedia/.noimage file will be ignored. The returned stream
/// would emit either List<File> data or an exception
Stream<dynamic> call(Account account, File root) async* {
try {
final items = await Ls(fileRepo)(account, root);
if (_shouldScanIgnoreDir(items)) {
return;
}
yield items.where((element) => element.isCollection != true).toList();
for (final i in items.where((element) =>
element.isCollection == true &&
!element.path
.endsWith(remote_storage_util.getRemoteStorageDir(account)))) {
2021-04-10 06:28:12 +02:00
yield* this(account, i);
}
2021-10-26 16:44:33 +02:00
} on CacheNotFoundException catch (e, stackTrace) {
2021-04-30 17:25:15 +02:00
_log.info("[call] Cache not found");
2021-10-26 16:44:33 +02:00
yield ExceptionEvent(e, stackTrace);
} catch (e, stackTrace) {
2021-04-27 22:06:16 +02:00
_log.shout(
"[call] Failed while listing dir" +
2021-09-04 14:35:04 +02:00
(shouldLogFileName ? ": ${root.path}" : ""),
2021-04-27 22:06:16 +02:00
e,
2021-10-26 16:44:33 +02:00
stackTrace);
2021-04-10 06:28:12 +02:00
// for some reason exception thrown here can't be caught outside
// rethrow;
2021-10-26 16:44:33 +02:00
yield ExceptionEvent(e, stackTrace);
2021-04-10 06:28:12 +02:00
}
}
/// Return if this dir should be ignored in a scan op based on files under
/// this dir
static bool _shouldScanIgnoreDir(Iterable<File> files) {
return files.any((element) {
final basename = element.filename;
2021-04-10 06:28:12 +02:00
return basename == ".nomedia" || basename == ".noimage";
});
}
final FileRepo fileRepo;
static final _log = Logger("use_case.scan_dir.ScanDir");
}