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())
|
id String @id @default(cuid())
|
||||||
Division Division @relation(fields: [idDivision], references: [id])
|
Division Division @relation(fields: [idDivision], references: [id])
|
||||||
idDivision String
|
idDivision String
|
||||||
|
idStorage String?
|
||||||
category String @default("FOLDER") // FOLDER OR FILE
|
category String @default("FOLDER") // FOLDER OR FILE
|
||||||
name String
|
name String
|
||||||
extension String
|
extension String
|
||||||
|
|||||||
@@ -129,6 +129,7 @@ export async function GET(request: Request) {
|
|||||||
|
|
||||||
formatDataShare = dataShare.map((v: any) => ({
|
formatDataShare = dataShare.map((v: any) => ({
|
||||||
..._.omit(v, ["DivisionDocumentFolderFile"]),
|
..._.omit(v, ["DivisionDocumentFolderFile"]),
|
||||||
|
idStorage: '',
|
||||||
id: v.DivisionDocumentFolderFile.id,
|
id: v.DivisionDocumentFolderFile.id,
|
||||||
category: v.DivisionDocumentFolderFile.category,
|
category: v.DivisionDocumentFolderFile.category,
|
||||||
name: v.DivisionDocumentFolderFile.name,
|
name: v.DivisionDocumentFolderFile.name,
|
||||||
@@ -156,6 +157,7 @@ export async function GET(request: Request) {
|
|||||||
category: true,
|
category: true,
|
||||||
name: true,
|
name: true,
|
||||||
extension: true,
|
extension: true,
|
||||||
|
idStorage: true,
|
||||||
path: true,
|
path: true,
|
||||||
User: {
|
User: {
|
||||||
select: {
|
select: {
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { prisma } from "@/module/_global";
|
import { DIR, funUploadFile, prisma } from "@/module/_global";
|
||||||
import { funGetUserByCookies } from "@/module/auth";
|
import { funGetUserByCookies } from "@/module/auth";
|
||||||
import _ from "lodash";
|
import _ from "lodash";
|
||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
@@ -70,33 +70,29 @@ export async function POST(request: Request) {
|
|||||||
|
|
||||||
const fExt = file.name.split(".").pop()
|
const fExt = file.name.split(".").pop()
|
||||||
const fName = file.name.replace("." + fExt, "")
|
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({
|
// create log user
|
||||||
data: {
|
const log = await createLogUser({ act: 'CREATE', desc: 'User mengupload file baru', table: 'divisionDocumentFolderFile', data: dataInsert.id })
|
||||||
name: fName,
|
return NextResponse.json({ success: true, message: "Berhasil upload file" }, { status: 200 });
|
||||||
path: idPath,
|
} else {
|
||||||
idDivision,
|
return NextResponse.json({ success: false, message: "Gagal upload file, coba lagi nanti" }, { status: 400 });
|
||||||
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 });
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
return NextResponse.json({ success: false, message: "Gagal upload file, coba lagi nanti", reason: (error as Error).message, }, { status: 500 });
|
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 {
|
export interface IDataDocument {
|
||||||
|
idStorage: string;
|
||||||
id: string;
|
id: string;
|
||||||
name: string;
|
name: string;
|
||||||
extension: string;
|
extension: string;
|
||||||
|
|||||||
@@ -114,7 +114,10 @@ export default function DrawerMenuDocumentDivision() {
|
|||||||
maxSize={3 * 1024 ** 2}
|
maxSize={3 * 1024 ** 2}
|
||||||
accept={['text/csv', 'image/png', 'image/jpeg', 'image/heic', 'application/pdf']}
|
accept={['text/csv', 'image/png', 'image/jpeg', 'image/heic', 'application/pdf']}
|
||||||
onReject={(files) => {
|
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?.()}>
|
<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 param = useParams<{ id: string }>()
|
||||||
const [isOpenModalView, setOpenModalView] = useState(false)
|
const [isOpenModalView, setOpenModalView] = useState(false)
|
||||||
const [isExtension, setExtension] = useState('')
|
const [isExtension, setExtension] = useState('')
|
||||||
const [idData, setIdData] = useState('')
|
const [idStorage, setIdStorage] = useState('')
|
||||||
const [name, setName] = useState('')
|
const [name, setName] = useState('')
|
||||||
const [isOpen, setOpen] = useState(false)
|
const [isOpen, setOpen] = useState(false)
|
||||||
const [isDelete, setIsDelete] = useState(false)
|
const [isDelete, setIsDelete] = useState(false)
|
||||||
@@ -337,7 +337,7 @@ export default function NavbarDocumentDivision() {
|
|||||||
router.push('?path=' + v.id)
|
router.push('?path=' + v.id)
|
||||||
} else if (v.category == "FILE" && selectedFiles.length == 0 && !dariSelectAll) {
|
} else if (v.category == "FILE" && selectedFiles.length == 0 && !dariSelectAll) {
|
||||||
setExtension(v.extension)
|
setExtension(v.extension)
|
||||||
setIdData(v.id)
|
setIdStorage(v.idStorage)
|
||||||
setOpenModalView(true)
|
setOpenModalView(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -380,7 +380,7 @@ export default function NavbarDocumentDivision() {
|
|||||||
router.push('?path=' + v.id)
|
router.push('?path=' + v.id)
|
||||||
} else if (v.category == "FILE" && selectedFiles.length == 0 && !dariSelectAll) {
|
} else if (v.category == "FILE" && selectedFiles.length == 0 && !dariSelectAll) {
|
||||||
setExtension(v.extension)
|
setExtension(v.extension)
|
||||||
setIdData(v.id)
|
setIdStorage(v.idStorage)
|
||||||
setOpenModalView(true)
|
setOpenModalView(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -485,7 +485,7 @@ export default function NavbarDocumentDivision() {
|
|||||||
</LayoutDrawer>
|
</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>
|
</Box>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user