Update app.py
Browse files
app.py
CHANGED
|
@@ -15,10 +15,97 @@ api = HfApi(token=HF_TOKEN)
|
|
| 15 |
# 你的 Bucket ID(格式:用户名/存储桶名)
|
| 16 |
BUCKET_ID = "nagose/filebed"
|
| 17 |
|
| 18 |
-
# 嵌入 HTML 前端
|
| 19 |
HTML_CONTENT = """<!DOCTYPE html>
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
@app.route('/')
|
| 24 |
def index():
|
|
|
|
| 15 |
# 你的 Bucket ID(格式:用户名/存储桶名)
|
| 16 |
BUCKET_ID = "nagose/filebed"
|
| 17 |
|
| 18 |
+
# 嵌入 HTML 前端
|
| 19 |
HTML_CONTENT = """<!DOCTYPE html>
|
| 20 |
+
<html>
|
| 21 |
+
<head>
|
| 22 |
+
<meta charset="UTF-8">
|
| 23 |
+
<title>HF Bucket 上传</title>
|
| 24 |
+
<style>
|
| 25 |
+
body { font-family: sans-serif; max-width: 800px; margin: 20px auto; }
|
| 26 |
+
.log { background: #1e293b; color: #b9e6f0; padding: 12px; border-radius: 8px; max-height: 300px; overflow: auto; }
|
| 27 |
+
.file-item { display: flex; justify-content: space-between; padding: 10px; background: #f8fafc; margin-bottom: 8px; }
|
| 28 |
+
button { padding: 8px 16px; background: #2563eb; color: white; border: none; border-radius: 4px; cursor: pointer; }
|
| 29 |
+
button.danger { background: #dc2626; }
|
| 30 |
+
input[type="file"] { margin: 10px 0; }
|
| 31 |
+
</style>
|
| 32 |
+
</head>
|
| 33 |
+
<body>
|
| 34 |
+
<h1>HF Bucket 上传测试</h1>
|
| 35 |
+
<input type="file" id="fileInput">
|
| 36 |
+
<button id="uploadBtn">上传</button>
|
| 37 |
+
<div class="log" id="log"></div>
|
| 38 |
+
<h2>文件列表</h2>
|
| 39 |
+
<div id="fileList"></div>
|
| 40 |
+
|
| 41 |
+
<script>
|
| 42 |
+
const fileInput = document.getElementById('fileInput');
|
| 43 |
+
const uploadBtn = document.getElementById('uploadBtn');
|
| 44 |
+
const logDiv = document.getElementById('log');
|
| 45 |
+
const fileListDiv = document.getElementById('fileList');
|
| 46 |
+
|
| 47 |
+
function addLog(msg, isErr = false) {
|
| 48 |
+
const p = document.createElement('p');
|
| 49 |
+
p.textContent = `[${new Date().toLocaleTimeString()}] ${isErr ? '❌ ' : ''}${msg}`;
|
| 50 |
+
if (isErr) p.style.color = '#f87171';
|
| 51 |
+
logDiv.appendChild(p);
|
| 52 |
+
logDiv.scrollTop = logDiv.scrollHeight;
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
async function loadList() {
|
| 56 |
+
try {
|
| 57 |
+
const res = await fetch('/list');
|
| 58 |
+
const files = await res.json();
|
| 59 |
+
if (files.length === 0) fileListDiv.innerHTML = '<p>暂无文件</p>';
|
| 60 |
+
else {
|
| 61 |
+
fileListDiv.innerHTML = files.map(f => `
|
| 62 |
+
<div class="file-item">
|
| 63 |
+
<a href="/file/${encodeURIComponent(f)}" target="_blank">${f}</a>
|
| 64 |
+
<button class="danger" onclick="deleteFile('${f.replace(/'/g, "\\\\'")}')">删除</button>
|
| 65 |
+
</div>
|
| 66 |
+
`).join('');
|
| 67 |
+
}
|
| 68 |
+
} catch (err) {
|
| 69 |
+
fileListDiv.innerHTML = '<p>加载失败</p>';
|
| 70 |
+
}
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
window.deleteFile = async function(filename) {
|
| 74 |
+
if (!confirm('确定删除?')) return;
|
| 75 |
+
const res = await fetch('/delete/' + encodeURIComponent(filename), { method: 'DELETE' });
|
| 76 |
+
if (res.ok) loadList();
|
| 77 |
+
};
|
| 78 |
+
|
| 79 |
+
uploadBtn.addEventListener('click', async () => {
|
| 80 |
+
const file = fileInput.files[0];
|
| 81 |
+
if (!file) return alert('请选择文件');
|
| 82 |
+
addLog('开始上传: ' + file.name);
|
| 83 |
+
uploadBtn.disabled = true;
|
| 84 |
+
|
| 85 |
+
const formData = new FormData();
|
| 86 |
+
formData.append('file', file);
|
| 87 |
+
|
| 88 |
+
try {
|
| 89 |
+
const res = await fetch('/upload', { method: 'POST', body: formData });
|
| 90 |
+
const data = await res.json();
|
| 91 |
+
if (res.ok) {
|
| 92 |
+
addLog('✅ 上传成功');
|
| 93 |
+
fileInput.value = '';
|
| 94 |
+
loadList();
|
| 95 |
+
} else {
|
| 96 |
+
addLog('❌ ' + data.error, true);
|
| 97 |
+
}
|
| 98 |
+
} catch (err) {
|
| 99 |
+
addLog('❌ ' + err.message, true);
|
| 100 |
+
} finally {
|
| 101 |
+
uploadBtn.disabled = false;
|
| 102 |
+
}
|
| 103 |
+
});
|
| 104 |
+
|
| 105 |
+
loadList();
|
| 106 |
+
</script>
|
| 107 |
+
</body>
|
| 108 |
+
</html>"""
|
| 109 |
|
| 110 |
@app.route('/')
|
| 111 |
def index():
|