fix(app): avoid hashing image preview payloads

This commit is contained in:
Mohamed Boudra
2026-04-22 23:15:28 +07:00
parent 339a0b6b43
commit ce6393a933
3 changed files with 20 additions and 7 deletions
+10 -4
View File
@@ -55,9 +55,10 @@ export function parseImageDataUrl(
if (!parsed.mimeType.toLowerCase().startsWith("image/")) { if (!parsed.mimeType.toLowerCase().startsWith("image/")) {
return null; return null;
} }
const fingerprint = `${parsed.mimeType}\0${parsed.base64.length}\0${parsed.base64.slice(0, 64)}\0${parsed.base64.slice(-64)}`;
return { return {
...parsed, ...parsed,
cacheKey: `data-image:${parsed.mimeType}:${parsed.base64.length}:${hashString(parsed.base64)}`, cacheKey: `data-image:${parsed.mimeType}:${parsed.base64.length}:${hashString(fingerprint)}`,
}; };
} catch { } catch {
return null; return null;
@@ -80,13 +81,18 @@ export function getFileNameFromPath(path: string | null | undefined): string | n
} }
export function createPreviewAttachmentId(input: { export function createPreviewAttachmentId(input: {
base64: string;
mimeType: string; mimeType: string;
path?: string | null; path?: string | null;
size?: number | null;
modifiedAt?: string | null;
contentLength?: number | null;
}): string { }): string {
const path = input.path?.trim() ?? ""; const path = input.path?.trim() ?? "";
const hash = hashString(`${input.mimeType}\0${path}\0${input.base64}`); const size = Number.isFinite(input.size) ? String(input.size) : "";
return `preview_${input.base64.length}_${hash}`; const modifiedAt = input.modifiedAt?.trim() ?? "";
const contentLength = Number.isFinite(input.contentLength) ? String(input.contentLength) : "";
const hash = hashString(`${input.mimeType}\0${path}\0${size}\0${modifiedAt}\0${contentLength}`);
return `preview_${size || contentLength || "unknown"}_${hash}`;
} }
export async function blobToBase64(blob: Blob): Promise<string> { export async function blobToBase64(blob: Blob): Promise<string> {
+3 -1
View File
@@ -76,9 +76,11 @@ async function createFilePanePreview(file: ExplorerFile | null): Promise<{
const { content: _content, ...imageFile } = file; const { content: _content, ...imageFile } = file;
const imageAttachment = await persistAttachmentFromBase64({ const imageAttachment = await persistAttachmentFromBase64({
id: createPreviewAttachmentId({ id: createPreviewAttachmentId({
base64: file.content,
mimeType: file.mimeType ?? "image/png", mimeType: file.mimeType ?? "image/png",
path: file.path, path: file.path,
size: file.size,
modifiedAt: file.modifiedAt,
contentLength: file.content.length,
}), }),
base64: file.content, base64: file.content,
mimeType: file.mimeType, mimeType: file.mimeType,
+7 -2
View File
@@ -635,9 +635,11 @@ function AssistantMarkdownImage({
return await persistAttachmentFromBase64({ return await persistAttachmentFromBase64({
id: createPreviewAttachmentId({ id: createPreviewAttachmentId({
base64: file.content,
mimeType: file.mimeType ?? "image/png", mimeType: file.mimeType ?? "image/png",
path: file.path || resolution.path, path: file.path || resolution.path,
size: file.size,
modifiedAt: file.modifiedAt,
contentLength: file.content.length,
}), }),
base64: file.content, base64: file.content,
mimeType: file.mimeType, mimeType: file.mimeType,
@@ -655,7 +657,10 @@ function AssistantMarkdownImage({
} }
return await persistAttachmentFromDataUrl({ return await persistAttachmentFromDataUrl({
id: createPreviewAttachmentId(dataImage), id: createPreviewAttachmentId({
mimeType: dataImage.mimeType,
contentLength: dataImage.base64.length,
}),
dataUrl: source, dataUrl: source,
mimeType: dataImage.mimeType, mimeType: dataImage.mimeType,
}); });