diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 84cc75f..43b4898 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -200,6 +200,7 @@ model ProjectFile { idProject String name String extension String + idStorage String? isActive Boolean @default(true) createdAt DateTime @default(now()) updatedAt DateTime @updatedAt diff --git a/src/app/api/project/[id]/route.ts b/src/app/api/project/[id]/route.ts index 5ba9c8d..2d5a51e 100644 --- a/src/app/api/project/[id]/route.ts +++ b/src/app/api/project/[id]/route.ts @@ -91,7 +91,8 @@ export async function GET(request: Request, context: { params: { id: string } }) select: { id: true, name: true, - extension: true + extension: true, + idStorage: true } }) diff --git a/src/app/api/project/file/[id]/route.ts b/src/app/api/project/file/[id]/route.ts index 5f390d7..0d51d27 100644 --- a/src/app/api/project/file/[id]/route.ts +++ b/src/app/api/project/file/[id]/route.ts @@ -1,4 +1,4 @@ -import { prisma } from "@/module/_global"; +import { DIR, funDeleteFile, funUploadFile, prisma } from "@/module/_global"; import { funGetUserByCookies } from "@/module/auth"; import { NextResponse } from "next/server"; import fs from "fs"; @@ -36,8 +36,7 @@ export async function DELETE(request: Request, context: { params: { id: string } } }) - - fs.unlink(`./public/file/project/${dataRelasi?.id}.${dataRelasi?.extension}`, (err) => { }) + const delStorage = await funDeleteFile({ fileId: String(dataRelasi?.idStorage) }) const deleteRelasi = await prisma.projectFile.delete({ where: { @@ -166,31 +165,27 @@ export async function POST(request: Request, context: { params: { id: string } } if (cekFile) { - const root = path.join(process.cwd(), "./public/file/project/"); for (var pair of body.entries()) { if (String(pair[0]).substring(0, 4) == "file") { const file = body.get(pair[0]) as File const fExt = file.name.split(".").pop() const fName = file.name.replace("." + fExt, "") + const upload = await funUploadFile({ file: file, dirId: DIR.project }) + if (upload.success) { + const insertToTable = await prisma.projectFile.create({ + data: { + idStorage: upload.data.id, + idProject: id, + name: fName, + extension: String(fExt), - const insertToTable = await prisma.projectFile.create({ - data: { - idProject: id, - name: fName, - extension: String(fExt) - }, - select: { - id: true - } - }) - - const nameFix = insertToTable.id + '.' + fExt - const filePath = path.join(root, nameFix) - // Konversi ArrayBuffer ke Buffer - const buffer = Buffer.from(await file.arrayBuffer()); - // Tulis file ke sistem - fs.writeFileSync(filePath, buffer); + }, + select: { + id: true + } + }) + } } } } diff --git a/src/app/api/project/route.ts b/src/app/api/project/route.ts index 5ffa314..feec0b7 100644 --- a/src/app/api/project/route.ts +++ b/src/app/api/project/route.ts @@ -1,4 +1,4 @@ -import { prisma } from "@/module/_global"; +import { DIR, funUploadFile, prisma } from "@/module/_global"; import { funGetUserByCookies } from "@/module/auth"; import _ from "lodash"; import moment from "moment"; @@ -169,31 +169,22 @@ export async function POST(request: Request) { } if (cekFile) { - const root = path.join(process.cwd(), "./public/file/project/"); for (var pair of body.entries()) { if (String(pair[0]).substring(0, 4) == "file") { const file = body.get(pair[0]) as File const fExt = file.name.split(".").pop() const fName = file.name.replace("." + fExt, "") - - - const insertToTable = await prisma.projectFile.create({ - data: { - idProject: data.id, - name: fName, - extension: String(fExt) - }, - select: { - id: true - } - }) - - const nameFix = insertToTable.id + '.' + fExt - const filePath = path.join(root, nameFix) - // Konversi ArrayBuffer ke Buffer - const buffer = Buffer.from(await file.arrayBuffer()); - // Tulis file ke sistem - fs.writeFileSync(filePath, buffer); + const upload = await funUploadFile({ file: file, dirId: DIR.project }) + if (upload.success) { + await prisma.projectFile.create({ + data: { + idStorage: upload.data.id, + idProject: data.id, + name: fName, + extension: String(fExt) + } + }) + } } } } diff --git a/src/module/_global/layout/layout_modal_view_file.tsx b/src/module/_global/layout/layout_modal_view_file.tsx index a895d3c..4565f7f 100644 --- a/src/module/_global/layout/layout_modal_view_file.tsx +++ b/src/module/_global/layout/layout_modal_view_file.tsx @@ -55,7 +55,7 @@ export default function LayoutModal({ opened, onClose, extension, fitur, file }: }}>