Feature logs

Deskripsi:
- Fitur baru log untuk melihat error pada server upload
- Baru di terapkan di create profile
This commit is contained in:
2024-12-16 15:41:02 +08:00
parent 4dd98c6183
commit 0bdc25b1e6
19 changed files with 815 additions and 349 deletions

View File

@@ -1,24 +1,50 @@
export async function funGlobal_DeleteFileById({ fileId }: { fileId: string }) {
try {
const res = await fetch(
`https://wibu-storage.wibudev.com/api/files/${fileId}/delete`,
{
method: "DELETE",
headers: {
Authorization: `Bearer ${process.env.WS_APIKEY}`,
},
}
);
import { clientLogger } from "@/util/clientLogger";
if (res.ok) {
const hasil = await res.json();
return { success: true };
export async function funGlobal_DeleteFileById({
fileId,
dirId,
}: {
fileId: string;
dirId?: string;
}) {
try {
const res = await fetch("/api/image/delete", {
method: "DELETE",
body: JSON.stringify({ fileId, dirId }),
});
const data = await res.json();
if (data.success) {
clientLogger.info(`File ${fileId} deleted successfully`);
return { success: true, message: "File berhasil dihapus" };
} else {
const errorText = await res.json();
return { success: false };
return { success: false, message: data.message };
}
} catch (error) {
return { success: false };
console.error("Upload error:", error);
return { success: false, message: "An unexpected error occurred" };
}
// try {
// const res = await fetch(
// `https://wibu-storage.wibudev.com/api/files/${fileId}/delete`,
// {
// method: "DELETE",
// headers: {
// Authorization: `Bearer ${process.env.WS_APIKEY}`,
// },
// }
// );
// if (res.ok) {
// const hasil = await res.json();
// return { success: true, message: "File berhasil dihapus" };
// } else {
// const errorText = await res.json();
// return { success: false, message: errorText.message };
// }
// } catch (error) {
// console.error("Upload error:", error);
// return { success: false, message: "An unexpected error occurred" };
// }
}

View File

@@ -0,0 +1,11 @@
import { DIRECTORY_ID } from "@/app/lib";
export async function funGetDirectoryNameByValue({
value,
}: {
value?: string | null;
}) {
if (!value) return null;
const object: any = DIRECTORY_ID;
return Object.keys(object).find((key) => object[key] === value);
}

View File

@@ -1,5 +1,5 @@
import { funGlobal_CheckProfile } from "./fun_check_profile";
import { funGetDirectoryNameByValue } from "./fun_get_directory_name";
import { funGlobal_getNomorAdmin } from "./fun_get_nomor_admin";
import { funGetUserIdByToken } from "./fun_get_user_id_by_token";
import { funGlobal_getMasterKategoriApp } from "./fun_master_kategori_app";
@@ -8,3 +8,4 @@ export { funGlobal_getMasterKategoriApp };
export { funGlobal_getNomorAdmin };
export { funGetUserIdByToken };
export { funGlobal_CheckProfile };
export { funGetDirectoryNameByValue };

View File

@@ -32,40 +32,20 @@ export async function funGlobal_UploadToStorage({
console.error("File terlalu besar");
return { success: false, message: "File size exceeds limit" };
}
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 30000); // Timeout 30 detik
const formData = new FormData();
formData.append("file", file);
formData.append("dirId", dirId);
try {
const res = await fetch("https://wibu-storage.wibudev.com/api/upload", {
method: "POST",
body: formData,
headers: {
Authorization: `Bearer ${Env_WS_APIKEY}`,
},
signal: controller.signal,
});
const upload = await fetch("/api/image/upload", {
method: "POST",
body: formData,
});
clearTimeout(timeoutId); // Bersihkan timeout jika selesai tepat waktu
const res = await upload.json();
if (res.ok) {
const dataRes = await res.json();
// const cekLog = await res.text();
// console.log(cekLog);
return { success: true, data: dataRes.data };
} else {
const errorText = await res.text();
console.error("Error:", errorText);
return { success: false, message: errorText };
}
} catch (error) {
clearTimeout(timeoutId); //
console.error("Error:", error);
return { success: false, message: "An unexpected error occurred" };
if (upload.ok) {
return { success: true, data: res.data, message: res.message };
} else {
return { success: false, data: {}, message: res.message };
}
}