From 12ed570a53fca584e36aab00ce55247b8c882686 Mon Sep 17 00:00:00 2001
From: Marcus Schiesser <mail@marcusschiesser.de>
Date: Tue, 1 Oct 2024 12:39:42 +0700
Subject: [PATCH] refactor: make saveDocument reusable (#332)

---
 .../llamaindex/typescript/documents/helper.ts      | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)

diff --git a/templates/components/llamaindex/typescript/documents/helper.ts b/templates/components/llamaindex/typescript/documents/helper.ts
index 0bbcc47f..3e878f08 100644
--- a/templates/components/llamaindex/typescript/documents/helper.ts
+++ b/templates/components/llamaindex/typescript/documents/helper.ts
@@ -15,8 +15,11 @@ export async function storeAndParseFile(
   fileBuffer: Buffer,
   mimeType: string,
 ) {
+  const fileExt = MIME_TYPE_TO_EXT[mimeType];
+  if (!fileExt) throw new Error(`Unsupported document type: ${mimeType}`);
+
   const documents = await loadDocuments(fileBuffer, mimeType);
-  await saveDocument(filename, fileBuffer, mimeType);
+  await saveDocument(filename, fileBuffer);
   for (const document of documents) {
     document.metadata = {
       ...document.metadata,
@@ -38,14 +41,7 @@ async function loadDocuments(fileBuffer: Buffer, mimeType: string) {
   return await reader.loadDataAsContent(fileBuffer);
 }
 
-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}`);
-
+export async function saveDocument(filename: string, fileBuffer: Buffer) {
   const filepath = `${UPLOADED_FOLDER}/${filename}`;
   const fileurl = `${process.env.FILESERVER_URL_PREFIX}/${filepath}`;
 
-- 
GitLab