diff --git a/src/app/api/image/upload/route.ts b/src/app/api/image/upload/route.ts index d0aef5e2..bf558db0 100644 --- a/src/app/api/image/upload/route.ts +++ b/src/app/api/image/upload/route.ts @@ -3,7 +3,11 @@ import backendLogger from "@/util/backendLogger"; import { NextResponse } from "next/server"; import sharp from "sharp"; export async function POST(request: Request) { + let fixFormData; const formData = await request.formData(); + const file: any = formData.get("file"); + const mimeType = file.type; + console.log("MIME Type:", mimeType); const valueOfDir = formData.get("dirId"); const keyOfDirectory = await funGetDirectoryNameByValue({ @@ -12,30 +16,33 @@ export async function POST(request: Request) { if (request.method === "POST") { try { - const file: any = formData.get("file"); - console.log("ini file baru>", file); + if (mimeType != "application/pdf") { + // Resize ukuran + const imageBuffer = await file.arrayBuffer(); + const resize = await sharp(imageBuffer).resize(2000).toBuffer(); - // Resize ukuran - const imageBuffer = await file.arrayBuffer(); - const resize = await sharp(imageBuffer).resize(2000).toBuffer(); + // Convert buffer ke Blob + const blob = new Blob([resize], { type: file.type }); - // Convert buffer ke Blob - const blob = new Blob([resize], { type: file.type }); + // Convert Blob ke File + const resizedFile = new File([blob], file.name, { + type: file.type, + lastModified: new Date().getTime(), + }); - // Convert Blob ke File - const resizedFile = new File([blob], file.name, { - type: file.type, - lastModified: new Date().getTime(), - }); + // Buat FormData baru + const newFormData = new FormData(); + newFormData.append("file", resizedFile); + newFormData.append("dirId", formData.get("dirId") as string); - // Buat FormData baru - const newFormData = new FormData(); - newFormData.append("file", resizedFile); - newFormData.append("dirId", formData.get("dirId") as string); + fixFormData = newFormData; + } else { + fixFormData = formData; + } const res = await fetch("https://wibu-storage.wibudev.com/api/upload", { method: "POST", - body: newFormData, + body: fixFormData, headers: { Authorization: `Bearer ${process.env.WS_APIKEY}`, }, diff --git a/src/app_modules/_global/button/comp_button_upload_photo.tsx b/src/app_modules/_global/button/comp_button_upload_photo.tsx index 378e7126..fa1185d0 100644 --- a/src/app_modules/_global/button/comp_button_upload_photo.tsx +++ b/src/app_modules/_global/button/comp_button_upload_photo.tsx @@ -1,12 +1,10 @@ "use client"; +import { clientLogger } from "@/util/clientLogger"; import { Button, FileButton } from "@mantine/core"; import { IconCamera } from "@tabler/icons-react"; +import { useState } from "react"; import { MainColor } from "../color"; -import { MAX_SIZE } from "../lib"; -import { PemberitahuanMaksimalFile } from "../lib/max_size"; -import { ComponentGlobal_NotifikasiPeringatan } from "../notif_global"; -import { clientLogger } from "@/util/clientLogger"; export function ComponentGlobal_ButtonUploadFileImage({ onSetFile, @@ -15,22 +13,27 @@ export function ComponentGlobal_ButtonUploadFileImage({ onSetFile: File | any; onSetImage: any | null; }) { + const [isLoading, setIsLoading] = useState(false); + return ( { try { + setIsLoading(true); const buffer = URL.createObjectURL( new Blob([new Uint8Array(await files.arrayBuffer())]) ); - if (files.size > MAX_SIZE) { - ComponentGlobal_NotifikasiPeringatan(PemberitahuanMaksimalFile); - return; - } else { - onSetFile(files); - onSetImage(buffer); - } + // if (files.size > MAX_SIZE) { + // ComponentGlobal_NotifikasiPeringatan(PemberitahuanMaksimalFile); + // return; + // } else { + // } + onSetFile(files); + onSetImage(buffer); + setIsLoading(false); } catch (error) { + setIsLoading(false); clientLogger.error("Upload error:", error); } }} @@ -39,6 +42,8 @@ export function ComponentGlobal_ButtonUploadFileImage({ {(props) => (