Skip to content

armgit / ente

Public Imported from GitHub
0

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

Bound remote ML metadata decompression

Neeraj Gupta965dc41f4aac
diff --git a/mobile/apps/photos/lib/services/filedata/filedata_service.dart b/mobile/apps/photos/lib/services/filedata/filedata_service.dart
index d89838fedb..a67eed89a2 100644
--- a/mobile/apps/photos/lib/services/filedata/filedata_service.dart
+++ b/mobile/apps/photos/lib/services/filedata/filedata_service.dart
@@ -15,6 +15,8 @@ import "package:photos/utils/file_key.dart";
import "package:photos/utils/gzip.dart";
import "package:shared_preferences/shared_preferences.dart";
+const _maxMLDataBytes = 4 * 1024 * 1024;
+
class FileDataService {
static final Computer _computer = Computer.shared();
final _logger = Logger("FileDataService");
@@ -166,17 +168,27 @@ Future<Map<int, FileDataEntity>> _decryptFileDataComputer(
) async {
final result = <int, FileDataEntity>{};
final inputs = args["inputs"] as List<_DecoderInput>;
+ final logger = Logger("FileDataService");
for (final input in inputs) {
- final decodedJson = decryptAndUnzipJsonSync(
- input.decryptionKey,
- encryptedData: input.data.encryptedData,
- header: input.data.decryptionHeader,
- );
- result[input.data.fileID] = FileDataEntity.fromRemote(
- input.data.fileID,
- input.data.type,
- decodedJson,
- );
+ try {
+ final decodedJson = decryptAndUnzipJsonSync(
+ input.decryptionKey,
+ encryptedData: input.data.encryptedData,
+ header: input.data.decryptionHeader,
+ maxOutputBytes: _maxMLDataBytes,
+ );
+ result[input.data.fileID] = FileDataEntity.fromRemote(
+ input.data.fileID,
+ input.data.type,
+ decodedJson,
+ );
+ } on FormatException catch (e, s) {
+ logger.warning(
+ "Ignoring unparseable ML data for file id ${input.data.fileID}",
+ e,
+ s,
+ );
+ }
}
return result;
}
diff --git a/web/packages/new/photos/services/ml/ml-data.ts b/web/packages/new/photos/services/ml/ml-data.ts
index fd9b23ad9f..f8504ecefd 100644
--- a/web/packages/new/photos/services/ml/ml-data.ts
+++ b/web/packages/new/photos/services/ml/ml-data.ts
@@ -4,10 +4,12 @@ import { fetchFilesData, putFileData } from "ente-gallery/services/file-data";
import type { EnteFile } from "ente-media/file";
import { nullToUndefined } from "ente-utils/transform";
import { z } from "zod";
-import { gunzip, gzip } from "../../utils/gzip";
+import { gunzipWithLimit, gzip } from "../../utils/gzip";
import type { RemoteCLIPIndex } from "./clip";
import type { RemoteFaceIndex } from "./face";
+const maxMLDataBytes = 4 * 1024 * 1024;
+
export interface RemoteMLData {
// Keep raw data when updating so unknown top-level keys survive.
raw: RawRemoteMLData;
@@ -82,7 +84,10 @@ export const fetchMLData = async (
remoteFileData,
file.key,
);
- const jsonString = await gunzip(decryptedBytes);
+ const jsonString = await gunzipWithLimit(
+ decryptedBytes,
+ maxMLDataBytes,
+ );
result.set(
fileID,
remoteMLDataFromJSONString(jsonString, updatedAt),
diff --git a/web/packages/new/photos/utils/gzip.ts b/web/packages/new/photos/utils/gzip.ts
index 9adafff20b..dd004fe915 100644
--- a/web/packages/new/photos/utils/gzip.ts
+++ b/web/packages/new/photos/utils/gzip.ts
@@ -18,8 +18,19 @@ export const gunzipWithLimit = async (
data: Uint8Array<ArrayBuffer>,
maxOutputBytes: number,
) => {
- const reader = new Blob([data])
- .stream()
+ // Chromium buffers each input chunk's entire output before enqueueing it.
+ let inputOffset = 0;
+ const compressedStream = new ReadableStream<Uint8Array<ArrayBuffer>>({
+ pull(controller) {
+ if (inputOffset >= data.byteLength) {
+ controller.close();
+ return;
+ }
+ controller.enqueue(data.subarray(inputOffset, inputOffset + 1024));
+ inputOffset += 1024;
+ },
+ });
+ const reader = compressedStream
.pipeThrough(new DecompressionStream("gzip"))
.getReader();
const chunks: Uint8Array<ArrayBuffer>[] = [];