upd: upload file

This commit is contained in:
amel
2024-09-11 16:46:45 +08:00
parent 57c245a880
commit 5ee3cb791c
7 changed files with 80 additions and 43 deletions

View File

@@ -1,4 +1,4 @@
import { prisma } from "@/module/_global";
import { funDeleteFile, funUploadFile, prisma } from "@/module/_global";
import { funGetUserByCookies } from "@/module/auth";
import _ from "lodash";
import { NextResponse } from "next/server";
@@ -118,26 +118,35 @@ export async function PUT(request: Request) {
})
if (String(file) != "undefined" && String(file) != "null") {
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);
// const fileName = user.id + '.' + fExt;
const fileName = 'COBAAYAA.' + 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" })
// Konversi ArrayBuffer ke Buffer
const buffer = Buffer.from(await file.arrayBuffer());
// Tulis file ke sistem
fs.writeFileSync(filePath, buffer);
// 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);
await prisma.user.update({
where: {
id: user.id
},
data: {
img: 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 })

View File

@@ -1,4 +1,4 @@
import { prisma } from "@/module/_global";
import { funUploadFile, prisma } from "@/module/_global";
import { funGetUserByCookies } from "@/module/auth";
import { createLogUser } from "@/module/user";
import _ from "lodash";
@@ -137,25 +137,31 @@ export async function POST(request: Request) {
});
if (String(file) != "undefined" && String(file) != "null") {
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);
const fileName = user.id + '.' + fExt;
const newFile = new File([file], fileName, { type: file.type });
await funUploadFile({ file: newFile, dirId: "cm0x8dbwn0005bp5tgmfcthzw" })
// Konversi ArrayBuffer ke Buffer
const buffer = Buffer.from(await file.arrayBuffer());
// 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);
// Tulis file ke sistem
fs.writeFileSync(filePath, buffer);
// // Konversi ArrayBuffer ke Buffer
// const buffer = Buffer.from(await file.arrayBuffer());
await prisma.user.update({
where: {
id: users.id
},
data: {
img: fileName
}
})
// // Tulis file ke sistem
// fs.writeFileSync(filePath, buffer);
// await prisma.user.update({
// where: {
// id: users.id
// },
// data: {
// img: fileName
// }
// })
}
// create log user