upd: dokumen
Deskripsi: - upload file - view file No Issues
This commit is contained in:
@@ -364,6 +364,7 @@ model DivisionDocumentFolderFile {
|
||||
id String @id @default(cuid())
|
||||
Division Division @relation(fields: [idDivision], references: [id])
|
||||
idDivision String
|
||||
idStorage String?
|
||||
category String @default("FOLDER") // FOLDER OR FILE
|
||||
name String
|
||||
extension String
|
||||
|
||||
@@ -129,6 +129,7 @@ export async function GET(request: Request) {
|
||||
|
||||
formatDataShare = dataShare.map((v: any) => ({
|
||||
..._.omit(v, ["DivisionDocumentFolderFile"]),
|
||||
idStorage: '',
|
||||
id: v.DivisionDocumentFolderFile.id,
|
||||
category: v.DivisionDocumentFolderFile.category,
|
||||
name: v.DivisionDocumentFolderFile.name,
|
||||
@@ -156,6 +157,7 @@ export async function GET(request: Request) {
|
||||
category: true,
|
||||
name: true,
|
||||
extension: true,
|
||||
idStorage: true,
|
||||
path: true,
|
||||
User: {
|
||||
select: {
|
||||
|
||||
@@ -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 { NextResponse } from "next/server";
|
||||
@@ -70,33 +70,29 @@ export async function POST(request: Request) {
|
||||
|
||||
const fExt = file.name.split(".").pop()
|
||||
const fName = file.name.replace("." + fExt, "")
|
||||
const upload = await funUploadFile({ file: file, dirId: DIR.document })
|
||||
if (upload.success) {
|
||||
const dataInsert = await prisma.divisionDocumentFolderFile.create({
|
||||
data: {
|
||||
name: fName,
|
||||
path: idPath,
|
||||
idDivision,
|
||||
category: "FILE",
|
||||
extension: String(fExt),
|
||||
createdBy: user.id,
|
||||
idStorage: upload.data.id
|
||||
},
|
||||
select: {
|
||||
id: true
|
||||
}
|
||||
});
|
||||
|
||||
const dataInsert = await prisma.divisionDocumentFolderFile.create({
|
||||
data: {
|
||||
name: fName,
|
||||
path: idPath,
|
||||
idDivision,
|
||||
category: "FILE",
|
||||
extension: String(fExt),
|
||||
createdBy: user.id,
|
||||
},
|
||||
select: {
|
||||
id: true
|
||||
}
|
||||
});
|
||||
|
||||
const root = path.join(process.cwd(), "./public/file/dokumen/");
|
||||
const nameFix = dataInsert.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);
|
||||
|
||||
// create log user
|
||||
const log = await createLogUser({ act: 'CREATE', desc: 'User mengupload file baru', table: 'divisionDocumentFolderFile', data: dataInsert.id })
|
||||
|
||||
return NextResponse.json({ success: true, message: "Berhasil upload file" }, { status: 200 });
|
||||
// create log user
|
||||
const log = await createLogUser({ act: 'CREATE', desc: 'User mengupload file baru', table: 'divisionDocumentFolderFile', data: dataInsert.id })
|
||||
return NextResponse.json({ success: true, message: "Berhasil upload file" }, { status: 200 });
|
||||
} else {
|
||||
return NextResponse.json({ success: false, message: "Gagal upload file, coba lagi nanti" }, { status: 400 });
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return NextResponse.json({ success: false, message: "Gagal upload file, coba lagi nanti", reason: (error as Error).message, }, { status: 500 });
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
export interface IDataDocument {
|
||||
idStorage: string;
|
||||
id: string;
|
||||
name: string;
|
||||
extension: string;
|
||||
|
||||
@@ -114,7 +114,10 @@ export default function DrawerMenuDocumentDivision() {
|
||||
maxSize={3 * 1024 ** 2}
|
||||
accept={['text/csv', 'image/png', 'image/jpeg', 'image/heic', 'application/pdf']}
|
||||
onReject={(files) => {
|
||||
return toast.error('File yang diizinkan: .csv, .png, .jpg, .heic, .pdf dengan ukuran maksimal 3 MB')
|
||||
refresh.set(true)
|
||||
setOpenModal(false)
|
||||
setOpenDrawerDocument(false)
|
||||
toast.error('File yang diizinkan: .csv, .png, .jpg, .heic, .pdf dengan ukuran maksimal 3 MB')
|
||||
}}
|
||||
>
|
||||
<Flex justify={'center'} align={'center'} direction={'column'} mb={20} onClick={() => openRef.current?.()}>
|
||||
|
||||
@@ -30,7 +30,7 @@ export default function NavbarDocumentDivision() {
|
||||
const param = useParams<{ id: string }>()
|
||||
const [isOpenModalView, setOpenModalView] = useState(false)
|
||||
const [isExtension, setExtension] = useState('')
|
||||
const [idData, setIdData] = useState('')
|
||||
const [idStorage, setIdStorage] = useState('')
|
||||
const [name, setName] = useState('')
|
||||
const [isOpen, setOpen] = useState(false)
|
||||
const [isDelete, setIsDelete] = useState(false)
|
||||
@@ -337,7 +337,7 @@ export default function NavbarDocumentDivision() {
|
||||
router.push('?path=' + v.id)
|
||||
} else if (v.category == "FILE" && selectedFiles.length == 0 && !dariSelectAll) {
|
||||
setExtension(v.extension)
|
||||
setIdData(v.id)
|
||||
setIdStorage(v.idStorage)
|
||||
setOpenModalView(true)
|
||||
}
|
||||
|
||||
@@ -380,7 +380,7 @@ export default function NavbarDocumentDivision() {
|
||||
router.push('?path=' + v.id)
|
||||
} else if (v.category == "FILE" && selectedFiles.length == 0 && !dariSelectAll) {
|
||||
setExtension(v.extension)
|
||||
setIdData(v.id)
|
||||
setIdStorage(v.idStorage)
|
||||
setOpenModalView(true)
|
||||
}
|
||||
|
||||
@@ -485,7 +485,7 @@ export default function NavbarDocumentDivision() {
|
||||
</LayoutDrawer>
|
||||
|
||||
|
||||
<LayoutModalViewFile opened={isOpenModalView} onClose={() => setOpenModalView(false)} file={idData + '.' + isExtension} extension={isExtension} fitur='dokumen' />
|
||||
<LayoutModalViewFile opened={isOpenModalView} onClose={() => setOpenModalView(false)} file={idStorage} extension={isExtension} fitur='dokumen' />
|
||||
</Box>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user