upd: upload base64
Deskripsi: - api upload base64 test No Issues
This commit is contained in:
@@ -162,6 +162,39 @@ 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> {
|
||||||
|
const remoteName = path.basename(base64File.name);
|
||||||
|
|
||||||
|
// 1. Dapatkan upload link (pakai Authorization)
|
||||||
|
const uploadUrlResponse = await fetchWithAuth(
|
||||||
|
config,
|
||||||
|
`${config.URL}/${config.REPO}/upload-link/`
|
||||||
|
);
|
||||||
|
const uploadUrl = (await uploadUrlResponse.text()).replace(/"/g, "");
|
||||||
|
|
||||||
|
// 2. Konversi base64 ke Blob
|
||||||
|
const binary = Buffer.from(base64File.data, "base64");
|
||||||
|
const blob = new Blob([binary]);
|
||||||
|
|
||||||
|
// 3. Siapkan form-data
|
||||||
|
const formData = new FormData();
|
||||||
|
formData.append("parent_dir", "/");
|
||||||
|
formData.append("relative_path", "syarat-dokumen"); // tanpa slash di akhir
|
||||||
|
formData.append("file", blob, remoteName);
|
||||||
|
|
||||||
|
// 4. Upload file TANPA Authorization header, token di query param
|
||||||
|
const res = await fetch(`${uploadUrl}?token=${config.TOKEN}`, {
|
||||||
|
method: "POST",
|
||||||
|
body: formData,
|
||||||
|
});
|
||||||
|
|
||||||
|
const text = await res.text();
|
||||||
|
|
||||||
|
if (!res.ok) throw new Error(`Upload failed: ${text}`);
|
||||||
|
return `✅ Uploaded ${base64File.name} successfully`;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
export async function removeFile(config: Config, fileName: string): Promise<string> {
|
export async function removeFile(config: Config, fileName: string): Promise<string> {
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { getLastUpdated } from "../lib/get-last-updated"
|
|||||||
import { generateNoPengaduan } from "../lib/no-pengaduan"
|
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 } from "../lib/seafile"
|
import { defaultConfigSF, uploadFile, uploadFileBase64 } from "../lib/seafile"
|
||||||
|
|
||||||
const PengaduanRoute = new Elysia({
|
const PengaduanRoute = new Elysia({
|
||||||
prefix: "pengaduan",
|
prefix: "pengaduan",
|
||||||
@@ -432,8 +432,7 @@ const PengaduanRoute = new Elysia({
|
|||||||
tags: ["mcp"]
|
tags: ["mcp"]
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.post("/upload",
|
.post("/upload", async ({ body }) => {
|
||||||
async ({ body }) => {
|
|
||||||
const { file } = body;
|
const { file } = body;
|
||||||
|
|
||||||
// Validasi file
|
// Validasi file
|
||||||
@@ -452,8 +451,7 @@ const PengaduanRoute = new Elysia({
|
|||||||
size: file.size,
|
size: file.size,
|
||||||
seafileResult: result
|
seafileResult: result
|
||||||
};
|
};
|
||||||
},
|
}, {
|
||||||
{
|
|
||||||
body: t.Object({
|
body: t.Object({
|
||||||
file: t.File({ format: "binary" })
|
file: t.File({ format: "binary" })
|
||||||
}),
|
}),
|
||||||
@@ -464,6 +462,41 @@ const PengaduanRoute = new Elysia({
|
|||||||
consumes: ["multipart/form-data"]
|
consumes: ["multipart/form-data"]
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
.post("/upload-base64", async ({ body }) => {
|
||||||
|
const { file } = body;
|
||||||
|
|
||||||
|
// Validasi file
|
||||||
|
if (!file) {
|
||||||
|
return { success: false, message: "File tidak ditemukan" };
|
||||||
|
}
|
||||||
|
|
||||||
|
// Konversi file ke base64
|
||||||
|
const buffer = await file.arrayBuffer();
|
||||||
|
const base64String = Buffer.from(buffer).toString("base64");
|
||||||
|
|
||||||
|
// (Opsional) jika perlu dikirim ke Seafile sebagai base64
|
||||||
|
const result = await uploadFileBase64(defaultConfigSF, { name: file.name, data: base64String });
|
||||||
|
|
||||||
|
return {
|
||||||
|
success: true,
|
||||||
|
message: "Upload berhasil",
|
||||||
|
filename: file.name,
|
||||||
|
size: file.size,
|
||||||
|
base64Preview: base64String.slice(0, 100) + "...", // hanya preview
|
||||||
|
seafileResult: result
|
||||||
|
};
|
||||||
|
}, {
|
||||||
|
body: t.Object({
|
||||||
|
file: t.File({ format: "binary" })
|
||||||
|
}),
|
||||||
|
detail: {
|
||||||
|
summary: "Upload File (Base64)",
|
||||||
|
description: "Tool untuk upload file ke Seafile dalam format Base64",
|
||||||
|
tags: ["mcp"],
|
||||||
|
consumes: ["multipart/form-data"]
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
.get("/list", async ({ query }) => {
|
.get("/list", async ({ query }) => {
|
||||||
const { take, page, search, status } = query
|
const { take, page, search, status } = query
|
||||||
const skip = !page ? 0 : (Number(page) - 1) * (!take ? 10 : Number(take))
|
const skip = !page ? 0 : (Number(page) - 1) * (!take ? 10 : Number(take))
|
||||||
|
|||||||
2
upload_base64.sh
Normal file
2
upload_base64.sh
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
curl -X POST http://localhost:3000/api/pengaduan/upload-base64 \
|
||||||
|
-F file=@package.json
|
||||||
Reference in New Issue
Block a user