mirror of
https://github.com/snapotter-hq/SnapOtter.git
synced 2026-08-03 07:46:42 +02:00
refactor: extract shared HEIC preview utility from file-store
This commit is contained in:
@@ -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<string | null> {
|
||||
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);
|
||||
}
|
||||
@@ -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<string | null> {
|
||||
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
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user