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
|
|
|
import 'package:nc_photos/entity/file_util.dart' as file_util;
|
|
|
|
|
|
|
|
/// Resync files inside an album with the file db
|
|
|
|
class ResyncAlbum {
|
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
|
|
|
}
|
2021-06-14 15:51:29 +02:00
|
|
|
return await AppDb.use((db) async {
|
|
|
|
final transaction =
|
2021-09-08 11:40:03 +02:00
|
|
|
db.transaction(AppDb.fileDbStoreName, idbModeReadOnly);
|
2021-06-14 15:51:29 +02:00
|
|
|
final store = transaction.objectStore(AppDb.fileDbStoreName);
|
|
|
|
final index = store.index(AppDbFileDbEntry.indexName);
|
|
|
|
final newItems = <AlbumItem>[];
|
2021-06-24 18:26:56 +02:00
|
|
|
for (final item in AlbumStaticProvider.of(album).items) {
|
2021-06-14 15:51:29 +02:00
|
|
|
if (item is AlbumFileItem) {
|
|
|
|
try {
|
|
|
|
newItems.add(await _syncOne(account, item, store, index));
|
|
|
|
} catch (e, stacktrace) {
|
|
|
|
_log.shout(
|
|
|
|
"[call] Failed syncing file in album" +
|
2021-09-04 14:35:04 +02:00
|
|
|
(shouldLogFileName ? ": '${item.file.path}'" : ""),
|
2021-06-14 15:51:29 +02:00
|
|
|
e,
|
|
|
|
stacktrace);
|
2021-07-17 11:09:17 +02:00
|
|
|
newItems.add(item);
|
2021-06-14 15:51:29 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
newItems.add(item);
|
|
|
|
}
|
|
|
|
}
|
2021-09-25 18:22:19 +02:00
|
|
|
return newItems;
|
2021-06-14 15:51:29 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<AlbumFileItem> _syncOne(Account account, AlbumFileItem item,
|
|
|
|
ObjectStore objStore, Index index) async {
|
2021-07-23 22:05:57 +02:00
|
|
|
Map? dbItem;
|
2021-06-14 15:51:29 +02:00
|
|
|
if (item.file.fileId != null) {
|
2021-09-08 12:44:14 +02:00
|
|
|
final List dbItems = await index.getAll(
|
|
|
|
AppDbFileDbEntry.toNamespacedFileId(account, item.file.fileId!));
|
2021-06-14 15:51:29 +02:00
|
|
|
// find the one owned by us
|
2021-07-23 22:05:57 +02:00
|
|
|
try {
|
|
|
|
dbItem = dbItems.firstWhere((element) {
|
|
|
|
final e = AppDbFileDbEntry.fromJson(element.cast<String, dynamic>());
|
|
|
|
return file_util.getUserDirName(e.file) == account.username;
|
|
|
|
});
|
|
|
|
} on StateError catch (_) {
|
|
|
|
// not found
|
|
|
|
}
|
2021-06-14 15:51:29 +02:00
|
|
|
} else {
|
|
|
|
dbItem = await objStore
|
2021-09-26 20:58:46 +02:00
|
|
|
.getObject(AppDbFileDbEntry.toPrimaryKey(account, item.file)) as Map?;
|
2021-06-14 15:51:29 +02:00
|
|
|
}
|
|
|
|
if (dbItem == null) {
|
|
|
|
_log.warning(
|
|
|
|
"[_syncOne] File doesn't exist in DB, removed?: '${item.file.path}'");
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
final dbEntry = AppDbFileDbEntry.fromJson(dbItem.cast<String, dynamic>());
|
|
|
|
return AlbumFileItem(file: dbEntry.file);
|
|
|
|
}
|
|
|
|
|
|
|
|
static final _log = Logger("use_case.resync_album.ResyncAlbum");
|
|
|
|
}
|