|
|
|
|
@ -0,0 +1,375 @@
|
|
|
|
|
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);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function validatePlan(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('面板返回了无效的对象存储上传计划。');
|
|
|
|
|
}
|
|
|
|
|
return { partSize, partCount, urls };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
const storage = config.storage || globalThis.localStorage;
|
|
|
|
|
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 canManage = config.canManage === true;
|
|
|
|
|
const form = documentImpl.getElementById('template-upload');
|
|
|
|
|
if (!form || !requestRuntime || typeof requestRuntime.mutate !== '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');
|
|
|
|
|
if ([nameInput, systemInput, fileInput, button, progressBox, progressBar,
|
|
|
|
|
progressLabel, hashLabel].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 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 {
|
|
|
|
|
const fingerprint = await uploadFingerprint({
|
|
|
|
|
name, osFamily, osVersion, filename: file.name,
|
|
|
|
|
size: file.size, lastModified: Number(file.lastModified || 0),
|
|
|
|
|
}, 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),
|
|
|
|
|
},
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
activeHash = beginFileHash(file, (loaded, total) => {
|
|
|
|
|
hashedBytes = loaded;
|
|
|
|
|
renderProgress(uploadedBytes, total, hashedBytes);
|
|
|
|
|
}, WorkerImpl);
|
|
|
|
|
|
|
|
|
|
if (!['source_ready', 'source_verifying', 'source_unknown'].includes(
|
|
|
|
|
String(intent.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 validated = validatePlan(plan, intent.id, file.size);
|
|
|
|
|
for (let index = 0; index < validated.partCount; index += 1) {
|
|
|
|
|
const start = index * validated.partSize;
|
|
|
|
|
const end = Math.min(file.size, start + validated.partSize);
|
|
|
|
|
const part = file.slice(start, end);
|
|
|
|
|
const etag = await putMultipartPart(
|
|
|
|
|
validated.urls[index],
|
|
|
|
|
part,
|
|
|
|
|
loaded => {
|
|
|
|
|
uploadedBytes = start + loaded;
|
|
|
|
|
renderProgress(uploadedBytes, file.size, hashedBytes);
|
|
|
|
|
},
|
|
|
|
|
XMLHttpRequestImpl,
|
|
|
|
|
activeController.signal
|
|
|
|
|
);
|
|
|
|
|
uploadedBytes = end;
|
|
|
|
|
renderProgress(uploadedBytes, file.size, hashedBytes);
|
|
|
|
|
await requestRuntime.mutate(
|
|
|
|
|
`/api/v1/admin/cloud-template-imports/${encodeURIComponent(intent.id)}/parts/${index + 1}`, {
|
|
|
|
|
method: 'PUT',
|
|
|
|
|
body: { size_bytes: part.size, etag },
|
|
|
|
|
signal: activeController.signal,
|
|
|
|
|
idempotencyKey: `${record.requestKey}.part.${index + 1}`,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
uploadedBytes = file.size;
|
|
|
|
|
renderProgress(uploadedBytes, file.size, hashedBytes);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
notice('对象存储上传完成,正在等待 SHA-256 校验…');
|
|
|
|
|
const sha256 = await activeHash.promise;
|
|
|
|
|
hashedBytes = file.size;
|
|
|
|
|
renderProgress(file.size, file.size, hashedBytes);
|
|
|
|
|
notice('正在核对对象存储分片并创建宿主机分发任务…');
|
|
|
|
|
const task = await requestRuntime.mutate(
|
|
|
|
|
`/api/v1/admin/cloud-template-imports/${encodeURIComponent(intent.id)}/complete`, {
|
|
|
|
|
method: 'POST', body: { sha256 }, response: 'task',
|
|
|
|
|
signal: activeController.signal,
|
|
|
|
|
idempotencyKey: `${record.requestKey}.complete`,
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
clearRecord(storage, storageKey);
|
|
|
|
|
fileInput.value = '';
|
|
|
|
|
onTask(task);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
activeHash?.cancel();
|
|
|
|
|
await activeHash?.promise.catch(() => {});
|
|
|
|
|
notice(error instanceof Error ? error.message : '云镜像上传失败。', true);
|
|
|
|
|
} finally {
|
|
|
|
|
activeHash = null;
|
|
|
|
|
activeController = null;
|
|
|
|
|
setBusy(false);
|
|
|
|
|
setControls(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
form.addEventListener('submit', submit);
|
|
|
|
|
setControls(false);
|
|
|
|
|
return Object.freeze({
|
|
|
|
|
destroy() {
|
|
|
|
|
form.removeEventListener('submit', submit);
|
|
|
|
|
activeController?.abort();
|
|
|
|
|
activeHash?.cancel();
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|