upd: upload foto user

This commit is contained in:
amel
2024-09-12 10:55:56 +08:00
parent 6cd32ceb22
commit c23b94ff07
8 changed files with 65 additions and 91 deletions

View File

@@ -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 { funGetUserByCookies } from "@/module/auth";
import _ from "lodash"; import _ from "lodash";
import { NextResponse } from "next/server"; import { NextResponse } from "next/server";
@@ -119,34 +119,21 @@ export async function PUT(request: Request) {
if (String(file) != "undefined" && String(file) != "null") { if (String(file) != "undefined" && String(file) != "null") {
const fExt = file.name.split(".").pop() const fExt = file.name.split(".").pop()
// const fileName = user.id + '.' + fExt; const fileName = user.id + '.' + fExt;
const fileName = 'COBAAYAA.' + fExt;
const newFile = new File([file], fileName, { type: file.type }); const newFile = new File([file], fileName, { type: file.type });
console.log(fileName, newFile.name) await funDeleteFile({ fileId: String(update.img) })
await funDeleteFile({ name: fileName, dirId: "cm0x8dbwn0005bp5tgmfcthzw" }) const upload = await funUploadFile({ file: newFile, dirId: DIR.user })
await funUploadFile({ file: newFile, dirId: "cm0x8dbwn0005bp5tgmfcthzw" }) 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 }) const log = await createLogUser({ act: 'UPDATE', desc: 'User mengupdate data profile', table: 'user', data: user.id })

View File

@@ -2,3 +2,11 @@ import { hookstate } from "@hookstate/core"
export const pwd_key_config = "fchgvjknlmdfnbvghhujlaknsdvjbhknlkmsdbdyu567t8y9u30r4587638y9uipkoeghjvuyi89ipkoefmnrjbhtiu4or9ipkoemnjfbhjiuoijdklnjhbviufojkejnshbiuojijknehgruyu" export const pwd_key_config = "fchgvjknlmdfnbvghhujlaknsdvjbhknlkmsdbdyu567t8y9u30r4587638y9uipkoeghjvuyi89ipkoefmnrjbhtiu4or9ipkoemnjfbhjiuoijdklnjhbviufojkejnshbiuojijknehgruyu"
export const globalRole = hookstate<string>('') export const globalRole = hookstate<string>('')
export const DIR = {
task: "cm0xhcqf0000dacbbixjb09yn",
project: "cm0xhc9sv000bacbb7rfikw1k",
document: "cm0xhbkf50009acbbtw03qo4l",
village: "cm0xhb91o0007acbbkx8rk8hj",
user: "cm0x8dbwn0005bp5tgmfcthzw",
}

View File

@@ -1,22 +1,21 @@
export async function funDeleteFile({ name, dirId }: { name: String, dirId: string }) { export async function funDeleteFile({ fileId }: { fileId: string }) {
try { try {
const res = await fetch(`https://wibu-storage.wibudev.com/api/dir/${dirId}/${name}`, { const res = await fetch(`https://wibu-storage.wibudev.com/api/files/${fileId}/delete`, {
method: "GET", method: "DELETE",
headers: { headers: {
Authorization: `Bearer eyJhbGciOiJIUzI1NiJ9.eyJ1c2VyIjp7ImlkIjoiY20wdnQ4bzFrMDAwMDEyenE1eXl1emd5YiIsIm5hbWUiOiJhbWFsaWEiLCJlbWFpbCI6ImFtYWxpYUBiaXAuY29tIiwiQXBpS2V5IjpbeyJpZCI6ImNtMHZ0OG8xcjAwMDIxMnpxZDVzejd3eTgiLCJuYW1lIjoiZGVmYXVsdCJ9XX0sImlhdCI6MTcyNTkzNTE5MiwiZXhwIjo0ODgxNjk1MTkyfQ.7U-HUnNBDmeq_6XXohiFZjFnh2rSzUPMHDdrUKOd7G4` Authorization: `Bearer ${process.env.WS_APIKEY}`
} }
}); });
if (res.ok) { if (res.ok) {
console.log("Berhasil dapat");
const hasil = await res.json() const hasil = await res.json()
console.log('berhasilAmalia', hasil) return { success: true }
} else { } else {
const errorText = await res.json(); const errorText = await res.json();
console.log('errorAmalia', errorText) return { success: false }
} }
} catch (error) { } catch (error) {
return { success: false }
console.error("Upload error:", error); console.error("Upload error:", error);
} }
} }

View File

@@ -13,12 +13,14 @@ export async function funUploadFile({ file, dirId }: { file: File, dirId: string
}); });
if (res.ok) { if (res.ok) {
console.log("File uploaded successfully"); const hasil = await res.json()
return { success: true, data: hasil.data }
} else { } else {
const errorText = await res.text(); const errorText = await res.text();
console.log('errorAmalia', errorText) return { success: false, data: {} }
} }
} catch (error) { } catch (error) {
console.error("Upload error:", error); console.error("Upload error:", error);
return { success: false, data: {} }
} }
} }

