2021-10-06 22:32:36 +02:00
|
|
|
import 'package:idb_shim/idb_client.dart';
|
|
|
|
import 'package:nc_photos/account.dart';
|
|
|
|
import 'package:nc_photos/app_db.dart';
|
2021-12-07 19:42:25 +01:00
|
|
|
import 'package:nc_photos/di_container.dart';
|
2021-10-06 22:32:36 +02:00
|
|
|
import 'package:nc_photos/entity/file.dart';
|
2022-01-24 21:32:18 +01:00
|
|
|
import 'package:quiver/iterables.dart';
|
2021-10-06 22:32:36 +02:00
|
|
|
|
|
|
|
class FindFile {
|
2021-12-07 19:42:25 +01:00
|
|
|
FindFile(this._c) : assert(require(_c));
|
|
|
|
|
|
|
|
static bool require(DiContainer c) => DiContainer.has(c, DiType.appDb);
|
2021-11-01 10:50:13 +01:00
|
|
|
|
2022-01-24 21:32:18 +01:00
|
|
|
/// Find list of files in the DB by [fileIds]
|
|
|
|
///
|
|
|
|
/// If an id is not found, [onFileNotFound] will be called. If
|
|
|
|
/// [onFileNotFound] is null, a [StateError] will be thrown
|
|
|
|
Future<List<File>> call(
|
|
|
|
Account account,
|
|
|
|
List<int> fileIds, {
|
|
|
|
void Function(int fileId)? onFileNotFound,
|
|
|
|
}) async {
|
|
|
|
final dbItems = await _c.appDb.use((db) async {
|
2022-01-01 21:34:40 +01:00
|
|
|
final transaction = db.transaction(AppDb.file2StoreName, idbModeReadOnly);
|
2022-01-24 21:32:18 +01:00
|
|
|
final fileStore = transaction.objectStore(AppDb.file2StoreName);
|
|
|
|
return await Future.wait(fileIds.map((id) =>
|
|
|
|
fileStore.getObject(AppDbFile2Entry.toPrimaryKey(account, id))));
|
2021-10-06 22:32:36 +02:00
|
|
|
});
|
2022-01-24 21:32:18 +01:00
|
|
|
final files = <File>[];
|
|
|
|
for (final pair in zip([fileIds, dbItems])) {
|
|
|
|
final dbItem = pair[1] as Map?;
|
|
|
|
if (dbItem == null) {
|
|
|
|
if (onFileNotFound == null) {
|
|
|
|
throw StateError("File ID not found: ${pair[0]}");
|
|
|
|
} else {
|
|
|
|
onFileNotFound(pair[0] as int);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
final dbEntry =
|
|
|
|
AppDbFile2Entry.fromJson(dbItem.cast<String, dynamic>());
|
|
|
|
files.add(dbEntry.file);
|
|
|
|
}
|
2022-01-02 09:00:49 +01:00
|
|
|
}
|
2022-01-24 21:32:18 +01:00
|
|
|
return files;
|
2021-10-06 22:32:36 +02:00
|
|
|
}
|
2021-11-01 10:50:13 +01:00
|
|
|
|
2021-12-07 19:42:25 +01:00
|
|
|
final DiContainer _c;
|
2021-10-06 22:32:36 +02:00
|
|
|
}
|