upd: coba api pengaduan dengan upload gambar
This commit is contained in:
@@ -162,6 +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);
|
||||||
|
|
||||||
@@ -194,6 +195,38 @@ export async function uploadFileBase64(config: Config, base64File: { name: strin
|
|||||||
return `✅ Uploaded ${base64File.name} successfully`;
|
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`;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import { mimeToExtension } from "../lib/mimetypeToExtension"
|
|||||||
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 { catFile, defaultConfigSF, testConnection, uploadFile, uploadFileBase64 } from "../lib/seafile"
|
import { catFile, defaultConfigSF, testConnection, uploadFile, uploadFileBase64, uploadFileToFolder } from "../lib/seafile"
|
||||||
|
|
||||||
const PengaduanRoute = new Elysia({
|
const PengaduanRoute = new Elysia({
|
||||||
prefix: "pengaduan",
|
prefix: "pengaduan",
|
||||||
@@ -100,7 +100,8 @@ const PengaduanRoute = new Elysia({
|
|||||||
|
|
||||||
// --- PENGADUAN ---
|
// --- PENGADUAN ---
|
||||||
.post("/create", async ({ body }) => {
|
.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()
|
const noPengaduan = await generateNoPengaduan()
|
||||||
let idCategoryFix = idCategory
|
let idCategoryFix = idCategory
|
||||||
let idWargaFix = idWarga
|
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) {
|
if (!category) {
|
||||||
const cariCategory = await prisma.categoryPengaduan.findFirst({
|
const cariCategory = await prisma.categoryPengaduan.findFirst({
|
||||||
where: {
|
where: {
|
||||||
@@ -163,7 +170,7 @@ const PengaduanRoute = new Elysia({
|
|||||||
idCategory: idCategoryFix,
|
idCategory: idCategoryFix,
|
||||||
idWarga: idWargaFix,
|
idWarga: idWargaFix,
|
||||||
location,
|
location,
|
||||||
image,
|
image: imageFix,
|
||||||
noPengaduan,
|
noPengaduan,
|
||||||
},
|
},
|
||||||
select: {
|
select: {
|
||||||
@@ -172,7 +179,7 @@ const PengaduanRoute = new Elysia({
|
|||||||
})
|
})
|
||||||
|
|
||||||
if (!pengaduan.id) {
|
if (!pengaduan.id) {
|
||||||
throw new Error("gagal membuat pengaduan")
|
return { success: false, message: 'gagal membuat pengaduan' }
|
||||||
}
|
}
|
||||||
|
|
||||||
await prisma.historyPengaduan.create({
|
await prisma.historyPengaduan.create({
|
||||||
@@ -188,7 +195,8 @@ const PengaduanRoute = new Elysia({
|
|||||||
title: t.String({ minLength: 1, error: "title harus diisi" }),
|
title: t.String({ minLength: 1, error: "title harus diisi" }),
|
||||||
detail: t.String({ minLength: 1, error: "detail harus diisi" }),
|
detail: t.String({ minLength: 1, error: "detail harus diisi" }),
|
||||||
location: t.String({ minLength: 1, error: "location 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" }),
|
idCategory: t.String({ minLength: 1, error: "idCategory harus diisi" }),
|
||||||
idWarga: t.String({ minLength: 1, error: "idWarga harus diisi" }),
|
idWarga: t.String({ minLength: 1, error: "idWarga harus diisi" }),
|
||||||
phone: t.String({ minLength: 1, error: "phone harus diisi" }),
|
phone: t.String({ minLength: 1, error: "phone harus diisi" }),
|
||||||
@@ -622,7 +630,7 @@ const PengaduanRoute = new Elysia({
|
|||||||
const { fileName } = query
|
const { fileName } = query
|
||||||
|
|
||||||
const connect = await testConnection(defaultConfigSF)
|
const connect = await testConnection(defaultConfigSF)
|
||||||
console.log({connect})
|
console.log({ connect })
|
||||||
|
|
||||||
const hasil = await catFile(defaultConfigSF, fileName)
|
const hasil = await catFile(defaultConfigSF, fileName)
|
||||||
console.log('hasilnya', hasil)
|
console.log('hasilnya', hasil)
|
||||||
|
|||||||
Reference in New Issue
Block a user