diff --git a/src/server/lib/mimetypeToExtension.ts b/src/server/lib/mimetypeToExtension.ts new file mode 100644 index 0000000..8c60062 --- /dev/null +++ b/src/server/lib/mimetypeToExtension.ts @@ -0,0 +1,42 @@ +export function mimeToExtension(mimeType: string): string { + const map: Record = { + "image/jpeg": "jpg", + "image/png": "png", + "image/gif": "gif", + "image/webp": "webp", + "image/svg+xml": "svg", + "image/bmp": "bmp", + "image/tiff": "tiff", + + "video/mp4": "mp4", + "video/webm": "webm", + "video/ogg": "ogv", + "video/quicktime": "mov", + + "audio/mpeg": "mp3", + "audio/wav": "wav", + "audio/ogg": "ogg", + "audio/webm": "weba", + + "application/pdf": "pdf", + "application/zip": "zip", + "application/x-zip-compressed": "zip", + "application/json": "json", + "application/javascript": "js", + "application/x-httpd-php": "php", + "application/msword": "doc", + "application/vnd.openxmlformats-officedocument.wordprocessingml.document": "docx", + "application/vnd.ms-excel": "xls", + "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": "xlsx", + "application/vnd.ms-powerpoint": "ppt", + "application/vnd.openxmlformats-officedocument.presentationml.presentation": "pptx", + + "text/plain": "txt", + "text/html": "html", + "text/css": "css", + "text/csv": "csv", + "text/xml": "xml", + }; + + return map[mimeType.toLowerCase()] || "bin"; // default jika tidak dikenal +} diff --git a/src/server/lib/seafile.ts b/src/server/lib/seafile.ts index 6f42299..65b03ba 100644 --- a/src/server/lib/seafile.ts +++ b/src/server/lib/seafile.ts @@ -162,7 +162,7 @@ export async function uploadFile(config: Config, file: File): Promise { if (!res.ok) throw new Error(`Upload failed: ${text}`); return `✅ Uploaded ${file.name} successfully`; } -export async function uploadFileBase64(config: Config, base64File: { name: string; data: string }): Promise { +export async function uploadFileBase64(config: Config, base64File: { name: string; data: string; }): Promise { const remoteName = path.basename(base64File.name); // 1. Dapatkan upload link (pakai Authorization) diff --git a/src/server/routes/pengaduan_route.ts b/src/server/routes/pengaduan_route.ts index 039ba25..06a819f 100644 --- a/src/server/routes/pengaduan_route.ts +++ b/src/server/routes/pengaduan_route.ts @@ -5,6 +5,8 @@ import { generateNoPengaduan } from "../lib/no-pengaduan" import { normalizePhoneNumber } from "../lib/normalizePhone" import { prisma } from "../lib/prisma" import { defaultConfigSF, uploadFile, uploadFileBase64 } from "../lib/seafile" +import { mimeToExtension } from "../lib/mimetypeToExtension" +import { v4 as uuidv4 } from "uuid" const PengaduanRoute = new Elysia({ prefix: "pengaduan", @@ -463,7 +465,9 @@ const PengaduanRoute = new Elysia({ }, }) .post("/upload-base64", async ({ body }) => { - const { data } = body; + const { data, mimetype } = body; + const ext = mimeToExtension(mimetype) + const name = `${uuidv4()}.${ext}` // Validasi file if (!data) { @@ -475,7 +479,7 @@ const PengaduanRoute = new Elysia({ // const base64String = Buffer.from(buffer).toString("base64"); // (Opsional) jika perlu dikirim ke Seafile sebagai base64 - const result = await uploadFileBase64(defaultConfigSF, { name: 'contoh', data: data }); + const result = await uploadFileBase64(defaultConfigSF, { name: name, data: data}); return { success: true, @@ -487,7 +491,8 @@ const PengaduanRoute = new Elysia({ }; }, { body: t.Object({ - data: t.String() + data: t.String(), + mimetype: t.String() }), detail: { summary: "Upload File (Base64)", diff --git a/x.ts b/x.ts index eecb117..51bbee3 100644 --- a/x.ts +++ b/x.ts @@ -10,7 +10,8 @@ const base64Data = fileBuffer.toString("base64"); // 3️⃣ Buat payload JSON const payload = { - file: base64Data, + data: base64Data, + mimetype: "image/png" }; // 4️⃣ Kirim ke server pakai fetch