diff --git a/next.config.ts b/next.config.ts index 0232ff9..7a6e1a8 100644 --- a/next.config.ts +++ b/next.config.ts @@ -4,6 +4,9 @@ const nextConfig: NextConfig = { /* config options here */ output: "standalone", reactCompiler: true, + experimental: { + proxyClientMaxBodySize: "100mb", + }, }; export default nextConfig; diff --git a/src/lib/utils.ts b/src/lib/utils.ts index 52f9301..29dd8d4 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -87,4 +87,103 @@ export const downloadBase64File = (base64: string, filename: string) => { }); downloadFile(blob, filename); -}; \ No newline at end of file +}; + +export type CompressImageFileOptions = { + maxWidth?: number; + maxHeight?: number; + quality?: number; + mimeType?: "image/jpeg" | "image/webp" | "image/png"; + skipBelowBytes?: number; +}; + +const getCompressedImageName = (fileName: string, mimeType: string) => { + const extension = mimeType === "image/png" ? "png" : mimeType === "image/webp" ? "webp" : "jpg"; + const baseName = fileName.replace(/\.[^.]+$/, ""); + return `${baseName || "image"}.${extension}`; +}; + +const loadImageElement = (file: File) => { + return new Promise((resolve, reject) => { + const url = URL.createObjectURL(file); + const image = new Image(); + + image.onload = () => { + URL.revokeObjectURL(url); + resolve(image); + }; + image.onerror = () => { + URL.revokeObjectURL(url); + reject(new Error("Failed to load image.")); + }; + image.src = url; + }); +}; + +const canvasToBlob = (canvas: HTMLCanvasElement, mimeType: string, quality: number) => { + return new Promise((resolve, reject) => { + canvas.toBlob( + (blob) => { + if (blob) resolve(blob); + else reject(new Error("Failed to compress image.")); + }, + mimeType, + quality + ); + }); +}; + +export const compressImageFile = async ( + file: File, + options: CompressImageFileOptions = {} +) => { + if (!file.type.startsWith("image/") || file.type === "image/svg+xml" || file.type === "image/gif") { + return file; + } + + const { + maxWidth = 1600, + maxHeight = 1600, + quality = 0.75, + mimeType = "image/jpeg", + skipBelowBytes = 500 * 1024, + } = options; + + if (file.size <= skipBelowBytes) return file; + + try { + const image = await loadImageElement(file); + const ratio = Math.min(maxWidth / image.naturalWidth, maxHeight / image.naturalHeight, 1); + const width = Math.max(1, Math.round(image.naturalWidth * ratio)); + const height = Math.max(1, Math.round(image.naturalHeight * ratio)); + const canvas = document.createElement("canvas"); + const ctx = canvas.getContext("2d"); + + if (!ctx) return file; + + canvas.width = width; + canvas.height = height; + + if (mimeType === "image/jpeg") { + ctx.fillStyle = "#fff"; + ctx.fillRect(0, 0, width, height); + } + + ctx.drawImage(image, 0, 0, width, height); + + const blob = await canvasToBlob(canvas, mimeType, quality); + if (blob.size >= file.size) return file; + + return new File([blob], getCompressedImageName(file.name, mimeType), { + type: blob.type || mimeType, + lastModified: Date.now(), + }); + } catch { + return file; + } +}; + +export const compressImageFiles = ( + files: File[], + options?: CompressImageFileOptions +) => Promise.all(files.map((file) => compressImageFile(file, options))); diff --git a/src/query/monitoring.ts b/src/query/monitoring.ts index 294a279..dbfe215 100644 --- a/src/query/monitoring.ts +++ b/src/query/monitoring.ts @@ -4,6 +4,7 @@ import {useMutation, useQuery, useQueryClient} from "@tanstack/react-query"; import {nextApi} from "@/lib/http-client"; import {qk} from "@/query/key/keys"; import {MonitoringGpsRequest, PerformanceMonitoringVisit} from "@/types/gwcf"; +import {compressImageFiles} from "@/lib/utils"; export function useSaveDepartureMutation() { const qc = useQueryClient(); @@ -22,10 +23,11 @@ export function useSaveArrivalMutation() { const qc = useQueryClient(); return useMutation({ - mutationFn: ({performanceId, gps, files}: {performanceId: number; gps: MonitoringGpsRequest; files?: File[]}) => { + mutationFn: async ({performanceId, gps, files}: {performanceId: number; gps: MonitoringGpsRequest; files?: File[]}) => { const formData = new FormData(); + const compressedFiles = files ? await compressImageFiles(files) : []; formData.append("gps", new Blob([JSON.stringify(gps)], {type: "application/json"})); - files?.forEach((file) => formData.append("files", file, file.name)); + compressedFiles.forEach((file) => formData.append("files", file, file.name)); return nextApi.postForm(`/monitoring/visit/arrival/${performanceId}`, formData); }, onSuccess: (_res, {performanceId}) => {