308 lines
12 KiB
HTML
308 lines
12 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="zh-CN">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>文件管理 - Supabase Storage</title>
|
|
<script src="supabase.js"></script>
|
|
<script>
|
|
if (!window.supabase) {
|
|
document.write('<script src="https://cdn.jsdelivr.net/npm/@supabase/supabase-js@2"><\/script>');
|
|
}
|
|
</script>
|
|
<style>
|
|
* { box-sizing: border-box; }
|
|
body {
|
|
margin: 0;
|
|
padding: 24px;
|
|
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
|
|
background: linear-gradient(135deg, #0ea5e9, #6366f1);
|
|
min-height: 100vh;
|
|
color: #0f172a;
|
|
}
|
|
.container {
|
|
max-width: 900px;
|
|
margin: 0 auto;
|
|
}
|
|
.card {
|
|
background: #fff;
|
|
border-radius: 14px;
|
|
box-shadow: 0 12px 30px rgba(15, 23, 42, 0.15);
|
|
overflow: hidden;
|
|
}
|
|
.header {
|
|
padding: 28px;
|
|
background: linear-gradient(135deg, #0ea5e9, #6366f1);
|
|
color: #fff;
|
|
}
|
|
.header h1 { margin: 0; font-size: 26px; }
|
|
.header p { margin: 8px 0 0; opacity: 0.9; }
|
|
.content { padding: 24px 28px 30px; }
|
|
.row { display: flex; gap: 12px; flex-wrap: wrap; align-items: center; margin-bottom: 16px; }
|
|
select, button {
|
|
padding: 10px 12px;
|
|
border-radius: 8px;
|
|
border: 1px solid #e2e8f0;
|
|
font-size: 14px;
|
|
}
|
|
button {
|
|
background: #2563eb;
|
|
color: #fff;
|
|
border: none;
|
|
cursor: pointer;
|
|
transition: transform .15s ease, box-shadow .15s ease;
|
|
}
|
|
button:hover { transform: translateY(-1px); box-shadow: 0 8px 20px rgba(37, 99, 235, 0.25); }
|
|
button:disabled { opacity: 0.6; cursor: not-allowed; box-shadow: none; transform: none; }
|
|
.user-badge {
|
|
background: #f8fafc;
|
|
border: 1px solid #e2e8f0;
|
|
border-radius: 10px;
|
|
padding: 12px 14px;
|
|
display: flex;
|
|
justify-content: space-between;
|
|
align-items: center;
|
|
margin-bottom: 16px;
|
|
gap: 8px;
|
|
}
|
|
.list {
|
|
border: 1px solid #e2e8f0;
|
|
border-radius: 12px;
|
|
overflow: hidden;
|
|
}
|
|
.file-row {
|
|
display: grid;
|
|
grid-template-columns: 32px 1fr 120px 160px;
|
|
gap: 12px;
|
|
padding: 14px 16px;
|
|
border-bottom: 1px solid #e2e8f0;
|
|
align-items: center;
|
|
}
|
|
.file-row:last-child { border-bottom: none; }
|
|
.name { font-weight: 600; color: #0f172a; word-break: break-all; }
|
|
.muted { color: #64748b; font-size: 13px; }
|
|
.pill {
|
|
display: inline-flex;
|
|
align-items: center;
|
|
gap: 6px;
|
|
background: #eef2ff;
|
|
color: #4338ca;
|
|
border-radius: 999px;
|
|
padding: 6px 10px;
|
|
font-size: 12px;
|
|
border: 1px solid #e0e7ff;
|
|
}
|
|
.link-btn {
|
|
padding: 8px 12px;
|
|
border-radius: 8px;
|
|
border: 1px solid #2563eb;
|
|
background: #fff;
|
|
color: #2563eb;
|
|
font-weight: 600;
|
|
cursor: pointer;
|
|
}
|
|
.link-btn:hover { background: #eef2ff; }
|
|
.empty {
|
|
padding: 24px;
|
|
text-align: center;
|
|
color: #64748b;
|
|
border: 1px dashed #cbd5e1;
|
|
border-radius: 12px;
|
|
}
|
|
@media (max-width: 720px) {
|
|
.file-row { grid-template-columns: 1fr; }
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="container">
|
|
<div class="card">
|
|
<div class="header">
|
|
<h1>📥 我的文件管理</h1>
|
|
<p>生成 1 天有效的签名下载链接 · 支持批量删除</p>
|
|
</div>
|
|
<div class="content">
|
|
<div class="user-badge">
|
|
<div>
|
|
<div class="muted">当前用户</div>
|
|
<div id="userEmail" style="font-weight:600;">未登录</div>
|
|
<div class="muted" id="userId">User ID: N/A</div>
|
|
</div>
|
|
<button onclick="logout()">退出登录</button>
|
|
</div>
|
|
|
|
<div class="row">
|
|
<div style="font-weight:600;">存储桶: <span class="pill" id="bucketName">test-public</span></div>
|
|
<button onclick="listFiles()" id="refreshBtn">刷新列表</button>
|
|
<button onclick="bulkDownload()" id="downloadBtn">批量下载</button>
|
|
<button onclick="bulkDelete()" id="deleteBtn" style="background:#dc2626;">批量删除</button>
|
|
<button onclick="window.location.href='upload.html'">去上传</button>
|
|
</div>
|
|
|
|
<div id="status" class="muted" style="margin-bottom:12px;"></div>
|
|
<div id="fileList" class="list"></div>
|
|
<div id="linkArea" style="margin-top:12px;"></div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
const SUPABASE_URL = 'https://amiap.hzau.edu.cn/supa';
|
|
const SUPABASE_ANON_KEY = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlIjoiYW5vbiIsImlzcyI6InN1cGFiYXNlIiwiaWF0IjoxNzYzODAyNjY5LCJleHAiOjIwNzkxNjI2Njl9.ltGXvQKpguLaf8Vzomn310hLgOZbrjqZT-F3rR00ulg';
|
|
const BUCKET = 'test-public';
|
|
const supabase = window.supabase.createClient(SUPABASE_URL, SUPABASE_ANON_KEY);
|
|
let currentUser = null;
|
|
|
|
window.addEventListener('load', async () => {
|
|
await checkAuth();
|
|
await listFiles();
|
|
});
|
|
|
|
async function checkAuth() {
|
|
const { data: { session } } = await supabase.auth.getSession();
|
|
if (!session) {
|
|
alert('请先登录');
|
|
window.location.href = 'login.html';
|
|
return;
|
|
}
|
|
currentUser = session.user;
|
|
document.getElementById('userEmail').textContent = currentUser.email;
|
|
document.getElementById('userId').textContent = `User ID: ${currentUser.id}`;
|
|
}
|
|
|
|
async function listFiles() {
|
|
if (!currentUser) return;
|
|
const listEl = document.getElementById('fileList');
|
|
const refreshBtn = document.getElementById('refreshBtn');
|
|
listEl.innerHTML = '';
|
|
showStatus('加载中...', false);
|
|
refreshBtn.disabled = true;
|
|
try {
|
|
const { data: objects, error } = await supabase.storage.from(BUCKET).list(currentUser.id, { limit: 500 });
|
|
if (error) throw error;
|
|
if (!objects || objects.length === 0) {
|
|
listEl.innerHTML = `<div class="empty">当前用户暂无文件</div>`;
|
|
showStatus('已加载', false);
|
|
refreshBtn.disabled = false;
|
|
return;
|
|
}
|
|
|
|
const rows = await Promise.all(objects.map(async (obj) => {
|
|
const path = `${currentUser.id}/${obj.name}`;
|
|
let signed = null;
|
|
try {
|
|
const { data, error: signErr } = await supabase.storage.from(BUCKET).createSignedUrl(path, 86400);
|
|
if (signErr) throw signErr;
|
|
signed = data.signedUrl;
|
|
} catch (e) {
|
|
const { data } = supabase.storage.from(BUCKET).getPublicUrl(path);
|
|
signed = data.publicUrl;
|
|
}
|
|
return renderRow(obj.name, obj.metadata?.size || obj.size || 0, signed, path);
|
|
}));
|
|
|
|
rows.forEach(r => listEl.appendChild(r));
|
|
showStatus(`共 ${objects.length} 个文件 · 链接有效期 1 天`, false);
|
|
} catch (err) {
|
|
console.error('list error', err);
|
|
listEl.innerHTML = `<div class="empty">加载失败: ${err.message}</div>`;
|
|
showStatus(`❌ 加载失败: ${err.message}`, true);
|
|
} finally {
|
|
refreshBtn.disabled = false;
|
|
}
|
|
}
|
|
|
|
function renderRow(name, size, url, path) {
|
|
const div = document.createElement('div');
|
|
div.className = 'file-row';
|
|
div.innerHTML = `
|
|
<div>
|
|
<input type="checkbox" class="selector" data-path="${path}" style="margin-right:8px;">
|
|
</div>
|
|
<div>
|
|
<div class="name">${name}</div>
|
|
<div class="muted">1天有效签名链接</div>
|
|
</div>
|
|
<div><span class="pill">${formatBytes(size)}</span></div>
|
|
<div style="text-align:right;">
|
|
<a class="link-btn" href="${url}" target="_blank" rel="noopener">下载</a>
|
|
</div>
|
|
`;
|
|
return div;
|
|
}
|
|
|
|
function formatBytes(bytes) {
|
|
if (!bytes) return '0 B';
|
|
const units = ['B', 'KB', 'MB', 'GB', 'TB'];
|
|
const i = Math.floor(Math.log(bytes) / Math.log(1024));
|
|
return `${(bytes / Math.pow(1024, i)).toFixed(2)} ${units[i]}`;
|
|
}
|
|
|
|
function getSelections() {
|
|
return Array.from(document.querySelectorAll('.selector:checked')).map(el => el.dataset.path);
|
|
}
|
|
|
|
async function bulkDelete() {
|
|
const paths = getSelections();
|
|
if (!paths.length) {
|
|
showStatus('请先勾选要删除的文件', true);
|
|
return;
|
|
}
|
|
const ok = confirm(`确认删除 ${paths.length} 个文件?此操作不可恢复!`);
|
|
if (!ok) return;
|
|
try {
|
|
const { error } = await supabase.storage.from(BUCKET).remove(paths);
|
|
if (error) throw error;
|
|
showStatus(`已删除 ${paths.length} 个文件`, false);
|
|
await listFiles();
|
|
} catch (e) {
|
|
showStatus(`❌ 删除失败: ${e.message}`, true);
|
|
}
|
|
}
|
|
|
|
async function bulkDownload() {
|
|
const paths = getSelections();
|
|
const linkArea = document.getElementById('linkArea');
|
|
linkArea.innerHTML = '';
|
|
if (!paths.length) {
|
|
showStatus('请先勾选要下载的文件', true);
|
|
return;
|
|
}
|
|
try {
|
|
const links = await Promise.all(paths.map(async (p) => {
|
|
try {
|
|
const { data, error } = await supabase.storage.from(BUCKET).createSignedUrl(p, 86400);
|
|
if (error) throw error;
|
|
return { path: p, url: data.signedUrl };
|
|
} catch (e) {
|
|
const { data } = supabase.storage.from(BUCKET).getPublicUrl(p);
|
|
return { path: p, url: data.publicUrl };
|
|
}
|
|
}));
|
|
const frag = document.createDocumentFragment();
|
|
links.forEach(({ path, url }) => {
|
|
const div = document.createElement('div');
|
|
div.innerHTML = `<a href="${url}" target="_blank" rel="noopener">${path}</a>`;
|
|
frag.appendChild(div);
|
|
});
|
|
linkArea.appendChild(frag);
|
|
showStatus(`已生成 ${links.length} 个下载链接(有效期 1 天)`, false);
|
|
} catch (e) {
|
|
showStatus(`❌ 生成下载链接失败: ${e.message}`, true);
|
|
}
|
|
}
|
|
|
|
function showStatus(msg, isError) {
|
|
const el = document.getElementById('status');
|
|
el.textContent = msg;
|
|
el.style.color = isError ? '#b91c1c' : '#64748b';
|
|
}
|
|
|
|
async function logout() {
|
|
await supabase.auth.signOut();
|
|
window.location.href = 'login.html';
|
|
}
|
|
</script>
|
|
</body>
|
|
</html>
|