nc-photos/lib/use_case/populate_album.dart

80 lines
2.9 KiB
Dart
Raw Normal View History

import 'package:logging/logging.dart';
import 'package:nc_photos/account.dart';
2021-11-01 10:50:13 +01:00
import 'package:nc_photos/app_db.dart';
2021-09-04 14:35:04 +02:00
import 'package:nc_photos/debug_util.dart';
import 'package:nc_photos/entity/album.dart';
2021-07-05 09:54:01 +02:00
import 'package:nc_photos/entity/album/item.dart';
import 'package:nc_photos/entity/album/provider.dart';
import 'package:nc_photos/entity/file.dart';
import 'package:nc_photos/entity/file/data_source.dart';
2022-01-15 11:35:15 +01:00
import 'package:nc_photos/entity/file_util.dart' as file_util;
2021-10-26 16:44:33 +02:00
import 'package:nc_photos/exception_event.dart';
import 'package:nc_photos/use_case/scan_dir.dart';
class PopulateAlbum {
2021-11-01 10:50:13 +01:00
const PopulateAlbum(this.appDb);
Future<List<AlbumItem>> call(Account account, Album album) async {
if (album.provider is AlbumStaticProvider) {
_log.warning(
"[call] Populate only make sense for dynamic albums: ${album.name}");
return AlbumStaticProvider.of(album).items;
2022-01-15 11:35:15 +01:00
} else if (album.provider is AlbumDirProvider) {
return _populateDirAlbum(account, album);
2022-01-15 11:35:15 +01:00
} else if (album.provider is AlbumMemoryProvider) {
return _populateMemoryAlbum(account, album);
} else {
throw ArgumentError(
"Unknown album provider: ${album.provider.runtimeType}");
}
}
Future<List<AlbumItem>> _populateDirAlbum(
Account account, Album album) async {
assert(album.provider is AlbumDirProvider);
final provider = album.provider as AlbumDirProvider;
final products = <AlbumItem>[];
for (final d in provider.dirs) {
2021-11-01 10:50:13 +01:00
final stream = ScanDir(FileRepo(FileCachedDataSource(appDb)))(account, d);
await for (final result in stream) {
2021-10-26 16:44:33 +02:00
if (result is ExceptionEvent) {
_log.shout(
2021-12-02 09:27:11 +01:00
"[_populateDirAlbum] Failed while scanning dir: ${logFilename(d.path)}",
2021-10-26 16:44:33 +02:00
result.error,
result.stackTrace);
continue;
}
products.addAll((result as List).cast<File>().map((f) => AlbumFileItem(
addedBy: account.username,
addedAt: DateTime.now(),
file: f,
)));
}
}
return products;
}
2022-01-15 11:35:15 +01:00
Future<List<AlbumItem>> _populateMemoryAlbum(
Account account, Album album) async {
assert(album.provider is AlbumMemoryProvider);
final provider = album.provider as AlbumMemoryProvider;
final date = DateTime(provider.year, provider.month, provider.day);
2022-01-15 20:07:39 +01:00
final from = date.subtract(const Duration(days: 2));
final to = date.add(const Duration(days: 3));
2022-01-27 22:05:48 +01:00
final files = await FileAppDbDataSource(appDb).listByDate(
account, from.millisecondsSinceEpoch, to.millisecondsSinceEpoch);
2022-01-15 11:35:15 +01:00
return files
.where((f) => file_util.isSupportedFormat(f))
.map((f) => AlbumFileItem(
addedBy: account.username,
addedAt: DateTime.now(),
file: f,
))
.toList();
}
2021-11-01 10:50:13 +01:00
final AppDb appDb;
static final _log = Logger("use_case.populate_album.PopulateAlbum");
}