Skip to content

Commit

Permalink
feat: add zip file support.
Browse files Browse the repository at this point in the history
  • Loading branch information
guangzhengli committed Jun 26, 2023
1 parent 99f8b63 commit db15915
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 6 deletions.
12 changes: 7 additions & 5 deletions components/Chat/Upload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -75,6 +75,7 @@ export const Upload = ({
case "txt":
case "md":
case "csv":
case "zip":
case "json":
return true;
default:
Expand Down Expand Up @@ -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) => {
Expand Down
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
12 changes: 12 additions & 0 deletions pages/api/files.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down Expand Up @@ -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' });
});
Expand Down
5 changes: 4 additions & 1 deletion utils/langchain/documentLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,18 @@ 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;
}
}

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),
Expand Down
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

1 comment on commit db15915

@vercel
Copy link

@vercel vercel bot commented on db15915 Jun 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.