Skip to content

Commit

Permalink
feat: validate the upload file type.
Browse files Browse the repository at this point in the history
  • Loading branch information
guangzhengli committed Jun 26, 2023
1 parent a8e08a1 commit 99f8b63
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
27 changes: 25 additions & 2 deletions components/Chat/Upload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {CHAT_FILES_MAX_SIZE} from "@/utils/app/const";
import {humanFileSize} from "@/utils/app/files";
import {useTranslation} from 'next-i18next';
import {v4 as uuidv4} from 'uuid';
import {EmbeddingCreateRequest} from "@/types/embedding";

interface Props {
onIndexChange: (index: LlamaIndex) => void;
Expand Down Expand Up @@ -50,15 +49,39 @@ export const Upload = ({
};

const validateFile = (file: File) => {
console.log(`select a file size: ${humanFileSize(file.size)}`);
console.log(`upload file size: ${humanFileSize(file.size)}`);
console.log(`file max size: ${humanFileSize(CHAT_FILES_MAX_SIZE)}`);
if (CHAT_FILES_MAX_SIZE != 0 && file.size > CHAT_FILES_MAX_SIZE) {
handleUploadError(`Please select a file smaller than ${humanFileSize(CHAT_FILES_MAX_SIZE)}`);
return false;
}

console.log(`upload file type: ${file.name.split('.').pop()!}`);
if (!validateFileType(file.name.split('.').pop()!)) {
handleUploadError(`Please upload file of these types: ${supportFileType}`);
return false;
}

return true;
};

const supportFileType = "pdf, epub, docx, txt, md, csv, json";

function validateFileType(fileType: string): boolean {
switch (fileType) {
case "pdf":
case "epub":
case "docx":
case "txt":
case "md":
case "csv":
case "json":
return true;
default:
return false;
}
}

const uploadFile = async (file: File) => {
const fileName = uuidv4();
const fileType = file.name.split('.').pop()!;
Expand Down
17 changes: 1 addition & 16 deletions utils/langchain/documentLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {PDFLoader} from "langchain/document_loaders/fs/pdf";
import {EPubLoader} from "langchain/document_loaders/fs/epub";
import {DocxLoader} from "langchain/document_loaders/fs/docx";
import {TextLoader} from "langchain/document_loaders/fs/text";
import { CSVLoader } from "langchain/document_loaders/fs/csv";
import {CSVLoader} from "langchain/document_loaders/fs/csv";
import {DirectoryLoader} from "langchain/document_loaders/fs/directory";
import {DocumentLoader} from "langchain/dist/document_loaders/base";
import {UnstructuredLoader} from "langchain/document_loaders/fs/unstructured";
Expand Down Expand Up @@ -57,19 +57,4 @@ export function getDirectoryLoader(path: string): DocumentLoader {
".csv": (path) => getDocumentLoader("csv", path),
}
);
}

export function validateFileType(fileType: string): boolean {
switch (fileType) {
case "pdf":
case "epub":
case "docx":
case "txt":
case "md":
case "csv":
case "json":
return true;
default:
return false;
}
}

0 comments on commit 99f8b63

Please sign in to comment.