|
|
const MAX_UPLOAD_BYTES = 4 * 1024 ** 3;
|
|
|
const MIN_UPLOAD_BYTES = 1024 ** 2;
|
|
|
const STORAGE_PREFIX = 'virt-cloud:template-upload:v1:';
|
|
|
const HEX_32 = /^[a-f0-9]{32}$/;
|
|
|
const IDEMPOTENCY_KEY = /^[A-Za-z0-9_.-]{16,128}$/;
|
|
|
|
|
|
function hex(bytes) {
|
|
|
return Array.from(new Uint8Array(bytes), value => value.toString(16)
|
|
|
.padStart(2, '0')).join('');
|
|
|
}
|
|
|
|
|
|
function randomKey(cryptoImpl) {
|
|
|
const suffix = typeof cryptoImpl?.randomUUID === 'function'
|
|
|
? cryptoImpl.randomUUID()
|
|
|
: hex(cryptoImpl.getRandomValues(new Uint8Array(16)));
|
|
|
return `template-upload-${suffix}`;
|
|
|
}
|
|
|
|
|
|
async function uploadFingerprint(metadata, cryptoImpl) {
|
|
|
if (!cryptoImpl?.subtle || typeof cryptoImpl.subtle.digest !== 'function') {
|
|
|
throw new Error('当前浏览器不支持安全的上传标识计算,请升级浏览器。');
|
|
|
}
|
|
|
const encoded = new TextEncoder().encode(JSON.stringify(metadata));
|
|
|
return hex(await cryptoImpl.subtle.digest('SHA-256', encoded));
|
|
|
}
|
|
|
|
|
|
function readRecord(storage, key, cryptoImpl) {
|
|
|
let record = null;
|
|
|
try {
|
|
|
record = JSON.parse(storage?.getItem(key) || 'null');
|
|
|
} catch {}
|
|
|
if (!record || !IDEMPOTENCY_KEY.test(record.requestKey || '')
|
|
|
|| (record.intentId && !HEX_32.test(record.intentId))) {
|
|
|
record = { requestKey: randomKey(cryptoImpl), intentId: '' };
|
|
|
}
|
|
|
try { storage?.setItem(key, JSON.stringify(record)); } catch {}
|
|
|
return record;
|
|
|
}
|
|
|
|
|
|
function persistRecord(storage, key, record) {
|
|
|
try { storage?.setItem(key, JSON.stringify(record)); } catch {}
|
|
|
}
|
|
|
|
|
|
function clearRecord(storage, key) {
|
|
|
try { storage?.removeItem(key); } catch {}
|
|
|
}
|
|
|
|
|
|
function versionedWorkerUrl() {
|
|
|
const current = new URL(import.meta.url);
|
|
|
const version = current.searchParams.get('v');
|
|
|
const worker = new URL('./template-upload-hash-worker.js', current);
|
|
|
worker.search = '';
|
|
|
if (version) worker.searchParams.set('v', version);
|
|
|
return worker.href;
|
|
|
}
|
|
|
|
|
|
function beginFileHash(file, onProgress, WorkerImpl) {
|
|
|
if (typeof WorkerImpl !== 'function') {
|
|
|
throw new Error('当前浏览器不支持后台校验云镜像,请升级浏览器。');
|
|
|
}
|
|
|
const worker = new WorkerImpl(versionedWorkerUrl(), { type: 'module' });
|
|
|
let settled = false;
|
|
|
let resolvePromise;
|
|
|
let rejectPromise;
|
|
|
const promise = new Promise((resolve, reject) => {
|
|
|
resolvePromise = resolve;
|
|
|
rejectPromise = reject;
|
|
|
});
|
|
|
const finish = (callback, value) => {
|
|
|
if (settled) return;
|
|
|
settled = true;
|
|
|
worker.terminate();
|
|
|
callback(value);
|
|
|
};
|
|
|
worker.addEventListener('message', event => {
|
|
|
if (event.data?.type === 'progress') {
|
|
|
onProgress(Number(event.data.loaded || 0),
|
|
|
Number(event.data.total || file.size));
|
|
|
return;
|
|
|
}
|
|
|
if (event.data?.type === 'complete'
|
|
|
&& /^[a-f0-9]{64}$/.test(event.data.sha256 || '')) {
|
|
|
finish(resolvePromise, event.data.sha256);
|
|
|
return;
|
|
|
}
|
|
|
if (event.data?.type === 'error') {
|
|
|
finish(rejectPromise, new Error(String(event.data.message
|
|
|
|| '云镜像 SHA-256 计算失败。')));
|
|
|
}
|
|
|
});
|
|
|
worker.addEventListener('error', () => {
|
|
|
finish(rejectPromise, new Error('云镜像 SHA-256 后台计算失败。'));
|
|
|
});
|
|
|
worker.postMessage({ file });
|
|
|
return {
|
|
|
promise,
|
|
|
cancel() {
|
|
|
finish(rejectPromise, new Error('云镜像校验已取消。'));
|
|
|
},
|
|
|
};
|
|
|
}
|
|
|
|
|
|
/** Upload one S3 part without cookies or panel authorization headers. */
|
|
|
export function putMultipartPart(url, blob, onProgress,
|
|
|
XMLHttpRequestImpl = globalThis.XMLHttpRequest, signal = null) {
|
|
|
const parsed = new URL(url);
|
|
|
if (parsed.protocol !== 'https:' || !parsed.hostname
|
|
|
|| parsed.username || parsed.password || parsed.hash) {
|
|
|
return Promise.reject(new Error('对象存储分片地址无效。'));
|
|
|
}
|
|
|
return new Promise((resolve, reject) => {
|
|
|
const request = new XMLHttpRequestImpl();
|
|
|
let settled = false;
|
|
|
const finish = (callback, value) => {
|
|
|
if (settled) return;
|
|
|
settled = true;
|
|
|
signal?.removeEventListener?.('abort', abort);
|
|
|
callback(value);
|
|
|
};
|
|
|
const abort = () => request.abort();
|
|
|
request.open('PUT', parsed.href, true);
|
|
|
request.withCredentials = false;
|
|
|
request.upload.addEventListener('progress', event => {
|
|
|
if (event.lengthComputable && typeof onProgress === 'function') {
|
|
|
onProgress(event.loaded, event.total);
|
|
|
}
|
|
|
});
|
|
|
request.addEventListener('load', () => {
|
|
|
if (request.status < 200 || request.status >= 300) {
|
|
|
finish(reject, new Error(`对象存储拒绝分片上传 (${request.status})。`));
|
|
|
return;
|
|
|
}
|
|
|
const etag = String(request.getResponseHeader('ETag') || '').trim();
|
|
|
if (!etag || etag.length > 256 || /[\x00-\x1f\x7f]/.test(etag)) {
|
|
|
finish(reject, new Error(
|
|
|
'对象存储未向浏览器公开 ETag。请为 Bucket CORS 添加 PUT,'
|
|
|
+ '并在 ExposeHeaders 中加入 ETag。'
|
|
|
));
|
|
|
return;
|
|
|
}
|
|
|
finish(resolve, etag);
|
|
|
});
|
|
|
request.addEventListener('error', () => finish(reject,
|
|
|
new Error('连接对象存储失败;已完成分片会由面板账本保留。')));
|
|
|
request.addEventListener('abort', () => finish(reject,
|
|
|
new Error('云镜像分片上传已取消。')));
|
|
|
signal?.addEventListener?.('abort', abort, { once: true });
|
|
|
if (signal?.aborted) abort();
|
|
|
else request.send(blob);
|
|
|
});
|
|
|
}
|
|
|
|
|
|
export function multipartPartSchedule(plan, intentId, fileSize) {
|
|
|
const partSize = Number(plan?.part_size_bytes);
|
|
|
const partCount = Number(plan?.part_count);
|
|
|
const urls = plan?.part_urls;
|
|
|
if (!plan?.intent || plan.intent.id !== intentId
|
|
|
|| !Number.isSafeInteger(partSize) || partSize < 1
|
|
|
|| !Number.isSafeInteger(partCount) || partCount < 1
|
|
|
|| !Array.isArray(urls) || urls.length !== partCount
|
|
|
|| partCount !== Math.ceil(fileSize / partSize)
|
|
|
|| urls.some(url => typeof url !== 'string' || !url)) {
|
|
|
throw new Error('面板返回了无效的对象存储上传计划。');
|
|
|
}
|
|
|
const completed = new Map();
|
|
|
if (!Array.isArray(plan.intent.parts)) {
|
|
|
throw new Error('面板返回的上传分片账本无效。');
|
|
|
}
|
|
|
for (const part of plan.intent.parts) {
|
|
|
const partNumber = Number(part?.part_number);
|
|
|
const size = Number(part?.size_bytes);
|
|
|
const start = (partNumber - 1) * partSize;
|
|
|
const expected = Math.min(fileSize, start + partSize) - start;
|
|
|
if (!Number.isSafeInteger(partNumber) || partNumber < 1
|
|
|
|| partNumber > partCount || !Number.isSafeInteger(size)
|
|
|
|| size !== expected || completed.has(partNumber)) {
|
|
|
throw new Error('面板返回的上传分片账本无效。');
|
|
|
}
|
|
|
completed.set(partNumber, size);
|
|
|
}
|
|
|
return urls.map((url, index) => {
|
|
|
const partNumber = index + 1;
|
|
|
const start = index * partSize;
|
|
|
const end = Math.min(fileSize, start + partSize);
|
|
|
return Object.freeze({
|
|
|
partNumber, start, end, url,
|
|
|
size: end - start,
|
|
|
completed: completed.has(partNumber),
|
|
|
});
|
|
|
});
|
|
|
}
|
|
|
|
|
|
function percentage(value, total) {
|
|
|
return total > 0 ? Math.min(100, Math.max(0, value * 100 / total)) : 0;
|
|
|
}
|
|
|
|
|
|
function taskReceipt(intent) {
|
|
|
const taskId = Number(intent?.task_id || 0);
|
|
|
return Number.isSafeInteger(taskId) && taskId > 0
|
|
|
? { task_id: String(taskId), status_url: `/api/v1/tasks/${taskId}` }
|
|
|
: null;
|
|
|
}
|
|
|
|
|
|
export function mountTemplateUpload(options) {
|
|
|
const config = options && typeof options === 'object' ? options : {};
|
|
|
const documentImpl = config.documentImpl || globalThis.document;
|
|
|
const cryptoImpl = config.cryptoImpl || globalThis.crypto;
|
|
|
let storage = config.storage;
|
|
|
if (storage === undefined) {
|
|
|
try { storage = globalThis.localStorage; } catch { storage = null; }
|
|
|
}
|
|
|
const WorkerImpl = config.WorkerImpl || globalThis.Worker;
|
|
|
const XMLHttpRequestImpl = config.XMLHttpRequestImpl
|
|
|
|| globalThis.XMLHttpRequest;
|
|
|
const requestRuntime = config.requestRuntime;
|
|
|
const notice = config.notice;
|
|
|
const onTask = config.onTask;
|
|
|
const onRemoteSettled = config.onRemoteSettled;
|
|
|
const confirmDanger = config.confirmDanger;
|
|
|
const canManage = config.canManage === true;
|
|
|
const form = documentImpl.getElementById('template-upload');
|
|
|
if (!form || !requestRuntime || typeof requestRuntime.mutate !== 'function'
|
|
|
|| typeof requestRuntime.read !== 'function'
|
|
|
|| typeof notice !== 'function' || typeof onTask !== 'function') {
|
|
|
throw new TypeError('Template upload module dependencies are incomplete.');
|
|
|
}
|
|
|
const nameInput = documentImpl.getElementById('template-upload-name');
|
|
|
const systemInput = documentImpl.getElementById('template-upload-system');
|
|
|
const fileInput = documentImpl.getElementById('template-upload-file');
|
|
|
const button = documentImpl.getElementById('template-upload-button');
|
|
|
const progressBox = documentImpl.getElementById('template-upload-progress');
|
|
|
const progressBar = documentImpl.getElementById('template-upload-progress-bar');
|
|
|
const progressLabel = documentImpl.getElementById('template-upload-progress-label');
|
|
|
const hashLabel = documentImpl.getElementById('template-upload-hash-label');
|
|
|
const activeList = documentImpl.getElementById('template-upload-active-list');
|
|
|
const activeEmpty = documentImpl.getElementById('template-upload-active-empty');
|
|
|
if ([nameInput, systemInput, fileInput, button, progressBox, progressBar,
|
|
|
progressLabel, hashLabel, activeList, activeEmpty].some(value => !value)) {
|
|
|
throw new TypeError('Template upload controls are incomplete.');
|
|
|
}
|
|
|
|
|
|
let activeController = null;
|
|
|
let activeHash = null;
|
|
|
const busy = () => typeof config.isBusy === 'function'
|
|
|
&& config.isBusy() === true;
|
|
|
const setBusy = value => config.setBusy?.(value);
|
|
|
const setControls = disabled => {
|
|
|
nameInput.disabled = disabled || !canManage;
|
|
|
systemInput.disabled = disabled || !canManage;
|
|
|
fileInput.disabled = disabled || !canManage;
|
|
|
button.disabled = disabled || !canManage;
|
|
|
};
|
|
|
const renderProgress = (uploaded, total, hashed = 0) => {
|
|
|
progressBox.hidden = false;
|
|
|
const uploadPercent = percentage(uploaded, total);
|
|
|
const hashPercent = percentage(hashed, total);
|
|
|
progressBar.value = uploadPercent;
|
|
|
progressLabel.textContent = uploaded >= total
|
|
|
? '对象存储上传完成,正在确认完整性…'
|
|
|
: `对象存储直传 ${uploadPercent.toFixed(1)}%`;
|
|
|
hashLabel.textContent = `SHA-256 校验 ${hashPercent.toFixed(1)}%`;
|
|
|
};
|
|
|
|
|
|
const stateLabels = Object.freeze({
|
|
|
prepared: '等待处理', fetching: '正在下载', uploading: '上传中',
|
|
|
upload_failed: '可继续', source_failed: '可重试',
|
|
|
source_verifying: '正在核对', source_unknown: '需要核查',
|
|
|
source_ready: '等待分发', distributing: '正在分发',
|
|
|
distribution_failed: '分发失败', cancel_pending: '正在取消',
|
|
|
});
|
|
|
const renderActive = intents => {
|
|
|
activeList.replaceChildren();
|
|
|
activeEmpty.hidden = intents.length > 0;
|
|
|
for (const intent of intents) {
|
|
|
if (!HEX_32.test(intent?.id || '')) continue;
|
|
|
const row = documentImpl.createElement('div');
|
|
|
row.className = 'cloud-template-upload-active-row';
|
|
|
const identity = documentImpl.createElement('div');
|
|
|
const title = documentImpl.createElement('strong');
|
|
|
title.textContent = String(intent.name || intent.source_filename || intent.id);
|
|
|
const detail = documentImpl.createElement('small');
|
|
|
const total = Number(intent.source_size_bytes || 0);
|
|
|
const uploaded = Number(intent.uploaded_bytes || 0);
|
|
|
detail.textContent = intent.source_kind === 'remote'
|
|
|
? `官方地址 · ${stateLabels[intent.state] || '状态待核查'}`
|
|
|
: `${stateLabels[intent.state] || '状态待核查'} · `
|
|
|
+ `${(uploaded / 1024 ** 2).toFixed(1)} / `
|
|
|
+ `${(total / 1024 ** 2).toFixed(1)} MiB`;
|
|
|
identity.append(title, detail);
|
|
|
row.append(identity);
|
|
|
if (intent.source_kind === 'remote' && intent.can_resume === true
|
|
|
&& canManage) {
|
|
|
const resume = documentImpl.createElement('button');
|
|
|
resume.type = 'button';
|
|
|
resume.className = 'btn btn-sm btn-outline-primary';
|
|
|
resume.textContent = '继续导入';
|
|
|
resume.addEventListener('click', async () => {
|
|
|
if (busy() || activeController) return;
|
|
|
setBusy(true); resume.disabled = true;
|
|
|
try {
|
|
|
const result = await requestRuntime.mutate(
|
|
|
`/api/v1/admin/cloud-template-imports/${encodeURIComponent(intent.id)}/resume`,
|
|
|
{ method: 'POST', body: {},
|
|
|
timeoutMs: 3600000,
|
|
|
idempotencyKey: `template-remote-resume-${intent.id}` }
|
|
|
);
|
|
|
onRemoteSettled?.();
|
|
|
const task = taskReceipt(result);
|
|
|
if (task) {
|
|
|
onTask(task, 'remote');
|
|
|
} else if (result?.protection_pending === true) {
|
|
|
notice('官方镜像主副本已保存,正在建立第二份对象存储副本。');
|
|
|
} else {
|
|
|
throw new Error('面板返回了无效的模板发布状态。');
|
|
|
}
|
|
|
} catch (error) {
|
|
|
notice(error instanceof Error ? error.message : '继续导入失败。', true);
|
|
|
} finally {
|
|
|
setBusy(false);
|
|
|
await loadActive();
|
|
|
}
|
|
|
});
|
|
|
row.append(resume);
|
|
|
}
|
|
|
if (intent.can_cancel === true && canManage) {
|
|
|
const cancel = documentImpl.createElement('button');
|
|
|
cancel.type = 'button';
|
|
|
cancel.className = 'btn btn-sm btn-outline-danger';
|
|
|
const remote = intent.source_kind === 'remote';
|
|
|
cancel.textContent = remote ? '取消导入' : '取消上传';
|
|
|
cancel.addEventListener('click', async () => {
|
|
|
if (busy() || activeController) return;
|
|
|
if (typeof confirmDanger === 'function' && !await confirmDanger({
|
|
|
tone: 'warning',
|
|
|
title: `${remote ? '取消导入' : '取消上传'} · ${String(intent.name || intent.source_filename)}`,
|
|
|
message: remote
|
|
|
? '仅会取消尚未进入对象存储发布阶段的下载;已经开始核对的对象不会被删除。'
|
|
|
: '已上传但尚未发布的对象存储分片会被终止;此操作不会删除已经发布的云镜像。',
|
|
|
confirmText: remote ? '取消导入' : '取消上传',
|
|
|
})) return;
|
|
|
setBusy(true); cancel.disabled = true;
|
|
|
try {
|
|
|
await requestRuntime.mutate(
|
|
|
`/api/v1/admin/cloud-template-imports/${encodeURIComponent(intent.id)}`,
|
|
|
{ method: 'DELETE', body: {},
|
|
|
idempotencyKey: `template-upload-cancel-${intent.id}` }
|
|
|
);
|
|
|
if (remote) onRemoteSettled?.();
|
|
|
notice(remote ? '未完成的远程导入已取消。'
|
|
|
: '未完成的云镜像上传已取消。');
|
|
|
} catch (error) {
|
|
|
notice(error instanceof Error ? error.message : '取消上传失败。', true);
|
|
|
} finally {
|
|
|
setBusy(false);
|
|
|
await loadActive();
|
|
|
}
|
|
|
});
|
|
|
row.append(cancel);
|
|
|
}
|
|
|
activeList.append(row);
|
|
|
}
|
|
|
};
|
|
|
const loadActive = async () => {
|
|
|
try {
|
|
|
const payload = await requestRuntime.read(
|
|
|
'/api/v1/admin/cloud-template-imports'
|
|
|
);
|
|
|
renderActive(Array.isArray(payload?.intents) ? payload.intents : []);
|
|
|
} catch (error) {
|
|
|
activeList.replaceChildren();
|
|
|
activeEmpty.hidden = false;
|
|
|
activeEmpty.textContent = error instanceof Error
|
|
|
? error.message : '无法读取未完成上传。';
|
|
|
}
|
|
|
};
|
|
|
|
|
|
const submit = async event => {
|
|
|
event.preventDefault();
|
|
|
if (!canManage || busy() || activeController) return;
|
|
|
const name = nameInput.value.trim();
|
|
|
const selection = systemInput.value;
|
|
|
const separator = selection.indexOf('|');
|
|
|
const osFamily = separator > 0 ? selection.slice(0, separator) : '';
|
|
|
const osVersion = separator > 0 ? selection.slice(separator + 1) : '';
|
|
|
const file = fileInput.files?.[0];
|
|
|
if (!name || name.length > 64) {
|
|
|
notice('请填写 1 至 64 个字符的模板名称。', true); return;
|
|
|
}
|
|
|
if (!osFamily || !osVersion) {
|
|
|
notice('请选择镜像实际对应的系统和版本。', true); return;
|
|
|
}
|
|
|
if (!file || !file.name.toLowerCase().endsWith('.qcow2')) {
|
|
|
notice('请选择扩展名为 .qcow2 的云镜像文件。', true); return;
|
|
|
}
|
|
|
if (!Number.isSafeInteger(file.size) || file.size < MIN_UPLOAD_BYTES
|
|
|
|| file.size > MAX_UPLOAD_BYTES) {
|
|
|
notice('QCOW2 文件必须在 1 MiB 至 4 GiB 之间。', true); return;
|
|
|
}
|
|
|
activeController = new AbortController();
|
|
|
setBusy(true);
|
|
|
setControls(true);
|
|
|
let hashedBytes = 0;
|
|
|
let uploadedBytes = 0;
|
|
|
renderProgress(0, file.size, 0);
|
|
|
notice('正在建立受控上传记录并计算 SHA-256…');
|
|
|
try {
|
|
|
activeHash = beginFileHash(file, (loaded, total) => {
|
|
|
hashedBytes = loaded;
|
|
|
renderProgress(uploadedBytes, total, hashedBytes);
|
|
|
}, WorkerImpl);
|
|
|
const sha256 = await activeHash.promise;
|
|
|
hashedBytes = file.size;
|
|
|
renderProgress(uploadedBytes, file.size, hashedBytes);
|
|
|
const fingerprint = await uploadFingerprint({
|
|
|
name, osFamily, osVersion, filename: file.name,
|
|
|
size: file.size, lastModified: Number(file.lastModified || 0),
|
|
|
sha256,
|
|
|
}, cryptoImpl);
|
|
|
const storageKey = STORAGE_PREFIX + fingerprint;
|
|
|
const record = readRecord(storage, storageKey, cryptoImpl);
|
|
|
const intent = await requestRuntime.mutate(
|
|
|
'/api/v1/admin/cloud-template-imports', {
|
|
|
method: 'POST',
|
|
|
body: {
|
|
|
name,
|
|
|
os_family: osFamily,
|
|
|
os_version: osVersion,
|
|
|
filename: file.name,
|
|
|
size_bytes: file.size,
|
|
|
last_modified: Number(file.lastModified || 0),
|
|
|
sha256,
|
|
|
},
|
|
|
idempotencyKey: record.requestKey,
|
|
|
signal: activeController.signal,
|
|
|
}
|
|
|
);
|
|
|
if (!HEX_32.test(intent?.id || '')) {
|
|
|
throw new Error('面板返回了无效的上传记录。');
|
|
|
}
|
|
|
record.intentId = intent.id;
|
|
|
persistRecord(storage, storageKey, record);
|
|
|
const existingTask = taskReceipt(intent);
|
|
|
if (existingTask) {
|
|
|
clearRecord(storage, storageKey);
|
|
|
fileInput.value = '';
|
|
|
onTask(existingTask);
|
|
|
return;
|
|
|
}
|
|
|
const state = String(intent.state || '');
|
|
|
const unknownCreate = state === 'source_unknown'
|
|
|
&& Number(intent.part_count || 0) === 0;
|
|
|
if (unknownCreate
|
|
|
|| !['source_ready', 'source_verifying', 'source_unknown'].includes(
|
|
|
state
|
|
|
)) {
|
|
|
const plan = await requestRuntime.mutate(
|
|
|
`/api/v1/admin/cloud-template-imports/${encodeURIComponent(intent.id)}/upload`, {
|
|
|
method: 'POST', body: {}, signal: activeController.signal,
|
|
|
idempotencyKey: `${record.requestKey}.start`,
|
|
|
}
|
|
|
);
|
|
|
const schedule = multipartPartSchedule(plan, intent.id, file.size);
|
|
|
for (const item of schedule) {
|
|
|
if (item.completed) {
|
|
|
uploadedBytes += item.size;
|
|
|
renderProgress(uploadedBytes, file.size, hashedBytes);
|
|
|
continue;
|
|
|
}
|
|
|
const part = file.slice(item.start, item.end);
|
|
|
const confirmedBeforePart = uploadedBytes;
|
|
|
const etag = await putMultipartPart(
|
|
|
item.url,
|
|
|
part,
|
|
|
loaded => {
|
|
|
renderProgress(
|
|
|
confirmedBeforePart + loaded,
|
|
|
file.size,
|
|
|
hashedBytes
|
|
|
);
|
|
|
},
|
|
|
XMLHttpRequestImpl,
|
|
|
activeController.signal
|
|
|
);
|
|
|
uploadedBytes = confirmedBeforePart + item.size;
|
|
|
renderProgress(uploadedBytes, file.size, hashedBytes);
|
|
|
await requestRuntime.mutate(
|
|
|
`/api/v1/admin/cloud-template-imports/${encodeURIComponent(intent.id)}/parts/${item.partNumber}`, {
|
|
|
method: 'PUT',
|
|
|
body: { size_bytes: part.size, etag },
|
|
|
signal: activeController.signal,
|
|
|
idempotencyKey: `${record.requestKey}.part.${item.partNumber}`,
|
|
|
}
|
|
|
);
|
|
|
}
|
|
|
} else {
|
|
|
uploadedBytes = file.size;
|
|
|
renderProgress(uploadedBytes, file.size, hashedBytes);
|
|
|
}
|
|
|
|
|
|
notice('对象存储上传完成,正在核对制品身份…');
|
|
|
renderProgress(file.size, file.size, hashedBytes);
|
|
|
notice('正在核对对象存储分片并创建宿主机分发任务…');
|
|
|
const result = await requestRuntime.mutate(
|
|
|
`/api/v1/admin/cloud-template-imports/${encodeURIComponent(intent.id)}/complete`, {
|
|
|
method: 'POST', body: { sha256 },
|
|
|
signal: activeController.signal,
|
|
|
idempotencyKey: `${record.requestKey}.complete`,
|
|
|
}
|
|
|
);
|
|
|
clearRecord(storage, storageKey);
|
|
|
fileInput.value = '';
|
|
|
const task = taskReceipt(result);
|
|
|
if (task) {
|
|
|
onTask(task);
|
|
|
} else if (result?.protection_pending === true) {
|
|
|
notice('镜像上传已完成,正在建立第二份对象存储副本。');
|
|
|
} else {
|
|
|
throw new Error('面板返回了无效的模板发布状态。');
|
|
|
}
|
|
|
} catch (error) {
|
|
|
activeHash?.cancel();
|
|
|
await activeHash?.promise.catch(() => {});
|
|
|
notice(error instanceof Error ? error.message : '云镜像上传失败。', true);
|
|
|
} finally {
|
|
|
activeHash = null;
|
|
|
activeController = null;
|
|
|
setBusy(false);
|
|
|
setControls(false);
|
|
|
await loadActive();
|
|
|
}
|
|
|
};
|
|
|
|
|
|
form.addEventListener('submit', submit);
|
|
|
setControls(false);
|
|
|
void loadActive();
|
|
|
return Object.freeze({
|
|
|
refresh: loadActive,
|
|
|
destroy() {
|
|
|
form.removeEventListener('submit', submit);
|
|
|
activeController?.abort();
|
|
|
activeHash?.cancel();
|
|
|
},
|
|
|
});
|
|
|
}
|