2022-08-16 16:35:35 +02:00
|
|
|
import 'package:collection/collection.dart';
|
2021-06-14 15:51:29 +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-07-05 22:20:24 +02:00
|
|
|
import 'package:nc_photos/di_container.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';
|
2022-07-05 22:20:24 +02:00
|
|
|
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';
|
2021-06-14 15:51:29 +02:00
|
|
|
|
|
|
|
/// Resync files inside an album with the file db
|
2022-12-16 16:01:04 +01:00
|
|
|
@npLog
|
2021-06-14 15:51:29 +02:00
|
|
|
class ResyncAlbum {
|
2022-07-05 22:20:24 +02:00
|
|
|
ResyncAlbum(this._c)
|
|
|
|
: assert(require(_c)),
|
|
|
|
assert(FindFile.require(_c));
|
|
|
|
|
|
|
|
static bool require(DiContainer c) => true;
|
2021-11-01 10:50:13 +01:00
|
|
|
|
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;
|
2022-07-05 22:20:24 +02:00
|
|
|
|
|
|
|
final files = await FindFile(_c)(
|
|
|
|
account,
|
2022-08-16 16:35:35 +02:00
|
|
|
items
|
|
|
|
.whereType<AlbumFileItem>()
|
|
|
|
.map((i) => i.file.fileId)
|
|
|
|
.whereNotNull()
|
|
|
|
.toList(),
|
2022-07-05 22:20:24 +02:00
|
|
|
onFileNotFound: (_) {},
|
2022-06-06 12:02:54 +02:00
|
|
|
);
|
2022-07-05 22:20:24 +02:00
|
|
|
final fileIt = files.iterator;
|
|
|
|
var nextFile = fileIt.moveNext() ? fileIt.current : null;
|
2022-01-02 09:06:39 +01:00
|
|
|
return items.map((i) {
|
|
|
|
if (i is AlbumFileItem) {
|
|
|
|
try {
|
2022-07-05 22:20:24 +02:00
|
|
|
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;
|
|
|
|
}
|
2022-01-02 09:06:39 +01:00
|
|
|
} 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
|
|
|
}
|
|
|
|
|
2022-07-05 22:20:24 +02:00
|
|
|
final DiContainer _c;
|
2021-06-14 15:51:29 +02:00
|
|
|
}
|