Add browser download confirmation and parameterized client startup
This commit is contained in:
1 parent
028fec64be
commit
3cf197a98c
25 files changed
+568
-46
No files matched your search
@@ -21,7 +21,7 @@ export function linkReferrer(source, target) {
|
||||
}
|
||||
export function requestFor(url, referrer = '', filename = '') {
|
||||
const normalized = httpUrl(url); if (!normalized) throw new Error('仅支持不含账号密码的 HTTP/HTTPS 下载地址');
|
||||
return {action: 'add', request_id: crypto.randomUUID(), method: 'GET', url: normalized,
|
||||
return {action: 'add', confirmation: 'required', request_id: crypto.randomUUID(), method: 'GET', url: normalized,
|
||||
referrer: httpUrl(referrer), filename: filename.split(/[\\/]/).pop().slice(0, 240), user_agent: navigator.userAgent};
|
||||
}
|
||||
const key = id => `pending_${id}`;
|
||||
@@ -54,6 +54,23 @@ async function finish(api, record) {
|
||||
return {status: 'recovery_pending', message: '桌面已接收任务,浏览器取消失败,将继续重试'};
|
||||
}
|
||||
}
|
||||
const acknowledged = (reply, request) => reply?.status === 'accepted' && reply.request_id === request.request_id
|
||||
&& typeof reply.task_id === 'string' && /^[1-9][0-9]{0,18}$/.test(reply.task_id);
|
||||
async function abandon(api, record, sendNative) {
|
||||
try {
|
||||
const reply = await sendNative({action: 'cancel', request_id: record.request.request_id});
|
||||
if (acknowledged(reply, record.request)) {
|
||||
record.phase = 'cancel'; record.task_id = reply.task_id;
|
||||
try { await save(api, record); } catch {} // The existing request journal still permits an idempotent retry.
|
||||
return finish(api, record);
|
||||
}
|
||||
if (reply?.status === 'awaiting_confirmation' && reply.request_id === record.request.request_id) {
|
||||
record.phase = 'confirming'; try { await save(api, record); } catch {}
|
||||
return {status: 'awaiting_confirmation', message: reply.message};
|
||||
}
|
||||
} catch {} // If the client disappeared, preserve the browser's original download.
|
||||
return restore(api, record, '接管未完成,请检查客户端添加窗口');
|
||||
}
|
||||
export async function handoff(api, item, request, sendNative, saved = null) {
|
||||
const record = saved || {id: item.id, request, phase: 'pause', created: Date.now(), original: httpUrl(item.finalUrl || item.url)};
|
||||
if (record.phase === 'cancel') return finish(api, record);
|
||||
@@ -62,13 +79,21 @@ export async function handoff(api, item, request, sendNative, saved = null) {
|
||||
// Journal intent before pausing, so a service-worker restart cannot strand a paused task.
|
||||
await save(api, record);
|
||||
if (!item.paused) await api.downloads.pause(item.id);
|
||||
record.phase = 'sending'; await save(api, record);
|
||||
const reply = await sendNative(record.request);
|
||||
if (reply?.status !== 'accepted' || reply.request_id !== request.request_id || typeof reply.task_id !== 'string' || !/^[1-9][0-9]{0,18}$/.test(reply.task_id))
|
||||
const confirming = record.phase === 'confirming';
|
||||
if (!confirming) record.phase = 'sending';
|
||||
await save(api, record);
|
||||
const reply = await sendNative(confirming ? {action: 'query', request_id: request.request_id} : record.request);
|
||||
if (reply?.status === 'awaiting_confirmation' && reply.request_id === request.request_id) {
|
||||
record.phase = 'confirming'; await save(api, record);
|
||||
return {status: 'awaiting_confirmation', message: reply.message || '请在客户端确认添加任务;取消后将恢复浏览器下载'};
|
||||
}
|
||||
if (!acknowledged(reply, request))
|
||||
return restore(api, record, reply?.message || '桌面未确认接管');
|
||||
record.phase = 'cancel'; record.task_id = reply.task_id; await save(api, record);
|
||||
record.phase = 'cancel'; record.task_id = reply.task_id;
|
||||
try { await save(api, record); } catch {}
|
||||
return finish(api, record);
|
||||
} catch (error) {
|
||||
if (record.phase === 'sending' || record.phase === 'confirming') return abandon(api, record, sendNative);
|
||||
return restore(api, record, error.message || '本地通信失败,结果未确认');
|
||||
}
|
||||
}
|
||||
@@ -76,9 +101,16 @@ export async function recover(api, sendNative, auto) {
|
||||
const values = await api.storage.local.get(null), outcomes = [];
|
||||
for (const record of Object.values(values).filter(v => v && v.request && Number.isInteger(v.id) && v.phase)) {
|
||||
const item = await current(api, record.id);
|
||||
if (!item || httpUrl(item.finalUrl || item.url) !== record.original || item.state !== 'in_progress') { await remove(api, record.id); continue; }
|
||||
if (record.phase === 'sending' && !item.paused) {
|
||||
await remove(api, record.id); outcomes.push({status: 'warning', message: '浏览器下载已恢复,已停止重复接管;请核对桌面任务列表'}); continue;
|
||||
if (!item || httpUrl(item.finalUrl || item.url) !== record.original || item.state !== 'in_progress') {
|
||||
if (record.phase === 'confirming' || record.phase === 'sending') outcomes.push(await abandon(api, record, sendNative));
|
||||
else await remove(api, record.id);
|
||||
continue;
|
||||
}
|
||||
if (record.phase === 'confirming' && (!item.paused || !auto || Date.now() - record.created > 120000)) {
|
||||
outcomes.push(await abandon(api, record, sendNative)); continue;
|
||||
}
|
||||
if (record.phase === 'sending' && (!item.paused || !auto || Date.now() - record.created > 120000)) {
|
||||
outcomes.push(await abandon(api, record, sendNative)); continue;
|
||||
}
|
||||
if (record.phase === 'cancel') outcomes.push(await finish(api, record));
|
||||
else if (!auto || Date.now() - record.created > 120000 || record.phase === 'resume') outcomes.push(await restore(api, record, '接管未完成'));
|
||||
|
||||
Reference in new issue
Block a user