From 722bc7f6383f3f00ecac91c353f2e6282241742d Mon Sep 17 00:00:00 2001 From: sijinhui Date: Fri, 17 May 2024 10:22:36 +0800 Subject: [PATCH] merge --- app/utils.ts | 58 +------------------------------ app/utils/chat.ts | 87 +++++++++++++++++++++++++++++------------------ yarn.lock | 5 +++ 3 files changed, 60 insertions(+), 90 deletions(-) diff --git a/app/utils.ts b/app/utils.ts index d3c45e69a..8f7adc7e2 100644 --- a/app/utils.ts +++ b/app/utils.ts @@ -83,58 +83,6 @@ export async function downloadAs(text: string, filename: string) { } } -// export function compressImage(file: File, maxSize: number): Promise { -// return new Promise((resolve, reject) => { -// const reader = new FileReader(); -// reader.onload = () => { -// // 直接解析为 Data URL -// resolve(reader.result as string); -// }; -// reader.onerror = reject; -// reader.readAsDataURL(file); -// }); - -// return new Promise((resolve, reject) => { -// const reader = new FileReader(); -// reader.onload = (readerEvent: any) => { -// const image = new Image(); -// image.onload = () => { -// let canvas = document.createElement("canvas"); -// let ctx = canvas.getContext("2d"); -// let width = image.width; -// let height = image.height; -// let quality = 0.9; -// let dataUrl; -// -// do { -// canvas.width = width; -// canvas.height = height; -// ctx?.clearRect(0, 0, canvas.width, canvas.height); -// ctx?.drawImage(image, 0, 0, width, height); -// dataUrl = canvas.toDataURL("image/jpeg", quality); -// -// if (dataUrl.length < maxSize) break; -// -// if (quality > 0.5) { -// // Prioritize quality reduction -// quality -= 0.1; -// } else { -// // Then reduce the size -// width *= 0.9; -// height *= 0.9; -// } -// } while (dataUrl.length > maxSize); -// -// resolve(dataUrl); -// }; -// image.onerror = reject; -// image.src = readerEvent.target.result; -// }; -// reader.onerror = reject; -// reader.readAsDataURL(file); -// }); -// } - export function readFromFile() { return new Promise((res, rej) => { const fileInput = document.createElement("input"); @@ -312,11 +260,7 @@ export function isVisionModel(model: string) { const isGpt4Turbo = model.includes("gpt-4-turbo") && !model.includes("preview"); - const isGpt4o = model === "gpt-4o"; - return ( - visionKeywords.some((keyword) => model.includes(keyword)) || - isGpt4Turbo || - isGpt4o + visionKeywords.some((keyword) => model.includes(keyword)) || isGpt4Turbo ); } diff --git a/app/utils/chat.ts b/app/utils/chat.ts index 991d06b73..7ec0d889e 100644 --- a/app/utils/chat.ts +++ b/app/utils/chat.ts @@ -3,39 +3,9 @@ import heic2any from "heic2any"; export function compressImage(file: File, maxSize: number): Promise { return new Promise((resolve, reject) => { const reader = new FileReader(); - reader.onload = (readerEvent: any) => { - const image = new Image(); - image.onload = () => { - let canvas = document.createElement("canvas"); - let ctx = canvas.getContext("2d"); - let width = image.width; - let height = image.height; - let quality = 0.9; - let dataUrl; - - do { - canvas.width = width; - canvas.height = height; - ctx?.clearRect(0, 0, canvas.width, canvas.height); - ctx?.drawImage(image, 0, 0, width, height); - dataUrl = canvas.toDataURL("image/jpeg", quality); - - if (dataUrl.length < maxSize) break; - - if (quality > 0.5) { - // Prioritize quality reduction - quality -= 0.1; - } else { - // Then reduce the size - width *= 0.9; - height *= 0.9; - } - } while (dataUrl.length > maxSize); - - resolve(dataUrl); - }; - image.onerror = reject; - image.src = readerEvent.target.result; + reader.onload = () => { + // 直接解析为 Data URL + resolve(reader.result as string); }; reader.onerror = reject; @@ -51,4 +21,55 @@ export function compressImage(file: File, maxSize: number): Promise { reader.readAsDataURL(file); }); + + // return new Promise((resolve, reject) => { + // const reader = new FileReader(); + // reader.onload = (readerEvent: any) => { + // const image = new Image(); + // image.onload = () => { + // let canvas = document.createElement("canvas"); + // let ctx = canvas.getContext("2d"); + // let width = image.width; + // let height = image.height; + // let quality = 0.9; + // let dataUrl; + // + // do { + // canvas.width = width; + // canvas.height = height; + // ctx?.clearRect(0, 0, canvas.width, canvas.height); + // ctx?.drawImage(image, 0, 0, width, height); + // dataUrl = canvas.toDataURL("image/jpeg", quality); + // + // if (dataUrl.length < maxSize) break; + // + // if (quality > 0.5) { + // // Prioritize quality reduction + // quality -= 0.1; + // } else { + // // Then reduce the size + // width *= 0.9; + // height *= 0.9; + // } + // } while (dataUrl.length > maxSize); + // + // resolve(dataUrl); + // }; + // image.onerror = reject; + // image.src = readerEvent.target.result; + // }; + // reader.onerror = reject; + // + // if (file.type.includes("heic")) { + // heic2any({ blob: file, toType: "image/jpeg" }) + // .then((blob) => { + // reader.readAsDataURL(blob as Blob); + // }) + // .catch((e) => { + // reject(e); + // }); + // } + // + // reader.readAsDataURL(file); + // }); } diff --git a/yarn.lock b/yarn.lock index 3154d54a8..1428d8fbf 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4438,6 +4438,11 @@ hastscript@^8.0.0: property-information "^6.0.0" space-separated-tokens "^2.0.0" +heic2any@^0.0.4: + version "0.0.4" + resolved "https://registry.yarnpkg.com/heic2any/-/heic2any-0.0.4.tgz#eddb8e6fec53c8583a6e18b65069bb5e8d19028a" + integrity sha512-3lLnZiDELfabVH87htnRolZ2iehX9zwpRyGNz22GKXIu0fznlblf0/ftppXKNqS26dqFSeqfIBhAmAj/uSp0cA== + highlight-words-core@^1.2.0: version "1.2.2" resolved "https://registry.npmmirror.com/highlight-words-core/-/highlight-words-core-1.2.2.tgz#1eff6d7d9f0a22f155042a00791237791b1eeaaa"