diff --git a/src/app/api/user/profile/route.ts b/src/app/api/user/profile/route.ts index a99c8bc..2a0ad66 100644 --- a/src/app/api/user/profile/route.ts +++ b/src/app/api/user/profile/route.ts @@ -1,4 +1,4 @@ -import { funDeleteFile, funUploadFile, prisma } from "@/module/_global"; +import { DIR, funDeleteFile, funUploadFile, prisma } from "@/module/_global"; import { funGetUserByCookies } from "@/module/auth"; import _ from "lodash"; import { NextResponse } from "next/server"; @@ -119,34 +119,21 @@ export async function PUT(request: Request) { if (String(file) != "undefined" && String(file) != "null") { const fExt = file.name.split(".").pop() - // const fileName = user.id + '.' + fExt; - const fileName = 'COBAAYAA.' + fExt; + const fileName = user.id + '.' + fExt; const newFile = new File([file], fileName, { type: file.type }); - console.log(fileName, newFile.name) - await funDeleteFile({ name: fileName, dirId: "cm0x8dbwn0005bp5tgmfcthzw" }) - await funUploadFile({ file: newFile, dirId: "cm0x8dbwn0005bp5tgmfcthzw" }) + await funDeleteFile({ fileId: String(update.img) }) + const upload = await funUploadFile({ file: newFile, dirId: DIR.user }) + if (upload.success) { + await prisma.user.update({ + where: { + id: user.id + }, + data: { + img: upload.data.id + } + }) + } - - // fs.unlink(`./public/image/user/${update.img}`, (err) => { }) - // const root = path.join(process.cwd(), "./public/image/user/"); - // const fExt = file.name.split(".").pop() - // const fileName = user.id + '.' + fExt; - // const filePath = path.join(root, fileName); - - // // Konversi ArrayBuffer ke Buffer - // const buffer = Buffer.from(await file.arrayBuffer()); - - // // Tulis file ke sistem - // fs.writeFileSync(filePath, buffer); - - // await prisma.user.update({ - // where: { - // id: user.id - // }, - // data: { - // img: fileName - // } - // }) } const log = await createLogUser({ act: 'UPDATE', desc: 'User mengupdate data profile', table: 'user', data: user.id }) diff --git a/src/module/_global/bin/val_global.ts b/src/module/_global/bin/val_global.ts index 4a5dc97..4a3b14e 100644 --- a/src/module/_global/bin/val_global.ts +++ b/src/module/_global/bin/val_global.ts @@ -1,4 +1,12 @@ import { hookstate } from "@hookstate/core" export const pwd_key_config = "fchgvjknlmdfnbvghhujlaknsdvjbhknlkmsdbdyu567t8y9u30r4587638y9uipkoeghjvuyi89ipkoefmnrjbhtiu4or9ipkoemnjfbhjiuoijdklnjhbviufojkejnshbiuojijknehgruyu" -export const globalRole = hookstate('') \ No newline at end of file +export const globalRole = hookstate('') +export const DIR = { + task: "cm0xhcqf0000dacbbixjb09yn", + project: "cm0xhc9sv000bacbb7rfikw1k", + document: "cm0xhbkf50009acbbtw03qo4l", + village: "cm0xhb91o0007acbbkx8rk8hj", + user: "cm0x8dbwn0005bp5tgmfcthzw", + +} \ No newline at end of file diff --git a/src/module/_global/fun/delete_file.ts b/src/module/_global/fun/delete_file.ts index 881c662..a4ec2c6 100644 --- a/src/module/_global/fun/delete_file.ts +++ b/src/module/_global/fun/delete_file.ts @@ -1,22 +1,21 @@ -export async function funDeleteFile({ name, dirId }: { name: String, dirId: string }) { +export async function funDeleteFile({ fileId }: { fileId: string }) { try { - const res = await fetch(`https://wibu-storage.wibudev.com/api/dir/${dirId}/${name}`, { - method: "GET", + const res = await fetch(`https://wibu-storage.wibudev.com/api/files/${fileId}/delete`, { + method: "DELETE", headers: { - Authorization: `Bearer eyJhbGciOiJIUzI1NiJ9.eyJ1c2VyIjp7ImlkIjoiY20wdnQ4bzFrMDAwMDEyenE1eXl1emd5YiIsIm5hbWUiOiJhbWFsaWEiLCJlbWFpbCI6ImFtYWxpYUBiaXAuY29tIiwiQXBpS2V5IjpbeyJpZCI6ImNtMHZ0OG8xcjAwMDIxMnpxZDVzejd3eTgiLCJuYW1lIjoiZGVmYXVsdCJ9XX0sImlhdCI6MTcyNTkzNTE5MiwiZXhwIjo0ODgxNjk1MTkyfQ.7U-HUnNBDmeq_6XXohiFZjFnh2rSzUPMHDdrUKOd7G4` + Authorization: `Bearer ${process.env.WS_APIKEY}` } }); if (res.ok) { - console.log("Berhasil dapat"); const hasil = await res.json() - console.log('berhasilAmalia', hasil) - + return { success: true } } else { const errorText = await res.json(); - console.log('errorAmalia', errorText) + return { success: false } } } catch (error) { + return { success: false } console.error("Upload error:", error); } } \ No newline at end of file diff --git a/src/module/_global/fun/upload_file.ts b/src/module/_global/fun/upload_file.ts index 46b3045..49ff249 100644 --- a/src/module/_global/fun/upload_file.ts +++ b/src/module/_global/fun/upload_file.ts @@ -13,12 +13,14 @@ export async function funUploadFile({ file, dirId }: { file: File, dirId: string }); if (res.ok) { - console.log("File uploaded successfully"); + const hasil = await res.json() + return { success: true, data: hasil.data } } else { const errorText = await res.text(); - console.log('errorAmalia', errorText) + return { success: false, data: {} } } } catch (error) { console.error("Upload error:", error); + return { success: false, data: {} } } } \ No newline at end of file diff --git a/src/module/_global/index.ts b/src/module/_global/index.ts index daf18c3..228b950 100644 --- a/src/module/_global/index.ts +++ b/src/module/_global/index.ts @@ -1,5 +1,5 @@ import prisma from "./bin/prisma"; -import { globalRole, pwd_key_config } from "./bin/val_global"; +import { DIR, globalRole, pwd_key_config } from "./bin/val_global"; import SkeletonDetailDiscussionComment from "./components/skeleton_detail_discussion_comment"; import SkeletonDetailDiscussionMember from "./components/skeleton_detail_discussion_member"; import SkeletonDetailListTugasTask from "./components/skeleton_detail_list_tugas_task"; @@ -40,3 +40,4 @@ export { WrapLayout } export { NoZoom } export { funUploadFile } export { funDeleteFile } +export { DIR } diff --git a/src/module/user/profile/lib/get_dir.ts b/src/module/user/profile/lib/get_dir.ts deleted file mode 100644 index 84ddf88..0000000 --- a/src/module/user/profile/lib/get_dir.ts +++ /dev/null @@ -1,21 +0,0 @@ -export async function getListDir() { - try { - const res = await fetch("https://wibu-storage.wibudev.com/api/dir/cm0x8a1as0001bp5te7354yrp/list", { - method: "GET", - headers: { - Authorization: `Bearer eyJhbGciOiJIUzI1NiJ9.eyJ1c2VyIjp7ImlkIjoiY20wdnQ4bzFrMDAwMDEyenE1eXl1emd5YiIsIm5hbWUiOiJhbWFsaWEiLCJlbWFpbCI6ImFtYWxpYUBiaXAuY29tIiwiQXBpS2V5IjpbeyJpZCI6ImNtMHZ0OG8xcjAwMDIxMnpxZDVzejd3eTgiLCJuYW1lIjoiZGVmYXVsdCJ9XX0sImlhdCI6MTcyNTkzNTE5MiwiZXhwIjo0ODgxNjk1MTkyfQ.7U-HUnNBDmeq_6XXohiFZjFnh2rSzUPMHDdrUKOd7G4` - } - }); - - if (res.ok) { - const hasil = await res.json() - console.log("File uploaded successfully"); - console.log(hasil) - } else { - const errorText = await res.text(); - console.log(errorText) - } - } catch (error) { - console.error("Upload error:", error); - } -} \ No newline at end of file diff --git a/src/module/user/profile/ui/edit_profile.tsx b/src/module/user/profile/ui/edit_profile.tsx index 75ca4b2..7e7146f 100644 --- a/src/module/user/profile/ui/edit_profile.tsx +++ b/src/module/user/profile/ui/edit_profile.tsx @@ -1,5 +1,5 @@ "use client" -import { LayoutNavbarNew, WARNA } from "@/module/_global"; +import { DIR, LayoutNavbarNew, WARNA } from "@/module/_global"; import { Avatar, Box, Button, Flex, Indicator, Modal, rem, Select, Skeleton, Stack, Text, TextInput } from "@mantine/core"; import toast from "react-hot-toast"; import LayoutModal from "@/module/_global/layout/layout_modal"; @@ -45,7 +45,7 @@ export default function EditProfile() { setLoading(true) const res = await funGetProfileByCookies() setData(res.data) - setIMG(`/api/file/img?jenis=image&cat=user&file=${res.data.img}`) + setIMG(`https://wibu-storage.wibudev.com/api/files/${res.data.img}`) setLoading(false) } catch (error) { console.error(error); @@ -258,31 +258,31 @@ export default function EditProfile() { zIndex: 999, backgroundColor: `${WARNA.bgWhite}`, }}> - {loading ? - + {loading ? + : - + } setValModal(false)} diff --git a/src/module/user/profile/ui/profile.tsx b/src/module/user/profile/ui/profile.tsx index 25cfcac..94e76fa 100644 --- a/src/module/user/profile/ui/profile.tsx +++ b/src/module/user/profile/ui/profile.tsx @@ -1,7 +1,6 @@ "use client" -import { LayoutIconBack, LayoutNavbarHome, SkeletonDetailProfile, WARNA } from "@/module/_global"; -import { ActionIcon, Anchor, Avatar, Box, Button, Flex, Grid, Group, SimpleGrid, Skeleton, Stack, Text } from "@mantine/core"; -import { HiUser } from "react-icons/hi2"; +import { DIR, LayoutIconBack, LayoutNavbarHome, SkeletonDetailProfile, WARNA } from "@/module/_global"; +import { ActionIcon, Avatar, Box, Grid, Group, Skeleton, Stack, Text } from "@mantine/core"; import { RiIdCardFill } from "react-icons/ri"; import { FaSquarePhone } from "react-icons/fa6"; import { MdEmail } from "react-icons/md"; @@ -27,8 +26,7 @@ export default function Profile() { setLoading(true) const res = await funGetProfileByCookies() setData(res.data) - setIMG(`/api/file/img?jenis=image&cat=user&file=${res.data.img}`) - // setIMG(`https://wibu-storage.wibudev.com/api/files/view/cm0x8dbwn0005bp5tgmfcthzw/${res.data.img}`) + setIMG(`https://wibu-storage.wibudev.com/api/files/${res.data.img}`) setLoading(false) } catch (error) { console.error(error);