nc-photos/lib/use_case/list_album.dart

63 lines
1.7 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-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';
class ListAlbum {
ListAlbum(this.fileRepo, this.albumRepo);
/// List all albums associated with [account]
Future<List<Album>> call(Account account) async {
2021-05-20 22:19:22 +02:00
final results = await _call(account);
if (results.isEmpty) {
if (await CompatV15.migrateAlbumFiles(account, fileRepo)) {
// migrated
return await _call(account);
} else {
// no need to migrate
return [];
}
} else {
return results;
}
}
Future<List<Album>> _call(Account account) async {
2021-04-10 06:28:12 +02:00
try {
2021-05-20 22:18:49 +02:00
final ls = await Ls(fileRepo)(
2021-04-10 06:28:12 +02:00
account,
File(
path: getAlbumFileRoot(account),
));
2021-05-20 22:18:49 +02:00
final albumFiles =
ls.where((element) => element.isCollection != true).toList();
2021-04-10 06:28:12 +02:00
final albums = <Album>[];
for (final f in albumFiles) {
final album = await albumRepo.get(account, f);
albums.add(album);
}
try {
albumRepo.cleanUp(account, albumFiles);
} catch (e, stacktrace) {
// not important, log and ignore
2021-05-20 22:19:22 +02:00
_log.shout("[_call] Failed while cleanUp", e, stacktrace);
2021-04-10 06:28:12 +02:00
}
return albums;
} catch (e) {
if (e is ApiException && e.response.statusCode == 404) {
// no albums
return [];
}
rethrow;
}
}
final FileRepo fileRepo;
final AlbumRepo albumRepo;
static final _log = Logger("use_case.list_album.ListAlbum");
}