diff --git a/CHANGELOG.md b/CHANGELOG.md index ae3cc4c..c54dfbf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,13 @@ # 更新日志 +## 2026-10-26 → ✨ 本地镜像支持导入 + +- ✨ 本地镜像页批量条新增「导入镜像」:选择 `docker save` 导出的 `.tar` / `.tar.gz` / `.tgz` 即可导入,按钮实时显示「上传 42%」 +- ✨ 导入完成后提示具体镜像名(如「已导入 1 个镜像:docker.m.daocloud.io/library/hello-world:latest」),列表自动刷新 +- 🔧 新增 `POST /api/images/import`:**不落盘、不占内存**,直接把上传流 pipe 到 Docker socket 的 `/images/load`,支持多镜像包 +- 🔧 错误处理:非镜像包返回 400「文件不是有效的镜像包(需要 docker save 导出的 tar 或 tar.gz)」,不再默默显示成功 +- ✅ 实测:gzip 包导入 hello-world 成功并在列表出现;纯 tar(8.4MB alpine)导入成功;随机垃圾文件返回 400 友好提示 + ## 2026-10-26 → ✨ 新增「备份与恢复」页 - ✨ 侧栏新增「备份与恢复」:勾选要备份的内容 → 立即备份 → 生成 `docker-center-backup-<时间戳>.tar.gz` diff --git a/public/app.js b/public/app.js index 1fb22e8..dfe70fa 100644 --- a/public/app.js +++ b/public/app.js @@ -930,6 +930,8 @@ async function renderImages() { 已选 ${[...state.imgSel].filter(id => imgs.some(i => i.id === id)).length} / ${imgs.length} 批量导出会打包成一个 tar.gz + + `; $('#view').innerHTML = `${batchBar} ${imgs.map(i => ` @@ -962,6 +964,36 @@ async function renderImages() { $$('#view .imgsel').forEach(cb => { cb.checked = sa.checked; }); syncImgSel(); }; + const imp = $('#imgImport'); + if (imp) imp.onclick = () => $('#imgFile').click(); + const fileInp = $('#imgFile'); + if (fileInp) fileInp.onchange = () => { + const f = fileInp.files && fileInp.files[0]; + if (!f) return; + const mb = (f.size / 1048576).toFixed(1); + if (!confirm(`导入镜像文件 ${f.name}(${mb} MB)?\n\n支持 docker save 导出的 .tar / .tar.gz,导入期间请勿关闭页面。`)) { fileInp.value = ''; return; } + const btn = $('#imgImport'); + btn.disabled = true; + const xhr = new XMLHttpRequest(); + xhr.open('POST', '/api/images/import'); + xhr.setRequestHeader('Content-Type', 'application/octet-stream'); + xhr.upload.onprogress = (e) => { + if (e.lengthComputable) btn.textContent = `上传 ${Math.round((e.loaded / e.total) * 100)}%`; + }; + xhr.onload = () => { + btn.disabled = false; btn.textContent = '导入镜像'; + fileInp.value = ''; + try { + const r = JSON.parse(xhr.responseText); + if (xhr.status === 200 && r.ok) { + toast(r.count ? `已导入 ${r.count} 个镜像:${r.loaded.slice(0, 3).join('、')}` : '导入完成(未识别到镜像名)'); + setTimeout(render, 800); + } else toast(r.error || '导入失败', true); + } catch (e) { toast('导入失败:' + xhr.responseText.slice(0, 120), true); } + }; + xhr.onerror = () => { btn.disabled = false; btn.textContent = '导入镜像'; toast('导入失败(网络错误)', true); }; + xhr.send(f); + }; const be = $('#imgExportSel'); if (be) be.onclick = () => { let list = [...state.imgSel]; diff --git a/server/index.js b/server/index.js index 3ca8c13..91bcc24 100644 --- a/server/index.js +++ b/server/index.js @@ -1135,6 +1135,30 @@ app.get('/api/images', async (req, res) => { } catch (e) { res.status(500).json({ error: e.message }); } }); +app.post('/api/images/import', (req, res) => { + const sock = process.env.DOCKER_SOCK || '/var/run/docker.sock'; + const up = http.request({ + socketPath: sock, path: '/images/load?quiet=0', method: 'POST', + headers: { 'Content-Type': 'application/x-tar', 'Transfer-Encoding': 'chunked' }, + }, (r) => { + let buf = ''; + r.on('data', (c) => { buf += c.toString('utf8'); }); + r.on('end', () => { + const errLine = buf.split(/\r?\n/).map(l => { try { return JSON.parse(l); } catch (_) { return null; } }).find(o => o && (o.error || o.errorDetail)); + if (r.statusCode !== 200 || errLine) { + const msg = errLine ? ((errLine.errorDetail && errLine.errorDetail.message) || errLine.error) : buf.slice(-300); + return res.status(400).json({ error: /invalid tar header|unexpected EOF/i.test(String(msg)) ? '文件不是有效的镜像包(需要 docker save 导出的 tar 或 tar.gz)' : '导入失败:' + msg }); + } + const loaded = [...buf.matchAll(/Loaded image:?\s*([^\s"\\]+)/g)].map(m => m[1]); + pushEvent('info', null, `导入镜像 ${loaded.length} 个`, loaded.slice(0, 3).join('、')); + res.json({ ok: true, loaded, count: loaded.length, raw: buf.slice(-800) }); + }); + }); + up.on('error', (e) => { if (!res.headersSent) res.status(500).json({ error: e.message }); else res.end(); }); + req.on('aborted', () => up.destroy()); + req.pipe(up); +}); + app.get('/api/images/export', async (req, res) => { try { const ids = String(req.query.ids || '').split(',').map(x => x.trim()).filter(Boolean).slice(0, 40);
镜像ID大小使用情况创建