From febccfc86399d5e41d446036e9a265328bdf3a2e Mon Sep 17 00:00:00 2001
From: shiyue <935732994@qq.com>
Date: Fri, 29 Aug 2025 18:07:03 +0800
Subject: [PATCH] =?UTF-8?q?feat:=20=E5=9C=A8utils=E4=B8=AD=E6=B7=BB?=
=?UTF-8?q?=E5=8A=A0base64ToBlobUrl=E5=87=BD=E6=95=B0=E4=BB=A5=E4=BC=98?=
=?UTF-8?q?=E5=8C=96=E5=9B=BE=E7=89=87=E6=98=BE=E7=A4=BA=E5=92=8C=E4=B8=8B?=
=?UTF-8?q?=E8=BD=BD?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
script.js | 96 +++++++++++++++++++++++++++++++++++++++++++++++++------
1 file changed, 87 insertions(+), 9 deletions(-)
diff --git a/script.js b/script.js
index e1f8adc..2b44a35 100644
--- a/script.js
+++ b/script.js
@@ -50,6 +50,45 @@ const ERROR_MESSAGES = {
// 工具函数
const utils = {
+ // 将base64转换为Blob URL
+ base64ToBlobUrl: function(base64Data) {
+ try {
+ // 检查是否已经是Blob URL
+ if (base64Data.startsWith('blob:')) {
+ return base64Data;
+ }
+
+ // 提取base64数据的MIME类型和纯数据部分
+ const parts = base64Data.split(';base64,');
+ if (parts.length !== 2) {
+ console.warn('Invalid base64 format, returning original data');
+ return base64Data;
+ }
+
+ const contentType = parts[0].split(':')[1];
+ const byteCharacters = atob(parts[1]);
+
+ // 转换为ArrayBuffer
+ const byteNumbers = new Array(byteCharacters.length);
+ for (let i = 0; i < byteCharacters.length; i++) {
+ byteNumbers[i] = byteCharacters.charCodeAt(i);
+ }
+ const byteArray = new Uint8Array(byteNumbers);
+
+ // 创建Blob和URL
+ const blob = new Blob([byteArray], { type: contentType });
+ const blobUrl = URL.createObjectURL(blob);
+
+ // 存储原始base64数据以便后续使用(如下载)
+ blobUrl._originalBase64 = base64Data;
+
+ return blobUrl;
+ } catch (error) {
+ console.error('Error converting base64 to Blob URL:', error);
+ return base64Data; // 出错时返回原始数据
+ }
+ },
+
// 格式化文件大小
formatFileSize: function(bytes) {
if (bytes === 0) return '0 Bytes';
@@ -669,10 +708,11 @@ const apiService = {
const messageContent = choice.message.content;
const images = [];
- // 提取图像数据
+ // 提取图像数据并转换为Blob URL
if (choice.message.images) {
choice.message.images.forEach(img => {
- images.push(img.image_url.url);
+ const blobUrl = utils.base64ToBlobUrl(img.image_url.url);
+ images.push(blobUrl);
});
}
@@ -773,8 +813,12 @@ const uiController = {
imageDiv.className = 'image-item fade-in';
const imageId = Date.now() + Math.random();
+
+ // 确保imageUrl是Blob URL
+ const displayUrl = imageUrl.startsWith('blob:') ? imageUrl : utils.base64ToBlobUrl(imageUrl);
+
imageDiv.innerHTML = `
-
+