diff --git a/components/Chat/Upload.tsx b/components/Chat/Upload.tsx index e16a70c..dae9520 100644 --- a/components/Chat/Upload.tsx +++ b/components/Chat/Upload.tsx @@ -65,7 +65,7 @@ export const Upload = ({ return true; }; - const supportFileType = "pdf, epub, docx, txt, md, csv, json"; + const supportFileType = "pdf, epub, docx, txt, md, csv, json, zip"; function validateFileType(fileType: string): boolean { switch (fileType) { @@ -75,6 +75,7 @@ export const Upload = ({ case "txt": case "md": case "csv": + case "zip": case "json": return true; default: @@ -121,13 +122,14 @@ export const Upload = ({ fileName: fileName, fileType: fileType, }) - }).then(res => { + }).then(async (res) => { if (!res.ok) { - console.log("save embedding failed:"); - throw new Error("save embedding failed`"); + const message = await res.text(); + console.log('save embedding failed: ', message); + throw new Error(`save embedding failed: ' ${message}`); } - }); + } const deleteFile = async (fileTempName: string) => { diff --git a/package-lock.json b/package-lock.json index 8fb537b..e98066c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16,6 +16,7 @@ "@radix-ui/react-tabs": "^1.0.4", "@supabase/supabase-js": "^2.21.0", "@tabler/icons-react": "^2.9.0", + "@types/adm-zip": "^0.5.0", "@types/multer": "^1.4.7", "class-variance-authority": "^0.6.0", "clsx": "^1.2.1", @@ -1065,6 +1066,14 @@ "node": ">= 10" } }, + "node_modules/@types/adm-zip": { + "version": "0.5.0", + "resolved": "https://registry.npmjs.org/@types/adm-zip/-/adm-zip-0.5.0.tgz", + "integrity": "sha512-FCJBJq9ODsQZUNURo5ILAQueuA8WJhRvuihS3ke2iI25mJlfV2LK8jG2Qj2z2AWg8U0FtWWqBHVRetceLskSaw==", + "dependencies": { + "@types/node": "*" + } + }, "node_modules/@types/body-parser": { "version": "1.19.2", "resolved": "https://registry.npmjs.org/@types/body-parser/-/body-parser-1.19.2.tgz", diff --git a/package.json b/package.json index ec3a385..6cb7b2e 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "@radix-ui/react-tabs": "^1.0.4", "@supabase/supabase-js": "^2.21.0", "@tabler/icons-react": "^2.9.0", + "@types/adm-zip": "^0.5.0", "@types/multer": "^1.4.7", "class-variance-authority": "^0.6.0", "clsx": "^1.2.1", diff --git a/pages/api/files.ts b/pages/api/files.ts index eb8af2a..30cbc41 100644 --- a/pages/api/files.ts +++ b/pages/api/files.ts @@ -1,6 +1,7 @@ import type {NextApiRequest, NextApiResponse} from 'next' import multer from "multer"; import fs from 'fs'; +import AdmZip from 'adm-zip'; import {NEXT_PUBLIC_CHAT_FILES_UPLOAD_PATH} from "@/utils/app/const"; export const config = { @@ -30,6 +31,17 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => { if (err) { return res.status(400).json({ message: err.message }); } + const fileName = req.query.fileName as string; + if (fileName.split('.').pop()! === 'zip') { + try { + const zip = new AdmZip(`${folderPath}/${fileName}`); + const finalPath = `${folderPath}/${fileName.split('.')[0]}`; + zip.extractAllTo(finalPath, true); + } catch (e) { + console.error(e); + return res.status(500).json({ message: (e as Error).message }); + } + } // File uploaded successfully res.status(200).json({ message: 'File uploaded successfully' }); }); diff --git a/utils/langchain/documentLoader.ts b/utils/langchain/documentLoader.ts index 67b5467..392a3d6 100644 --- a/utils/langchain/documentLoader.ts +++ b/utils/langchain/documentLoader.ts @@ -39,6 +39,8 @@ export function getDocumentLoader(fileType: string, filePath: string): DocumentL case "csv": loader = new CSVLoader(filePath); return loader; + case "zip": + return getDirectoryLoader(filePath) default: loader = new UnstructuredLoader(filePath); return loader; @@ -46,8 +48,9 @@ export function getDocumentLoader(fileType: string, filePath: string): DocumentL } export function getDirectoryLoader(path: string): DocumentLoader { + const zipFilePath = path.split('.')[0]; return new DirectoryLoader( - path, { + zipFilePath, { ".pdf": (path) => getDocumentLoader("pdf", path), ".epub": (path) => getDocumentLoader("epub", path), ".txt": (path) => getDocumentLoader("txt", path), diff --git a/yarn.lock b/yarn.lock index 578db37..74e67be 100644 --- a/yarn.lock +++ b/yarn.lock @@ -442,6 +442,13 @@ resolved "https://registry.npmjs.org/@tootallnate/once/-/once-2.0.0.tgz" integrity sha512-XCuKFP5PS55gnMVu3dty8KPatLqUoy/ZYzDzAGCQ8JNFCkLXzmI7vNHCR+XpbZaMWQK/vQubr7PkYq8g470J/A== +"@types/adm-zip@^0.5.0": + version "0.5.0" + resolved "https://registry.npmjs.org/@types/adm-zip/-/adm-zip-0.5.0.tgz" + integrity sha512-FCJBJq9ODsQZUNURo5ILAQueuA8WJhRvuihS3ke2iI25mJlfV2LK8jG2Qj2z2AWg8U0FtWWqBHVRetceLskSaw== + dependencies: + "@types/node" "*" + "@types/body-parser@*": version "1.19.2" resolved "https://registry.npmjs.org/@types/body-parser/-/body-parser-1.19.2.tgz"