From fd4abb3bddfae1cae2b41e25c0fc14391c8d06c2 Mon Sep 17 00:00:00 2001 From: Thuc Pham <51660321+thucpn@users.noreply.github.com> Date: Thu, 29 Aug 2024 15:47:50 +0700 Subject: [PATCH] fix: keep origin upload filename (#268) --- .changeset/strong-kids-brush.md | 5 +++++ .../llamaindex/typescript/documents/helper.ts | 16 +++++++++++----- .../llamaindex/typescript/documents/upload.ts | 2 +- 3 files changed, 17 insertions(+), 6 deletions(-) create mode 100644 .changeset/strong-kids-brush.md diff --git a/.changeset/strong-kids-brush.md b/.changeset/strong-kids-brush.md new file mode 100644 index 00000000..b4b587a0 --- /dev/null +++ b/.changeset/strong-kids-brush.md @@ -0,0 +1,5 @@ +--- +"create-llama": patch +--- + +fix: keep origin upload filename diff --git a/templates/components/llamaindex/typescript/documents/helper.ts b/templates/components/llamaindex/typescript/documents/helper.ts index 14573036..0bbcc47f 100644 --- a/templates/components/llamaindex/typescript/documents/helper.ts +++ b/templates/components/llamaindex/typescript/documents/helper.ts @@ -1,5 +1,4 @@ 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}`; diff --git a/templates/components/llamaindex/typescript/documents/upload.ts b/templates/components/llamaindex/typescript/documents/upload.ts index 9cfb545f..4f205a60 100644 --- a/templates/components/llamaindex/typescript/documents/upload.ts +++ b/templates/components/llamaindex/typescript/documents/upload.ts @@ -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); } -- GitLab