2021-04-10 06:28:12 +02:00
|
|
|
import 'package:logging/logging.dart';
|
|
|
|
import 'package:nc_photos/account.dart';
|
2021-12-07 19:42:25 +01:00
|
|
|
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';
|
2021-09-28 11:05:33 +02:00
|
|
|
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 {
|
2021-12-07 19:42:25 +01:00
|
|
|
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-07-09 18:33:07 +02:00
|
|
|
///
|
2021-11-08 22:41:29 +01:00
|
|
|
/// The returned stream would emit either [Album] or [ExceptionEvent]
|
2021-07-09 18:33:07 +02:00
|
|
|
Stream<dynamic> call(Account account) async* {
|
|
|
|
bool hasAlbum = false;
|
|
|
|
await for (final result in _call(account)) {
|
|
|
|
hasAlbum = true;
|
|
|
|
yield result;
|
|
|
|
}
|
|
|
|
if (!hasAlbum) {
|
2021-12-07 19:42:25 +01:00
|
|
|
if (await CompatV15.migrateAlbumFiles(account, _c.fileRepo)) {
|
2021-07-09 18:33:07 +02:00
|
|
|
// migrated, try again
|
|
|
|
yield* _call(account);
|
2021-05-20 22:19:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-09 18:33:07 +02:00
|
|
|
Stream<dynamic> _call(Account account) async* {
|
|
|
|
List<File> ls;
|
2021-04-10 06:28:12 +02:00
|
|
|
try {
|
2021-12-07 19:42:25 +01:00
|
|
|
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
|
|
|
));
|
2021-09-28 11:05:33 +02:00
|
|
|
} catch (e, stackTrace) {
|
2021-04-10 06:28:12 +02:00
|
|
|
if (e is ApiException && e.response.statusCode == 404) {
|
|
|
|
// no albums
|
2021-07-09 18:33:07 +02:00
|
|
|
return;
|
2021-04-10 06:28:12 +02:00
|
|
|
}
|
2021-09-28 11:05:33 +02:00
|
|
|
yield ExceptionEvent(e, stackTrace);
|
2021-07-18 19:16:57 +02:00
|
|
|
return;
|
2021-04-10 06:28:12 +02:00
|
|
|
}
|
2021-07-09 18:33:07 +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];
|
2021-07-09 18:33:07 +02:00
|
|
|
try {
|
2021-08-06 06:52:20 +02:00
|
|
|
if (CompatV25.isAlbumFileNeedMigration(f)) {
|
2021-12-07 19:42:25 +01:00
|
|
|
f = await CompatV25.migrateAlbumFile(_c, account, f);
|
2021-08-06 06:52:20 +02:00
|
|
|
}
|
|
|
|
albumFiles[i] = f;
|
2021-12-07 19:42:25 +01:00
|
|
|
yield await _c.albumRepo.get(account, f);
|
2021-09-28 11:05:33 +02:00
|
|
|
} catch (e, stackTrace) {
|
|
|
|
yield ExceptionEvent(e, stackTrace);
|
2021-07-09 18:33:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
try {
|
2021-12-07 19:42:25 +01:00
|
|
|
_c.albumRepo.cleanUp(
|
2021-07-22 08:27:14 +02:00
|
|
|
account, remote_storage_util.getRemoteAlbumsDir(account), albumFiles);
|
2021-07-09 18:33:07 +02:00
|
|
|
} catch (e, stacktrace) {
|
|
|
|
// not important, log and ignore
|
|
|
|
_log.shout("[_call] Failed while cleanUp", e, stacktrace);
|
|
|
|
}
|
2021-04-10 06:28:12 +02:00
|
|
|
}
|
|
|
|
|
2021-12-07 19:42:25 +01:00
|
|
|
final DiContainer _c;
|
2021-04-10 06:28:12 +02:00
|
|
|
|
|
|
|
static final _log = Logger("use_case.list_album.ListAlbum");
|
|
|
|
}
|