2021-09-08 12:44:14 +02:00
|
|
|
import 'package:idb_shim/idb_client.dart';
|
|
|
|
import 'package:logging/logging.dart';
|
|
|
|
import 'package:nc_photos/account.dart';
|
|
|
|
import 'package:nc_photos/app_db.dart';
|
2021-09-10 19:10:26 +02:00
|
|
|
import 'package:nc_photos/entity/face.dart';
|
2021-09-08 12:44:14 +02:00
|
|
|
import 'package:nc_photos/entity/file.dart';
|
|
|
|
import 'package:nc_photos/entity/file_util.dart' as file_util;
|
|
|
|
import 'package:nc_photos/exception.dart';
|
2021-10-29 13:46:51 +02:00
|
|
|
import 'package:nc_photos/string_extension.dart';
|
2021-09-08 12:44:14 +02:00
|
|
|
|
|
|
|
class PopulatePerson {
|
2021-11-01 10:50:13 +01:00
|
|
|
const PopulatePerson(this.appDb);
|
|
|
|
|
2021-09-10 19:10:26 +02:00
|
|
|
/// Return a list of files of the faces
|
|
|
|
Future<List<File>> call(Account account, List<Face> faces) async {
|
2021-11-01 10:50:13 +01:00
|
|
|
return await appDb.use((db) async {
|
2021-09-08 12:44:14 +02:00
|
|
|
final transaction =
|
|
|
|
db.transaction(AppDb.fileDbStoreName, idbModeReadOnly);
|
|
|
|
final store = transaction.objectStore(AppDb.fileDbStoreName);
|
|
|
|
final index = store.index(AppDbFileDbEntry.indexName);
|
|
|
|
final products = <File>[];
|
2021-09-10 19:10:26 +02:00
|
|
|
for (final f in faces) {
|
2021-09-08 12:44:14 +02:00
|
|
|
try {
|
|
|
|
products.add(await _populateOne(account, f, store, index));
|
|
|
|
} catch (e, stackTrace) {
|
|
|
|
_log.severe("[call] Failed populating file of face: ${f.fileId}", e,
|
|
|
|
stackTrace);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return products;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<File> _populateOne(
|
|
|
|
Account account, Face face, ObjectStore store, Index index) async {
|
|
|
|
final List dbItems = await index
|
|
|
|
.getAll(AppDbFileDbEntry.toNamespacedFileId(account, face.fileId));
|
|
|
|
// find the one owned by us
|
|
|
|
Map? dbItem;
|
|
|
|
try {
|
|
|
|
dbItem = dbItems.firstWhere((element) {
|
|
|
|
final e = AppDbFileDbEntry.fromJson(element.cast<String, dynamic>());
|
2021-10-29 13:46:51 +02:00
|
|
|
return file_util
|
|
|
|
.getUserDirName(e.file)
|
|
|
|
.equalsIgnoreCase(account.username);
|
2021-09-08 12:44:14 +02:00
|
|
|
});
|
|
|
|
} on StateError catch (_) {
|
|
|
|
// not found
|
|
|
|
}
|
|
|
|
if (dbItem == null) {
|
|
|
|
_log.warning(
|
|
|
|
"[_populateOne] File doesn't exist in DB, removed?: '${face.fileId}'");
|
|
|
|
throw CacheNotFoundException();
|
|
|
|
}
|
|
|
|
final dbEntry = AppDbFileDbEntry.fromJson(dbItem.cast<String, dynamic>());
|
|
|
|
return dbEntry.file;
|
|
|
|
}
|
|
|
|
|
2021-11-01 10:50:13 +01:00
|
|
|
final AppDb appDb;
|
|
|
|
|
2021-09-08 12:44:14 +02:00
|
|
|
static final _log = Logger("use_case.populate_album.PopulatePerson");
|
|
|
|
}
|