chore: v3.26.226(测试版:先落地再显示 / 单次下载)
This commit is contained in:
1 parent
ae6549354d
commit
f0ccaf79d3
14 files changed
+211
-91
No files matched your search
+72
-3
@@ -51,6 +51,7 @@
|
||||
return {
|
||||
id: e.id, url: e.url, source: e.source, status: e.status,
|
||||
bytes: e.bytes || 0, mime: e.mime || '', w: e.w || 0, h: e.h || 0,
|
||||
progress: e.progress || 0, receivedBytes: e.receivedBytes || 0, totalBytes: e.totalBytes || 0,
|
||||
ref: e.ref || null, displayRef: e.displayRef || null, thumbRef: e.thumbRef || null,
|
||||
retries: e.retries || 0, lastError: e.lastError || '', requestId: e.requestId || '',
|
||||
createdAt: e.createdAt, updatedAt: e.updatedAt, meta: e.meta || {},
|
||||
@@ -83,17 +84,31 @@
|
||||
|
||||
async function runOne (e) {
|
||||
active++
|
||||
setStatus(e, STATE.DOWNLOADING, { lastError: '' })
|
||||
setStatus(e, STATE.DOWNLOADING, { lastError: '', progress: 0, receivedBytes: 0, totalBytes: 0 })
|
||||
let lastErr = ''
|
||||
// 2026-12-02:下载进度(有 content-length 才算百分比;拿不到就退回「下载中」文案)。
|
||||
// 事件节流 250ms —— 每 chunk 都触发会让画布整树重渲染,反而拖慢下载。
|
||||
const onProgress = (received, total) => {
|
||||
const rcv = Math.max(0, Number(received) || 0)
|
||||
const tot = Math.max(0, Number(total) || 0)
|
||||
e.receivedBytes = rcv
|
||||
e.totalBytes = tot
|
||||
e.progress = tot > 0 ? Math.min(1, rcv / tot) : 0
|
||||
const t = now()
|
||||
if (t - (e._progAt || 0) < 250 && !(tot > 0 && rcv >= tot)) return
|
||||
e._progAt = t
|
||||
emit('progress', publicView(e))
|
||||
}
|
||||
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
||||
try {
|
||||
const got = typeof d.fetchBytes === 'function' ? await d.fetchBytes(e.url) : { ok: false, error: 'no-fetcher' }
|
||||
const got = typeof d.fetchBytes === 'function' ? await d.fetchBytes(e.url, onProgress) : { ok: false, error: 'no-fetcher' }
|
||||
if (!got || !got.ok || !got.dataUrl) throw new Error((got && got.error) || 'fetch-failed')
|
||||
const ref = typeof d.writeRef === 'function' ? await d.writeRef(got.dataUrl) : null
|
||||
if (!ref) throw new Error('write-ref-failed')
|
||||
active--
|
||||
setStatus(e, STATE.READY, {
|
||||
ref,
|
||||
progress: 1,
|
||||
bytes: Number(got.bytes || 0) || Math.round(String(got.dataUrl).length * 0.75),
|
||||
mime: String(got.mime || ''),
|
||||
w: Number(got.w || 0) || e.w || 0,
|
||||
@@ -221,5 +236,59 @@
|
||||
return { enqueue, retry, cancel, release, remove, get, list, stats, restore, markMissing, STATE }
|
||||
}
|
||||
|
||||
global.IWBImageIngest = { create, STATE }
|
||||
// 带进度的取字节:流式读 body,按 content-length 报百分比;拿不到长度就只报已收字节。
|
||||
// 失败(CORS/非 2xx/无 body)抛错,交给宿主回退到既有 wBlobFromSrc。
|
||||
function blobToDataUrl (blob) {
|
||||
return new Promise((resolve) => {
|
||||
try {
|
||||
const fr = new FileReader()
|
||||
fr.onload = () => resolve(String(fr.result || ''))
|
||||
fr.onerror = () => resolve('')
|
||||
fr.readAsDataURL(blob)
|
||||
} catch (e) { resolve('') }
|
||||
})
|
||||
}
|
||||
async function fetchWithProgress (url, onProgress) {
|
||||
const res = await fetch(url)
|
||||
if (!res || !res.ok) throw new Error('http-' + String((res && res.status) || 'error'))
|
||||
const total = Number(res.headers && res.headers.get && res.headers.get('content-length')) || 0
|
||||
let blob = null
|
||||
if (res.body && typeof res.body.getReader === 'function') {
|
||||
const reader = res.body.getReader()
|
||||
const chunks = []
|
||||
let received = 0
|
||||
for (;;) {
|
||||
const step = await reader.read()
|
||||
if (!step || step.done) break
|
||||
if (step.value) { chunks.push(step.value); received += step.value.length || 0 }
|
||||
if (typeof onProgress === 'function') onProgress(received, total)
|
||||
}
|
||||
blob = new Blob(chunks, { type: String((res.headers && res.headers.get && res.headers.get('content-type')) || '') })
|
||||
} else {
|
||||
blob = await res.blob()
|
||||
if (typeof onProgress === 'function') onProgress(blob ? blob.size : 0, total)
|
||||
}
|
||||
const dataUrl = blob ? await blobToDataUrl(blob) : ''
|
||||
if (!dataUrl) return { ok: false, error: 'empty-bytes' }
|
||||
return { ok: true, dataUrl, mime: String((blob && blob.type) || ''), bytes: (blob && blob.size) || 0 }
|
||||
}
|
||||
|
||||
// 宿主取字节适配器:流式(带进度)→ 失败回退宿主既有 blob 取法;整体带超时。
|
||||
// 放进本模块是为了让 app.js 只留一行装配(Workbench 行数棘轮)。
|
||||
function createFetchBytes (deps) {
|
||||
const d = deps || {}
|
||||
const timeoutMs = Math.max(1000, Number(d.timeoutMs) || 300000)
|
||||
return async function fetchBytes (url, onProgress) {
|
||||
let timer = null
|
||||
const job = fetchWithProgress(url, onProgress).catch(async () => {
|
||||
const blob = await d.blobFromSrc(url)
|
||||
const dataUrl = blob ? await d.blobToDataUrl(blob) : ''
|
||||
return dataUrl ? { ok: true, dataUrl, mime: String((blob && blob.type) || '') } : { ok: false, error: 'empty-bytes' }
|
||||
})
|
||||
const timeout = new Promise((resolve) => { timer = setTimeout(() => resolve({ ok: false, error: 'timeout' }), timeoutMs) })
|
||||
try { return await Promise.race([job, timeout]) } catch (e) { return { ok: false, error: String((e && e.message) || 'fetch-failed') } } finally { try { if (timer) clearTimeout(timer) } catch (e2) { /* ignore */ } }
|
||||
}
|
||||
}
|
||||
|
||||
global.IWBImageIngest = { create, STATE, fetchWithProgress, createFetchBytes }
|
||||
})(typeof window !== 'undefined' ? window : globalThis)
|
||||
Reference in new issue
Block a user