Skip to content
Snippets Groups Projects
Unverified Commit fd4abb3b authored by Thuc Pham's avatar Thuc Pham Committed by GitHub
Browse files

fix: keep origin upload filename (#268)

parent bedde2bf
No related branches found
No related tags found
No related merge requests found
---
"create-llama": patch
---
fix: keep origin upload filename
import fs from "fs";
import crypto from "node:crypto";
import { getExtractors } from "../../engine/loader";
const MIME_TYPE_TO_EXT: Record<string, string> = {
......@@ -11,9 +10,13 @@ const MIME_TYPE_TO_EXT: Record<string, string> = {
const UPLOADED_FOLDER = "output/uploaded";
export async function storeAndParseFile(fileBuffer: Buffer, mimeType: string) {
export async function storeAndParseFile(
filename: string,
fileBuffer: Buffer,
mimeType: string,
) {
const documents = await loadDocuments(fileBuffer, mimeType);
const { filename } = await saveDocument(fileBuffer, mimeType);
await saveDocument(filename, fileBuffer, mimeType);
for (const document of documents) {
document.metadata = {
...document.metadata,
......@@ -35,11 +38,14 @@ async function loadDocuments(fileBuffer: Buffer, mimeType: string) {
return await reader.loadDataAsContent(fileBuffer);
}
async function saveDocument(fileBuffer: Buffer, mimeType: string) {
async function saveDocument(
filename: string,
fileBuffer: Buffer,
mimeType: string,
) {
const fileExt = MIME_TYPE_TO_EXT[mimeType];
if (!fileExt) throw new Error(`Unsupported document type: ${mimeType}`);
const filename = `${crypto.randomUUID()}.${fileExt}`;
const filepath = `${UPLOADED_FOLDER}/${filename}`;
const fileurl = `${process.env.FILESERVER_URL_PREFIX}/${filepath}`;
......
......@@ -27,6 +27,6 @@ export async function uploadDocument(
}
// run the pipeline for other vector store indexes
const documents = await storeAndParseFile(fileBuffer, mimeType);
const documents = await storeAndParseFile(filename, fileBuffer, mimeType);
return runPipeline(index, documents);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment