upload base64 fix
This commit is contained in:
42
src/server/lib/mimetypeToExtension.ts
Normal file
42
src/server/lib/mimetypeToExtension.ts
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
export function mimeToExtension(mimeType: string): string {
|
||||||
|
const map: Record<string, string> = {
|
||||||
|
"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
|
||||||
|
}
|
||||||
@@ -162,7 +162,7 @@ export async function uploadFile(config: Config, file: File): Promise<string> {
|
|||||||
if (!res.ok) throw new Error(`Upload failed: ${text}`);
|
if (!res.ok) throw new Error(`Upload failed: ${text}`);
|
||||||
return `✅ Uploaded ${file.name} successfully`;
|
return `✅ Uploaded ${file.name} successfully`;
|
||||||
}
|
}
|
||||||
export async function uploadFileBase64(config: Config, base64File: { name: string; data: string }): Promise<string> {
|
export async function uploadFileBase64(config: Config, base64File: { name: string; data: string; }): Promise<string> {
|
||||||
const remoteName = path.basename(base64File.name);
|
const remoteName = path.basename(base64File.name);
|
||||||
|
|
||||||
// 1. Dapatkan upload link (pakai Authorization)
|
// 1. Dapatkan upload link (pakai Authorization)
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ import { generateNoPengaduan } from "../lib/no-pengaduan"
|
|||||||
import { normalizePhoneNumber } from "../lib/normalizePhone"
|
import { normalizePhoneNumber } from "../lib/normalizePhone"
|
||||||
import { prisma } from "../lib/prisma"
|
import { prisma } from "../lib/prisma"
|
||||||
import { defaultConfigSF, uploadFile, uploadFileBase64 } from "../lib/seafile"
|
import { defaultConfigSF, uploadFile, uploadFileBase64 } from "../lib/seafile"
|
||||||
|
import { mimeToExtension } from "../lib/mimetypeToExtension"
|
||||||
|
import { v4 as uuidv4 } from "uuid"
|
||||||
|
|
||||||
const PengaduanRoute = new Elysia({
|
const PengaduanRoute = new Elysia({
|
||||||
prefix: "pengaduan",
|
prefix: "pengaduan",
|
||||||
@@ -463,7 +465,9 @@ const PengaduanRoute = new Elysia({
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
.post("/upload-base64", async ({ body }) => {
|
.post("/upload-base64", async ({ body }) => {
|
||||||
const { data } = body;
|
const { data, mimetype } = body;
|
||||||
|
const ext = mimeToExtension(mimetype)
|
||||||
|
const name = `${uuidv4()}.${ext}`
|
||||||
|
|
||||||
// Validasi file
|
// Validasi file
|
||||||
if (!data) {
|
if (!data) {
|
||||||
@@ -475,7 +479,7 @@ const PengaduanRoute = new Elysia({
|
|||||||
// const base64String = Buffer.from(buffer).toString("base64");
|
// const base64String = Buffer.from(buffer).toString("base64");
|
||||||
|
|
||||||
// (Opsional) jika perlu dikirim ke Seafile sebagai 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 {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
@@ -487,7 +491,8 @@ const PengaduanRoute = new Elysia({
|
|||||||
};
|
};
|
||||||
}, {
|
}, {
|
||||||
body: t.Object({
|
body: t.Object({
|
||||||
data: t.String()
|
data: t.String(),
|
||||||
|
mimetype: t.String()
|
||||||
}),
|
}),
|
||||||
detail: {
|
detail: {
|
||||||
summary: "Upload File (Base64)",
|
summary: "Upload File (Base64)",
|
||||||
|
|||||||
3
x.ts
3
x.ts
@@ -10,7 +10,8 @@ const base64Data = fileBuffer.toString("base64");
|
|||||||
|
|
||||||
// 3️⃣ Buat payload JSON
|
// 3️⃣ Buat payload JSON
|
||||||
const payload = {
|
const payload = {
|
||||||
file: base64Data,
|
data: base64Data,
|
||||||
|
mimetype: "image/png"
|
||||||
};
|
};
|
||||||
|
|
||||||
// 4️⃣ Kirim ke server pakai fetch
|
// 4️⃣ Kirim ke server pakai fetch
|
||||||
|
|||||||
Reference in New Issue
Block a user