Open History to see the last commit
1import { hostPreviewMimeTypeFromExtension } from "@t3tools/shared/filePreview";
2import * as Base64 from "effect/encoding/Base64";
3
4/**
5 * A URL an `<img>` can show for an image taken out of an archive. SVG uses a data: URL, which
6 * has an opaque origin wherever it is opened; other types use a blob: URL.
7 */
8export function archiveImageUrl(
9 path: string,
10 bytes: Uint8Array<ArrayBuffer>,
11): { readonly url: string; readonly revoke: () => void } {
12 const mimeType =
13 hostPreviewMimeTypeFromExtension(path.slice(path.lastIndexOf("."))) ??
14 "application/octet-stream";
15 if (mimeType === "image/svg+xml") {
16 return { url: `data:${mimeType};base64,${Base64.encode(bytes)}`, revoke: () => undefined };
17 }
18 const url = URL.createObjectURL(new Blob([bytes], { type: mimeType }));
19 return { url, revoke: () => URL.revokeObjectURL(url) };
20}