From fc51ac5b62bb062b026a9e374542c0533c2de003 Mon Sep 17 00:00:00 2001 From: jinghui <181050043@qq.com> Date: Mon, 26 Oct 2026 21:47:42 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E9=95=9C=E5=83=8F=E5=AF=BC=E5=87=BA=20?= =?UTF-8?q?tar.gz=20+=20=E6=9C=AA=E5=90=AF=E5=8A=A8=E5=BA=94=E7=94=A8?= =?UTF-8?q?=E6=95=B4=E5=8D=A1=E7=82=B9=E5=87=BB=E5=90=AF=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 9 +++++++++ public/app.js | 37 ++++++++++++++++++++++++++++--------- public/style.css | 10 ++++++++++ server/index.js | 22 ++++++++++++++++++++++ 4 files changed, 69 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 85efbc0..a7de490 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,14 @@ # 更新日志 +## 2026-10-26 → ✨ 镜像导出 + 未启动应用整卡点击启动 + +- ✨ 本地镜像每行新增「导出」按钮:`GET /api/images/:id/export` 用 `docker save` 流式导出并 gzip 压缩,文件名自动取镜像 tag(如 `docker.m.daocloud.io_library_hello-world_latest.tar.gz`) +- ✨ 导出对大镜像友善:边读边压边发,不落临时文件;连接中断自动销毁流 +- ✨ 被运行中容器占用的镜像也能导出(导出与删除权限分离) +- ✨ 「有镜像但未启动的应用」卡片**整卡可点**(鼠标手型 + 悬浮上浮效果),点击即 `docker compose up -d`;卡片内「启动」按钮同样可用,不再需要点小按钮 +- 🔧 启动中按钮显示「启动中…」,防重复点击;成功后 1.2 秒自动刷新 +- ✅ 实测:导出 hello-world 得到 9 个文件的合法 tar.gz(OCI 布局),响应头 `Content-Disposition` 文件名正确;点 `pendclick-check` 卡片后容器 `Up`、卡片从待启动列表消失 + ## 2026-10-26 → ✨ 本地镜像显示使用状态,运行中镜像不可删除 - ✨ 镜像表格新增「使用情况」列:`运行中 N 个容器`(红)/ `已停止容器引用 N`(黄)/ `未使用`(灰),悬停显示具体容器名 diff --git a/public/app.js b/public/app.js index 5e26132..b25eea2 100644 --- a/public/app.js +++ b/public/app.js @@ -629,7 +629,7 @@ async function renderContainers(silent, mode = 'running') {
有镜像但未启动的应用${pend.items.length} 个 · 扫描 ${esc((pend.root || '').replace(/^.*\/docker$/, '.../docker'))}
${pend.items.map(p => ` -
+
${esc(p.name)}${p.services} 个服务
${esc(p.file.replace((pend.root || '') + '/', ''))}
${p.images.missing.length @@ -657,15 +657,23 @@ async function renderContainers(silent, mode = 'running') { $$('#view [data-pend-cmd]').forEach(b => b.onclick = () => { alert(`在宿主机执行:\n\ndocker compose -f ${b.dataset.pendCmd} up -d`); }); - $$('#view [data-pend]').forEach(b => b.onclick = async () => { - const name = b.dataset.pend; - if (!confirm(`启动应用「${name}」?\n\n将执行 docker compose up -d,会创建并运行该项目下的容器。`)) return; - b.disabled = true; b.textContent = '启动中…'; + const startPend = async (name, el) => { + if (el) { el.disabled = true; el.dataset.txt = el.textContent; el.textContent = '启动中…'; } try { const r = await api('/api/pending-apps/start', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name }) }); - if (r.ok) { toast(`「${name}」已启动`); setTimeout(() => render(), 1500); } - else { alert(`启动失败:\n\n${r.output || '未知错误'}`); b.disabled = false; b.textContent = '启动'; } - } catch (e) { toast(e.message, true); b.disabled = false; b.textContent = '启动'; } + toast(r.message || `已启动 ${name}`); + setTimeout(render, 1200); + } catch (e) { + toast(e.message, true); + if (el) { el.disabled = false; el.textContent = el.dataset.txt || '启动'; } + } + }; + $$('#view [data-pend]').forEach(b => b.onclick = (e) => { e.stopPropagation(); startPend(b.dataset.pend, b); }); + $$('#view [data-pend-card]').forEach(card => { + card.onclick = (e) => { + if (e.target.closest('[data-pend]') || e.target.closest('[data-pend-cmd]')) return; + startPend(card.dataset.pendCard, card.querySelector('[data-pend]')); + }; }); if (mode === 'stopped') { @@ -869,10 +877,21 @@ async function renderImages() { ${fmtBytes(i.size)} ${useCell(i)} ${new Date(i.created * 1000).toLocaleDateString('zh-CN')} - ${i.canDelete ? `` : '使用中,不可删除'} + + + ${i.canDelete ? `` : '使用中,不可删除'} + `).join('')}
共 ${s.total} 个镜像 · 合计 ${fmtBytes(s.totalSize)} · 使用中 ${s.inUse} 个 · 可删除 ${s.deletable} 个(可释放 ${fmtBytes(s.deletableSize)})
`; + $$('#view [data-export]').forEach(b => b.onclick = () => { + const id = b.dataset.export; + const a = document.createElement('a'); + a.href = `/api/images/${encodeURIComponent(id)}/export`; + a.download = ''; + document.body.appendChild(a); a.click(); a.remove(); + toast('开始导出,浏览器下载中…(大镜像可能需要 1-2 分钟)'); + }); $$('#view [data-img]').forEach(b => b.onclick = async () => { if (!confirm('确定删除该镜像?')) return; try { await api(`/api/images/${encodeURIComponent(b.dataset.img)}?force=1`, { method: 'DELETE' }); toast('已删除'); render(); } diff --git a/public/style.css b/public/style.css index cdca5b8..53d9fc3 100644 --- a/public/style.css +++ b/public/style.css @@ -333,3 +333,13 @@ table.tb th.num{text-align:right} .batch-bar .btn[disabled]{opacity:.5;cursor:not-allowed} .chk{display:flex;align-items:center;margin-right:2px;flex-shrink:0} .chk input{width:15px;height:15px;cursor:pointer;accent-color:#e5484d} + +/* 镜像导出 / 未启动应用整卡点击 */ +.btn.sm{font-size:11.5px;padding:3px 9px;height:26px} +.btn.ghost{background:#fff;border:1px solid #dfe6ef;color:#5a6b82} +.btn.ghost:hover{background:#f5f8fc;border-color:#c9d5e4;color:#2d3b4f} +.img-acts{white-space:nowrap;text-align:right} +.img-acts .btn + .btn{margin-left:6px} +.pend-card.act{cursor:pointer;transition:box-shadow .15s,border-color .15s,transform .15s} +.pend-card.act:hover{border-color:#c9d8ee;box-shadow:0 4px 14px rgba(26,115,232,.12);transform:translateY(-1px)} +.pend-card.act:active{transform:translateY(0)} diff --git a/server/index.js b/server/index.js index dc5e08e..29f7151 100644 --- a/server/index.js +++ b/server/index.js @@ -7,6 +7,7 @@ const fsp = require('fs/promises'); const path = require('path'); const os = require('os'); const { PassThrough } = require('stream'); +const zlib = require('zlib'); const { execFile } = require('child_process'); const docker = new Docker({ socketPath: process.env.DOCKER_SOCK || '/var/run/docker.sock' }); @@ -1058,6 +1059,27 @@ app.get('/api/images', async (req, res) => { } catch (e) { res.status(500).json({ error: e.message }); } }); +app.get('/api/images/:id/export', async (req, res) => { + try { + const img = await docker.getImage(req.params.id).inspect(); + const tag = (img.RepoTags && img.RepoTags[0]) || ''; + const base = tag ? tag.replace(/[^a-zA-Z0-9._-]+/g, '_').replace(/^_+|_+$/g, '') : img.Id.replace('sha256:', '').slice(0, 12); + const name = `${base || 'image'}.tar.gz`; + res.setHeader('Content-Type', 'application/gzip'); + res.setHeader('Content-Disposition', `attachment; filename="${name}"`); + res.setHeader('X-Image-Name', name); + const stream = await docker.getImage(img.Id).get(); + const gz = zlib.createGzip({ level: 3 }); + stream.on('error', (e) => { try { res.end(); } catch (_) {} }); + gz.on('error', () => { try { res.end(); } catch (_) {} }); + res.on('close', () => { try { stream.destroy(); gz.destroy(); } catch (_) {} }); + stream.pipe(gz).pipe(res); + } catch (e) { + if (!res.headersSent) res.status(500).json({ error: e.message }); + else try { res.end(); } catch (_) {} + } +}); + app.delete('/api/images/:id', async (req, res) => { try { const id = req.params.id;