From 7a29e6161e0c1247550d6094b014f71882ffb5ec Mon Sep 17 00:00:00 2001 From: ashim-hq Date: Sun, 19 Apr 2026 15:47:24 +0800 Subject: [PATCH] refactor: extract shared HEIC preview utility from file-store --- apps/web/src/lib/image-preview.ts | 29 +++++++++++++++++++++++++++++ apps/web/src/stores/file-store.ts | 30 +----------------------------- 2 files changed, 30 insertions(+), 29 deletions(-) create mode 100644 apps/web/src/lib/image-preview.ts diff --git a/apps/web/src/lib/image-preview.ts b/apps/web/src/lib/image-preview.ts new file mode 100644 index 00000000..e24eaf4e --- /dev/null +++ b/apps/web/src/lib/image-preview.ts @@ -0,0 +1,29 @@ +import { formatHeaders } from "@/lib/api"; + +const HEIF_EXTENSIONS = new Set(["heic", "heif", "hif"]); + +export function needsServerPreview(file: File): boolean { + const ext = file.name.split(".").pop()?.toLowerCase() ?? ""; + return HEIF_EXTENSIONS.has(ext); +} + +export async function fetchDecodedPreview(file: File): Promise { + try { + const formData = new FormData(); + formData.append("file", file); + const res = await fetch("/api/v1/preview", { + method: "POST", + headers: formatHeaders(), + body: formData, + }); + if (!res.ok) return null; + const blob = await res.blob(); + return URL.createObjectURL(blob); + } catch { + return null; + } +} + +export function revokePreviewUrl(url: string): void { + URL.revokeObjectURL(url); +} diff --git a/apps/web/src/stores/file-store.ts b/apps/web/src/stores/file-store.ts index e49a0dd3..eeed0f25 100644 --- a/apps/web/src/stores/file-store.ts +++ b/apps/web/src/stores/file-store.ts @@ -1,5 +1,5 @@ import { create } from "zustand"; -import { formatHeaders } from "@/lib/api"; +import { fetchDecodedPreview, needsServerPreview } from "@/lib/image-preview"; export interface FileEntry { file: File; @@ -77,34 +77,6 @@ function deriveFiles(entries: FileEntry[]): File[] { return prevFiles; } -// --------------------------------------------------------------------------- -// HEIC/HEIF preview helpers -// --------------------------------------------------------------------------- - -const HEIF_EXTENSIONS = new Set(["heic", "heif", "hif"]); - -function needsServerPreview(file: File): boolean { - const ext = file.name.split(".").pop()?.toLowerCase() ?? ""; - return HEIF_EXTENSIONS.has(ext); -} - -async function fetchDecodedPreview(file: File): Promise { - try { - const formData = new FormData(); - formData.append("file", file); - const res = await fetch("/api/v1/preview", { - method: "POST", - headers: formatHeaders(), - body: formData, - }); - if (!res.ok) return null; - const blob = await res.blob(); - return URL.createObjectURL(blob); - } catch { - return null; - } -} - // --------------------------------------------------------------------------- // Store // ---------------------------------------------------------------------------