fix(download): 出图不再弹「另存为」;自动下载改为从图库原图复制 v3.25.33

① 删除 runtime-workbench-logic 里遗留的逐张自动下载(默认开、无 UI 开关),
   它走交互式 downloadUrl:写失败回退 dialog.save(默认路径是旧目录)→ 每张都弹框;
   四级兜底最后一级 openExternal → 弹出浏览器、落到浏览器下载目录。
② wSaveBlobNative 弹框只由「有没有导出目录」决定:有目录静默写,失败先 grant_dir
   重授权重试,仍失败只 toast;没目录才弹一次选位置。
③ archiveGenerationResults 改为「归档 → 刷新 → 自动下载」串行;wAddOutputs 新增
   awaitArchive 等本批原图落盘;resolveSrc 按 url 回查图库索引取 out_orig_ 原图,
   本地命中不碰图床;dataURL 直接解码 Blob。
④ 删除死代码 wSaveGenImageToDir。

app.js 行数 8947(守门上限内)。verify: build/check-dist/lint/typecheck/test:unit 全过。
This commit is contained in:
qiqi committed 2026-11-19 16:46:35 +08:00
1 parent 92370c9254
commit 7b8d755cac
8 files changed
+95 -51

No files matched your search

+34 -34
View File
@@ -66,25 +66,14 @@
await wTauri.fs.writeFile(target, new Uint8Array(await blob.arrayBuffer()))
return true
}
// 桌面端自动保存生成图:写默认下载文件夹,取字节走 plugin-http(不受图床 CORS 限制),失败只返回 false
const wSaveGenImageToDir = async (item) => {
try {
const dir = wDownloadDir()
if (!dir || !item || !item.url) return false
const resp = await wTauri.http.fetch(item.url)
if (!resp || !resp.ok) return false
const blob = await resp.blob()
const base = wSanitizeFileName(item.name || ('gen_' + Date.now()))
const name = (ImageFormat && ImageFormat.fileNameFor) ? ImageFormat.fileNameFor(item.url, base, blob.type) : base
return await wWriteBlobToDir(dir, name, blob)
} catch (e) { return false }
}
// 出图自动下载:三态逻辑在 src/download-auto.js;这里只装配依赖
// 出图自动下载:三态逻辑在 src/download-auto.js。resolveSrc 只从「图库已落盘的原图」复制(按 url 回查索引),本地命中不碰图床。
const wAutoSaver = (typeof window !== 'undefined' && window.IWBDownloadAuto)
? window.IWBDownloadAuto.create({
getDir: wDownloadDir,
isOn: wAutoDownload,
resolveSrc: (item) => wResolveOutputSrc(item, true),
resolveSrc: (item) => window.IWBDownloadAuto.resolveLocalFirst(item, { resolveDirect: (x) => wResolveOutputSrc(x, true), listFiles: () => (ModularOutputStore ? ModularOutputStore.list({ metaOnly: true }).then((r) => (r && r.ok && r.value && r.value.files) || []).catch(() => []) : lsGet(OUT_KEY, [])), readIdb: (k) => IDB.get(k).catch(() => null) }),
// dataURL 直接解码成 Blob(不再 fetch(dataURL));远程 URL 返回 null 让调用方走 fetch
toBlob: (src) => (String(src || '').indexOf('data:') === 0 ? wBlobFromDataUrl(src) : null),
write: wWriteBlobToDir,
toast: (msg, type) => { try { const f = (typeof window !== 'undefined') ? window.__iwbAutoToast__ : null; if (typeof f === 'function') f(msg, type) } catch (e) { /* 组件外调用时忽略 */ } },
})
@@ -95,21 +84,25 @@
if (!invoke) throw new Error('Tauri 不可用')
return await invoke('open_app_data_dir')
}
// 桌面端保存二进制:弹系统「另存为」→ fs 直接写 Uint8Array。
// 刻意不经 base64:Blob → arrayBuffer → Uint8Array,省掉一次全量编码/解码。
// 桌面端保存二进制:有导出目录直接写 Uint8Array(不弹框),没目录才弹「另存为」;刻意不经 base64,省一次全量编解码。
const wSaveBlobNative = async (blob, name, title) => {
const save = wTauri && wTauri.dialog && wTauri.dialog.save
const writeFile = wTauri && wTauri.fs && wTauri.fs.writeFile
if (!save || !writeFile) throw new Error('Tauri 保存接口不可用(检查 dialog/fs 插件是否初始化)')
if (!writeFile) throw new Error('Tauri 文件写入不可用(检查 fs 插件是否初始化)')
const fileName = String(name || 'image.png')
const ext = (fileName.split('.').pop() || 'png').toLowerCase()
// 路径来自 save() 的返回值:tauri-plugin-dialog 会把该路径动态加入 fs scope,
// 因此 capabilities 里不需要静态放开任何目录(不放开 $HOME 这类宽权限)。
// 「不再逐个询问」:设了默认目录就直接落盘;写失败(如重启后授权失效)自动退回对话框
// 有目录就直接落盘,没有才弹框(v3.25.20:是否弹框由「有没有目录」决定,不再看开关)
if (wDownloadDir()) {
try { await wWriteBlobToDir(wDownloadDir(), fileName, blob); return true } catch (e) { /* 回退对话框 */ }
const dir = wDownloadDir()
// 是否弹框只看「有没有导出目录」:有目录=静默落盘,写失败也不弹框(旧实现会回退「另存为」,默认路径还是旧目录,叠上出图自动下载就是每张都弹);没目录=弹一次选位置。
if (dir) {
try { await wWriteBlobToDir(dir, fileName, blob); return true } catch (e) { /* fs scope 仍是上一次运行的 */ }
try {
const invoke = wTauri && wTauri.core && wTauri.core.invoke
if (invoke) { await invoke('grant_dir', { dir: String(dir), record: false }); await wWriteBlobToDir(dir, fileName, blob); return true }
} catch (e) { /* 仍然不可写 */ }
wDownloadToast('导出目录不可写:' + dir + '(请到「设置 → 图片导出目录」重新选择)')
return false
}
if (!save) throw new Error('Tauri 保存对话框不可用(检查 dialog 插件是否初始化)')
// 初始路径 = 默认下载文件夹 + 文件名(没设置则只给文件名,跟随系统上次位置)
const defaultPath = await wDefaultSavePath(fileName)
const path = await save({ title: title || '保存', defaultPath, filters: [{ name: ext.toUpperCase() + ' 文件', extensions: [ext] }] })
@@ -1930,7 +1923,9 @@
for (const f of hydList) if (f && f.url && f.data) outThumbCacheSet(f.url, f.data)
return { files: list, retention: ret }
}
async function wAddOutputs(results) {
// opts.awaitArchive=true:等本批新图原图落盘再返回(自动下载要从本地原图复制,避免与后台归档竞态)
async function wAddOutputs(results, opts) {
const awaitArchive = !!(opts && opts.awaitArchive)
if (ModularOutputStore) {
const items = []
for (const r of (results || [])) {
@@ -1954,12 +1949,14 @@
const newNames = new Set(items.map((x) => x && x.name).filter(Boolean))
const needThumb = curFiles.filter((item) => item && newNames.has(item.name) && !item.idb && !wThumbFail.has(wThumbFailKey(item)))
if (needThumb.length) {
runWithConcurrency(needThumb, 2, (item) => storeOutputThumb(item)).catch((e) => {
const thumbJob = runWithConcurrency(needThumb, 2, (item) => storeOutputThumb(item))
const onThumbFail = (e) => {
// 整批缩略图失败此前是**零反馈**:图库里看不到缩略图,用户不知道发生了什么。
// 现在:记录 + 提示一次(整批失败才提示,不逐张刷屏)。
try { if (CrashLog && typeof CrashLog.record === 'function') CrashLog.record('thumb-batch-failed', { scope: 'output-store', message: '缩略图批量生成失败(' + needThumb.length + ' 张):' + String((e && e.message) || e), version: APP_VERSION }) } catch (err) { /* ignore */ }
wLogNotice('部分图片的缩略图生成失败(' + needThumb.length + ' 张),图库中可能显示为空白。', 'err')
})
}
if (awaitArchive) { try { await thumbJob } catch (e) { onThumbFail(e) } } else thumbJob.catch(onThumbFail)
}
for (const item of curFiles) {
if (item && newNames.has(item.name) && imageFolderApi) imageFolderApi.save(item, item.data).catch(() => {})
@@ -1990,11 +1987,13 @@
if (removedNames.length) wPruneOutputIdb(removedNames)
const needThumb2 = idx.filter((f) => keptNames.has(f.name) && !f.idb)
if (needThumb2.length) {
runWithConcurrency(needThumb2, 5, (f) => storeOutputThumb(f)).catch((e) => {
const thumbJob2 = runWithConcurrency(needThumb2, 5, (f) => storeOutputThumb(f))
const onThumbFail2 = (e) => {
// 同上:整批失败原先零反馈(用户只看到图库空白,不知道原因)
try { if (CrashLog && typeof CrashLog.record === 'function') CrashLog.record('thumb-batch-failed', { scope: 'output-store', message: '缩略图批量生成失败(' + needThumb2.length + ' 张):' + String((e && e.message) || e), version: APP_VERSION }) } catch (err) { /* ignore */ }
wLogNotice('部分图片的缩略图生成失败(' + needThumb2.length + ' 张),图库中可能显示为空白。', 'err')
})
}
if (awaitArchive) { try { await thumbJob2 } catch (e) { onThumbFail2(e) } } else thumbJob2.catch(onThumbFail2)
}
for (const f of idx) {
if (keptNames.has(f.name) && imageFolderApi) imageFolderApi.save(f, f.data).catch(() => {})
@@ -3633,11 +3632,12 @@
if (typeof hook.afterResult !== 'function') continue
try { hook.afterResult(results) } catch (e) { /* 插件钩子异常不得影响结果归档 */ }
}
Promise.resolve().then(() => wAddOutputs(results)).then(() => {
if (typeof refresh === 'function') return refresh()
}).catch(() => { /* 图库缓存失败不影响画布结果 */ })
// 出图自动下载(勾选且设了目录才生效;失败只提示,不影响生图)
try { if (wAutoSaver) Promise.resolve().then(() => wAutoSaver.saveResults(results)).catch(() => {}) } catch (e) { /* ignore */ }
// 归档 → 刷新 → 自动下载串行:自动下载必须等本批原图写进图库(out_orig_)后再跑,才谈得上「从本地字节复制」。
Promise.resolve().then(async () => {
try { await wAddOutputs(results, { awaitArchive: true }) } catch (e) { /* 图库缓存失败不影响画布结果 */ }
try { if (typeof refresh === 'function') await refresh() } catch (e) { /* ignore */ }
try { if (wAutoSaver) await wAutoSaver.saveResults(results) } catch (e) { /* 自动下载失败不影响生图 */ }
})
return Promise.resolve(results)
}