Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

refactor

Neeraj Guptac8a8cbee60ac
diff --git a/mobile/lib/services/filedata/filedata_service.dart b/mobile/lib/services/filedata/filedata_service.dart
index cae1ad66a8..ec4c27fb17 100644
--- a/mobile/lib/services/filedata/filedata_service.dart
+++ b/mobile/lib/services/filedata/filedata_service.dart
@@ -15,28 +15,18 @@ import "package:shared_preferences/shared_preferences.dart";
class FileDataService {
FileDataService._privateConstructor();
- static const String _derivedDataType = "derivedMeta";
static final Computer _computer = Computer.shared();
-
static final FileDataService instance = FileDataService._privateConstructor();
-
final _logger = Logger("FileDataService");
final _dio = NetworkClient.instance.enteDio;
void init(SharedPreferences prefs) {}
- Future<void> putDerivedMetaData(
- EnteFile file,
- FileDataEntity fileML, {
- RemoteClipEmbedding? clipEmbedding,
- RemoteFaceEmbedding? faceEmbedding,
- }) async {
- fileML.putClipIfNotNull(clipEmbedding);
- fileML.putFaceIfNotNull(faceEmbedding);
- fileML.putSanityCheck();
+ Future<void> putFileData(EnteFile file, FileDataEntity data) async {
+ data.validate();
final ChaChaEncryptionResult encryptionResult = await gzipAndEncryptJson(
- fileML.remoteRawData,
+ data.remoteRawData,
getFileKey(file),
);
@@ -45,7 +35,7 @@ class FileDataService {
"/files/data/",
data: {
"fileID": file.uploadedFileID!,
- "type": _derivedDataType,
+ "type": data.type.toJson(),
"encryptedData": encryptionResult.encData,
"decryptionHeader": encryptionResult.header,
},
@@ -57,14 +47,15 @@ class FileDataService {
}
Future<FileDataResponse> getFilesData(
- Set<int> fileIds,
- ) async {
+ Set<int> fileIds, {
+ DataType type = DataType.derivedMeta,
+ }) async {
try {
final res = await _dio.post(
"/files/fetch-data/",
data: {
"fileIDs": fileIds.toList(),
- "type": _derivedDataType,
+ "type": type.toJson(),
},
);
final remoteEntries = res.data['data'] as List;
@@ -75,7 +66,6 @@ class FileDataService {
for (var entry in remoteEntries) {
encFileData.add(EncryptedFileData.fromMap(entry));
}
-
final fileIdToDataMap = await decryptRemoteFileData(encFileData);
return FileDataResponse(
fileIdToDataMap,
@@ -99,17 +89,17 @@ class FileDataService {
final inputs = <_DecoderInput>[];
final fileMap = await FilesDB.instance
.getFilesFromIDs(remoteData.map((e) => e.fileID).toList());
- for (final embedding in remoteData) {
- final file = fileMap[embedding.fileID];
+ for (final data in remoteData) {
+ final file = fileMap[data.fileID];
if (file == null) {
continue;
}
final fileKey = getFileKey(file);
- final input = _DecoderInput(embedding, fileKey);
+ final input = _DecoderInput(data, fileKey);
inputs.add(input);
}
return _computer.compute<Map<String, dynamic>, Map<int, FileDataEntity>>(
- _decryptFileMLComputer,
+ _decryptFileDataComputer,
param: {
"inputs": inputs,
},
@@ -117,7 +107,7 @@ class FileDataService {
}
}
-Future<Map<int, FileDataEntity>> _decryptFileMLComputer(
+Future<Map<int, FileDataEntity>> _decryptFileDataComputer(
Map<String, dynamic> args,
) async {
final result = <int, FileDataEntity>{};
@@ -130,6 +120,7 @@ Future<Map<int, FileDataEntity>> _decryptFileMLComputer(
);
result[input.data.fileID] = FileDataEntity.fromRemote(
input.data.fileID,
+ input.data.type,
decodedJson,
);
}
diff --git a/mobile/lib/services/filedata/model/file_data.dart b/mobile/lib/services/filedata/model/file_data.dart
index bb4fd774f4..f6f501e47b 100644
--- a/mobile/lib/services/filedata/model/file_data.dart
+++ b/mobile/lib/services/filedata/model/file_data.dart
@@ -3,49 +3,74 @@ import "package:photos/face/model/face.dart";
const _faceKey = 'face';
const _clipKey = 'clip';
+enum DataType {
+ derivedMeta('derivedMeta');
+
+ final String value;
+ const DataType(this.value);
+
+ static DataType fromString(String type) {
+ return DataType.values.firstWhere(
+ (e) => e.value == type,
+ orElse: () {
+ throw Exception('Unknown type: $type');
+ },
+ );
+ }
+
+ String toJson() => value;
+ static DataType fromJson(String json) => DataType.fromString(json);
+}
+
class FileDataEntity {
final int fileID;
final Map<String, dynamic> remoteRawData;
+ final DataType type;
FileDataEntity(
this.fileID,
this.remoteRawData,
+ this.type,
);
- void putSanityCheck() {
- if (remoteRawData[_faceKey] == null) {
- throw Exception('Face embedding is null');
- }
- if (remoteRawData[_clipKey] == null) {
- throw Exception('Clip embedding is null');
+ void validate() {
+ if (type == DataType.derivedMeta) {
+ if (remoteRawData[_faceKey] == null) {
+ throw Exception('Face embedding is null');
+ }
+ if (remoteRawData[_clipKey] == null) {
+ throw Exception('Clip embedding is null');
+ }
+ } else {
+ throw Exception('Invalid type ${type.value}');
}
}
factory FileDataEntity.fromRemote(
int fileID,
+ String type,
Map<String, dynamic> json,
) {
return FileDataEntity(
fileID,
json,
+ DataType.fromString(type),
);
}
- static FileDataEntity empty(int i) {
+ static FileDataEntity empty(int fileID, DataType type) {
final Map<String, dynamic> json = {};
- return FileDataEntity(i, json);
+ return FileDataEntity(fileID, json, type);
}
- void putFaceIfNotNull(RemoteFaceEmbedding? faceEmbedding) {
- if (faceEmbedding != null) {
- remoteRawData[_faceKey] = faceEmbedding.toJson();
- }
+ void putFace(RemoteFaceEmbedding faceEmbedding) {
+ assert(type == DataType.derivedMeta, 'Invalid type ${type.value}');
+ remoteRawData[_faceKey] = faceEmbedding.toJson();
}
- void putClipIfNotNull(RemoteClipEmbedding? clipEmbedding) {
- if (clipEmbedding != null) {
- remoteRawData[_clipKey] = clipEmbedding.toJson();
- }
+ void putClip(RemoteClipEmbedding clipEmbedding) {
+ assert(type == DataType.derivedMeta, 'Invalid type ${type.value}');
+ remoteRawData[_clipKey] = clipEmbedding.toJson();
}
RemoteFaceEmbedding? get faceEmbedding => remoteRawData[_faceKey] != null
diff --git a/mobile/lib/services/machine_learning/ml_service.dart b/mobile/lib/services/machine_learning/ml_service.dart
index 1d95a3eb44..7db1ea13a4 100644
--- a/mobile/lib/services/machine_learning/ml_service.dart
+++ b/mobile/lib/services/machine_learning/ml_service.dart
@@ -450,28 +450,34 @@ class MLService {
}
_logger.info("inserting ${faces.length} faces for ${result.fileId}");
if (!result.errorOccured) {
- await FileDataService.instance.putDerivedMetaData(
+ final FileDataEntity dataEntity = instruction.existingRemoteFileML ??
+ FileDataEntity.empty(
+ instruction.file.uploadedFileID!,
+ DataType.derivedMeta,
+ );
+ if (result.facesRan) {
+ dataEntity.putFace(
+ RemoteFaceEmbedding(
+ faces,
+ result.mlVersion,
+ client: client,
+ height: result.decodedImageSize.height,
+ width: result.decodedImageSize.width,
+ ),
+ );
+ }
+ if (result.clipRan) {
+ dataEntity.putClip(
+ RemoteClipEmbedding(
+ result.clip!.embedding,
+ version: result.mlVersion,
+ client: client,
+ ),
+ );
+ }
+ await FileDataService.instance.putFileData(
instruction.file,
- instruction.existingRemoteFileML ??
- FileDataEntity.empty(
- instruction.file.uploadedFileID!,
- ),
- faceEmbedding: result.facesRan
- ? RemoteFaceEmbedding(
- faces,
- result.mlVersion,
- client: client,
- height: result.decodedImageSize.height,
- width: result.decodedImageSize.width,
- )
- : null,
- clipEmbedding: result.clipRan
- ? RemoteClipEmbedding(
- result.clip!.embedding,
- version: result.mlVersion,
- client: client,
- )
- : null,
+ dataEntity,
);
} else {
_logger.warning(