upd: dokumen divisi
Deskripsi: - uplaod file No Issues
This commit is contained in:
@@ -181,7 +181,7 @@ export async function GET(request: Request) {
|
|||||||
allData.push(...formatDataShare)
|
allData.push(...formatDataShare)
|
||||||
}
|
}
|
||||||
|
|
||||||
const formatData = _.orderBy(allData, ['category', 'name'])
|
const formatData = _.orderBy(allData, ['category', 'name'], ['desc', 'asc']);
|
||||||
|
|
||||||
let pathNow = path
|
let pathNow = path
|
||||||
let jalur = []
|
let jalur = []
|
||||||
|
|||||||
100
src/app/api/document/upload/route.ts
Normal file
100
src/app/api/document/upload/route.ts
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
import { prisma } from "@/module/_global";
|
||||||
|
import { funGetUserByCookies } from "@/module/auth";
|
||||||
|
import _ from "lodash";
|
||||||
|
import { NextResponse } from "next/server";
|
||||||
|
import fs from "fs";
|
||||||
|
import path from "path";
|
||||||
|
|
||||||
|
|
||||||
|
// UPLOAD FILE
|
||||||
|
export async function POST(request: Request) {
|
||||||
|
try {
|
||||||
|
const user = await funGetUserByCookies()
|
||||||
|
if (user.id == undefined) {
|
||||||
|
return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 401 });
|
||||||
|
}
|
||||||
|
|
||||||
|
const body = await request.formData()
|
||||||
|
const dataBody = body.get("data")
|
||||||
|
const file = body.get("file") as File
|
||||||
|
const fileName = file.name
|
||||||
|
|
||||||
|
|
||||||
|
const { idPath, idDivision } = JSON.parse(dataBody as string)
|
||||||
|
|
||||||
|
const cekDivision = await prisma.division.count({
|
||||||
|
where: {
|
||||||
|
id: String(idDivision),
|
||||||
|
isActive: true
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
if (cekDivision == 0) {
|
||||||
|
return NextResponse.json({ success: false, message: "Gagal mendapatkan divisi, data tidak ditemukan" }, { status: 404 });
|
||||||
|
}
|
||||||
|
|
||||||
|
if (idPath != "home") {
|
||||||
|
const cekPath = await prisma.divisionDocumentFolderFile.count({
|
||||||
|
where: {
|
||||||
|
isActive: true,
|
||||||
|
id: idPath
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
if (cekPath == 0) {
|
||||||
|
return NextResponse.json({ success: false, message: "Gagal mendapatkan path, data tidak ditemukan" }, { status: 404 });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const nameFile = await prisma.divisionDocumentFolderFile.findMany({
|
||||||
|
where: {
|
||||||
|
idDivision,
|
||||||
|
path: idPath,
|
||||||
|
category: "FILE",
|
||||||
|
isActive: true
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
const dataOmit = nameFile.map((v: any) => ({
|
||||||
|
..._.omit(v, [""]),
|
||||||
|
file: v.name + '.' + v.extension,
|
||||||
|
}))
|
||||||
|
|
||||||
|
const cek = dataOmit.some((i: any) => i.file == fileName)
|
||||||
|
|
||||||
|
if (cek) {
|
||||||
|
return NextResponse.json({ success: false, message: "Terdapat file dengan nama yang sama" }, { status: 400 });
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
const fExt = file.name.split(".").pop()
|
||||||
|
const fName = file.name.replace("." + fExt, "")
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
return NextResponse.json({ success: true, message: "Berhasil upload file" }, { status: 200 });
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error);
|
||||||
|
return NextResponse.json({ success: false, message: "Gagal upload file, coba lagi nanti", reason: (error as Error).message, }, { status: 500 });
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -77,4 +77,12 @@ export const funShareDocument = async (data: IShareDocument) => {
|
|||||||
body: JSON.stringify(data),
|
body: JSON.stringify(data),
|
||||||
});
|
});
|
||||||
return await response.json().catch(() => null);
|
return await response.json().catch(() => null);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const funUploadFileDocument = async (data: FormData) => {
|
||||||
|
const response = await fetch(`/api/document/upload`, {
|
||||||
|
method: "POST",
|
||||||
|
body: data,
|
||||||
|
});
|
||||||
|
return await response.json().catch(() => null);
|
||||||
|
}
|
||||||
@@ -2,14 +2,16 @@
|
|||||||
import { LayoutDrawer, WARNA } from '@/module/_global';
|
import { LayoutDrawer, WARNA } from '@/module/_global';
|
||||||
import { ActionIcon, Box, Button, Divider, Flex, Grid, Modal, SimpleGrid, Stack, Text, TextInput } from '@mantine/core';
|
import { ActionIcon, Box, Button, Divider, Flex, Grid, Modal, SimpleGrid, Stack, Text, TextInput } from '@mantine/core';
|
||||||
import { useParams, useRouter, useSearchParams } from 'next/navigation';
|
import { useParams, useRouter, useSearchParams } from 'next/navigation';
|
||||||
import React, { useState } from 'react';
|
import React, { useRef, useState } from 'react';
|
||||||
import toast from 'react-hot-toast';
|
import toast from 'react-hot-toast';
|
||||||
import { FaFolderClosed, FaRegImage } from 'react-icons/fa6';
|
import { FaFolderClosed, FaRegImage } from 'react-icons/fa6';
|
||||||
import { HiDocumentText } from 'react-icons/hi2';
|
import { HiDocumentText } from 'react-icons/hi2';
|
||||||
import { IoAddCircle, IoDocumentText } from 'react-icons/io5';
|
import { IoAddCircle, IoDocumentText } from 'react-icons/io5';
|
||||||
import { funCreateFolder } from '../lib/api_document';
|
import { funCreateFolder, funUploadFileDocument } from '../lib/api_document';
|
||||||
import { useHookstate } from '@hookstate/core';
|
import { useHookstate } from '@hookstate/core';
|
||||||
import { globalRefreshDocument } from '../lib/val_document';
|
import { globalRefreshDocument } from '../lib/val_document';
|
||||||
|
import { Dropzone } from '@mantine/dropzone';
|
||||||
|
import _ from 'lodash';
|
||||||
|
|
||||||
export default function DrawerMenuDocumentDivision() {
|
export default function DrawerMenuDocumentDivision() {
|
||||||
const [openDrawerDocument, setOpenDrawerDocument] = useState(false)
|
const [openDrawerDocument, setOpenDrawerDocument] = useState(false)
|
||||||
@@ -19,6 +21,8 @@ export default function DrawerMenuDocumentDivision() {
|
|||||||
const searchParams = useSearchParams()
|
const searchParams = useSearchParams()
|
||||||
const path = searchParams.get('path')
|
const path = searchParams.get('path')
|
||||||
const refresh = useHookstate(globalRefreshDocument)
|
const refresh = useHookstate(globalRefreshDocument)
|
||||||
|
const openRef = useRef<() => void>(null)
|
||||||
|
const [fileForm, setFileForm] = useState<any>()
|
||||||
|
|
||||||
const [bodyFolder, setBodyFolder] = useState({
|
const [bodyFolder, setBodyFolder] = useState({
|
||||||
name: '',
|
name: '',
|
||||||
@@ -42,6 +46,29 @@ export default function DrawerMenuDocumentDivision() {
|
|||||||
setOpenDrawerDocument(false)
|
setOpenDrawerDocument(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function onUploadFile(data: any) {
|
||||||
|
try {
|
||||||
|
const fd = new FormData()
|
||||||
|
fd.append(`file`, data)
|
||||||
|
fd.append("data", JSON.stringify({
|
||||||
|
idPath: (path == undefined || path == '' || path == null) ? 'home' : path,
|
||||||
|
idDivision: param.id
|
||||||
|
}))
|
||||||
|
|
||||||
|
const res = await funUploadFileDocument(fd)
|
||||||
|
if (!res.success) {
|
||||||
|
toast.error(res.message)
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
toast.error("Gagal upload file, coba lagi nanti");
|
||||||
|
}
|
||||||
|
|
||||||
|
refresh.set(true)
|
||||||
|
setOpenModal(false)
|
||||||
|
setOpenDrawerDocument(false)
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Box>
|
<Box>
|
||||||
<Stack pt={10}>
|
<Stack pt={10}>
|
||||||
@@ -59,11 +86,13 @@ export default function DrawerMenuDocumentDivision() {
|
|||||||
</Flex>
|
</Flex>
|
||||||
</SimpleGrid>
|
</SimpleGrid>
|
||||||
</Stack>
|
</Stack>
|
||||||
<LayoutDrawer opened={openDrawerDocument} onClose={() => setOpenDrawerDocument(false)} title={''} size='lg' >
|
|
||||||
|
<LayoutDrawer opened={openDrawerDocument} onClose={() => setOpenDrawerDocument(false)} title={''}>
|
||||||
<SimpleGrid
|
<SimpleGrid
|
||||||
cols={{ base: 2, sm: 2, lg: 2 }}
|
cols={{ base: 2, sm: 2, lg: 2 }}
|
||||||
onClick={() => setOpenDrawerDocument(true)}
|
onClick={() => setOpenDrawerDocument(true)}
|
||||||
>
|
>
|
||||||
|
|
||||||
<Flex onClick={() => setOpenModal(true)} justify={'center'} align={'center'} direction={'column'} mb={20} >
|
<Flex onClick={() => setOpenModal(true)} justify={'center'} align={'center'} direction={'column'} mb={20} >
|
||||||
<Box>
|
<Box>
|
||||||
<ActionIcon variant="filled" color="#DFE8EA" size={61} radius="xl" aria-label="Settings">
|
<ActionIcon variant="filled" color="#DFE8EA" size={61} radius="xl" aria-label="Settings">
|
||||||
@@ -74,17 +103,33 @@ export default function DrawerMenuDocumentDivision() {
|
|||||||
<Text c={WARNA.biruTua}>Membuat Folder</Text>
|
<Text c={WARNA.biruTua}>Membuat Folder</Text>
|
||||||
</Box>
|
</Box>
|
||||||
</Flex>
|
</Flex>
|
||||||
<Flex justify={'center'} align={'center'} direction={'column'} mb={20}>
|
<Dropzone
|
||||||
<Box>
|
openRef={openRef}
|
||||||
<ActionIcon variant="filled" color="#DFE8EA" size={61} radius="xl" aria-label="Settings">
|
onDrop={async (files) => {
|
||||||
<HiDocumentText size={40} color={WARNA.biruTua} />
|
if (!files || _.isEmpty(files))
|
||||||
</ActionIcon>
|
return toast.error('Tidak ada file yang dipilih')
|
||||||
</Box>
|
onUploadFile(files[0])
|
||||||
<Box mt={10}>
|
}}
|
||||||
<Text c={WARNA.biruTua}>Upload Dokumen</Text>
|
activateOnClick={false}
|
||||||
</Box>
|
maxSize={3 * 1024 ** 2}
|
||||||
</Flex>
|
accept={['text/csv', 'image/png', 'image/jpeg', 'image/heic', 'application/pdf']}
|
||||||
<Flex justify={'center'} align={'center'} direction={'column'} mb={20} >
|
onReject={(files) => {
|
||||||
|
return 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?.()}>
|
||||||
|
<Box>
|
||||||
|
<ActionIcon variant="filled" color="#DFE8EA" size={61} radius="xl" aria-label="Settings">
|
||||||
|
<HiDocumentText size={40} color={WARNA.biruTua} />
|
||||||
|
</ActionIcon>
|
||||||
|
</Box>
|
||||||
|
<Box mt={10}>
|
||||||
|
<Text c={WARNA.biruTua}>Upload File</Text>
|
||||||
|
</Box>
|
||||||
|
</Flex>
|
||||||
|
</Dropzone>
|
||||||
|
|
||||||
|
{/* <Flex justify={'center'} align={'center'} direction={'column'} mb={20} >
|
||||||
<Box>
|
<Box>
|
||||||
<ActionIcon variant="filled" color="#DFE8EA" size={61} radius="xl" aria-label="Settings">
|
<ActionIcon variant="filled" color="#DFE8EA" size={61} radius="xl" aria-label="Settings">
|
||||||
<FaRegImage size={40} color={WARNA.biruTua} />
|
<FaRegImage size={40} color={WARNA.biruTua} />
|
||||||
@@ -93,7 +138,7 @@ export default function DrawerMenuDocumentDivision() {
|
|||||||
<Box mt={10}>
|
<Box mt={10}>
|
||||||
<Text c={WARNA.biruTua}>Upload Foto</Text>
|
<Text c={WARNA.biruTua}>Upload Foto</Text>
|
||||||
</Box>
|
</Box>
|
||||||
</Flex>
|
</Flex> */}
|
||||||
</SimpleGrid>
|
</SimpleGrid>
|
||||||
</LayoutDrawer>
|
</LayoutDrawer>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user