Skip to content

Commit

Permalink
refactor: make saveDocument reusable (#332)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcusschiesser authored Oct 1, 2024
1 parent bde3daa commit 12ed570
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions templates/components/llamaindex/typescript/documents/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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}`;

Expand Down

0 comments on commit 12ed570

Please sign in to comment.