2025-10-09 17:05:19 +09:00
|
|
|
"use client";
|
|
|
|
|
import { useState } from "react";
|
|
|
|
|
import { useToast } from "@/app/components/ui/ToastProvider";
|
|
|
|
|
|
2025-10-09 17:58:12 +09:00
|
|
|
type Props = {
|
|
|
|
|
onUploaded: (url: string) => void;
|
|
|
|
|
multiple?: boolean;
|
|
|
|
|
maxWidth?: number;
|
|
|
|
|
maxHeight?: number;
|
|
|
|
|
quality?: number; // 0..1
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
async function readFileAsDataUrl(file: File): Promise<string> {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
const reader = new FileReader();
|
|
|
|
|
reader.onload = () => resolve(String(reader.result));
|
|
|
|
|
reader.onerror = reject;
|
|
|
|
|
reader.readAsDataURL(file);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function resizeImageToBlob(dataUrl: string, opts: { maxWidth: number; maxHeight: number; quality: number }): Promise<Blob> {
|
|
|
|
|
const img = document.createElement("img");
|
|
|
|
|
img.decoding = "async";
|
|
|
|
|
img.loading = "eager";
|
|
|
|
|
img.src = dataUrl;
|
|
|
|
|
await new Promise((res, rej) => { img.onload = () => res(null); img.onerror = rej; });
|
|
|
|
|
const { naturalWidth: w, naturalHeight: h } = img;
|
|
|
|
|
const scale = Math.min(1, opts.maxWidth / w || 1, opts.maxHeight / h || 1);
|
|
|
|
|
const targetW = Math.max(1, Math.round(w * scale));
|
|
|
|
|
const targetH = Math.max(1, Math.round(h * scale));
|
|
|
|
|
const canvas = document.createElement("canvas");
|
|
|
|
|
canvas.width = targetW;
|
|
|
|
|
canvas.height = targetH;
|
|
|
|
|
const ctx = canvas.getContext("2d");
|
|
|
|
|
if (!ctx) throw new Error("Canvas unsupported");
|
|
|
|
|
ctx.drawImage(img, 0, 0, targetW, targetH);
|
|
|
|
|
return await new Promise((resolve) => canvas.toBlob((b) => resolve(b as Blob), "image/webp", opts.quality));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function UploadButton({ onUploaded, multiple = false, maxWidth = 1600, maxHeight = 1600, quality = 0.9 }: Props) {
|
2025-10-09 17:05:19 +09:00
|
|
|
const { show } = useToast();
|
|
|
|
|
const [loading, setLoading] = useState(false);
|
|
|
|
|
async function onChange(e: React.ChangeEvent<HTMLInputElement>) {
|
2025-10-09 17:16:27 +09:00
|
|
|
const files = Array.from(e.target.files ?? []);
|
|
|
|
|
if (files.length === 0) return;
|
2025-10-09 17:05:19 +09:00
|
|
|
try {
|
|
|
|
|
setLoading(true);
|
2025-10-09 17:16:27 +09:00
|
|
|
for (const f of files) {
|
2025-10-09 17:58:12 +09:00
|
|
|
// 클라에서 리사이즈 및 webp 변환
|
|
|
|
|
const dataUrl = await readFileAsDataUrl(f);
|
|
|
|
|
const blob = await resizeImageToBlob(dataUrl, { maxWidth, maxHeight, quality });
|
2025-10-09 17:16:27 +09:00
|
|
|
const fd = new FormData();
|
2025-10-09 17:58:12 +09:00
|
|
|
fd.append("file", new File([blob], `${Date.now()}.webp`, { type: "image/webp" }));
|
2025-10-09 17:16:27 +09:00
|
|
|
const r = await fetch("/api/uploads", { method: "POST", body: fd });
|
|
|
|
|
const data = await r.json();
|
|
|
|
|
if (!r.ok) throw new Error(JSON.stringify(data));
|
|
|
|
|
onUploaded(data.url);
|
|
|
|
|
}
|
2025-10-09 17:05:19 +09:00
|
|
|
show("업로드 완료");
|
|
|
|
|
} catch (e) {
|
|
|
|
|
show("업로드 실패");
|
|
|
|
|
} finally {
|
|
|
|
|
setLoading(false);
|
|
|
|
|
e.currentTarget.value = "";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return <label style={{ display: "inline-block" }}>
|
|
|
|
|
<span style={{ padding: "6px 10px", border: "1px solid #ddd", borderRadius: 6, cursor: "pointer" }}>{loading ? "업로드 중..." : "이미지 업로드"}</span>
|
2025-10-09 17:16:27 +09:00
|
|
|
<input type="file" accept="image/*" onChange={onChange} style={{ display: "none" }} multiple={multiple} />
|
2025-10-09 17:05:19 +09:00
|
|
|
</label>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|