2023-03-13 12:55:39 +01:00
|
|
|
import 'package:clock/clock.dart';
|
2022-01-29 12:31:32 +01:00
|
|
|
import 'package:kiwi/kiwi.dart';
|
2021-06-26 16:28:21 +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';
|
2022-01-29 12:31:32 +01:00
|
|
|
import 'package:nc_photos/di_container.dart';
|
2021-06-26 16:28:21 +02:00
|
|
|
import 'package:nc_photos/entity/album.dart';
|
2021-07-05 09:54:01 +02:00
|
|
|
import 'package:nc_photos/entity/album/item.dart';
|
2021-06-26 16:28:21 +02:00
|
|
|
import 'package:nc_photos/entity/album/provider.dart';
|
|
|
|
import 'package:nc_photos/entity/file.dart';
|
2021-10-26 16:44:33 +02:00
|
|
|
import 'package:nc_photos/exception_event.dart';
|
2022-01-29 12:31:32 +01:00
|
|
|
import 'package:nc_photos/use_case/list_tagged_file.dart';
|
2021-06-26 16:28:21 +02:00
|
|
|
import 'package:nc_photos/use_case/scan_dir.dart';
|
2022-12-16 16:01:04 +01:00
|
|
|
import 'package:np_codegen/np_codegen.dart';
|
2021-06-26 16:28:21 +02:00
|
|
|
|
2022-12-16 16:01:04 +01:00
|
|
|
part 'populate_album.g.dart';
|
|
|
|
|
|
|
|
@npLog
|
2021-06-26 16:28:21 +02:00
|
|
|
class PopulateAlbum {
|
2022-07-05 22:20:24 +02:00
|
|
|
PopulateAlbum(this._c) : assert(require(_c));
|
|
|
|
|
|
|
|
static bool require(DiContainer c) => DiContainer.has(c, DiType.fileRepo);
|
2021-11-01 10:50:13 +01:00
|
|
|
|
2021-06-26 16:28:21 +02:00
|
|
|
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) {
|
2021-06-26 16:28:21 +02:00
|
|
|
return _populateDirAlbum(account, album);
|
2022-01-29 12:31:32 +01:00
|
|
|
} else if (album.provider is AlbumTagProvider) {
|
|
|
|
return _populateTagAlbum(account, album);
|
2021-06-26 16:28:21 +02:00
|
|
|
} 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) {
|
2022-07-05 22:20:24 +02:00
|
|
|
final stream = ScanDir(_c.fileRepo)(account, d);
|
2021-06-26 16:28:21 +02:00
|
|
|
await for (final result in stream) {
|
2021-10-26 16:44:33 +02:00
|
|
|
if (result is ExceptionEvent) {
|
2021-06-26 16:28:21 +02:00
|
|
|
_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);
|
2021-06-26 16:28:21 +02:00
|
|
|
continue;
|
|
|
|
}
|
2021-10-23 20:13:06 +02:00
|
|
|
products.addAll((result as List).cast<File>().map((f) => AlbumFileItem(
|
2022-07-11 20:14:42 +02:00
|
|
|
addedBy: account.userId,
|
2023-03-13 12:55:39 +01:00
|
|
|
addedAt: clock.now(),
|
2021-10-23 20:13:06 +02:00
|
|
|
file: f,
|
|
|
|
)));
|
2021-06-26 16:28:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return products;
|
|
|
|
}
|
|
|
|
|
2022-01-29 12:31:32 +01:00
|
|
|
Future<List<AlbumItem>> _populateTagAlbum(
|
|
|
|
Account account, Album album) async {
|
|
|
|
assert(album.provider is AlbumTagProvider);
|
|
|
|
final provider = album.provider as AlbumTagProvider;
|
|
|
|
final products = <AlbumItem>[];
|
|
|
|
final c = KiwiContainer().resolve<DiContainer>();
|
|
|
|
final files = await ListTaggedFile(c)(account, provider.tags);
|
|
|
|
products.addAll(files.map((f) => AlbumFileItem(
|
2022-07-11 20:14:42 +02:00
|
|
|
addedBy: account.userId,
|
2023-03-13 12:55:39 +01:00
|
|
|
addedAt: clock.now(),
|
2022-01-29 12:31:32 +01:00
|
|
|
file: f,
|
|
|
|
)));
|
|
|
|
return products;
|
|
|
|
}
|
|
|
|
|
2022-07-05 22:20:24 +02:00
|
|
|
final DiContainer _c;
|
2021-06-26 16:28:21 +02:00
|
|
|
}
|