feat(template): upload images directly with multipart

go
codex 3 weeks ago
parent dd574c308f
commit eb225204ee

@ -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();
},
});
}

@ -284,6 +284,59 @@
padding-top: 18px;
}
.cloud-template-upload-progress {
margin-top: 16px;
padding: 13px 14px;
border: 1px solid #d7e5ec;
border-radius: 10px;
background: #f6fbfc;
}
.cloud-template-upload-progress[hidden] {
display: none;
}
.cloud-template-upload-progress-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 8px;
color: #395064;
font-size: 13px;
}
.cloud-template-upload-progress-head span {
color: #718397;
white-space: nowrap;
}
.cloud-template-upload-progress progress {
display: block;
width: 100%;
height: 9px;
overflow: hidden;
border: 0;
border-radius: 999px;
background: #dce9ed;
accent-color: #16a3ad;
}
.cloud-template-upload-progress progress::-webkit-progress-bar {
border-radius: 999px;
background: #dce9ed;
}
.cloud-template-upload-progress progress::-webkit-progress-value {
border-radius: 999px;
background: #16a3ad;
}
.cloud-template-upload-progress progress::-moz-progress-bar {
border-radius: 999px;
background: #16a3ad;
}
.cloud-template-catalog-actions {
display: flex;
align-items: center;
@ -1774,6 +1827,12 @@
}
@media (max-width: 767.98px) {
.cloud-template-upload-progress-head {
align-items: flex-start;
flex-direction: column;
gap: 4px;
}
.cloud-template-catalog-actions {
width: 100%;
justify-content: space-between;

@ -19,6 +19,7 @@ const requestRuntime = window.VenixAdminRequestRuntime.create({
csrfToken: () => state.csrf,
taskReceiptValidator: taskUrl,
});
let templateUploadController = null;
const confirmDanger = options => window.VenixAdminDangerDialog.confirm(options);
// Permission state is rendered by the server on the page. API endpoints keep
// the authoritative authorization check; this only controls the UI affordances.
@ -26,7 +27,6 @@ const canManage = root?.dataset.canManage === '1';
const collection = (payload, key = 'items') => Array.isArray(payload) ? payload : Array.isArray(payload?.[key]) ? payload[key] : Array.isArray(payload?.items) ? payload.items : [];
function element(tag, className = '', content) { const node = document.createElement(tag); node.className = className; if (content !== undefined) node.textContent = String(content); return node; }
function notice(message, error = false) { const box = byId('cloud-admin-status'); box.textContent = message; box.className = `alert ${error ? 'alert-danger' : 'alert-info'} cloud-admin-notice`; }
function uploadErrorMessage(payload, status) { if (Array.isArray(payload?.detail)) return payload.detail.map(item => item.msg || '输入错误').join(';'); return String(payload?.detail || payload?.message || payload?.msg || `请求失败 (${status})`); }
async function requestJson(path, options = {}) {
const method = String(options.method || 'GET').toUpperCase();
try {
@ -63,36 +63,6 @@ async function acceptedTask(path, options = {}) {
throw error;
}
}
function uploadApi(path, file, onProgress) {
if (location.protocol !== 'https:' && !['localhost', '127.0.0.1', '[::1]'].includes(location.hostname)) return Promise.reject(new Error('请通过 HTTPS 上传云镜像。'));
return new Promise((resolve, reject) => {
const request = new XMLHttpRequest();
request.open('POST', path, true);
request.withCredentials = true;
request.setRequestHeader('Accept', 'application/json');
request.setRequestHeader('X-Admin-CSRF-Token', state.csrf);
request.setRequestHeader('Content-Type', 'application/octet-stream');
request.upload.addEventListener('progress', event => {
if (event.lengthComputable && typeof onProgress === 'function') onProgress(event.loaded, event.total);
});
request.addEventListener('load', () => {
let payload = null;
try { payload = request.responseText ? JSON.parse(request.responseText) : null; }
catch {
reject(new Error(request.status === 413
? '上传被 Web 服务器拒绝,请提高 PHP 与反代的请求体上限。'
: '服务返回非 JSON 响应,请检查 PHP 和反代配置。'));
return;
}
if (request.status === 401) { location.assign('/auth/login'); reject(new Error('登录已过期。')); return; }
if (request.status < 200 || request.status >= 300 || payload?.ret === 0) { reject(new Error(uploadErrorMessage(payload, request.status))); return; }
resolve(payload);
});
request.addEventListener('error', () => reject(new Error('云镜像上传失败,请检查网络或反代限制。')));
request.addEventListener('abort', () => reject(new Error('云镜像上传已取消。')));
request.send(file);
});
}
function formList(value) { return String(value || '').split(/[\s,]+/).filter(Boolean); }
function profile(host) { return { ...host, ...(host.profile || host.config || {}) }; }
async function loadHosts() {
@ -1406,46 +1376,6 @@ async function importTemplate(event) {
try { const result = await api('/api/v1/admin/cloud-templates/import', { method: 'POST', body }); if (importProgress(result, '导入云镜像', async () => { await loadCatalog(); notice('镜像导入已完成。'); })) notice('任务已接收,正在后台导入。'); }
catch (error) { notice(error.message, true); } finally { state.busy = false; button.disabled = !canManage; }
}
async function uploadCloudTemplate(event) {
event.preventDefault();
if (state.busy) return;
const name = byId('template-upload-name').value.trim();
const selection = byId('template-upload-system').value;
const separator = selection.indexOf('|');
const osFamily = separator > 0 ? selection.slice(0, separator) : '';
const osVersion = separator > 0 ? selection.slice(separator + 1) : '';
const file = byId('template-upload-file').files?.[0];
if (!name || name.length > 64) return notice('请填写 1 至 64 个字符的模版名称。', true);
if (!osFamily || !osVersion) return notice('请选择镜像实际对应的系统和版本。', true);
if (!file || !file.name.toLowerCase().endsWith('.qcow2')) return notice('请选择扩展名为 .qcow2 的云镜像文件。', true);
if (file.size < 1024 ** 2 || file.size > 4 * 1024 ** 3) return notice('QCOW2 文件必须在 1 MiB 至 4 GiB 之间。', true);
const button = byId('template-upload-button');
const system = byId('template-upload-system');
const input = byId('template-upload-file');
state.busy = true;
button.disabled = true;
system.disabled = true;
input.disabled = true;
notice('正在上传云镜像,请勿关闭页面…');
const query = new URLSearchParams({ name, os_family: osFamily, os_version: osVersion, filename: file.name });
try {
const result = await uploadApi(`/api/v1/admin/cloud-templates/upload?${query}`, file, (loaded, total) => {
const percent = total > 0 ? Math.min(100, loaded * 100 / total) : 0;
notice(percent >= 100 ? '文件已上传至面板,正在校验并保存云镜像…' : `正在上传云镜像 ${percent.toFixed(1)}%,请勿关闭页面…`);
});
input.value = '';
if (importProgress(result, '导入本地云镜像', async () => { await loadCatalog(); notice('本地云镜像上传并导入完成。'); })) {
notice('文件上传完成,导入任务已接收。');
}
} catch (error) {
notice(error.message, true);
} finally {
state.busy = false;
button.disabled = !canManage;
system.disabled = !canManage;
input.disabled = !canManage;
}
}
async function importRescue(event) {
event.preventDefault();
if (state.busy) return;
@ -1479,6 +1409,7 @@ async function importRescue(event) {
async function initialize() {
if (!root) return;
window.addEventListener('pagehide', () => {
templateUploadController?.destroy();
for (const observer of state.taskObservers) observer.stop('pagehide');
state.taskObservers.clear();
});
@ -1504,7 +1435,20 @@ async function initialize() {
if (!canManage) { byId('host-form').querySelectorAll('input,select,textarea,button').forEach(input => { input.disabled = true; }); byId('host-enabled').disabled = true; }
notice(canManage ? '请选择宿主机编辑,或添加新的宿主机。' : '当前管理员仅可查看配置。');
}
else if (root.dataset.cloudAdmin === 'templates') { byId('template-import')?.addEventListener('submit', importTemplate); byId('template-upload')?.addEventListener('submit', uploadCloudTemplate); byId('template-catalog-refresh')?.addEventListener('click', loadCatalog); byId('template-startup-form').addEventListener('submit', saveStartupScript); byId('template-startup-close').addEventListener('click', closeStartupScript); byId('template-startup-script').addEventListener('input', updateStartupScriptControls); byId('template-startup-dialog').addEventListener('cancel', event => { event.preventDefault(); closeStartupScript(); }); await loadCatalog(); }
else if (root.dataset.cloudAdmin === 'templates') {
const { mountTemplateUpload } = await import(versionedModuleUrl('./admin-template-upload.js'));
templateUploadController = mountTemplateUpload({
requestRuntime, notice, canManage,
isBusy: () => state.busy,
setBusy: value => { state.busy = value; },
onTask: task => {
if (importProgress(task, '导入本地云镜像', async () => {
await loadCatalog(); notice('本地云镜像上传并导入完成。');
})) notice('文件已直传对象存储,导入任务已接收。');
},
});
byId('template-import')?.addEventListener('submit', importTemplate); byId('template-catalog-refresh')?.addEventListener('click', loadCatalog); byId('template-startup-form').addEventListener('submit', saveStartupScript); byId('template-startup-close').addEventListener('click', closeStartupScript); byId('template-startup-script').addEventListener('input', updateStartupScriptControls); byId('template-startup-dialog').addEventListener('cancel', event => { event.preventDefault(); closeStartupScript(); }); await loadCatalog();
}
else if (root.dataset.cloudAdmin === 'rescue') { byId('rescue-import')?.addEventListener('submit', importRescue); byId('rescue-catalog-refresh')?.addEventListener('click', loadCatalog); await loadCatalog(); }
} catch (error) { notice(error.message, true); }
}

@ -0,0 +1,20 @@
const assetVersion = new URL(import.meta.url).searchParams.get('v');
const moduleUrl = new URL('./template-upload-hash.js', import.meta.url);
moduleUrl.search = '';
if (assetVersion) moduleUrl.searchParams.set('v', assetVersion);
const { hashBlob } = await import(moduleUrl.href);
self.addEventListener('message', async event => {
const file = event.data?.file;
try {
const sha256 = await hashBlob(file, (loaded, total) => {
self.postMessage({ type: 'progress', loaded, total });
});
self.postMessage({ type: 'complete', sha256 });
} catch (error) {
self.postMessage({
type: 'error',
message: error instanceof Error ? error.message : 'SHA-256 计算失败。',
});
}
});

@ -0,0 +1,171 @@
const K = new Uint32Array([
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
]);
function rotateRight(value, bits) {
return (value >>> bits) | (value << (32 - bits));
}
/** Incremental SHA-256 with bounded memory for multi-gigabyte browser files. */
export class IncrementalSha256 {
constructor() {
this.state = new Uint32Array([
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
]);
this.buffer = new Uint8Array(64);
this.bufferLength = 0;
this.bytesHashed = 0;
this.finished = false;
this.words = new Uint32Array(64);
}
update(input) {
if (this.finished) throw new Error('SHA-256 is already finalized.');
const bytes = input instanceof Uint8Array
? input : new Uint8Array(input);
this.bytesHashed += bytes.length;
let offset = 0;
if (this.bufferLength > 0) {
const take = Math.min(64 - this.bufferLength, bytes.length);
this.buffer.set(bytes.subarray(0, take), this.bufferLength);
this.bufferLength += take;
offset += take;
if (this.bufferLength === 64) {
this.transform(this.buffer);
this.bufferLength = 0;
}
}
while (offset + 64 <= bytes.length) {
this.transform(bytes.subarray(offset, offset + 64));
offset += 64;
}
if (offset < bytes.length) {
this.buffer.set(bytes.subarray(offset), 0);
this.bufferLength = bytes.length - offset;
}
return this;
}
transform(block) {
const words = this.words;
for (let index = 0; index < 16; index += 1) {
const offset = index * 4;
words[index] = ((block[offset] << 24) | (block[offset + 1] << 16)
| (block[offset + 2] << 8) | block[offset + 3]) >>> 0;
}
for (let index = 16; index < 64; index += 1) {
const left = words[index - 15];
const right = words[index - 2];
const sigma0 = rotateRight(left, 7) ^ rotateRight(left, 18)
^ (left >>> 3);
const sigma1 = rotateRight(right, 17) ^ rotateRight(right, 19)
^ (right >>> 10);
words[index] = (words[index - 16] + sigma0
+ words[index - 7] + sigma1) >>> 0;
}
let [a, b, c, d, e, f, g, h] = this.state;
for (let index = 0; index < 64; index += 1) {
const upper = rotateRight(e, 6) ^ rotateRight(e, 11)
^ rotateRight(e, 25);
const choose = (e & f) ^ (~e & g);
const first = (h + upper + choose + K[index] + words[index]) >>> 0;
const lower = rotateRight(a, 2) ^ rotateRight(a, 13)
^ rotateRight(a, 22);
const majority = (a & b) ^ (a & c) ^ (b & c);
const second = (lower + majority) >>> 0;
h = g;
g = f;
f = e;
e = (d + first) >>> 0;
d = c;
c = b;
b = a;
a = (first + second) >>> 0;
}
this.state[0] = (this.state[0] + a) >>> 0;
this.state[1] = (this.state[1] + b) >>> 0;
this.state[2] = (this.state[2] + c) >>> 0;
this.state[3] = (this.state[3] + d) >>> 0;
this.state[4] = (this.state[4] + e) >>> 0;
this.state[5] = (this.state[5] + f) >>> 0;
this.state[6] = (this.state[6] + g) >>> 0;
this.state[7] = (this.state[7] + h) >>> 0;
}
digest() {
if (!this.finished) {
const bitLength = this.bytesHashed * 8;
this.buffer[this.bufferLength] = 0x80;
this.bufferLength += 1;
if (this.bufferLength > 56) {
this.buffer.fill(0, this.bufferLength, 64);
this.transform(this.buffer);
this.bufferLength = 0;
}
this.buffer.fill(0, this.bufferLength, 56);
const high = Math.floor(bitLength / 0x100000000);
const low = bitLength >>> 0;
this.buffer[56] = high >>> 24;
this.buffer[57] = high >>> 16;
this.buffer[58] = high >>> 8;
this.buffer[59] = high;
this.buffer[60] = low >>> 24;
this.buffer[61] = low >>> 16;
this.buffer[62] = low >>> 8;
this.buffer[63] = low;
this.transform(this.buffer);
this.finished = true;
}
const output = new Uint8Array(32);
for (let index = 0; index < this.state.length; index += 1) {
const word = this.state[index];
const offset = index * 4;
output[offset] = word >>> 24;
output[offset + 1] = word >>> 16;
output[offset + 2] = word >>> 8;
output[offset + 3] = word;
}
return output;
}
hex() {
return Array.from(this.digest(), byte => byte.toString(16)
.padStart(2, '0')).join('');
}
}
export async function hashBlob(blob, onProgress = null,
chunkBytes = 8 * 1024 * 1024) {
if (!blob || !Number.isSafeInteger(blob.size) || blob.size < 0
|| typeof blob.slice !== 'function'
|| !Number.isSafeInteger(chunkBytes) || chunkBytes < 1) {
throw new TypeError('Invalid browser file for SHA-256.');
}
const hasher = new IncrementalSha256();
let offset = 0;
while (offset < blob.size) {
const end = Math.min(blob.size, offset + chunkBytes);
const buffer = await blob.slice(offset, end).arrayBuffer();
hasher.update(new Uint8Array(buffer));
offset = end;
if (typeof onProgress === 'function') onProgress(offset, blob.size);
}
if (blob.size === 0 && typeof onProgress === 'function') onProgress(0, 0);
return hasher.hex();
}

@ -47,7 +47,7 @@
<div>
<p class="admin-eyebrow mb-1">Upload</p>
<h5 class="mb-1">从本机上传 QCOW2</h5>
<p class="text-muted mb-0">上传本地制作并完成清理的独立镜像。</p>
<p class="text-muted mb-0">浏览器分片直传对象存储,文件内容不经过面板服务器。</p>
</div>
<span class="cloud-admin-card-icon"><i class="mdi mdi-cloud-upload-outline"></i></span>
</div>
@ -59,7 +59,11 @@
<optgroup label="Ubuntu"><option value="Ubuntu|20.04">Ubuntu 20.04</option><option value="Ubuntu|22.04">Ubuntu 22.04</option><option value="Ubuntu|24.04">Ubuntu 24.04</option><option value="Ubuntu|25.04">Ubuntu 25.04</option><option value="Ubuntu|25.10">Ubuntu 25.10</option><option value="Ubuntu|26.04">Ubuntu 26.04</option></optgroup>
<optgroup label="CentOS Stream"><option value="CentOS Stream|9">CentOS Stream 9</option><option value="CentOS Stream|10">CentOS Stream 10</option></optgroup>
</select></div>
<div><label for="template-upload-file" class="form-label">QCOW2 文件(最大 4 GiB)</label><input id="template-upload-file" class="form-control" type="file" accept=".qcow2,application/octet-stream" required><div class="form-text">只接受未加密且不含 backing file 的独立 QCOW2。系统版本必须与镜像内容一致,上传期间请勿关闭页面。</div></div>
<div><label for="template-upload-file" class="form-label">QCOW2 文件(最大 4 GiB)</label><input id="template-upload-file" class="form-control" type="file" accept=".qcow2,application/octet-stream" required><div class="form-text">只接受未加密且不含 backing file 的独立 QCOW2。浏览器会并行计算 SHA-256;Bucket CORS 必须允许面板来源执行 PUT 并公开 ETag。</div></div>
</div>
<div id="template-upload-progress" class="cloud-template-upload-progress" role="status" aria-live="polite" hidden>
<div class="cloud-template-upload-progress-head"><strong id="template-upload-progress-label">准备上传…</strong><span id="template-upload-hash-label">SHA-256 校验 0.0%</span></div>
<progress id="template-upload-progress-bar" max="100" value="0">0%</progress>
</div>
<div class="cloud-admin-form-actions"><button id="template-upload-button" class="btn btn-primary" type="submit" {if !$admin_can_manage|default:false}disabled{/if}><i class="mdi mdi-upload me-1"></i>上传并导入</button></div>
</div>

@ -1,2 +1,2 @@
{* Shared Venix CSS/JS cache key. Increment whenever a shared browser asset changes. *}
{assign var=venix_asset_version value='2026103118' scope='global'}
{assign var=venix_asset_version value='2026103119' scope='global'}

@ -58,6 +58,7 @@ $consumers = [
'public/cloud/admin-resources.js',
'public/cloud/agent-settings.js',
'public/cloud/admin-object-storage.js',
'public/cloud/admin-template-upload.js',
];
$combined = '';
foreach ($consumers as $consumer) {
@ -70,10 +71,14 @@ foreach ($consumers as $consumer) {
$assert(substr_count($combined, 'VenixAdminRequestRuntime') >= 6
&& substr_count($combined, 'VenixAdminDangerDialog') >= 4,
'Administrator consumers are not wired to the shared action modules.');
$assert(str_contains($combined, 'XMLHttpRequest')
&& str_contains($combined, "request.upload.addEventListener('progress'"),
'The cloud-template file upload must retain its dedicated progress transport.');
$assert(preg_match("/venix_asset_version value='2026103118'/", $assets) === 1,
$assert(str_contains($combined, "request.open('PUT', parsed.href, true)")
&& str_contains($combined, 'request.withCredentials = false')
&& str_contains($combined, "request.getResponseHeader('ETag')")
&& str_contains($combined,
"'/api/v1/admin/cloud-template-imports'")
&& !str_contains($combined, '/admin/cloud-templates/upload?'),
'The cloud-template file upload must use credential-free direct multipart PUT.');
$assert(preg_match("/venix_asset_version value='2026103119'/", $assets) === 1,
'Shared administrator assets must use the current cache version.');
echo "Administrator shared action runtime contract passed.\n";

@ -0,0 +1,71 @@
import { putMultipartPart } from '../public/cloud/admin-template-upload.js';
if (typeof globalThis.URL !== 'function') {
globalThis.URL = class URL {
constructor(value) {
const match = /^(https?):\/\/([^/@#]+)([^#]*)$/.exec(String(value));
if (!match) throw new TypeError('Invalid URL');
this.protocol = `${match[1]}:`;
this.hostname = match[2].split(':')[0];
this.username = '';
this.password = '';
this.hash = '';
this.href = String(value);
}
};
}
class FakeUpload {
addEventListener(name, callback) { this[name] = callback; }
}
class FakeRequest {
static etag = '"part-etag"';
static last = null;
constructor() {
this.upload = new FakeUpload();
this.listeners = {};
this.status = 200;
FakeRequest.last = this;
}
open(method, url, async) { Object.assign(this, { method, url, async }); }
addEventListener(name, callback) { this.listeners[name] = callback; }
getResponseHeader(name) {
return name.toLowerCase() === 'etag' ? FakeRequest.etag : null;
}
send(blob) {
this.blob = blob;
this.upload.progress?.({ lengthComputable: true, loaded: blob.size,
total: blob.size });
this.listeners.load?.();
}
abort() { this.listeners.abort?.(); }
}
let progress = 0;
const etag = await putMultipartPart(
'https://objects.example.test/bucket/object?partNumber=1&uploadId=test',
{ size: 4 }, loaded => { progress = loaded; }, FakeRequest
);
if (etag !== '"part-etag"' || FakeRequest.last.method !== 'PUT'
|| FakeRequest.last.withCredentials !== false
|| FakeRequest.last.blob.size !== 4 || progress !== 4) {
throw new Error('Direct multipart PUT changed its credential or progress boundary.');
}
FakeRequest.etag = '';
try {
await putMultipartPart(
'https://objects.example.test/bucket/object?partNumber=2&uploadId=test',
{ size: 4 }, null, FakeRequest
);
throw new Error('A response without an exposed ETag was accepted.');
} catch (error) {
if (!String(error.message).includes('ExposeHeaders')) throw error;
}
(globalThis.print || console.log)(
'Administrator direct multipart PUT behavior passed.'
);

@ -7,8 +7,18 @@ $routes = file_get_contents($root . '/config/routes_cloud.php');
$controller = file_get_contents(
$root . '/app/Controllers/CloudAdminController.php'
);
$browser = file_get_contents(
$root . '/public/cloud/admin-template-upload.js'
);
$worker = file_get_contents(
$root . '/public/cloud/template-upload-hash-worker.js'
);
$template = file_get_contents(
$root . '/resources/views/venix/admin/cloud/templates.tpl'
);
if (!is_string($schema) || !is_string($setup) || !is_string($routes)
|| !is_string($controller)) {
|| !is_string($controller) || !is_string($browser)
|| !is_string($worker) || !is_string($template)) {
throw new RuntimeException('Template-import contract sources are unreadable.');
}
$assert = static function (bool $condition, string $message): void {
@ -49,5 +59,15 @@ $assert(str_contains($routes, "'/admin/cloud-template-imports'")
&& str_contains($controller, 'recordPart(')
&& str_contains($controller, 'completeUpload('),
'Administrator template-import intent routes are not wired.');
$assert(str_contains($browser,
"request.open('PUT', parsed.href, true)")
&& str_contains($browser, 'request.withCredentials = false')
&& str_contains($browser, "request.getResponseHeader('ETag')")
&& str_contains($browser, '/api/v1/admin/cloud-template-imports')
&& str_contains($browser, 'idempotencyKey: record.requestKey')
&& str_contains($worker, 'hashBlob(file')
&& str_contains($template, 'id="template-upload-progress"')
&& str_contains($template, '文件内容不经过面板服务器'),
'Browser template upload lost direct multipart, hashing, or progress wiring.');
echo "Cloud-template import intent schema and route contract passed.\n";

@ -0,0 +1,35 @@
import { IncrementalSha256, hashBlob } from
'../public/cloud/template-upload-hash.js';
const ascii = value => Uint8Array.from(
Array.from(value, character => character.charCodeAt(0))
);
const vectors = [
['', 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'],
['abc', 'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad'],
['The quick brown fox jumps over the lazy dog',
'd7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592'],
];
for (const [input, expected] of vectors) {
const hasher = new IncrementalSha256();
const bytes = ascii(input);
for (let offset = 0; offset < bytes.length; offset += 2) {
hasher.update(bytes.subarray(offset, offset + 2));
}
if (hasher.hex() !== expected) {
throw new Error(`Incremental SHA-256 vector failed: ${JSON.stringify(input)}`);
}
if (typeof Blob === 'function') {
const progress = [];
const digest = await hashBlob(new Blob([bytes]),
(loaded, total) => progress.push([loaded, total]), 1);
if (digest !== expected || (bytes.length > 0
&& progress.at(-1)?.[0] !== bytes.length)) {
throw new Error(`Blob SHA-256 vector failed: ${JSON.stringify(input)}`);
}
}
}
(globalThis.print || console.log)(
'Template-upload incremental SHA-256 behavior passed.'
);

@ -68,6 +68,9 @@ $shopFormScript = file_get_contents($views . '/admin/shop/form_js.tpl');
$templateAdmin = file_get_contents($views . '/admin/cloud/templates.tpl');
$rescueAdmin = file_get_contents($views . '/admin/cloud/rescue.tpl');
$adminScript = file_get_contents($public . '/admin.js');
$templateUploadScript = file_get_contents(
$public . '/admin-template-upload.js'
);
$cloudAdminController = file_get_contents($root . '/app/Controllers/CloudAdminController.php');
$cloudRoutes = file_get_contents($root . '/config/routes_cloud.php');
$api = file_get_contents($root . '/app/Controllers/CloudApiController.php');
@ -653,7 +656,8 @@ $assert(str_contains($runtimeDeployScript, 'centosstream: "CentOS Stream"')
&& str_contains($runtimeDeployScript, '/^[0-9]+(?:\\.[0-9]+){0,2}$/'),
'Cloud image family icons or dotted Ubuntu versions are not mapped correctly.');
$importStart = strpos($adminScript, 'async function importTemplate(');
$importEnd = $importStart === false ? false : strpos($adminScript, 'async function uploadCloudTemplate(', $importStart);
$importEnd = $importStart === false ? false
: strpos($adminScript, 'async function importRescue(', $importStart);
$importScript = $importStart === false || $importEnd === false ? ''
: substr($adminScript, $importStart, $importEnd - $importStart);
$assert(!str_contains($templateAdmin, 'catalog-host')
@ -671,11 +675,6 @@ $assert(str_contains($importScript, 'if (sha256) body.sha256 = sha256;')
&& !str_contains($importScript, 'host_id')
&& !str_contains($importScript, 'catalog-host'),
'Global cloud image imports must omit blank SHA-256 and all host selectors.');
$uploadStart = strpos($adminScript, 'async function uploadCloudTemplate(');
$uploadEnd = $uploadStart === false ? false
: strpos($adminScript, 'async function importRescue(', $uploadStart);
$uploadScript = $uploadStart === false || $uploadEnd === false ? ''
: substr($adminScript, $uploadStart, $uploadEnd - $uploadStart);
$assert(str_contains($templateAdmin, 'id="template-upload"')
&& str_contains($templateAdmin, 'id="template-upload-system"')
&& str_contains($templateAdmin, 'id="template-upload-file"')
@ -683,14 +682,19 @@ $assert(str_contains($templateAdmin, 'id="template-upload"')
&& str_contains($templateAdmin, '不含 backing file')
&& str_contains($templateAdmin, '最大 4 GiB'),
'Cloud template admin must offer a constrained local QCOW2 upload form.');
$assert(str_contains($uploadScript, "file.name.toLowerCase().endsWith('.qcow2')")
&& str_contains($uploadScript, '4 * 1024 ** 3')
&& str_contains($uploadScript, "os_family: osFamily")
&& str_contains($uploadScript, "os_version: osVersion")
&& str_contains($uploadScript, '/api/v1/admin/cloud-templates/upload?')
&& str_contains($adminScript, "request.setRequestHeader('Content-Type', 'application/octet-stream')")
&& str_contains($adminScript, "request.upload.addEventListener('progress'"),
'Browser QCOW2 upload must stream raw bytes with explicit metadata and progress.');
$assert(str_contains($templateUploadScript,
"file.name.toLowerCase().endsWith('.qcow2')")
&& str_contains($templateUploadScript, '4 * 1024 ** 3')
&& str_contains($templateUploadScript, 'os_family: osFamily')
&& str_contains($templateUploadScript, 'os_version: osVersion')
&& str_contains($templateUploadScript,
"request.open('PUT', parsed.href, true)")
&& str_contains($templateUploadScript,
"request.getResponseHeader('ETag')")
&& str_contains($templateUploadScript,
'/api/v1/admin/cloud-template-imports')
&& !str_contains($adminScript, '/api/v1/admin/cloud-templates/upload?'),
'Browser QCOW2 upload must use resumable direct multipart storage with progress.');
$rescueImportStart = strpos($adminScript, 'async function importRescue(');
$rescueImportEnd = $rescueImportStart === false ? false
: strpos($adminScript, 'async function initialize(', $rescueImportStart);

Loading…
Cancel
Save