upd: upload image anggota

This commit is contained in:
amel
2024-09-12 11:39:08 +08:00
parent 7ae49bc068
commit 047311c8b9
3 changed files with 19 additions and 34 deletions

View File

@@ -1,7 +1,7 @@
import { prisma } from "@/module/_global"; import { DIR, funDeleteFile, funUploadFile, prisma } from "@/module/_global";
import { funGetUserByCookies } from "@/module/auth"; import { funGetUserByCookies } from "@/module/auth";
import { createLogUser } from "@/module/user"; import { createLogUser } from "@/module/user";
import _ from "lodash"; import _, { update } from "lodash";
import { NextResponse } from "next/server"; import { NextResponse } from "next/server";
import path from "path"; import path from "path";
import fs from "fs"; import fs from "fs";
@@ -203,22 +203,17 @@ export async function PUT(request: Request, context: { params: { id: string } })
}); });
if (String(file) != "undefined" && String(file) != "null") { if (String(file) != "undefined" && String(file) != "null") {
fs.unlink(`./public/image/user/${updates.img}`, (err) => { })
const root = path.join(process.cwd(), "./public/image/user/");
const fExt = file.name.split(".").pop() const fExt = file.name.split(".").pop()
const fileName = id + '.' + fExt; const fileName = id + '.' + fExt;
const filePath = path.join(root, fileName); const newFile = new File([file], fileName, { type: file.type });
await funDeleteFile({ fileId: String(updates.img) })
// Konversi ArrayBuffer ke Buffer const upload = await funUploadFile({ file: newFile, dirId: DIR.user })
const buffer = Buffer.from(await file.arrayBuffer());
fs.writeFileSync(filePath, buffer);
await prisma.user.update({ await prisma.user.update({
where: { where: {
id: id id: id
}, },
data: { data: {
img: fileName img: upload.data.id
} }
}) })
} }

View File

@@ -1,4 +1,4 @@
import { funUploadFile, prisma } from "@/module/_global"; import { DIR, funUploadFile, prisma } from "@/module/_global";
import { funGetUserByCookies } from "@/module/auth"; import { funGetUserByCookies } from "@/module/auth";
import { createLogUser } from "@/module/user"; import { createLogUser } from "@/module/user";
import _ from "lodash"; import _ from "lodash";
@@ -141,27 +141,17 @@ export async function POST(request: Request) {
const fExt = file.name.split(".").pop() const fExt = file.name.split(".").pop()
const fileName = user.id + '.' + fExt; const fileName = user.id + '.' + fExt;
const newFile = new File([file], fileName, { type: file.type }); const newFile = new File([file], fileName, { type: file.type });
await funUploadFile({ file: newFile, dirId: "cm0x8dbwn0005bp5tgmfcthzw" }) const upload = await funUploadFile({ file: newFile, dirId: DIR.user })
if (upload.success) {
// const root = path.join(process.cwd(), "./public/image/user/"); await prisma.user.update({
// const fExt = file.name.split(".").pop() where: {
// const fileName = users.id + '.' + fExt; id: users.id
// const filePath = path.join(root, fileName); },
data: {
// // Konversi ArrayBuffer ke Buffer img: upload.data.id
// const buffer = Buffer.from(await file.arrayBuffer()); }
})
// // Tulis file ke sistem }
// fs.writeFileSync(filePath, buffer);
// await prisma.user.update({
// where: {
// id: users.id
// },
// data: {
// img: fileName
// }
// })
} }
// create log user // create log user

View File

@@ -442,7 +442,7 @@ export default function CreateMember() {
onBlur={() => setTouched({ ...touched, gender: true })} onBlur={() => setTouched({ ...touched, gender: true })}
error={ error={
touched.gender && ( touched.gender && (
listData.gender == "" ? "Gender Tidak Boleh Kosong" : null listData.gender == "" ? "Jenis Kelamin Tidak Boleh Kosong" : null
) )
} }
/> />