From 981811efd18ed262009d8bc7121986e8cdf07946 Mon Sep 17 00:00:00 2001
From: Alex Yang <himself65@outlook.com>
Date: Wed, 2 Oct 2024 14:31:03 -0700
Subject: [PATCH] fix(cloud): llama parse reader save image incorrectly (#1287)

---
 .changeset/real-ears-design.md |  5 +++++
 packages/cloud/src/reader.ts   | 20 ++++++--------------
 2 files changed, 11 insertions(+), 14 deletions(-)
 create mode 100644 .changeset/real-ears-design.md

diff --git a/.changeset/real-ears-design.md b/.changeset/real-ears-design.md
new file mode 100644
index 000000000..307b1a96f
--- /dev/null
+++ b/.changeset/real-ears-design.md
@@ -0,0 +1,5 @@
+---
+"@llamaindex/cloud": patch
+---
+
+fix(cloud): llama parse reader save image incorrectly
diff --git a/packages/cloud/src/reader.ts b/packages/cloud/src/reader.ts
index 7731cd5f4..88c0fac38 100644
--- a/packages/cloud/src/reader.ts
+++ b/packages/cloud/src/reader.ts
@@ -1,11 +1,11 @@
-import { createClient, createConfig, type Client } from "@hey-api/client-fetch";
+import { type Client, createClient, createConfig } from "@hey-api/client-fetch";
 import { Document, FileReader } from "@llamaindex/core/schema";
-import { fs, getEnv } from "@llamaindex/env";
+import { fs, getEnv, path } from "@llamaindex/env";
 import { filetypeinfo } from "magic-bytes.js";
 import {
-  ParsingService,
   type Body_upload_file_api_v1_parsing_upload_post,
   type ParserLanguages,
+  ParsingService,
 } from "./api";
 import { sleep } from "./utils";
 
@@ -510,14 +510,7 @@ export class LlamaParseReader extends FileReader {
     jobId: string,
     imageName: string,
   ): Promise<string> {
-    // Get the full path
-    let imagePath = `${downloadPath}/${jobId}-${imageName}`;
-    // Get a valid image path
-    if (!imagePath.endsWith(".png") && !imagePath.endsWith(".jpg")) {
-      imagePath += ".png";
-    }
-
-    return imagePath;
+    return path.join(downloadPath, `${jobId}-${imageName}`);
   }
 
   private async fetchAndSaveImage(
@@ -538,10 +531,9 @@ export class LlamaParseReader extends FileReader {
     if (response.error) {
       throw new Error(`Failed to download image: ${response.error.detail}`);
     }
-    const arrayBuffer = (await response.data) as ArrayBuffer;
-    const buffer = new Uint8Array(arrayBuffer);
+    const blob = (await response.data) as Blob;
     // Write the image buffer to the specified imagePath
-    await fs.writeFile(imagePath, buffer);
+    await fs.writeFile(imagePath, new Uint8Array(await blob.arrayBuffer()));
   }
 
   // Filters out invalid values (null, undefined, empty string) of specific params.
-- 
GitLab