nc-photos/lib/use_case/find_file.dart

29 lines
982 B
Dart
Raw Normal View History

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';
import 'package:nc_photos/di_container.dart';
2021-10-06 22:32:36 +02:00
import 'package:nc_photos/entity/file.dart';
class FindFile {
FindFile(this._c) : assert(require(_c));
static bool require(DiContainer c) => DiContainer.has(c, DiType.appDb);
2021-11-01 10:50:13 +01:00
2021-10-06 22:32:36 +02:00
/// Find the [File] in the DB by [fileId]
Future<File> call(Account account, int fileId) async {
final dbItem = await _c.appDb.use((db) async {
final transaction = db.transaction(AppDb.file2StoreName, idbModeReadOnly);
final store = transaction.objectStore(AppDb.file2StoreName);
return await store
.getObject(AppDbFile2Entry.toPrimaryKey(account, fileId)) as Map?;
2021-10-06 22:32:36 +02:00
});
if (dbItem == null) {
throw StateError("File ID not found: $fileId");
}
final dbEntry = AppDbFile2Entry.fromJson(dbItem.cast<String, dynamic>());
return dbEntry.file;
2021-10-06 22:32:36 +02:00
}
2021-11-01 10:50:13 +01:00
final DiContainer _c;
2021-10-06 22:32:36 +02:00
}