From 9f96ef8d83df66dad64c53d8a04e65a5708291b0 Mon Sep 17 00:00:00 2001 From: Neeraj Gupta <254676+ua741@users.noreply.github.com> Date: Wed, 14 Aug 2024 12:27:36 +0530 Subject: [PATCH] [mob] Switch to person_v2 where data is gzipped --- mobile/lib/models/api/entity/type.dart | 12 ++++++ mobile/lib/services/entity_service.dart | 42 +++++++++++++------ .../face_ml/person/person_service.dart | 20 ++++----- 3 files changed, 52 insertions(+), 22 deletions(-) diff --git a/mobile/lib/models/api/entity/type.dart b/mobile/lib/models/api/entity/type.dart index 88e60d62f3..431ea4e57b 100644 --- a/mobile/lib/models/api/entity/type.dart +++ b/mobile/lib/models/api/entity/type.dart @@ -3,6 +3,7 @@ import "package:flutter/foundation.dart"; enum EntityType { location, person, + personV2, unknown, } @@ -12,18 +13,29 @@ EntityType typeFromString(String type) { return EntityType.location; case "person": return EntityType.location; + case "person_v2": + return EntityType.personV2; } debugPrint("unexpected collection type $type"); return EntityType.unknown; } extension EntityTypeExtn on EntityType { + bool isZipped() { + if (this == EntityType.location || this == EntityType.person) { + return false; + } + return true; + } + String typeToString() { switch (this) { case EntityType.location: return "location"; case EntityType.person: return "person"; + case EntityType.personV2: + return "person_v2"; case EntityType.unknown: return "unknown"; } diff --git a/mobile/lib/services/entity_service.dart b/mobile/lib/services/entity_service.dart index 3f00d264fd..e979d4fd9e 100644 --- a/mobile/lib/services/entity_service.dart +++ b/mobile/lib/services/entity_service.dart @@ -14,6 +14,7 @@ import "package:photos/models/api/entity/key.dart"; import "package:photos/models/api/entity/type.dart"; import "package:photos/models/local_entity_data.dart"; import "package:photos/utils/crypto_util.dart"; +import "package:photos/utils/gzip.dart"; import 'package:shared_preferences/shared_preferences.dart'; class EntityService { @@ -61,11 +62,18 @@ class EntityService { }) async { final String plainText = jsonEncode(jsonMap); final key = await getOrCreateEntityKey(type); - final encryptedKeyData = - await CryptoUtil.encryptChaCha(utf8.encode(plainText), key); - final String encryptedData = - CryptoUtil.bin2base64(encryptedKeyData.encryptedData!); - final String header = CryptoUtil.bin2base64(encryptedKeyData.header!); + late String encryptedData, header; + if (type.isZipped()) { + final ChaChaEncryptionResult result = + await gzipAndEncryptJson(jsonMap, key); + encryptedData = result.encData; + header = result.header; + } else { + final encryptedKeyData = + await CryptoUtil.encryptChaCha(utf8.encode(plainText), key); + encryptedData = CryptoUtil.bin2base64(encryptedKeyData.encryptedData!); + header = CryptoUtil.bin2base64(encryptedKeyData.header!); + } debugPrint( " ${id == null ? 'Adding' : 'Updating'} entity of type: " + type.typeToString(), @@ -93,7 +101,7 @@ class EntityService { Future syncEntities() async { try { await _remoteToLocalSync(EntityType.location); - await _remoteToLocalSync(EntityType.person); + await _remoteToLocalSync(EntityType.personV2); } catch (e) { _logger.severe("Failed to sync entities", e); } @@ -126,12 +134,22 @@ class EntityService { final List entities = []; for (EntityData e in result) { try { - final decryptedValue = await CryptoUtil.decryptChaCha( - CryptoUtil.base642bin(e.encryptedData!), - entityKey, - CryptoUtil.base642bin(e.header!), - ); - final String plainText = utf8.decode(decryptedValue); + late String plainText; + if (type.isZipped()) { + final jsonMap = await decryptAndUnzipJson( + entityKey, + encryptedData: e.encryptedData!, + header: e.header!, + ); + plainText = jsonEncode(jsonMap); + } else { + final Uint8List decryptedValue = await CryptoUtil.decryptChaCha( + CryptoUtil.base642bin(e.encryptedData!), + entityKey, + CryptoUtil.base642bin(e.header!), + ); + plainText = utf8.decode(decryptedValue); + } entities.add( LocalEntityData( id: e.id, diff --git a/mobile/lib/services/machine_learning/face_ml/person/person_service.dart b/mobile/lib/services/machine_learning/face_ml/person/person_service.dart index 9a94b28160..5a5f4b23a6 100644 --- a/mobile/lib/services/machine_learning/face_ml/person/person_service.dart +++ b/mobile/lib/services/machine_learning/face_ml/person/person_service.dart @@ -37,7 +37,7 @@ class PersonService { } Future> getPersons() async { - final entities = await entityService.getEntities(EntityType.person); + final entities = await entityService.getEntities(EntityType.personV2); return entities .map( (e) => PersonEntity(e.id, PersonData.fromJson(json.decode(e.data))), @@ -46,7 +46,7 @@ class PersonService { } Future getPerson(String id) { - return entityService.getEntity(EntityType.person, id).then((e) { + return entityService.getEntity(EntityType.personV2, id).then((e) { if (e == null) { return null; } @@ -55,7 +55,7 @@ class PersonService { } Future> getPersonsMap() async { - final entities = await entityService.getEntities(EntityType.person); + final entities = await entityService.getEntities(EntityType.personV2); final Map map = {}; for (var e in entities) { final person = @@ -95,7 +95,7 @@ class PersonService { ) .toList(); entityService - .addOrUpdate(EntityType.person, personData.toJson(), id: personID) + .addOrUpdate(EntityType.personV2, personData.toJson(), id: personID) .ignore(); personData.logStats(); } @@ -163,7 +163,7 @@ class PersonService { isHidden: isHidden, ); final result = await entityService.addOrUpdate( - EntityType.person, + EntityType.personV2, data.toJson(), ); await faceMLDataDB.assignClusterToPerson( @@ -181,7 +181,7 @@ class PersonService { final personData = person.data; personData.assigned!.removeWhere((element) => element.id != clusterID); await entityService.addOrUpdate( - EntityType.person, + EntityType.personV2, personData.toJson(), id: personID, ); @@ -216,7 +216,7 @@ class PersonService { } await entityService.addOrUpdate( - EntityType.person, + EntityType.personV2, personData.toJson(), id: person.remoteID, ); @@ -232,7 +232,7 @@ class PersonService { final PersonEntity justName = PersonEntity(personID, PersonData(name: entity.data.name)); await entityService.addOrUpdate( - EntityType.person, + EntityType.personV2, justName.data.toJson(), id: personID, ); @@ -249,7 +249,7 @@ class PersonService { Future fetchRemoteClusterFeedback() async { await entityService.syncEntities(); - final entities = await entityService.getEntities(EntityType.person); + final entities = await entityService.getEntities(EntityType.personV2); entities.sort((a, b) => a.updatedAt.compareTo(b.updatedAt)); final Map faceIdToClusterID = {}; final Map clusterToPersonID = {}; @@ -307,7 +307,7 @@ class PersonService { Future _updatePerson(PersonEntity updatePerson) async { await entityService.addOrUpdate( - EntityType.person, + EntityType.personV2, updatePerson.data.toJson(), id: updatePerson.remoteID, );