Skip to content

Commit

Permalink
hotfix for code review
Browse files Browse the repository at this point in the history
  • Loading branch information
lloydzhou committed Jul 19, 2024
1 parent 052004d commit 862c2e8
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions app/utils/chat.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { CACHE_URL_PREFIX, UPLOAD_URL } from "@/app/constant";
// import heic2any from "heic2any";
import { RequestMessage } from "@/app/client/api";

export function compressImage(file: Blob, maxSize: number): Promise<string> {
Expand Down Expand Up @@ -42,14 +41,18 @@ export function compressImage(file: Blob, maxSize: number): Promise<string> {
reader.onerror = reject;

if (file.type.includes("heic")) {
const heic2any = require("heic2any");
heic2any({ blob: file, toType: "image/jpeg" })
.then((blob: Blob) => {
reader.readAsDataURL(blob);
})
.catch((e: any) => {
reject(e);
});
try {
const heic2any = require("heic2any");
heic2any({ blob: file, toType: "image/jpeg" })
.then((blob: Blob) => {
reader.readAsDataURL(blob);
})
.catch((e: any) => {
reject(e);
});
} catch (e) {
reject(e);
}
}

reader.readAsDataURL(file);
Expand All @@ -65,8 +68,12 @@ export async function preProcessImageContent(
const result = [];
for (const part of content) {
if (part?.type == "image_url" && part?.image_url?.url) {
const url = await cacheImageToBase64Image(part?.image_url?.url);
result.push({ type: part.type, image_url: { url } });
try {
const url = await cacheImageToBase64Image(part?.image_url?.url);
result.push({ type: part.type, image_url: { url } });
} catch (error) {
console.error("Error processing image URL:", error);
}
} else {
result.push({ ...part });
}
Expand All @@ -92,7 +99,7 @@ export function cacheImageToBase64Image(imageUrl: string) {
}
return Promise.resolve(imageCaches[imageUrl]);
}
return imageUrl;
return Promise.resolve(imageUrl);
}

export function base64Image2Blob(base64Data: string, contentType: string) {
Expand Down

0 comments on commit 862c2e8

Please sign in to comment.