nc-photos/lib/use_case/find_file.dart

32 lines
1.1 KiB
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/entity/file.dart';
import 'package:nc_photos/entity/file_util.dart' as file_util;
class FindFile {
2021-11-01 10:50:13 +01:00
const FindFile(this.appDb);
2021-10-06 22:32:36 +02:00
/// Find the [File] in the DB by [fileId]
Future<File> call(Account account, int fileId) async {
2021-11-01 10:50:13 +01:00
return await appDb.use((db) async {
2021-10-06 22:32:36 +02:00
final transaction =
db.transaction(AppDb.fileDbStoreName, idbModeReadOnly);
final store = transaction.objectStore(AppDb.fileDbStoreName);
final index = store.index(AppDbFileDbEntry.indexName);
final List dbItems = await index
.getAll(AppDbFileDbEntry.toNamespacedFileId(account, fileId));
// find the one owned by us
final dbItem = dbItems.firstWhere((element) {
final e = AppDbFileDbEntry.fromJson(element.cast<String, dynamic>());
2021-11-12 22:13:02 +01:00
return file_util.getUserDirName(e.file) == account.username;
2021-10-06 22:32:36 +02:00
});
final dbEntry = AppDbFileDbEntry.fromJson(dbItem.cast<String, dynamic>());
return dbEntry.file;
});
}
2021-11-01 10:50:13 +01:00
final AppDb appDb;
2021-10-06 22:32:36 +02:00
}