From e4a288d6cff3e8bcbf137dc08144d8c8871bc43b Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Tue, 23 Jul 2024 15:13:35 +0530 Subject: [PATCH] Move to separate file --- web/packages/new/photos/services/gzip.ts | 30 ++++++++++++++++ .../new/photos/services/ml/embedding.ts | 34 +------------------ 2 files changed, 31 insertions(+), 33 deletions(-) create mode 100644 web/packages/new/photos/services/gzip.ts diff --git a/web/packages/new/photos/services/gzip.ts b/web/packages/new/photos/services/gzip.ts new file mode 100644 index 0000000000..a5731bfcc2 --- /dev/null +++ b/web/packages/new/photos/services/gzip.ts @@ -0,0 +1,30 @@ +/** + * Compress the given {@link string} using "gzip" and return the resultant + * bytes. See {@link gunzip} for the reverse operation. + * + * This is syntactic sugar to deal with the string/blob/stream/bytes + * conversions, but it should not be taken as an abstraction layer. If your code + * can directly use a ReadableStream, then then data -> stream -> data round + * trip is unnecessary. + */ +export const gzip = async (string: string) => { + const compressedStream = new Blob([string]) + .stream() + // This code only runs on the desktop app currently, so we can rely on + // the existence of new web features the CompressionStream APIs. + .pipeThrough(new CompressionStream("gzip")); + return new Uint8Array(await new Response(compressedStream).arrayBuffer()); +}; + +/** + * Decompress the given "gzip" compressed {@link data} and return the resultant + * string. See {@link gzip} for the reverse operation. + */ +export const gunzip = async (data: Uint8Array) => { + const decompressedStream = new Blob([data]) + .stream() + // This code only runs on the desktop app currently, so we can rely on + // the existence of new web features the CompressionStream APIs. + .pipeThrough(new DecompressionStream("gzip")); + return new Response(decompressedStream).text(); +}; diff --git a/web/packages/new/photos/services/ml/embedding.ts b/web/packages/new/photos/services/ml/embedding.ts index beb80309eb..18d71ed87e 100644 --- a/web/packages/new/photos/services/ml/embedding.ts +++ b/web/packages/new/photos/services/ml/embedding.ts @@ -5,6 +5,7 @@ import { apiURL } from "@/base/origins"; import type { EnteFile } from "@/new/photos/types/file"; import { nullToUndefined } from "@/utils/transform"; import { z } from "zod"; +import { gunzip, gzip } from "../gzip"; import { type RemoteCLIPIndex } from "./clip"; import { type RemoteFaceIndex } from "./face"; @@ -335,36 +336,3 @@ const putEmbedding = async ( }); ensureOk(res); }; - -// MARK: - GZIP - -/** - * Compress the given {@link string} using "gzip" and return the resultant - * bytes. See {@link gunzip} for the reverse operation. - * - * This is syntactic sugar to deal with the string/blob/stream/bytes - * conversions, but it should not be taken as an abstraction layer. If your code - * can directly use a ReadableStream, then then data -> stream -> data round - * trip is unnecessary. - */ -const gzip = async (string: string) => { - const compressedStream = new Blob([string]) - .stream() - // This code only runs on the desktop app currently, so we can rely on - // the existence of new web features the CompressionStream APIs. - .pipeThrough(new CompressionStream("gzip")); - return new Uint8Array(await new Response(compressedStream).arrayBuffer()); -}; - -/** - * Decompress the given "gzip" compressed {@link data} and return the resultant - * string. See {@link gzip} for the reverse operation. - */ -const gunzip = async (data: Uint8Array) => { - const decompressedStream = new Blob([data]) - .stream() - // This code only runs on the desktop app currently, so we can rely on - // the existence of new web features the CompressionStream APIs. - .pipeThrough(new DecompressionStream("gzip")); - return new Response(decompressedStream).text(); -};