ente/lib/face_search_manager.dart

79 lines
2.3 KiB
Dart
Raw Normal View History

2020-04-05 12:22:38 +00:00
import 'package:dio/dio.dart';
import 'package:photos/core/configuration.dart';
2020-07-20 11:03:09 +00:00
import 'package:photos/db/files_db.dart';
2020-05-02 16:28:54 +00:00
import 'package:logging/logging.dart';
2020-04-05 12:22:38 +00:00
import 'package:photos/models/face.dart';
2020-06-19 23:03:26 +00:00
import 'package:photos/models/file.dart';
import 'package:photos/utils/file_name_util.dart';
2020-04-05 12:22:38 +00:00
class FaceSearchManager {
2020-06-02 14:24:37 +00:00
final _logger = Logger("FaceSearchManager");
2020-04-05 12:22:38 +00:00
final _dio = Dio();
2020-04-05 14:00:44 +00:00
FaceSearchManager._privateConstructor();
static final FaceSearchManager instance =
FaceSearchManager._privateConstructor();
2020-04-05 12:22:38 +00:00
Future<List<Face>> getFaces() {
return _dio
2020-05-17 10:18:09 +00:00
.get(
Configuration.instance.getHttpEndpoint() + "/photos/faces",
options: Options(
headers: {"X-Auth-Token": Configuration.instance.getToken()}),
)
2020-04-05 12:22:38 +00:00
.then((response) => (response.data["faces"] as List)
.map((face) => new Face.fromJson(face))
.toList())
.catchError(_onError);
}
2020-07-14 12:15:55 +00:00
Future<List<File>> getFaceSearchResults(
Face face, int beforeCreationTime, int limit) async {
_logger.info("Fetching since creation " + beforeCreationTime.toString());
2020-06-02 14:24:37 +00:00
final result = await _dio
2020-05-17 10:18:09 +00:00
.get(
2020-06-02 14:24:37 +00:00
Configuration.instance.getHttpEndpoint() +
2020-07-30 08:06:54 +00:00
"/search/face/" +
face.id.toString(),
2020-06-02 14:24:37 +00:00
queryParameters: {
2020-07-14 12:15:55 +00:00
"limit": limit,
"beforeCreationTime": beforeCreationTime,
2020-06-02 14:24:37 +00:00
},
options:
Options(headers: {"X-Auth-Token": Configuration.instance.getToken()}),
)
.then((response) {
return (response.data["result"] as List)
2020-06-19 23:03:26 +00:00
.map((p) => File.fromJson(p))
2020-06-02 14:24:37 +00:00
.toList();
}).catchError(_onError);
2020-06-19 23:03:26 +00:00
final files = List<File>();
if (result == null) {
return throw ("Oops. Could not fetch search results.");
}
2020-06-19 23:03:26 +00:00
for (File file in result) {
2020-06-02 14:24:37 +00:00
try {
2020-07-20 11:03:09 +00:00
files.add(await FilesDB.instance.getMatchingFile(
2020-08-09 22:34:59 +00:00
file.localID,
file.title,
file.deviceFolder,
file.creationTime,
file.modificationTime,
2020-06-19 23:03:26 +00:00
alternateTitle: getHEICFileNameForJPG(file)));
2020-06-02 14:24:37 +00:00
} catch (e) {
// Not available locally
2020-06-19 23:03:26 +00:00
files.add(file);
2020-06-02 14:24:37 +00:00
}
}
2020-06-19 23:03:26 +00:00
files.sort((first, second) {
return second.creationTime.compareTo(first.creationTime);
});
2020-06-19 23:03:26 +00:00
return files;
2020-04-05 14:00:44 +00:00
}
2020-04-05 12:22:38 +00:00
void _onError(error) {
2020-06-02 14:24:37 +00:00
_logger.severe(error);
2020-04-05 12:22:38 +00:00
}
}