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';
|
|
|
|
|
|
|
|
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
|
|
|
|
2021-10-06 22:32:36 +02:00
|
|
|
/// Find the [File] in the DB by [fileId]
|
|
|
|
Future<File> call(Account account, int fileId) async {
|
2022-01-02 09:00:49 +01:00
|
|
|
final dbItem = await _c.appDb.use((db) async {
|
2022-01-01 21:34:40 +01:00
|
|
|
final transaction = db.transaction(AppDb.file2StoreName, idbModeReadOnly);
|
|
|
|
final store = transaction.objectStore(AppDb.file2StoreName);
|
2022-01-02 09:00:49 +01:00
|
|
|
return await store
|
2022-01-01 21:34:40 +01:00
|
|
|
.getObject(AppDbFile2Entry.toPrimaryKey(account, fileId)) as Map?;
|
2021-10-06 22:32:36 +02:00
|
|
|
});
|
2022-01-02 09:00:49 +01: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
|
|
|
|
2021-12-07 19:42:25 +01:00
|
|
|
final DiContainer _c;
|
2021-10-06 22:32:36 +02:00
|
|
|
}
|