2022-07-05 22:20:24 +02:00
|
|
|
import 'package:logging/logging.dart';
|
2021-10-06 22:32:36 +02:00
|
|
|
import 'package:nc_photos/account.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';
|
2023-02-20 15:21:35 +01:00
|
|
|
import 'package:nc_photos/entity/sqlite/database.dart' as sql;
|
2022-12-16 16:01:04 +01:00
|
|
|
import 'package:np_codegen/np_codegen.dart';
|
2023-08-25 19:31:06 +02:00
|
|
|
import 'package:np_collection/np_collection.dart';
|
2021-10-06 22:32:36 +02:00
|
|
|
|
2022-12-16 16:01:04 +01:00
|
|
|
part 'find_file.g.dart';
|
|
|
|
|
|
|
|
@npLog
|
2021-10-06 22:32:36 +02:00
|
|
|
class FindFile {
|
2021-12-07 19:42:25 +01:00
|
|
|
FindFile(this._c) : assert(require(_c));
|
|
|
|
|
2022-07-05 22:20:24 +02:00
|
|
|
static bool require(DiContainer c) => DiContainer.has(c, DiType.sqliteDb);
|
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 {
|
2022-07-05 22:20:24 +02:00
|
|
|
_log.info("[call] fileIds: ${fileIds.toReadableString()}");
|
|
|
|
final dbFiles = await _c.sqliteDb.use((db) async {
|
|
|
|
return await db.completeFilesByFileIds(fileIds, appAccount: account);
|
|
|
|
});
|
|
|
|
final files = await dbFiles.convertToAppFile(account);
|
|
|
|
final fileMap = <int, File>{};
|
|
|
|
for (final f in files) {
|
|
|
|
fileMap[f.fileId!] = f;
|
|
|
|
}
|
|
|
|
|
|
|
|
return () sync* {
|
|
|
|
for (final id in fileIds) {
|
|
|
|
final f = fileMap[id];
|
|
|
|
if (f == null) {
|
|
|
|
if (onFileNotFound == null) {
|
|
|
|
throw StateError("File ID not found: $id");
|
|
|
|
} else {
|
|
|
|
onFileNotFound(id);
|
|
|
|
}
|
2022-01-24 21:32:18 +01:00
|
|
|
} else {
|
2022-07-05 22:20:24 +02:00
|
|
|
yield fileMap[id]!;
|
2022-01-24 21:32:18 +01:00
|
|
|
}
|
|
|
|
}
|
2022-07-05 22:20:24 +02:00
|
|
|
}()
|
|
|
|
.toList();
|
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;
|
2022-06-06 12:02:54 +02:00
|
|
|
}
|