feat: 本地镜像支持批量删除

This commit is contained in:
jinghui committed 2026-10-26 22:46:27 +08:00
1 parent 7b50823340
commit 34a7917ed1
3 files changed
+48

No files matched your search

+7
View File
@@ -1,5 +1,12 @@
# 更新日志
## 2026-10-26 → ✨ 本地镜像支持批量删除
- ✨ 本地镜像批量条新增「删除选中 N 个」:与「导出选中」「导入镜像」并排,勾选后一次清理多个镜像
- 🔧 新增 `POST /api/images/batch-delete`(单批 100 个):被运行中容器占用的跳过并回报「被 N 个运行中容器占用」,被已停止容器引用的也跳过(提示先删容器)
- 🔧 删除用非强制模式(`force:false`),不会误删被依赖的镜像;结果写事件记录
- ✅ 实测:接口批量删 2 个测试镜像成功、混入 1 个运行中镜像被跳过(`ok=false 被 1 个运行中容器占用`);UI 勾选 2 个 → 按钮「删除选中 2 个」→ toast「已删除 2 个镜像」、表格同步消失
## 2026-10-26 → ✨ 自动刷新可暂停,且不再打断操作
- ✨ 顶栏新增「⏸ 暂停 / ▶ 继续」按钮:暂停后数据不再自动变化,状态存 `localStorage['dc.paused']`,刷新页面仍保持
+16
View File
@@ -978,6 +978,7 @@ async function renderImages() {
<span class="muted">已选 <b id="imgSelCount">${[...state.imgSel].filter(id => imgs.some(i => i.id === id)).length}</b> / ${imgs.length}</span>
<span class="muted">批量导出会打包成一个 tar.gz</span>
<button class="btn primary" id="imgExportSel" disabled>导出选中</button>
<button class="btn danger" id="imgDelSel" disabled>删除选中</button>
<button class="btn ghost" id="imgImport">导入镜像</button>
<input type="file" id="imgFile" accept=".tar,.gz,.tgz,.tar.gz" style="display:none" />
</div>`;
@@ -1001,6 +1002,7 @@ async function renderImages() {
const syncImgSel = () => {
const n = $('#imgSelCount'); if (n) n.textContent = state.imgSel.size;
const b = $('#imgExportSel'); if (b) { b.disabled = state.imgSel.size === 0; b.textContent = state.imgSel.size ? `导出选中 ${state.imgSel.size} 个` : '导出选中'; }
const d = $('#imgDelSel'); if (d) { d.disabled = state.imgSel.size === 0; d.textContent = state.imgSel.size ? `删除选中 ${state.imgSel.size} 个` : '删除选中'; }
const a = $('#imgSelAll'); if (a) a.checked = state.imgSel.size > 0 && state.imgSel.size === idsAll.length;
};
$$('#view .imgsel').forEach(cb => {
@@ -1042,6 +1044,20 @@ async function renderImages() {
xhr.onerror = () => { btn.disabled = false; btn.textContent = '导入镜像'; toast('导入失败(网络错误)', true); };
xhr.send(f);
};
const bd = $('#imgDelSel');
if (bd) bd.onclick = async () => {
const list = [...state.imgSel];
if (!list.length) return;
if (!confirm(`确定删除选中的 ${list.length} 个镜像?\n\n被容器占用的镜像会自动跳过(不删容器、不删卷)。`)) return;
bd.disabled = true; bd.textContent = '删除中…';
try {
const r = await api('/api/images/batch-delete', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ ids: list }) });
const fail = r.results.filter(x => !x.ok);
toast(fail.length ? `已删除 ${r.deleted} 个,跳过 ${fail.length} 个(${fail[0].error})` : `已删除 ${r.deleted} 个镜像`);
state.imgSel.clear();
setTimeout(render, 600);
} catch (e) { toast(e.message, true); bd.disabled = false; bd.textContent = '删除选中'; }
};
const be = $('#imgExportSel');
if (be) be.onclick = () => {
let list = [...state.imgSel];
+25
View File
@@ -1135,6 +1135,31 @@ app.get('/api/images', async (req, res) => {
} catch (e) { res.status(500).json({ error: e.message }); }
});
app.post('/api/images/batch-delete', async (req, res) => {
try {
const ids = Array.isArray(req.body && req.body.ids) ? req.body.ids.filter(x => typeof x === 'string').slice(0, 100) : [];
if (!ids.length) return res.status(400).json({ error: '未选择镜像' });
const conts = await docker.listContainers({ all: true });
const results = [];
for (const id of ids) {
try {
const img = await docker.getImage(id).inspect();
const holders = conts.filter(c => c.ImageID === img.Id);
if (holders.length) {
const running = holders.filter(c => c.State === 'running').length;
results.push({ id, tag: (img.RepoTags || [])[0] || img.Id.slice(7, 19), ok: false, error: running ? `被 ${running} 个运行中容器占用` : `被 ${holders.length} 个已停止容器引用` });
continue;
}
await docker.getImage(id).remove({ force: false });
results.push({ id, tag: (img.RepoTags || [])[0] || img.Id.slice(7, 19), ok: true });
} catch (e) { results.push({ id, tag: '', ok: false, error: e.message }); }
}
const ok = results.filter(r => r.ok).length;
pushEvent('info', null, `批量删除镜像 ${ok} 个`, `跳过/失败 ${results.length - ok} 个`);
res.json({ ok: true, deleted: ok, results });
} 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({