nc-photos/app/lib/use_case/resync_album.dart

70 lines
2 KiB
Dart
Raw Normal View History

import 'package:collection/collection.dart';
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';
import 'package:nc_photos/di_container.dart';
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';
import 'package:nc_photos/use_case/find_file.dart';
2022-12-16 16:01:04 +01:00
import 'package:np_codegen/np_codegen.dart';
part 'resync_album.g.dart';
/// Resync files inside an album with the file db
2022-12-16 16:01:04 +01:00
@npLog
class ResyncAlbum {
ResyncAlbum(this._c)
: assert(require(_c)),
assert(FindFile.require(_c));
static bool require(DiContainer c) => true;
2021-11-01 10:50:13 +01: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) {
throw ArgumentError(
"Resync only make sense for static albums: ${album.name}");
2021-06-24 18:26:56 +02:00
}
final items = AlbumStaticProvider.of(album).items;
final files = await FindFile(_c)(
account,
items
.whereType<AlbumFileItem>()
.map((i) => i.file.fileId)
.whereNotNull()
.toList(),
onFileNotFound: (_) {},
2022-06-06 12:02:54 +02:00
);
final fileIt = files.iterator;
var nextFile = fileIt.moveNext() ? fileIt.current : null;
return items.map((i) {
if (i is AlbumFileItem) {
try {
if (i.file.fileId! == nextFile?.fileId) {
final newItem = i.copyWith(
file: nextFile,
);
nextFile = fileIt.moveNext() ? fileIt.current : null;
return newItem;
} else {
_log.warning("[call] File not found: ${logFilename(i.file.path)}");
return i;
}
} catch (e, stackTrace) {
_log.shout(
"[call] Failed syncing file in album: ${logFilename(i.file.path)}",
e,
stackTrace);
return i;
}
} else {
return i;
}
}).toList();
}
final DiContainer _c;
}