View File

@@ -1,5 +1,5 @@
import prisma from "./bin/prisma"; 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 SkeletonDetailDiscussionComment from "./components/skeleton_detail_discussion_comment";
import SkeletonDetailDiscussionMember from "./components/skeleton_detail_discussion_member"; import SkeletonDetailDiscussionMember from "./components/skeleton_detail_discussion_member";
import SkeletonDetailListTugasTask from "./components/skeleton_detail_list_tugas_task"; import SkeletonDetailListTugasTask from "./components/skeleton_detail_list_tugas_task";
@@ -40,3 +40,4 @@ export { WrapLayout }
export { NoZoom } export { NoZoom }
export { funUploadFile } export { funUploadFile }
export { funDeleteFile } export { funDeleteFile }
export { DIR }

View File

@@ -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);
}
}

View File

@@ -1,5 +1,5 @@
"use client" "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 { Avatar, Box, Button, Flex, Indicator, Modal, rem, Select, Skeleton, Stack, Text, TextInput } from "@mantine/core";
import toast from "react-hot-toast"; import toast from "react-hot-toast";
import LayoutModal from "@/module/_global/layout/layout_modal"; import LayoutModal from "@/module/_global/layout/layout_modal";
@@ -45,7 +45,7 @@ export default function EditProfile() {
setLoading(true) setLoading(true)
const res = await funGetProfileByCookies() const res = await funGetProfileByCookies()
setData(res.data) 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) setLoading(false)
} catch (error) { } catch (error) {
console.error(error); console.error(error);
@@ -259,30 +259,30 @@ export default function EditProfile() {
backgroundColor: `${WARNA.bgWhite}`, backgroundColor: `${WARNA.bgWhite}`,
}}> }}>
{loading ? {loading ?
<Skeleton height={50} radius={30} /> <Skeleton height={50} radius={30} />
: :
<Button <Button
c={"white"} c={"white"}
bg={WARNA.biruTua} bg={WARNA.biruTua}
size="md" size="md"
radius={30} radius={30}
fullWidth fullWidth
onClick={() => { onClick={() => {
if ( if (
data.nik !== "" && data.nik !== "" &&
data.name !== "" && data.name !== "" &&
data.email !== "" && data.email !== "" &&
data.phone !== "" && data.phone !== "" &&
data.gender !== "" data.gender !== ""
) { ) {
setValModal(true) setValModal(true)
} else { } else {
toast.error("Mohon lengkapi semua form"); toast.error("Mohon lengkapi semua form");
} }
}} }}
> >
Simpan Simpan
</Button> </Button>
} }
</Box> </Box>
<LayoutModal opened={isValModal} onClose={() => setValModal(false)} <LayoutModal opened={isValModal} onClose={() => setValModal(false)}

View File

@@ -1,7 +1,6 @@
"use client" "use client"
import { LayoutIconBack, LayoutNavbarHome, SkeletonDetailProfile, WARNA } from "@/module/_global"; import { DIR, LayoutIconBack, LayoutNavbarHome, SkeletonDetailProfile, WARNA } from "@/module/_global";
import { ActionIcon, Anchor, Avatar, Box, Button, Flex, Grid, Group, SimpleGrid, Skeleton, Stack, Text } from "@mantine/core"; import { ActionIcon, Avatar, Box, Grid, Group, Skeleton, Stack, Text } from "@mantine/core";
import { HiUser } from "react-icons/hi2";
import { RiIdCardFill } from "react-icons/ri"; import { RiIdCardFill } from "react-icons/ri";
import { FaSquarePhone } from "react-icons/fa6"; import { FaSquarePhone } from "react-icons/fa6";
import { MdEmail } from "react-icons/md"; import { MdEmail } from "react-icons/md";
@@ -27,8 +26,7 @@ export default function Profile() {
setLoading(true) setLoading(true)
const res = await funGetProfileByCookies() const res = await funGetProfileByCookies()
setData(res.data) 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}`)
// setIMG(`https://wibu-storage.wibudev.com/api/files/view/cm0x8dbwn0005bp5tgmfcthzw/${res.data.img}`)
setLoading(false) setLoading(false)
} catch (error) { } catch (error) {
console.error(error); console.error(error);