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 { createLogUser } from "@/module/user";
import _ from "lodash";
import _, { update } from "lodash";
import { NextResponse } from "next/server";
import path from "path";
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") {
fs.unlink(`./public/image/user/${updates.img}`, (err) => { })
const root = path.join(process.cwd(), "./public/image/user/");
const fExt = file.name.split(".").pop()
const fileName = id + '.' + fExt;
const filePath = path.join(root, fileName);
// Konversi ArrayBuffer ke Buffer
const buffer = Buffer.from(await file.arrayBuffer());
fs.writeFileSync(filePath, buffer);
const newFile = new File([file], fileName, { type: file.type });
await funDeleteFile({ fileId: String(updates.img) })
const upload = await funUploadFile({ file: newFile, dirId: DIR.user })
await prisma.user.update({
where: {
id: id
},
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 { createLogUser } from "@/module/user";
import _ from "lodash";
@@ -141,27 +141,17 @@ export async function POST(request: Request) {
const fExt = file.name.split(".").pop()
const fileName = user.id + '.' + fExt;
const newFile = new File([file], fileName, { type: file.type });
await funUploadFile({ file: newFile, dirId: "cm0x8dbwn0005bp5tgmfcthzw" })
// const root = path.join(process.cwd(), "./public/image/user/");
// const fExt = file.name.split(".").pop()
// const fileName = users.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: users.id
// },
// data: {
// img: fileName
// }
// })
const upload = await funUploadFile({ file: newFile, dirId: DIR.user })
if (upload.success) {
await prisma.user.update({
where: {
id: users.id
},
data: {
img: upload.data.id
}
})
}
}
// create log user

View File

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