2021-06-14 15:51:29 +02:00
|
|
|
import 'package:idb_shim/idb_client.dart';
|
|
|
|
import 'package:logging/logging.dart';
|
|
|
|
import 'package:nc_photos/account.dart';
|
|
|
|
import 'package:nc_photos/app_db.dart';
|
2021-09-04 14:35:04 +02:00
|
|
|
import 'package:nc_photos/debug_util.dart';
|
2021-06-14 15:51:29 +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-24 18:26:56 +02:00
|
|
|
import 'package:nc_photos/entity/album/provider.dart';
|
2021-06-14 15:51:29 +02:00
|
|
|
|
|
|
|
/// Resync files inside an album with the file db
|
|
|
|
class ResyncAlbum {
|
2021-11-01 10:50:13 +01:00
|
|
|
const ResyncAlbum(this.appDb);
|
|
|
|
|
2021-09-25 18:22:19 +02:00
|
|
|
Future<List<AlbumItem>> call(Account account, Album album) async {
|
|
|
|
_log.info("[call] Resync album: ${album.name}");
|
2021-06-24 18:26:56 +02:00
|
|
|
if (album.provider is! AlbumStaticProvider) {
|
2021-09-25 18:22:19 +02:00
|
|
|
throw ArgumentError(
|
|
|
|
"Resync only make sense for static albums: ${album.name}");
|
2021-06-24 18:26:56 +02:00
|
|
|
}
|
2022-01-02 09:06:39 +01:00
|
|
|
final items = AlbumStaticProvider.of(album).items;
|
|
|
|
final fileIds =
|
|
|
|
items.whereType<AlbumFileItem>().map((i) => i.file.fileId!).toList();
|
|
|
|
final dbItems = Map.fromEntries(await appDb.use((db) async {
|
2022-01-01 21:34:40 +01:00
|
|
|
final transaction = db.transaction(AppDb.file2StoreName, idbModeReadOnly);
|
|
|
|
final store = transaction.objectStore(AppDb.file2StoreName);
|
2022-01-02 09:06:39 +01:00
|
|
|
return await Future.wait(fileIds.map(
|
|
|
|
(id) async => MapEntry(
|
|
|
|
id,
|
|
|
|
await store.getObject(AppDbFile2Entry.toPrimaryKey(account, id))
|
|
|
|
as Map?,
|
|
|
|
),
|
|
|
|
));
|
|
|
|
}));
|
|
|
|
return items.map((i) {
|
|
|
|
if (i is AlbumFileItem) {
|
|
|
|
try {
|
|
|
|
final dbItem = dbItems[i.file.fileId]!;
|
|
|
|
final dbEntry =
|
|
|
|
AppDbFile2Entry.fromJson(dbItem.cast<String, dynamic>());
|
|
|
|
return i.copyWith(
|
|
|
|
file: dbEntry.file,
|
|
|
|
);
|
|
|
|
} catch (e, stackTrace) {
|
|
|
|
_log.shout(
|
|
|
|
"[call] Failed syncing file in album: ${logFilename(i.file.path)}",
|
|
|
|
e,
|
|
|
|
stackTrace);
|
|
|
|
return i;
|
2021-06-14 15:51:29 +02:00
|
|
|
}
|
2022-01-02 09:06:39 +01:00
|
|
|
} else {
|
|
|
|
return i;
|
2021-06-14 15:51:29 +02:00
|
|
|
}
|
2022-01-02 09:06:39 +01:00
|
|
|
}).toList();
|
2021-06-14 15:51:29 +02:00
|
|
|
}
|
|
|
|
|
2021-11-01 10:50:13 +01:00
|
|
|
final AppDb appDb;
|
|
|
|
|
2021-06-14 15:51:29 +02:00
|
|
|
static final _log = Logger("use_case.resync_album.ResyncAlbum");
|
|
|
|
}
|