nc-photos/lib/use_case/list_album.dart

71 lines
2 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/entity/album.dart';
import 'package:nc_photos/entity/file.dart';
import 'package:nc_photos/exception.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-04-10 06:28:12 +02:00
import 'package:nc_photos/use_case/ls.dart';
import 'package:tuple/tuple.dart';
2021-04-10 06:28:12 +02:00
class ListAlbum {
ListAlbum(this.fileRepo, this.albumRepo);
/// List all albums associated with [account]
///
/// The returned stream would emit either Album data or a tuple of exception
/// and stacktrace
Stream<dynamic> call(Account account) async* {
bool hasAlbum = false;
await for (final result in _call(account)) {
hasAlbum = true;
yield result;
}
if (!hasAlbum) {
2021-05-20 22:19:22 +02:00
if (await CompatV15.migrateAlbumFiles(account, 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(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
));
2021-07-18 19:16:57 +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
}
2021-07-18 19:16:57 +02:00
yield Tuple2(e, stacktrace);
return;
2021-04-10 06:28:12 +02:00
}
final albumFiles =
ls.where((element) => element.isCollection != true).toList();
for (final f in albumFiles) {
try {
yield await albumRepo.get(account, f);
} catch (e, stacktrace) {
yield Tuple2(e, stacktrace);
}
}
try {
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 FileRepo fileRepo;
final AlbumRepo albumRepo;
static final _log = Logger("use_case.list_album.ListAlbum");
}