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

80 lines
2.4 KiB
Dart
Raw Normal View History

2021-04-10 06:28:12 +02:00
import 'package:logging/logging.dart';
import 'package:nc_photos/account.dart';
import 'package:nc_photos/di_container.dart';
2021-04-10 06:28:12 +02:00
import 'package:nc_photos/entity/album.dart';
import 'package:nc_photos/entity/file.dart';
import 'package:nc_photos/exception.dart';
import 'package:nc_photos/exception_event.dart';
2021-05-23 19:18:24 +02:00
import 'package:nc_photos/remote_storage_util.dart' as remote_storage_util;
2021-05-20 22:19:22 +02:00
import 'package:nc_photos/use_case/compat/v15.dart';
2021-08-06 06:52:20 +02:00
import 'package:nc_photos/use_case/compat/v25.dart';
2021-04-10 06:28:12 +02:00
import 'package:nc_photos/use_case/ls.dart';
class ListAlbum {
ListAlbum(this._c) : assert(require(_c));
static bool require(DiContainer c) =>
DiContainer.has(c, DiType.albumRepo) &&
DiContainer.has(c, DiType.fileRepo);
2021-04-10 06:28:12 +02:00
/// List all albums associated with [account]
///
2021-11-08 22:41:29 +01:00
/// The returned stream would emit either [Album] or [ExceptionEvent]
Stream<dynamic> call(Account account) async* {
bool hasAlbum = false;
await for (final result in _call(account)) {
hasAlbum = true;
yield result;
}
if (!hasAlbum) {
if (await CompatV15.migrateAlbumFiles(account, _c.fileRepo)) {
// migrated, try again
yield* _call(account);
2021-05-20 22:19:22 +02:00
}
}
}
Stream<dynamic> _call(Account account) async* {
List<File> ls;
2021-04-10 06:28:12 +02:00
try {
ls = await Ls(_c.fileRepo)(
2021-04-10 06:28:12 +02:00
account,
File(
2021-05-23 19:18:24 +02:00
path: remote_storage_util.getRemoteAlbumsDir(account),
2021-04-10 06:28:12 +02:00
));
} catch (e, stackTrace) {
2021-04-10 06:28:12 +02:00
if (e is ApiException && e.response.statusCode == 404) {
// no albums
return;
2021-04-10 06:28:12 +02:00
}
yield ExceptionEvent(e, stackTrace);
2021-07-18 19:16:57 +02:00
return;
2021-04-10 06:28:12 +02:00
}
final albumFiles =
ls.where((element) => element.isCollection != true).toList();
2021-08-06 06:52:20 +02:00
for (var i = 0; i < albumFiles.length; ++i) {
var f = albumFiles[i];
try {
2021-08-06 06:52:20 +02:00
if (CompatV25.isAlbumFileNeedMigration(f)) {
f = await CompatV25.migrateAlbumFile(_c, account, f);
2021-08-06 06:52:20 +02:00
}
albumFiles[i] = f;
yield await _c.albumRepo.get(account, f);
} catch (e, stackTrace) {
yield ExceptionEvent(e, stackTrace);
}
}
try {
_c.albumRepo.cleanUp(
account, remote_storage_util.getRemoteAlbumsDir(account), albumFiles);
} catch (e, stacktrace) {
// not important, log and ignore
_log.shout("[_call] Failed while cleanUp", e, stacktrace);
}
2021-04-10 06:28:12 +02:00
}
final DiContainer _c;
2021-04-10 06:28:12 +02:00
static final _log = Logger("use_case.list_album.ListAlbum");
}