feat(js): 简化图片下载逻辑并移除文件类型检测功能

- 移除 getImageExtension 函数和相关的内容类型映射逻辑
- 修改 buildImageFilename 函数,不再依赖内容类型参数,默认使用 .png 扩展名
- 将图片下载方式从 fetch + blob 改为 iframe 方式
- 添加 iframe 自动清理机制,默认延迟 60000ms 后移除
- 移除 Blob URL 创建和手动清理逻辑
- 优化下载成功提示信息
```
This commit is contained in:
公司git 2026-04-26 10:53:23 +08:00
parent 4ece904c25
commit 4b9f152aea

View File

@ -105,23 +105,9 @@ function switchMode(mode) {
updateCostPreview(); // 切换模式时同步计费预览
}
function getImageExtension(contentType) {
const type = (contentType || '').split(';')[0].trim().toLowerCase();
const extensionMap = {
'image/jpeg': 'jpg',
'image/jpg': 'jpg',
'image/png': 'png',
'image/webp': 'webp',
'image/gif': 'gif',
'image/bmp': 'bmp',
'image/avif': 'avif'
};
return extensionMap[type] || 'png';
}
function buildImageFilename(url, contentType = '') {
function buildImageFilename(url) {
downloadFilenameSerial += 1;
const fallbackName = `ai-vision-image-${Date.now()}-${downloadFilenameSerial}.${getImageExtension(contentType)}`;
const fallbackName = `ai-vision-image-${Date.now()}-${downloadFilenameSerial}.png`;
try {
const parsed = new URL(url, window.location.href);
@ -157,35 +143,22 @@ async function downloadImage(url, options = {}) {
if (!url) return false;
const silent = options.silent === true;
const cleanupDelay = options.cleanupDelay || 60000;
try {
if (!silent) showToast('正在准备下载...', 'info');
const filenameHint = buildImageFilename(url);
const response = await fetch(buildDownloadProxyUrl(url, filenameHint), {
credentials: 'same-origin'
});
const filename = buildImageFilename(url);
const iframe = document.createElement('iframe');
iframe.style.display = 'none';
iframe.src = buildDownloadProxyUrl(url, filename);
document.body.appendChild(iframe);
if (!response.ok) {
let message = `HTTP ${response.status}`;
const responseType = response.headers.get('Content-Type') || '';
if (responseType.includes('application/json')) {
const data = await response.json();
message = data.error || message;
}
throw new Error(message);
}
setTimeout(() => {
if (iframe.parentNode) iframe.parentNode.removeChild(iframe);
}, cleanupDelay);
const blob = await response.blob();
const filename = buildImageFilename(url, blob.type);
const blobUrl = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = blobUrl;
a.download = filename;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
setTimeout(() => window.URL.revokeObjectURL(blobUrl), 1000);
if (!silent) showToast('下载已开始,请留意浏览器下载列表', 'success');
return true;
} catch (e) {
console.error('下载失败:', e);