some minor improvements

This commit is contained in:
Gabriel Gomes 2025-03-29 21:18:20 +00:00
parent 22869ee58c
commit 0a6c0b5950
2 changed files with 17 additions and 15 deletions

View file

@ -17,7 +17,7 @@ export const getMetadataChunk = async (
): Promise<{ keyword: string; text: string } | null> => {
const chunks = decodePng(new Uint8Array(await blobToArrayBuffer(blob)));
const iTXtChunk = chunks.find((chunk) => chunk.name === "iTXt");
const iTXtChunk = chunks.find((chunk) => chunk.name === PNGChunkType.iTXt);
if (iTXtChunk) {
try {
const decoded = decodeITXt(iTXtChunk.data);
@ -27,7 +27,7 @@ export const getMetadataChunk = async (
}
}
const tEXtChunk = chunks.find((chunk) => chunk.name === "tEXt");
const tEXtChunk = chunks.find((chunk) => chunk.name === PNGChunkType.tEXt);
if (tEXtChunk) {
return tEXt.decode(tEXtChunk.data);
}
@ -48,9 +48,9 @@ export const encodePngMetadata = async ({
const filteredChunks = chunks.filter(
(chunk) =>
!(chunk.name === "tEXt" &&
!(chunk.name === PNGChunkType.tEXt &&
tEXt.decode(chunk.data).keyword === MIME_TYPES.excalidraw) &&
!(chunk.name === "iTXt" &&
!(chunk.name === PNGChunkType.iTXt &&
decodeITXt(chunk.data).keyword === MIME_TYPES.excalidraw)
);
@ -68,7 +68,8 @@ export const encodePngMetadata = async ({
MIME_TYPES.excalidraw,
encodedData,
{
compressed: false, //Already compressed in encode
compressed: true,
compressedMethod: 0,
language: "en",
translated: ""
}
@ -101,13 +102,13 @@ export const decodePngMetadata = async (blob: Blob) => {
) {
return metadata.text;
}
throw new Error("FAILED");
throw new Error("Malformed or unexpected metadata format");
}
return decode(encodedData);
} catch (error: any) {
console.error(error);
throw new Error("FAILED");
throw new Error("Malformed or unexpected metadata format");
}
}
throw new Error("INVALID");
throw new Error("Invalid or unsupported PNG metadata format");
};

View file

@ -32,23 +32,26 @@ interface Clipboard extends EventTarget {
// PNG encoding/decoding
// -----------------------------------------------------------------------------
enum PNGChunkType {
tEXt = "tEXt",
iTXt = "iTXt",
}
type TEXtChunk = { name: "tEXt"; data: Uint8Array };
type ITXtChunk = { name: "iTXt"; data: Uint8Array };
declare module "png-chunk-text" {
function encode(
name: string,
value: string,
): { name: "tEXt"; data: Uint8Array };
): { name: PNGChunkType.tEXt; data: Uint8Array };
function decode(data: Uint8Array): { keyword: string; text: string };
}
declare module "png-chunk-itxt" {
function encode(
keyword: string,
text: string,
options?: { compressed?: boolean; language?: string; translated?: string },
): { name: "iTXt"; data: Uint8Array };
options?: { compressed?: boolean; compressedMethod: number; language?: string; translated?: string },
): { name: PNGChunkType.iTXt; data: Uint8Array };
function decode(data: Uint8Array): {
keyword: string;
text: string;
@ -57,12 +60,10 @@ declare module "png-chunk-itxt" {
};
export { encode, decode };
}
declare module "png-chunks-encode" {
function encode(chunks: (TEXtChunk | ITXtChunk)[]): Uint8Array;
export = encode;
}
declare module "png-chunks-extract" {
function extract(buffer: Uint8Array): (TEXtChunk | ITXtChunk)[];
export = extract;