1. 增加服务器图片管理

This commit is contained in:
2026-08-10 22:33:04 +08:00
parent df35018805
commit 86cdc146c7
12 changed files with 751 additions and 7 deletions
@@ -0,0 +1,339 @@
{% extends "admin/base_site.html" %}
{% load i18n static %}
{% block extrastyle %}
{{ block.super }}
<style>
.mf-header {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 16px;
flex-wrap: wrap;
gap: 12px;
}
.mf-stats {
display: flex;
gap: 16px;
flex-wrap: wrap;
margin-bottom: 20px;
}
.mf-stat-card {
flex: 1;
min-width: 180px;
padding: 14px 18px;
background: #fff;
border-radius: 8px;
border: 1px solid #e5e6eb;
box-shadow: 0 1px 2px rgba(0,0,0,0.03);
}
.mf-stat-card .label { color: #86909c; font-size: 0.85rem; }
.mf-stat-card .value { font-size: 1.4rem; font-weight: 600; color: #1d2129; margin-top: 4px; }
.mf-path-bar {
padding: 10px 16px;
background: #f7f8fa;
border-radius: 6px;
margin-bottom: 16px;
display: flex;
align-items: center;
flex-wrap: wrap;
gap: 6px;
font-size: 0.9rem;
}
.mf-path-bar a { color: #165DFF; text-decoration: none; }
.mf-path-bar a:hover { text-decoration: underline; }
.mf-path-bar .sep { color: #c9cdd4; }
.mf-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
gap: 12px;
margin-top: 16px;
}
.mf-item {
background: #fff;
border: 1px solid #e5e6eb;
border-radius: 8px;
overflow: hidden;
transition: box-shadow .2s, transform .2s;
display: flex;
flex-direction: column;
position: relative;
}
.mf-item:hover {
box-shadow: 0 4px 16px rgba(0,0,0,0.1);
transform: translateY(-2px);
}
.mf-item.dir { background: #fff7e6; border-color: #ffd591; }
.mf-thumb {
width: 100%;
aspect-ratio: 1;
background: #f7f8fa center/contain no-repeat;
display: flex;
align-items: center;
justify-content: center;
font-size: 2.5rem;
position: relative;
}
.mf-thumb.dir-thumb { background: #fff1b8; }
.mf-info {
padding: 8px 10px;
flex: 1;
display: flex;
flex-direction: column;
gap: 2px;
}
.mf-name {
font-size: 0.85rem;
color: #1d2129;
word-break: break-all;
line-height: 1.3;
max-height: 2.6em;
overflow: hidden;
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
}
.mf-meta { font-size: 0.75rem; color: #86909c; }
.mf-actions {
display: flex;
border-top: 1px solid #f2f3f5;
}
.mf-actions a, .mf-actions button {
flex: 1;
padding: 6px;
font-size: 0.78rem;
text-align: center;
border: none;
cursor: pointer;
background: #fff;
color: #165DFF;
text-decoration: none;
border-right: 1px solid #f2f3f5;
}
.mf-actions a:last-child, .mf-actions button:last-child { border-right: none; }
.mf-actions button.delete { color: #f53f3f; }
.mf-actions a:hover, .mf-actions button:hover { background: #f7f8fa; }
.mf-actions button.delete:hover { background: #ffecec; }
.mf-actions a:only-child { border-right: none; }
.mf-empty {
text-align: center;
padding: 60px 20px;
color: #86909c;
background: #fff;
border: 1px dashed #e5e6eb;
border-radius: 8px;
}
.mf-preview-mask {
position: fixed;
inset: 0;
background: rgba(0,0,0,0.75);
display: none;
align-items: center;
justify-content: center;
z-index: 9999;
padding: 40px;
}
.mf-preview-mask.show { display: flex; }
.mf-preview-box {
max-width: 90%;
max-height: 90%;
background: #fff;
padding: 12px;
border-radius: 8px;
position: relative;
display: flex;
flex-direction: column;
}
.mf-preview-box img {
max-width: 80vw;
max-height: 70vh;
object-fit: contain;
}
.mf-preview-box iframe {
border: 1px solid #e5e6eb;
border-radius: 4px;
}
.mf-preview-close {
position: absolute;
top: -36px; right: 0;
background: #fff;
border: none;
padding: 6px 12px;
border-radius: 4px;
cursor: pointer;
font-size: 1rem;
}
.mf-preview-path {
margin-top: 8px;
font-size: 0.85rem;
color: #86909c;
word-break: break-all;
}
.mf-notice {
padding: 12px 16px;
background: #e8f0ff;
color: #0e42d2;
border-radius: 6px;
border-left: 3px solid #165DFF;
margin-bottom: 16px;
font-size: 0.9rem;
}
.mf-notice strong { color: #165DFF; }
</style>
{% endblock %}
{% block content %}
<div class="mf-header">
<h1 style="margin:0;">{{ title }}</h1>
<div style="color:#86909c;">共占用:<strong style="color:#1d2129;">{{ usage.total }}</strong></div>
</div>
<div class="mf-notice">
<strong>说明:</strong>这里可以浏览和删除所有媒体目录的文件(包括富文本编辑器上传的图片、产品图片、轮播图等)。
删除文件前请确认其不再被任何页面引用,否则页面会显示破图。
<strong>uploads/</strong> 目录存放的是富文本编辑器(CKEditor)上传的图片。
</div>
<div class="mf-stats">
{% for d in usage.top_dirs %}
<div class="mf-stat-card">
<div class="label">{{ d.name }}</div>
<div class="value">{{ d.size }}</div>
<div style="font-size:0.8rem;color:#86909c;margin-top:4px;">{{ d.count }} 个文件</div>
</div>
{% empty %}
<div class="mf-stat-card" style="grid-column:1/-1;">
<div class="label">暂无数据</div>
<div class="value">-</div>
</div>
{% endfor %}
</div>
<div class="mf-path-bar">
<a href="{{ list_url }}">📁 根目录</a>
{% for crumb in data.breadcrumb %}
<span class="sep">/</span>
<a href="{{ list_url }}?path={{ crumb.path }}">{{ crumb.name }}</a>
{% endfor %}
</div>
{% if data.parent_path != None %}
<a href="{{ list_url }}?path={{ data.parent_path }}" style="display:inline-block;margin-bottom:8px;">⬆ 返回上级</a>
{% endif %}
{% if data.subdirs or data.files %}
<div class="mf-grid">
{% for d in data.subdirs %}
<div class="mf-item dir">
<a href="{{ list_url }}?path={{ d.path }}" class="mf-thumb dir-thumb" title="进入目录" style="text-decoration:none;color:#d46b08;">
📁
</a>
<div class="mf-info">
<div class="mf-name"><strong>{{ d.name }}</strong></div>
<div class="mf-meta">{{ d.file_count }} 个 · {{ d.size }}</div>
</div>
<div class="mf-actions">
<a href="{{ list_url }}?path={{ d.path }}">打开</a>
</div>
</div>
{% endfor %}
{% for f in data.files %}
<div class="mf-item" data-path="{{ f.path }}">
<div class="mf-thumb"
{% if f.is_image %}style="background-image:url('{{ f.url }}');"{% endif %}
onclick="{% if f.is_image %}showPreview('{{ f.url }}', '{{ f.path }}', '{{ f.size }}');{% endif %}">
{% if not f.is_image %}
{% if f.name|slice:"-4:" == ".pdf" %}📄{% elif f.name|slice:"-4:" == ".mp4" or f.name|slice:"-4:" == ".webm" %}🎬{% else %}📎{% endif %}
{% endif %}
</div>
<div class="mf-info">
<div class="mf-name" title="{{ f.name }}">{{ f.name }}</div>
<div class="mf-meta">{{ f.size }}</div>
</div>
<div class="mf-actions">
{% if f.is_image %}
<a href="{{ f.url }}" target="_blank">原图</a>
{% else %}
<a href="{{ f.url }}" target="_blank">下载</a>
{% endif %}
<button class="delete" onclick="confirmDelete('{{ f.path }}', this)">删除</button>
</div>
</div>
{% endfor %}
</div>
{% else %}
<div class="mf-empty">
📂 此目录为空
</div>
{% endif %}
{# 图片预览 #}
<div class="mf-preview-mask" id="previewMask" onclick="if(event.target===this)closePreview();">
<div class="mf-preview-box">
<button class="mf-preview-close" onclick="closePreview();">✕ 关闭</button>
<img id="previewImg" alt="" src="">
<div class="mf-preview-path">
路径:<span id="previewPath"></span>
<span id="previewSize"></span>
</div>
</div>
</div>
<script>
const deleteUrl = '{{ delete_url }}';
const listUrl = '{{ list_url }}';
const currentDir = '{{ data.current_path }}';
function confirmDelete(path, btn) {
const name = path.split('/').pop();
if (!confirm(`确定要删除文件 "${name}" 吗?\n\n路径:${path}\n\n删除后不可恢复!`)) return;
btn.disabled = true;
btn.textContent = '删除中...';
const fd = new FormData();
fd.append('path', path);
fd.append('csrfmiddlewaretoken', '{{ csrf_token }}');
fetch(deleteUrl, {
method: 'POST',
body: fd,
headers: {'X-Requested-With': 'XMLHttpRequest'},
}).then(r => r.json()).then(resp => {
if (resp.ok) {
const item = btn.closest('.mf-item');
item.style.transition = 'opacity .3s, transform .3s';
item.style.opacity = '0';
item.style.transform = 'scale(0.8)';
setTimeout(() => item.remove(), 280);
} else {
alert('删除失败:' + resp.msg);
btn.disabled = false;
btn.textContent = '删除';
}
}).catch(() => {
alert('网络错误,请刷新后重试');
btn.disabled = false;
btn.textContent = '删除';
});
}
function showPreview(url, path, size) {
document.getElementById('previewImg').src = url;
document.getElementById('previewPath').textContent = path;
document.getElementById('previewSize').textContent = ' · ' + size;
document.getElementById('previewMask').classList.add('show');
}
function closePreview() {
document.getElementById('previewMask').classList.remove('show');
document.getElementById('previewImg').src = '';
}
document.addEventListener('keydown', e => { if (e.key === 'Escape') closePreview(); });
</script>
{% endblock %}