diff --git a/src/app/api/document/more/route.ts b/src/app/api/document/more/route.ts index 89b5bfc..20a697c 100644 --- a/src/app/api/document/more/route.ts +++ b/src/app/api/document/more/route.ts @@ -1,4 +1,4 @@ -import { prisma } from "@/module/_global"; +import { DIR, funCopyFile, prisma } from "@/module/_global"; import { funGetUserByCookies } from "@/module/auth"; import { createLogUser } from "@/module/user"; import _ from "lodash"; @@ -106,85 +106,55 @@ export async function PUT(request: Request) { let name = dataItem[i].name; const category = dataItem[i].category; const extension = dataItem[i].extension; + const idStorage = dataItem[i].idStorage; - let status = false - let numb = 1 - do { - const cekName = await prisma.divisionDocumentFolderFile.count({ - where: { - path: path, - isActive: true, + const copyOnStorage = await funCopyFile({ fileId: idStorage, dirId: DIR.document }) + if (copyOnStorage.success) { + let status = false + let numb = 1 + do { + const cekName = await prisma.divisionDocumentFolderFile.count({ + where: { + path: path, + isActive: true, + extension, + name + } + }) + + if (cekName > 0) { + name = dataItem[i].name + " (" + numb + ")" + numb++ + status = false + } else { + status = true + } + } while (status == false); + + + const create = await prisma.divisionDocumentFolderFile.create({ + data: { + name, + path, + idDivision, + category, extension, - name + idStorage: copyOnStorage.data.id, + createdBy: user.id + }, + select: { + id: true } }) - if (cekName > 0) { - name = dataItem[i].name + " (" + numb + ")" - numb++ - status = false - } else { - status = true - } - } while (status == false); - - - const create = await prisma.divisionDocumentFolderFile.create({ - data: { - name, - path, - idDivision, - category, - extension, - createdBy: user.id - }, - select: { - id: true - } - }) - - // let newPath = create.id - // let idPath = dataItem[i].id; - // let statusCek = false - // do { - // const cekFolder = await prisma.divisionDocumentFolderFile.findMany({ - // where: { - // isActive: true, - // path: idPath - // } - // }) - - // if (cekFolder.length == 0) { - // statusCek = true - // } else { - // for (let index = 0; index < cekFolder.length; index++) { - // const addChildren = await prisma.divisionDocumentFolderFile.create({ - // data: { - // name: cekFolder[index].name, - // path: newPath, - // idDivision, - // category: cekFolder[index].category, - // extension: cekFolder[index].extension, - // createdBy: user.id - // }, - // select: { - // id: true - // } - // }) - - // newPath = create.id - // } - - - // } - - - // } while (statusCek == false); - + // create log user + const log = await createLogUser({ act: 'CREATE', desc: 'User menyalin file', table: 'divisionDocumentFolderFile', data: create.id }) + } } - return NextResponse.json({ success: true, message: "Berhasil salin item" }, { status: 200 }); + + } catch (error) { console.error(error); return NextResponse.json({ success: false, message: "Gagal salin item, coba lagi nanti", reason: (error as Error).message, }, { status: 500 }); @@ -222,7 +192,8 @@ export async function DELETE(request: Request) { } - + // create log user + const log = await createLogUser({ act: 'CREATE', desc: 'User membagikan item', table: 'divisionDocumentShare', data: '' }) return NextResponse.json({ success: true, message: "Berhasil membagikan item" }, { status: 200 }); } catch (error) { console.error(error); diff --git a/src/module/_global/fun/copy_file.ts b/src/module/_global/fun/copy_file.ts new file mode 100644 index 0000000..eabce8f --- /dev/null +++ b/src/module/_global/fun/copy_file.ts @@ -0,0 +1,22 @@ +export async function funCopyFile({ fileId, dirId }: { fileId: string, dirId: string }) { + try { + const res = await fetch(`https://wibu-storage.wibudev.com/api/files/copy/${dirId}/${dirId}`, { + method: "POST", + body: JSON.stringify({ fileId: fileId }), + headers: { + Authorization: `Bearer ${process.env.WS_APIKEY}` + } + }); + + if (res.ok) { + const hasil = await res.json() + return { success: true, data: hasil.data } + } else { + const errorText = await res.json(); + return { success: false, data: {} } + } + } catch (error) { + console.error("Copy error:", error); + return { success: false, data: {} } + } +} \ No newline at end of file diff --git a/src/module/_global/index.ts b/src/module/_global/index.ts index c29f0b3..bcdf19e 100644 --- a/src/module/_global/index.ts +++ b/src/module/_global/index.ts @@ -6,6 +6,7 @@ import SkeletonDetailListTugasTask from "./components/skeleton_detail_list_tugas import SkeletonDetailProfile from "./components/skeleton_detail_profile"; import SkeletonSingle from "./components/skeleton_single"; import WrapLayout from "./components/wrap_layout"; +import { funCopyFile } from "./fun/copy_file"; import { funDeleteFile } from "./fun/delete_file"; import { funUploadFile } from "./fun/upload_file"; import { WARNA } from "./fun/WARNA"; @@ -42,3 +43,4 @@ export { funUploadFile } export { funDeleteFile } export { DIR } export { TEMA } +export { funCopyFile } diff --git a/src/module/auth/api/funDetectCookies.ts b/src/module/auth/api/funDetectCookies.ts index e8fd0d4..2500fd6 100644 --- a/src/module/auth/api/funDetectCookies.ts +++ b/src/module/auth/api/funDetectCookies.ts @@ -7,9 +7,13 @@ import { cookies } from "next/headers" export default async function funDetectCookies() { const cookiesnya = cookies() const c = cookiesnya.get("sessionCookieSDM") - if (!c || _.isUndefined(c) || !c.value || _.isEmpty(c.value)) return false + if (!c || _.isUndefined(c) || !c.value || _.isEmpty(c.value)) { + return false + } const dataCookies = await unsealData(c!.value, { password: pwd_key_config as string }) - if (_.isEmpty(_.toString(dataCookies))) return false + if (_.isEmpty(dataCookies)) { + return false + } return true } \ No newline at end of file diff --git a/src/module/document/ui/drawer_more.tsx b/src/module/document/ui/drawer_more.tsx index d70d093..97ff2f8 100644 --- a/src/module/document/ui/drawer_more.tsx +++ b/src/module/document/ui/drawer_more.tsx @@ -9,12 +9,14 @@ import { funCopyDocument, funMoveDocument } from "../lib/api_document"; import { useHookstate } from "@hookstate/core"; import { globalRefreshDocument } from "../lib/val_document"; import { useParams } from "next/navigation"; +import { useShallowEffect } from "@mantine/hooks"; export default function DrawerMore({ data }: { data: IDataDocument[] }) { const [isCut, setIsCut] = useState(false) const [isCopy, setIsCopy] = useState(false) const refresh = useHookstate(globalRefreshDocument) const param = useParams<{ id: string }>() + const [forbidCopy, setForbidCopy] = useState(true) async function onMoveItem(path: string) { @@ -51,6 +53,16 @@ export default function DrawerMore({ data }: { data: IDataDocument[] }) { } + function cekFileSelected() { + const cek = data.some((i: any) => i.category == "FOLDER") + setForbidCopy(cek) + } + + useShallowEffect(() => { + cekFileSelected() + }, [data]) + + return ( @@ -66,14 +78,17 @@ export default function DrawerMore({ data }: { data: IDataDocument[] }) { Pindah - setIsCopy(true)} justify={'center'} align={'center'} direction={'column'} > - - - - - Salin - - + { + (!forbidCopy) && + setIsCopy(true)} justify={'center'} align={'center'} direction={'column'} > + + + + + Salin + + + } diff --git a/src/module/document/ui/navbar_document_division.tsx b/src/module/document/ui/navbar_document_division.tsx index 8e09dec..bc07c04 100644 --- a/src/module/document/ui/navbar_document_division.tsx +++ b/src/module/document/ui/navbar_document_division.tsx @@ -38,6 +38,7 @@ export default function NavbarDocumentDivision() { const [share, setShare] = useState(false) const [more, setMore] = useState(false) const [shareSelected, setShareSelected] = useState(false) + const [copyAllowed, setCopyAllowed] = useState(true) const searchParams = useSearchParams() const path = searchParams.get('path') const [dataDocument, setDataDocument] = useState([]) @@ -46,7 +47,7 @@ export default function NavbarDocumentDivision() { const [selectedFiles, setSelectedFiles] = useState([]) const [selectAll, setSelectAll] = useState(false) const [dariSelectAll, setDariSelectAll] = useState(false) - const isMobile = useMediaQuery('(max-width: 369px)'); + const isMobile = useMediaQuery('(max-width: 369px)'); const [bodyRename, setBodyRename] = useState({ id: '', name: '', @@ -69,6 +70,7 @@ export default function NavbarDocumentDivision() { extension: dataDocument[index].extension, category: dataDocument[index].category, share: dataDocument[index].share, + idStorage: dataDocument[index].idStorage } ]) } @@ -88,6 +90,13 @@ export default function NavbarDocumentDivision() { } else { setShareSelected(false) } + + const cek = selectedFiles.some((i: any) => i?.category == 'FOLDER') + if (cek || shareSelected || selectedFiles.length > 1) { + setCopyAllowed(false) + } else { + setCopyAllowed(true) + } } const handleSelectAll = () => { @@ -102,6 +111,7 @@ export default function NavbarDocumentDivision() { extension: dataDocument[index].extension, category: dataDocument[index].category, share: dataDocument[index].share, + idStorage: dataDocument[index].idStorage } setSelectedFiles((selectedFiles: any) => [...selectedFiles, newArr]) } @@ -216,6 +226,28 @@ export default function NavbarDocumentDivision() { setRename(true) } + const onDownload = async () => { + try { + const fileUrl = `https://wibu-storage.wibudev.com/api/files/${selectedFiles[0].idStorage}`; + const response = await fetch(fileUrl); + const blob = await response.blob(); + + // Create a link element, use Blob URL + const link = document.createElement("a"); + const url = window.URL.createObjectURL(blob); + link.href = url; + link.download = `${selectedFiles[0].name}.${selectedFiles[0].extension}`; // Nama file yang akan diunduh + document.body.appendChild(link); + link.click(); + + // Cleanup + window.URL.revokeObjectURL(url); + document.body.removeChild(link); + } catch (error) { + alert(error); + } + }; + return ( {(selectedFiles.length > 0 || dariSelectAll) && ( @@ -245,9 +277,14 @@ export default function NavbarDocumentDivision() { }}> - - 0) ? 'white' : '#656060'} /> - 0) ? 'white' : '#656060'}>Unduh + { + if ((selectedFiles.length > 0 && copyAllowed)) { + onDownload() + } + }}> + 0 && copyAllowed) ? 'white' : '#656060'} /> + 0 && copyAllowed) ? 'white' : '#656060'}>Unduh