Portofolio
Fix: - api/mobile/portofolio/[id]/route.ts: tambah put dan delete File: Add: - src/app/api/mobile/file/: upload & delete ### No Issue
This commit is contained in:
41
src/app/api/mobile/file/[id]/route.ts
Normal file
41
src/app/api/mobile/file/[id]/route.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
export { DELETE };
|
||||
|
||||
async function DELETE(
|
||||
request: Request,
|
||||
{ params }: { params: { id: string } }
|
||||
) {
|
||||
try {
|
||||
const { id } = params;
|
||||
|
||||
const deleteFile = await fetch(
|
||||
`https://wibu-storage.wibudev.com/api/files/${id}/delete`,
|
||||
{
|
||||
method: "DELETE",
|
||||
headers: {
|
||||
Authorization: `Bearer ${process.env.WS_APIKEY}`,
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
if (deleteFile.ok) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: true,
|
||||
message: "File berhasil dihapus",
|
||||
},
|
||||
{ status: 200 }
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
message: "Gagal menghapus file, coba lagi nanti (error: 500)",
|
||||
reason: (error as Error).message,
|
||||
},
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
51
src/app/api/mobile/file/route.ts
Normal file
51
src/app/api/mobile/file/route.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { funGetDirectoryNameByValue } from "@/app_modules/_global/fun/get";
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
export async function POST(request: Request) {
|
||||
const formData = await request.formData();
|
||||
const dirId = formData.get("dirId");
|
||||
|
||||
const keyOfDirectory = await funGetDirectoryNameByValue({
|
||||
value: dirId as string,
|
||||
});
|
||||
|
||||
try {
|
||||
const res = await fetch("https://wibu-storage.wibudev.com/api/upload", {
|
||||
method: "POST",
|
||||
body: formData,
|
||||
headers: {
|
||||
Authorization: `Bearer ${process.env.WS_APIKEY}`,
|
||||
},
|
||||
});
|
||||
|
||||
const dataRes = await res.json();
|
||||
|
||||
if (res.ok) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: true,
|
||||
data: dataRes.data,
|
||||
message: "Success upload file " + keyOfDirectory,
|
||||
},
|
||||
{ status: 200 }
|
||||
);
|
||||
} else {
|
||||
const errorText = await res.text();
|
||||
console.log(`Failed upload ${keyOfDirectory}: ${errorText}`);
|
||||
return NextResponse.json(
|
||||
{ success: false, message: errorText },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
console.log("Error upload >>", (error as Error).message || error);
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
message: "Failed upload file",
|
||||
reason: (error as Error).message || error,
|
||||
},
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user