2020-04-05 12:22:38 +00:00
|
|
|
import 'package:dio/dio.dart';
|
2020-05-04 20:08:20 +00:00
|
|
|
import 'package:photos/core/configuration.dart';
|
2020-05-18 15:38:15 +00:00
|
|
|
import 'package:photos/db/photo_db.dart';
|
2020-05-02 16:28:54 +00:00
|
|
|
import 'package:logging/logging.dart';
|
2020-04-05 12:22:38 +00:00
|
|
|
|
2020-05-04 20:08:20 +00:00
|
|
|
import 'package:photos/models/face.dart';
|
2020-06-19 23:03:26 +00:00
|
|
|
import 'package:photos/models/file.dart';
|
2020-06-17 20:16:46 +00:00
|
|
|
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-06-19 23:03:26 +00:00
|
|
|
Future<List<File>> getFaceSearchResults(Face face) async {
|
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() +
|
|
|
|
"/photos/search/face/" +
|
|
|
|
face.faceID.toString(),
|
|
|
|
queryParameters: {
|
2020-06-06 21:12:18 +00:00
|
|
|
"limit": 100,
|
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>();
|
|
|
|
for (File file in result) {
|
2020-06-02 14:24:37 +00:00
|
|
|
try {
|
2020-06-19 23:03:26 +00:00
|
|
|
files.add(await FileDB.instance.getMatchingFile(file.localId,
|
|
|
|
file.title, file.deviceFolder, file.createTimestamp,
|
|
|
|
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) {
|
2020-06-06 12:00:18 +00:00
|
|
|
return second.createTimestamp.compareTo(first.createTimestamp);
|
|
|
|
});
|
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
|
|
|
}
|
|
|
|
}
|