personal-file-hub
7fa8ef24ae
String(s == null ? "") was missing the ':' branch, producing a SyntaxError that prevented all JS from executing. Replaced with nullish coalescing: String(s ?? "").
559 行
17 KiB
HTML
559 行
17 KiB
HTML
<!doctype html>
|
|
<html lang="zh-CN">
|
|
<head>
|
|
<meta charset="utf-8" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
<title>个人文件存储库 · AI 问答</title>
|
|
<style>
|
|
:root {
|
|
color-scheme: light dark;
|
|
--ink: #1a1a2e;
|
|
--paper: #f0f2f5;
|
|
--card: #ffffff;
|
|
--accent: #4361ee;
|
|
--accent-hover: #3a56d4;
|
|
--line: #e0e0e0;
|
|
--muted: #888;
|
|
--danger: #e5484d;
|
|
--ok: #30a46c;
|
|
}
|
|
* { box-sizing: border-box; margin: 0; padding: 0; }
|
|
body {
|
|
min-height: 100svh;
|
|
background: var(--paper);
|
|
color: var(--ink);
|
|
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
|
|
}
|
|
header {
|
|
background: var(--card);
|
|
border-bottom: 1px solid var(--line);
|
|
padding: 16px 24px;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: space-between;
|
|
}
|
|
header h1 { font-size: 1.25rem; font-weight: 700; }
|
|
header .status { font-size: 0.8rem; color: var(--muted); }
|
|
.container {
|
|
max-width: 1200px;
|
|
margin: 0 auto;
|
|
padding: 24px;
|
|
display: grid;
|
|
grid-template-columns: 1fr 1fr;
|
|
gap: 24px;
|
|
min-height: calc(100svh - 60px);
|
|
}
|
|
@media (max-width: 860px) { .container { grid-template-columns: 1fr; } }
|
|
.panel {
|
|
background: var(--card);
|
|
border: 1px solid var(--line);
|
|
border-radius: 12px;
|
|
display: flex;
|
|
flex-direction: column;
|
|
overflow: hidden;
|
|
}
|
|
.panel-header {
|
|
padding: 16px 20px;
|
|
border-bottom: 1px solid var(--line);
|
|
font-weight: 700;
|
|
font-size: 1rem;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: space-between;
|
|
}
|
|
.panel-body { flex: 1; overflow-y: auto; padding: 20px; }
|
|
|
|
/* upload zone */
|
|
.upload-zone {
|
|
border: 2px dashed var(--line);
|
|
border-radius: 10px;
|
|
padding: 32px 20px;
|
|
text-align: center;
|
|
cursor: pointer;
|
|
transition: border-color 0.2s, background 0.2s;
|
|
}
|
|
.upload-zone:hover, .upload-zone.dragover {
|
|
border-color: var(--accent);
|
|
background: rgba(67, 97, 238, 0.05);
|
|
}
|
|
.upload-zone p { color: var(--muted); font-size: 0.9rem; margin-top: 8px; }
|
|
.upload-icon { font-size: 2rem; }
|
|
.upload-progress {
|
|
margin-top: 12px;
|
|
font-size: 0.85rem;
|
|
color: var(--accent);
|
|
}
|
|
|
|
/* file list */
|
|
.file-list { list-style: none; }
|
|
.file-item {
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 12px;
|
|
padding: 12px 0;
|
|
border-bottom: 1px solid var(--line);
|
|
}
|
|
.file-item:last-child { border-bottom: none; }
|
|
.file-icon { font-size: 1.5rem; flex-shrink: 0; }
|
|
.file-info { flex: 1; min-width: 0; }
|
|
.file-name {
|
|
font-weight: 600;
|
|
font-size: 0.9rem;
|
|
white-space: nowrap;
|
|
overflow: hidden;
|
|
text-overflow: ellipsis;
|
|
}
|
|
.file-meta { font-size: 0.75rem; color: var(--muted); margin-top: 2px; }
|
|
.file-actions { display: flex; gap: 6px; flex-shrink: 0; }
|
|
.btn {
|
|
border: none;
|
|
border-radius: 6px;
|
|
padding: 6px 12px;
|
|
font-size: 0.8rem;
|
|
cursor: pointer;
|
|
font-weight: 600;
|
|
transition: background 0.15s;
|
|
}
|
|
.btn-primary { background: var(--accent); color: #fff; }
|
|
.btn-primary:hover { background: var(--accent-hover); }
|
|
.btn-sm { background: var(--paper); color: var(--ink); padding: 5px 10px; font-size: 0.75rem; }
|
|
.btn-sm:hover { background: var(--line); }
|
|
.btn-danger { background: transparent; color: var(--danger); border: 1px solid var(--line); }
|
|
.btn-danger:hover { background: rgba(229,72,77,0.1); }
|
|
|
|
/* chat */
|
|
.chat-messages {
|
|
display: flex;
|
|
flex-direction: column;
|
|
gap: 12px;
|
|
margin-bottom: 16px;
|
|
}
|
|
.msg {
|
|
max-width: 85%;
|
|
padding: 10px 14px;
|
|
border-radius: 12px;
|
|
font-size: 0.9rem;
|
|
line-height: 1.5;
|
|
white-space: pre-wrap;
|
|
word-break: break-word;
|
|
}
|
|
.msg.user {
|
|
align-self: flex-end;
|
|
background: var(--accent);
|
|
color: #fff;
|
|
border-bottom-right-radius: 4px;
|
|
}
|
|
.msg.ai {
|
|
align-self: flex-start;
|
|
background: var(--paper);
|
|
color: var(--ink);
|
|
border-bottom-left-radius: 4px;
|
|
}
|
|
.msg.ai code { background: rgba(0,0,0,0.08); padding: 1px 4px; border-radius: 3px; font-size: 0.85em; }
|
|
.msg.ai pre { background: rgba(0,0,0,0.08); padding: 8px; border-radius: 6px; overflow-x: auto; margin: 6px 0; }
|
|
.msg.ai pre code { background: none; padding: 0; }
|
|
.typing-indicator { color: var(--muted); font-size: 0.85rem; }
|
|
.chat-input-area {
|
|
display: flex;
|
|
gap: 8px;
|
|
border-top: 1px solid var(--line);
|
|
padding-top: 16px;
|
|
}
|
|
.chat-input {
|
|
flex: 1;
|
|
border: 1px solid var(--line);
|
|
border-radius: 8px;
|
|
padding: 10px 14px;
|
|
font-size: 0.9rem;
|
|
font-family: inherit;
|
|
resize: none;
|
|
min-height: 42px;
|
|
max-height: 120px;
|
|
}
|
|
.chat-input:focus { outline: none; border-color: var(--accent); }
|
|
.empty-state { text-align: center; color: var(--muted); padding: 40px 20px; font-size: 0.9rem; }
|
|
|
|
/* share link modal */
|
|
.modal-overlay {
|
|
position: fixed;
|
|
inset: 0;
|
|
background: rgba(0,0,0,0.4);
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
z-index: 100;
|
|
}
|
|
.modal {
|
|
background: var(--card);
|
|
border-radius: 12px;
|
|
padding: 24px;
|
|
max-width: 480px;
|
|
width: 90%;
|
|
}
|
|
.modal h3 { margin-bottom: 12px; }
|
|
.modal .share-link {
|
|
display: flex;
|
|
gap: 8px;
|
|
margin-top: 12px;
|
|
}
|
|
.modal input {
|
|
flex: 1;
|
|
border: 1px solid var(--line);
|
|
border-radius: 6px;
|
|
padding: 8px 12px;
|
|
font-size: 0.85rem;
|
|
}
|
|
.modal .close { margin-top: 16px; width: 100%; }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<header>
|
|
<h1>📁 个人文件存储库</h1>
|
|
<span class="status" id="headerStatus">加载中...</span>
|
|
</header>
|
|
<div class="container">
|
|
<!-- 文件管理面板 -->
|
|
<div class="panel">
|
|
<div class="panel-header">
|
|
<span>文件管理</span>
|
|
<button class="btn btn-sm" onclick="loadFiles()">刷新</button>
|
|
</div>
|
|
<div class="panel-body">
|
|
<div class="upload-zone" id="uploadZone" onclick="document.getElementById('fileInput').click()">
|
|
<div class="upload-icon">⬆️</div>
|
|
<p>点击或拖拽文件到此处上传</p>
|
|
<input type="file" id="fileInput" style="display:none" multiple>
|
|
</div>
|
|
<div class="upload-progress" id="uploadProgress"></div>
|
|
<ul class="file-list" id="fileList" style="margin-top:16px"></ul>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- AI 对话面板 -->
|
|
<div class="panel">
|
|
<div class="panel-header">
|
|
<span>🤖 AI 智能问答</span>
|
|
<button class="btn btn-sm" onclick="clearChat()">清空</button>
|
|
</div>
|
|
<div class="panel-body" id="chatBody">
|
|
<div class="chat-messages" id="chatMessages">
|
|
<div class="empty-state">向 AI 提问任何问题,例如:<br>"帮我总结一下这个文件库的用途"</div>
|
|
</div>
|
|
<div class="chat-input-area">
|
|
<textarea class="chat-input" id="chatInput" placeholder="输入问题,Enter 发送..." rows="1"></textarea>
|
|
<button class="btn btn-primary" id="sendBtn" onclick="sendChat()">发送</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
const API = "/api";
|
|
const fileInput = document.getElementById("fileInput");
|
|
const uploadZone = document.getElementById("uploadZone");
|
|
const uploadProgress = document.getElementById("uploadProgress");
|
|
const fileList = document.getElementById("fileList");
|
|
const chatMessages = document.getElementById("chatMessages");
|
|
const chatInput = document.getElementById("chatInput");
|
|
const sendBtn = document.getElementById("sendBtn");
|
|
let chatHistory = [];
|
|
|
|
// ── health ──
|
|
async function loadHealth() {
|
|
try {
|
|
const res = await fetch(`${API}/health`);
|
|
const data = await res.json();
|
|
const b = data.bindings || {};
|
|
const parts = [];
|
|
if (b.database) parts.push("DB ✓");
|
|
if (b.object_storage) parts.push("存储 ✓");
|
|
if (b.model_router) parts.push("AI ✓");
|
|
document.getElementById("headerStatus").textContent = parts.length ? parts.join(" · ") : "未就绪";
|
|
} catch (e) {
|
|
document.getElementById("headerStatus").textContent = "连接失败";
|
|
}
|
|
}
|
|
|
|
// ── file size format ──
|
|
function formatSize(bytes) {
|
|
if (bytes === 0) return "0 B";
|
|
const k = 1024;
|
|
const sizes = ["B", "KB", "MB", "GB"];
|
|
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
return parseFloat((bytes / Math.pow(k, i)).toFixed(1)) + " " + sizes[i];
|
|
}
|
|
|
|
function fileIcon(type) {
|
|
if (type && type.startsWith("image/")) return "🖼️";
|
|
if (type && type.startsWith("video/")) return "🎬";
|
|
if (type && type.startsWith("audio/")) return "🎵";
|
|
if (type && type.includes("pdf")) return "📄";
|
|
if (type && (type.includes("zip") || type.includes("compressed"))) return "📦";
|
|
if (type && type.startsWith("text/")) return "📝";
|
|
return "📎";
|
|
}
|
|
|
|
// ── files ──
|
|
async function loadFiles() {
|
|
try {
|
|
const res = await fetch(`${API}/files`);
|
|
const data = await res.json();
|
|
if (!data.ok) throw new Error(data.error);
|
|
if (data.files.length === 0) {
|
|
fileList.innerHTML = '<li class="empty-state">暂无文件,上传一个试试</li>';
|
|
return;
|
|
}
|
|
fileList.innerHTML = data.files.map(f => `
|
|
<li class="file-item">
|
|
<span class="file-icon">${fileIcon(f.content_type)}</span>
|
|
<div class="file-info">
|
|
<div class="file-name" title="${esc(f.filename)}">${esc(f.filename)}</div>
|
|
<div class="file-meta">${formatSize(f.size)} · ${new Date(f.created_at).toLocaleString("zh-CN")}</div>
|
|
</div>
|
|
<div class="file-actions">
|
|
<button class="btn btn-sm" onclick="downloadFile(${f.id})">下载</button>
|
|
<button class="btn btn-sm" onclick="shareFile('${f.share_token}')">分享</button>
|
|
<button class="btn btn-danger" onclick="deleteFile(${f.id}, '${esc(f.filename)}')">删除</button>
|
|
</div>
|
|
</li>
|
|
`).join("");
|
|
} catch (e) {
|
|
fileList.innerHTML = `<li class="empty-state">加载失败: ${esc(e.message)}</li>`;
|
|
}
|
|
}
|
|
|
|
function esc(s) {
|
|
return String(s ?? "").replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""");
|
|
}
|
|
|
|
// ── upload ──
|
|
fileInput.addEventListener("change", (e) => {
|
|
for (const file of e.target.files) uploadFile(file);
|
|
fileInput.value = "";
|
|
});
|
|
|
|
uploadZone.addEventListener("dragover", (e) => {
|
|
e.preventDefault();
|
|
uploadZone.classList.add("dragover");
|
|
});
|
|
uploadZone.addEventListener("dragleave", () => uploadZone.classList.remove("dragover"));
|
|
uploadZone.addEventListener("drop", (e) => {
|
|
e.preventDefault();
|
|
uploadZone.classList.remove("dragover");
|
|
for (const file of e.dataTransfer.files) uploadFile(file);
|
|
});
|
|
|
|
async function uploadFile(file) {
|
|
const progress = document.createElement("div");
|
|
progress.textContent = `⏳ 上传中: ${file.name}...`;
|
|
uploadProgress.appendChild(progress);
|
|
try {
|
|
// 1. 获取预签名上传 URL
|
|
const res1 = await fetch(`${API}/upload`, {
|
|
method: "POST",
|
|
headers: { "content-type": "application/json" },
|
|
body: JSON.stringify({ filename: file.name, content_type: file.type || "application/octet-stream", size: file.size }),
|
|
});
|
|
const data1 = await res1.json();
|
|
if (!data1.ok) throw new Error(data1.error);
|
|
|
|
// 2. PUT 文件到对象存储
|
|
const putRes = await fetch(data1.upload_url, {
|
|
method: "PUT",
|
|
headers: { "content-type": file.type || "application/octet-stream" },
|
|
body: file,
|
|
});
|
|
if (!putRes.ok) throw new Error(`上传失败: ${putRes.status}`);
|
|
|
|
// 3. 确认入库
|
|
const res2 = await fetch(`${API}/files`, {
|
|
method: "POST",
|
|
headers: { "content-type": "application/json" },
|
|
body: JSON.stringify({ object_key: data1.object_key, filename: file.name, content_type: file.type || "application/octet-stream", size: file.size }),
|
|
});
|
|
const data2 = await res2.json();
|
|
if (!data2.ok) throw new Error(data2.error);
|
|
|
|
progress.textContent = `✅ ${file.name} 上传成功`;
|
|
progress.style.color = "var(--ok)";
|
|
loadFiles();
|
|
} catch (e) {
|
|
progress.textContent = `❌ ${file.name} 失败: ${e.message}`;
|
|
progress.style.color = "var(--danger)";
|
|
}
|
|
setTimeout(() => progress.remove(), 5000);
|
|
}
|
|
|
|
async function downloadFile(id) {
|
|
try {
|
|
const res = await fetch(`${API}/files/${id}/download`);
|
|
const data = await res.json();
|
|
if (!data.ok) throw new Error(data.error);
|
|
window.open(data.download_url, "_blank");
|
|
} catch (e) {
|
|
alert("下载失败: " + e.message);
|
|
}
|
|
}
|
|
|
|
function shareFile(token) {
|
|
const url = `${location.origin}/s/${token}`;
|
|
const overlay = document.createElement("div");
|
|
overlay.className = "modal-overlay";
|
|
overlay.innerHTML = `
|
|
<div class="modal">
|
|
<h3>🔗 分享链接</h3>
|
|
<p style="font-size:0.85rem;color:var(--muted)">任何人可通过此链接下载文件(1小时内有效)</p>
|
|
<div class="share-link">
|
|
<input type="text" value="${url}" id="shareInput" readonly>
|
|
<button class="btn btn-primary" onclick="copyShare()">复制</button>
|
|
</div>
|
|
<button class="btn btn-sm close" onclick="this.closest('.modal-overlay').remove()">关闭</button>
|
|
</div>`;
|
|
document.body.appendChild(overlay);
|
|
overlay.addEventListener("click", (e) => { if (e.target === overlay) overlay.remove(); });
|
|
}
|
|
|
|
function copyShare() {
|
|
const input = document.getElementById("shareInput");
|
|
input.select();
|
|
navigator.clipboard.writeText(input.value).then(() => {
|
|
alert("链接已复制到剪贴板");
|
|
}).catch(() => {
|
|
document.execCommand("copy");
|
|
alert("链接已复制");
|
|
});
|
|
}
|
|
|
|
async function deleteFile(id, name) {
|
|
if (!confirm(`确定删除 "${name}" 吗?`)) return;
|
|
try {
|
|
const res = await fetch(`${API}/files/${id}`, { method: "DELETE" });
|
|
const data = await res.json();
|
|
if (!data.ok) throw new Error(data.error);
|
|
loadFiles();
|
|
} catch (e) {
|
|
alert("删除失败: " + e.message);
|
|
}
|
|
}
|
|
|
|
// ── chat ──
|
|
chatInput.addEventListener("keydown", (e) => {
|
|
if (e.key === "Enter" && !e.shiftKey) {
|
|
e.preventDefault();
|
|
sendChat();
|
|
}
|
|
});
|
|
chatInput.addEventListener("input", () => {
|
|
chatInput.style.height = "auto";
|
|
chatInput.style.height = Math.min(chatInput.scrollHeight, 120) + "px";
|
|
});
|
|
|
|
function clearChat() {
|
|
chatHistory = [];
|
|
chatMessages.innerHTML = '<div class="empty-state">向 AI 提问任何问题...</div>';
|
|
}
|
|
|
|
async function sendChat() {
|
|
const question = chatInput.value.trim();
|
|
if (!question) return;
|
|
chatInput.value = "";
|
|
chatInput.style.height = "auto";
|
|
sendBtn.disabled = true;
|
|
|
|
// 清空空状态
|
|
const empty = chatMessages.querySelector(".empty-state");
|
|
if (empty) empty.remove();
|
|
|
|
// 用户消息
|
|
const userMsg = document.createElement("div");
|
|
userMsg.className = "msg user";
|
|
userMsg.textContent = question;
|
|
chatMessages.appendChild(userMsg);
|
|
|
|
// AI 消息占位
|
|
const aiMsg = document.createElement("div");
|
|
aiMsg.className = "msg ai";
|
|
aiMsg.innerHTML = '<span class="typing-indicator">思考中...</span>';
|
|
chatMessages.appendChild(aiMsg);
|
|
chatMessages.scrollTop = chatMessages.scrollHeight;
|
|
|
|
try {
|
|
const res = await fetch(`${API}/chat`, {
|
|
method: "POST",
|
|
headers: { "content-type": "application/json" },
|
|
body: JSON.stringify({ question, history: chatHistory }),
|
|
});
|
|
|
|
if (!res.ok) {
|
|
const errData = await res.json().catch(() => ({}));
|
|
throw new Error(errData.error || `HTTP ${res.status}`);
|
|
}
|
|
|
|
const reader = res.body.getReader();
|
|
const decoder = new TextDecoder();
|
|
let buffer = "";
|
|
let answer = "";
|
|
|
|
aiMsg.innerHTML = "";
|
|
|
|
while (true) {
|
|
const { done, value } = await reader.read();
|
|
if (done) break;
|
|
buffer += decoder.decode(value, { stream: true });
|
|
const lines = buffer.split("\n");
|
|
buffer = lines.pop() || "";
|
|
for (const line of lines) {
|
|
const trimmed = line.trim();
|
|
if (!trimmed.startsWith("data:")) continue;
|
|
const payload = trimmed.slice(5).trim();
|
|
if (payload === "[DONE]") continue;
|
|
try {
|
|
const parsed = JSON.parse(payload);
|
|
if (parsed.content) {
|
|
answer += parsed.content;
|
|
aiMsg.innerHTML = renderMarkdown(answer);
|
|
chatMessages.scrollTop = chatMessages.scrollHeight;
|
|
}
|
|
if (parsed.error) {
|
|
aiMsg.innerHTML = '<span style="color:var(--danger)">错误: ' + esc(parsed.error) + "</span>";
|
|
}
|
|
} catch (_) {}
|
|
}
|
|
}
|
|
if (!answer) aiMsg.innerHTML = '<span class="typing-indicator">(无回复)</span>';
|
|
chatHistory.push({ role: "user", content: question });
|
|
chatHistory.push({ role: "assistant", content: answer });
|
|
} catch (e) {
|
|
aiMsg.innerHTML = '<span style="color:var(--danger)">错误: ' + esc(e.message) + "</span>";
|
|
}
|
|
sendBtn.disabled = false;
|
|
chatInput.focus();
|
|
}
|
|
|
|
// 简易 Markdown 渲染
|
|
function renderMarkdown(text) {
|
|
let html = esc(text);
|
|
// 代码块
|
|
html = html.replace(/```(\w*)\n?([\s\S]*?)```/g, (m, lang, code) => {
|
|
return '<pre><code>' + code.replace(/\n$/, "") + "</code></pre>";
|
|
});
|
|
// 行内代码
|
|
html = html.replace(/`([^`]+)`/g, "<code>$1</code>");
|
|
// 加粗
|
|
html = html.replace(/\*\*([^*]+)\*\*/g, "<strong>$1</strong>");
|
|
// 标题
|
|
html = html.replace(/^### (.+)$/gm, "<strong>$1</strong>");
|
|
html = html.replace(/^## (.+)$/gm, "<strong>$1</strong>");
|
|
html = html.replace(/^# (.+)$/gm, "<strong>$1</strong>");
|
|
// 列表
|
|
html = html.replace(/^- (.+)$/gm, "• $1");
|
|
return html;
|
|
}
|
|
|
|
// ── init ──
|
|
loadHealth();
|
|
loadFiles();
|
|
</script>
|
|
</body>
|
|
</html>
|