upd: coba api pengaduan dengan upload gambar

This commit is contained in:
2025-11-12 14:57:22 +08:00
parent eacc8fc220
commit 63c88161d3
2 changed files with 47 additions and 6 deletions

View File

@@ -162,6 +162,7 @@ export async function uploadFile(config: Config, file: File): Promise<string> {
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<string> {
const remoteName = path.basename(base64File.name);
@@ -194,6 +195,38 @@ export async function uploadFileBase64(config: Config, base64File: { name: strin
return `✅ Uploaded ${base64File.name} successfully`;
}
export async function uploadFileToFolder(config: Config, base64File: { name: string; data: string; }, folder: 'syarat-dokumen' | 'pengaduan'): 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", folder); // 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`;
}

View File

@@ -6,7 +6,7 @@ import { mimeToExtension } from "../lib/mimetypeToExtension"
import { generateNoPengaduan } from "../lib/no-pengaduan"
import { normalizePhoneNumber } from "../lib/normalizePhone"
import { prisma } from "../lib/prisma"
import { catFile, defaultConfigSF, testConnection, uploadFile, uploadFileBase64 } from "../lib/seafile"
import { catFile, defaultConfigSF, testConnection, uploadFile, uploadFileBase64, uploadFileToFolder } from "../lib/seafile"
const PengaduanRoute = new Elysia({
prefix: "pengaduan",
@@ -100,7 +100,8 @@ const PengaduanRoute = new Elysia({
// --- PENGADUAN ---
.post("/create", async ({ body }) => {
const { title, detail, location, image, idCategory, idWarga, phone } = body
const { title, detail, location, imageData, imageMime, idCategory, idWarga, phone } = body
let imageFix = null
const noPengaduan = await generateNoPengaduan()
let idCategoryFix = idCategory
let idWargaFix = idWarga
@@ -110,6 +111,12 @@ const PengaduanRoute = new Elysia({
}
})
if (!imageData && !imageMime) {
const ext = mimeToExtension(imageMime)
imageFix = `${uuidv4()}.${ext}`
await uploadFileToFolder(defaultConfigSF, { name: imageFix, data: imageData }, "pengaduan")
}
if (!category) {
const cariCategory = await prisma.categoryPengaduan.findFirst({
where: {
@@ -163,7 +170,7 @@ const PengaduanRoute = new Elysia({
idCategory: idCategoryFix,
idWarga: idWargaFix,
location,
image,
image: imageFix,
noPengaduan,
},
select: {
@@ -172,7 +179,7 @@ const PengaduanRoute = new Elysia({
})
if (!pengaduan.id) {
throw new Error("gagal membuat pengaduan")
return { success: false, message: 'gagal membuat pengaduan' }
}
await prisma.historyPengaduan.create({
@@ -188,7 +195,8 @@ const PengaduanRoute = new Elysia({
title: t.String({ minLength: 1, error: "title harus diisi" }),
detail: t.String({ minLength: 1, error: "detail harus diisi" }),
location: t.String({ minLength: 1, error: "location harus diisi" }),
image: t.Any(),
imageData: t.String({ optional: true, description: "base64 encoded image data" }),
imageMime: t.String({ optional: true, description: "mime type of image" }),
idCategory: t.String({ minLength: 1, error: "idCategory harus diisi" }),
idWarga: t.String({ minLength: 1, error: "idWarga harus diisi" }),
phone: t.String({ minLength: 1, error: "phone harus diisi" }),
@@ -622,7 +630,7 @@ const PengaduanRoute = new Elysia({
const { fileName } = query
const connect = await testConnection(defaultConfigSF)
console.log({connect})
console.log({ connect })
const hasil = await catFile(defaultConfigSF, fileName)
console.log('hasilnya', hasil)