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> => { ): Promise<{ keyword: string; text: string } | null> => {
const chunks = decodePng(new Uint8Array(await blobToArrayBuffer(blob))); 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) { if (iTXtChunk) {
try { try {
const decoded = decodeITXt(iTXtChunk.data); 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) { if (tEXtChunk) {
return tEXt.decode(tEXtChunk.data); return tEXt.decode(tEXtChunk.data);
} }
@ -48,9 +48,9 @@ export const encodePngMetadata = async ({
const filteredChunks = chunks.filter( const filteredChunks = chunks.filter(
(chunk) => (chunk) =>
!(chunk.name === "tEXt" && !(chunk.name === PNGChunkType.tEXt &&
tEXt.decode(chunk.data).keyword === MIME_TYPES.excalidraw) && tEXt.decode(chunk.data).keyword === MIME_TYPES.excalidraw) &&
!(chunk.name === "iTXt" && !(chunk.name === PNGChunkType.iTXt &&
decodeITXt(chunk.data).keyword === MIME_TYPES.excalidraw) decodeITXt(chunk.data).keyword === MIME_TYPES.excalidraw)
); );
@ -68,7 +68,8 @@ export const encodePngMetadata = async ({
MIME_TYPES.excalidraw, MIME_TYPES.excalidraw,
encodedData, encodedData,
{ {
compressed: false, //Already compressed in encode compressed: true,
compressedMethod: 0,
language: "en", language: "en",
translated: "" translated: ""
} }
@ -101,13 +102,13 @@ export const decodePngMetadata = async (blob: Blob) => {
) { ) {
return metadata.text; return metadata.text;
} }
throw new Error("FAILED"); throw new Error("Malformed or unexpected metadata format");
} }
return decode(encodedData); return decode(encodedData);
} catch (error: any) { } catch (error: any) {
console.error(error); 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 // PNG encoding/decoding
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
enum PNGChunkType {
tEXt = "tEXt",
iTXt = "iTXt",
}
type TEXtChunk = { name: "tEXt"; data: Uint8Array }; type TEXtChunk = { name: "tEXt"; data: Uint8Array };
type ITXtChunk = { name: "iTXt"; data: Uint8Array }; type ITXtChunk = { name: "iTXt"; data: Uint8Array };
declare module "png-chunk-text" { declare module "png-chunk-text" {
function encode( function encode(
name: string, name: string,
value: string, value: string,
): { name: "tEXt"; data: Uint8Array }; ): { name: PNGChunkType.tEXt; data: Uint8Array };
function decode(data: Uint8Array): { keyword: string; text: string }; function decode(data: Uint8Array): { keyword: string; text: string };
} }
declare module "png-chunk-itxt" { declare module "png-chunk-itxt" {
function encode( function encode(
keyword: string, keyword: string,
text: string, text: string,
options?: { compressed?: boolean; language?: string; translated?: string }, options?: { compressed?: boolean; compressedMethod: number; language?: string; translated?: string },
): { name: "iTXt"; data: Uint8Array }; ): { name: PNGChunkType.iTXt; data: Uint8Array };
function decode(data: Uint8Array): { function decode(data: Uint8Array): {
keyword: string; keyword: string;
text: string; text: string;
@ -57,12 +60,10 @@ declare module "png-chunk-itxt" {
}; };
export { encode, decode }; export { encode, decode };
} }
declare module "png-chunks-encode" { declare module "png-chunks-encode" {
function encode(chunks: (TEXtChunk | ITXtChunk)[]): Uint8Array; function encode(chunks: (TEXtChunk | ITXtChunk)[]): Uint8Array;
export = encode; export = encode;
} }
declare module "png-chunks-extract" { declare module "png-chunks-extract" {
function extract(buffer: Uint8Array): (TEXtChunk | ITXtChunk)[]; function extract(buffer: Uint8Array): (TEXtChunk | ITXtChunk)[];
export = extract; export = extract;