加入「拾匣」:密码保护的私有文件柜
- 新增 cabinet 应用:Folder / StoredFile 及迁移 - 浏览公开;下载、新建目录、上传需要密码解锁(session 120 分钟) - 文件存放在 MEDIA_ROOT 之外的 storage/,下载必须经过视图鉴权 - 支持多级目录、文件名清洗、重名自动编号、分批上传与进度 - 页面:分类卡片、面包屑、文件行点击下载、上传进度、密码弹窗 - 修复弹窗命中测试:浮层移到 body、提升 z-index、去掉模糊与位移动画 - 首页新增「拾匣」入口
This commit is contained in:
@@ -0,0 +1,579 @@
|
||||
/* =========================================================
|
||||
拾匣 · 私有文件柜
|
||||
公开浏览,密码解锁下载 / 上传。
|
||||
========================================================= */
|
||||
|
||||
body {
|
||||
font-family: Inter, 'Noto Sans SC', -apple-system, 'PingFang SC', sans-serif;
|
||||
}
|
||||
main#article {
|
||||
max-width: 1080px;
|
||||
padding-top: clamp(88px, 12vh, 116px);
|
||||
padding-bottom: 80px;
|
||||
}
|
||||
.read-progress,
|
||||
.toc { display: none; }
|
||||
|
||||
.cb {
|
||||
--ok: #6b8f71;
|
||||
--ok-soft: rgba(107, 143, 113, .14);
|
||||
max-width: 1000px;
|
||||
margin: 0 auto;
|
||||
}
|
||||
:root[data-theme="dark"] .cb {
|
||||
--ok: #7fae86;
|
||||
--ok-soft: rgba(127, 174, 134, .16);
|
||||
}
|
||||
|
||||
/* ---------- 头部 ---------- */
|
||||
.cb-head {
|
||||
text-align: center;
|
||||
}
|
||||
.cb-eyebrow {
|
||||
margin: 0 0 16px;
|
||||
color: var(--ink-faint);
|
||||
font-size: 10.5px;
|
||||
font-weight: 500;
|
||||
letter-spacing: .26em;
|
||||
}
|
||||
.cb-eyebrow span { margin-right: 8px; color: var(--gold); font-size: 10px; }
|
||||
|
||||
.cb-head h1 {
|
||||
margin: 0 0 12px;
|
||||
color: var(--ink);
|
||||
font-family: 'Noto Serif SC', serif;
|
||||
font-size: clamp(1.75rem, 4vw, 2.5rem);
|
||||
font-weight: 900;
|
||||
line-height: 1.35;
|
||||
letter-spacing: .01em;
|
||||
text-wrap: balance;
|
||||
}
|
||||
.cb-sub {
|
||||
max-width: 560px;
|
||||
margin: 0 auto;
|
||||
color: var(--ink-faint);
|
||||
font-size: 13px;
|
||||
line-height: 1.9;
|
||||
}
|
||||
|
||||
/* ---------- 工具条 ---------- */
|
||||
.cb-bar {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: 14px;
|
||||
flex-wrap: wrap;
|
||||
margin-top: 30px;
|
||||
padding: 12px 0;
|
||||
border-top: 1px solid var(--line);
|
||||
border-bottom: 1px solid var(--line);
|
||||
}
|
||||
.cb-crumbs {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
flex-wrap: wrap;
|
||||
min-width: 0;
|
||||
font-size: 12.5px;
|
||||
}
|
||||
.cb-crumbs a {
|
||||
color: var(--ink-soft);
|
||||
text-decoration: none;
|
||||
transition: color .2s ease;
|
||||
}
|
||||
.cb-crumbs a:hover { color: var(--accent); }
|
||||
.cb-crumbs span { color: var(--ink); font-weight: 600; }
|
||||
.cb-crumbs i { color: var(--ink-faint); font-style: normal; opacity: .6; }
|
||||
|
||||
.cb-actions {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
.cb-lock {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: 7px;
|
||||
padding: 5px 12px;
|
||||
border: 1px solid var(--line);
|
||||
border-radius: 999px;
|
||||
background: var(--paper-soft);
|
||||
color: var(--ink-faint);
|
||||
font-size: 11.5px;
|
||||
letter-spacing: .03em;
|
||||
}
|
||||
.cb-lock i {
|
||||
width: 6px; height: 6px;
|
||||
border-radius: 50%;
|
||||
background: currentColor;
|
||||
opacity: .6;
|
||||
}
|
||||
.cb-lock.is-open {
|
||||
color: var(--ok);
|
||||
border-color: color-mix(in srgb, var(--ok) 40%, transparent);
|
||||
background: var(--ok-soft);
|
||||
}
|
||||
.cb-lock.is-open i {
|
||||
opacity: 1;
|
||||
box-shadow: 0 0 0 3px color-mix(in srgb, var(--ok) 20%, transparent);
|
||||
}
|
||||
|
||||
.cb-btn {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: 6px;
|
||||
padding: 7px 14px;
|
||||
border: 1px solid var(--line);
|
||||
border-radius: 999px;
|
||||
background: color-mix(in srgb, var(--paper) 70%, transparent);
|
||||
color: var(--ink-soft);
|
||||
font-family: inherit;
|
||||
font-size: 12px;
|
||||
cursor: pointer;
|
||||
transition: all .2s ease;
|
||||
}
|
||||
.cb-btn:hover {
|
||||
color: var(--accent);
|
||||
border-color: color-mix(in srgb, var(--accent) 45%, transparent);
|
||||
background: var(--accent-soft);
|
||||
transform: translateY(-1px);
|
||||
}
|
||||
.cb-btn:disabled { opacity: .55; cursor: wait; transform: none; }
|
||||
.cb-upload-trigger { cursor: pointer; user-select: none; }
|
||||
.cb-file-input {
|
||||
position: absolute;
|
||||
width: 1px; height: 1px;
|
||||
padding: 0; margin: -1px;
|
||||
overflow: hidden;
|
||||
clip: rect(0, 0, 0, 0);
|
||||
white-space: nowrap;
|
||||
border: 0;
|
||||
}
|
||||
.cb-btn-ghost {
|
||||
border-color: transparent;
|
||||
background: transparent;
|
||||
color: var(--ink-faint);
|
||||
}
|
||||
.cb-btn-ghost:hover { background: var(--paper-soft); }
|
||||
.cb-btn-primary {
|
||||
border-color: transparent;
|
||||
background: linear-gradient(135deg, var(--accent), var(--gold));
|
||||
color: #fff;
|
||||
font-weight: 600;
|
||||
box-shadow: 0 10px 22px -14px var(--accent);
|
||||
}
|
||||
.cb-btn-primary:hover {
|
||||
color: #fff;
|
||||
background: linear-gradient(135deg, var(--accent), var(--accent));
|
||||
border-color: transparent;
|
||||
}
|
||||
|
||||
/* ---------- 统计 ---------- */
|
||||
.cb-meta {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
flex-wrap: wrap;
|
||||
margin-top: 10px;
|
||||
color: var(--ink-faint);
|
||||
font-size: 11.5px;
|
||||
}
|
||||
.cb-meta i {
|
||||
width: 3px; height: 3px;
|
||||
border-radius: 50%;
|
||||
background: currentColor;
|
||||
opacity: .5;
|
||||
}
|
||||
.cb-hint {
|
||||
margin-left: auto;
|
||||
color: var(--accent);
|
||||
font-size: 11px;
|
||||
}
|
||||
|
||||
/* ---------- 区块 ---------- */
|
||||
.cb-section { margin-top: 30px; }
|
||||
.cb-section-title {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
margin: 0 0 12px;
|
||||
color: var(--ink-faint);
|
||||
font-family: 'Noto Serif SC', serif;
|
||||
font-size: 12.5px;
|
||||
font-weight: 700;
|
||||
letter-spacing: .14em;
|
||||
}
|
||||
.cb-section-title::after {
|
||||
content: '';
|
||||
flex: 1;
|
||||
height: 1px;
|
||||
background: linear-gradient(90deg, var(--line), transparent);
|
||||
}
|
||||
|
||||
/* ---------- 分类卡 ---------- */
|
||||
.cb-folders {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
|
||||
gap: 12px;
|
||||
}
|
||||
.cb-folder {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 12px;
|
||||
padding: 15px 16px;
|
||||
border: 1px solid var(--line);
|
||||
border-radius: 16px;
|
||||
background: var(--paper);
|
||||
box-shadow: var(--shadow-sm);
|
||||
text-decoration: none;
|
||||
transition: transform .25s ease, box-shadow .25s ease, border-color .25s ease;
|
||||
}
|
||||
.cb-folder:hover {
|
||||
transform: translateY(-2px);
|
||||
border-color: color-mix(in srgb, var(--accent) 30%, var(--line));
|
||||
box-shadow: var(--shadow);
|
||||
}
|
||||
.cb-folder-icon {
|
||||
flex: none;
|
||||
display: grid;
|
||||
place-items: center;
|
||||
width: 38px; height: 38px;
|
||||
border-radius: 11px;
|
||||
background: var(--accent-soft);
|
||||
color: var(--accent);
|
||||
}
|
||||
.cb-folder-icon svg { width: 20px; height: 20px; }
|
||||
.cb-folder-body {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 3px;
|
||||
min-width: 0;
|
||||
}
|
||||
.cb-folder-name {
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
color: var(--ink);
|
||||
font-family: 'Noto Serif SC', serif;
|
||||
font-size: 14.5px;
|
||||
font-weight: 700;
|
||||
}
|
||||
.cb-folder-meta {
|
||||
color: var(--ink-faint);
|
||||
font-size: 11px;
|
||||
}
|
||||
.cb-folder-arrow {
|
||||
margin-left: auto;
|
||||
color: var(--ink-faint);
|
||||
font-size: 13px;
|
||||
opacity: .5;
|
||||
transition: transform .2s ease, opacity .2s ease;
|
||||
}
|
||||
.cb-folder:hover .cb-folder-arrow {
|
||||
opacity: 1;
|
||||
transform: translateX(3px);
|
||||
color: var(--accent);
|
||||
}
|
||||
|
||||
/* ---------- 文件列表 ---------- */
|
||||
.cb-files {
|
||||
border: 1px solid var(--line);
|
||||
border-radius: 18px;
|
||||
background: var(--paper);
|
||||
box-shadow: var(--shadow-sm);
|
||||
overflow: hidden;
|
||||
}
|
||||
.cb-file {
|
||||
display: grid;
|
||||
grid-template-columns: 28px minmax(0, 1fr) 92px 132px 76px;
|
||||
align-items: center;
|
||||
gap: 12px;
|
||||
padding: 11px 16px;
|
||||
border-bottom: 1px solid var(--line-soft);
|
||||
transition: background .18s ease;
|
||||
}
|
||||
.cb-file:last-child { border-bottom: 0; }
|
||||
.cb-file:not(.cb-file-head) { cursor: pointer; }
|
||||
.cb-file:hover { background: var(--paper-soft); }
|
||||
|
||||
.cb-file-head {
|
||||
color: var(--ink-faint);
|
||||
font-size: 10px;
|
||||
letter-spacing: .12em;
|
||||
background: var(--paper-soft);
|
||||
padding-top: 9px;
|
||||
padding-bottom: 9px;
|
||||
}
|
||||
.cb-file-head:hover { background: var(--paper-soft); }
|
||||
|
||||
.cb-file-icon {
|
||||
display: grid;
|
||||
place-items: center;
|
||||
color: var(--ink-faint);
|
||||
}
|
||||
.cb-file-icon svg { width: 17px; height: 17px; }
|
||||
|
||||
.cb-file-name {
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
color: var(--ink);
|
||||
font-size: 13px;
|
||||
}
|
||||
.cb-file-size,
|
||||
.cb-file-time {
|
||||
color: var(--ink-faint);
|
||||
font-size: 11px;
|
||||
font-variant-numeric: tabular-nums;
|
||||
}
|
||||
.cb-download {
|
||||
justify-self: end;
|
||||
padding: 4px 12px;
|
||||
border: 1px solid var(--line);
|
||||
border-radius: 999px;
|
||||
background: var(--paper);
|
||||
color: var(--ink-soft);
|
||||
font-family: inherit;
|
||||
font-size: 11px;
|
||||
cursor: pointer;
|
||||
transition: all .2s ease;
|
||||
}
|
||||
.cb-download:hover {
|
||||
color: var(--accent);
|
||||
border-color: color-mix(in srgb, var(--accent) 45%, transparent);
|
||||
background: var(--accent-soft);
|
||||
}
|
||||
.cb-download:disabled { opacity: .55; cursor: wait; }
|
||||
|
||||
/* ---------- 空状态 / 说明 ---------- */
|
||||
.cb-empty {
|
||||
margin-top: 30px;
|
||||
padding: 60px 20px;
|
||||
border: 1px dashed var(--line);
|
||||
border-radius: 20px;
|
||||
text-align: center;
|
||||
}
|
||||
.cb-empty-mark {
|
||||
margin: 0 0 10px;
|
||||
color: var(--gold);
|
||||
font-size: 22px;
|
||||
}
|
||||
.cb-empty h2 {
|
||||
margin: 0 0 6px;
|
||||
color: var(--ink);
|
||||
font-family: 'Noto Serif SC', serif;
|
||||
font-size: 17px;
|
||||
}
|
||||
.cb-empty p {
|
||||
margin: 0;
|
||||
color: var(--ink-faint);
|
||||
font-size: 12.5px;
|
||||
}
|
||||
|
||||
.cb-note {
|
||||
margin: 30px 0 0;
|
||||
padding-top: 16px;
|
||||
border-top: 1px dashed var(--line);
|
||||
color: var(--ink-faint);
|
||||
font-size: 11px;
|
||||
line-height: 1.9;
|
||||
}
|
||||
|
||||
/* ---------- 弹窗 ---------- */
|
||||
.cb-modal {
|
||||
position: fixed;
|
||||
inset: 0;
|
||||
z-index: 1000;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
padding: 20px;
|
||||
background: rgba(18, 14, 9, .62);
|
||||
pointer-events: auto;
|
||||
}
|
||||
.cb-modal[hidden] { display: none; }
|
||||
.cb-modal-card {
|
||||
position: relative;
|
||||
z-index: 1;
|
||||
width: min(92vw, 400px);
|
||||
padding: 26px;
|
||||
border: 1px solid var(--line);
|
||||
border-radius: 22px;
|
||||
background: var(--paper);
|
||||
box-shadow: 0 40px 80px -40px rgba(0, 0, 0, .45);
|
||||
pointer-events: auto;
|
||||
animation: cb-fade .18s ease both;
|
||||
}
|
||||
.cb-modal-eyebrow {
|
||||
margin: 0 0 10px;
|
||||
color: var(--gold);
|
||||
font-size: 10px;
|
||||
letter-spacing: .2em;
|
||||
}
|
||||
.cb-modal-card h2 {
|
||||
margin: 0 0 8px;
|
||||
color: var(--ink);
|
||||
font-family: 'Noto Serif SC', serif;
|
||||
font-size: 19px;
|
||||
font-weight: 900;
|
||||
}
|
||||
.cb-modal-sub {
|
||||
margin: 0 0 16px;
|
||||
color: var(--ink-faint);
|
||||
font-size: 12px;
|
||||
line-height: 1.8;
|
||||
}
|
||||
.cb-modal-card input {
|
||||
width: 100%;
|
||||
padding: 10px 12px;
|
||||
border: 1px solid var(--line);
|
||||
border-radius: 10px;
|
||||
background: var(--paper-soft);
|
||||
color: var(--ink);
|
||||
font-family: inherit;
|
||||
font-size: 13px;
|
||||
outline: none;
|
||||
transition: border-color .2s ease, box-shadow .2s ease;
|
||||
}
|
||||
.cb-modal-card input:focus {
|
||||
border-color: color-mix(in srgb, var(--accent) 50%, transparent);
|
||||
box-shadow: 0 0 0 3px var(--accent-soft);
|
||||
}
|
||||
.cb-modal-error {
|
||||
margin: 8px 0 0;
|
||||
color: var(--accent);
|
||||
font-size: 12px;
|
||||
}
|
||||
.cb-modal-actions {
|
||||
display: flex;
|
||||
justify-content: flex-end;
|
||||
gap: 8px;
|
||||
margin-top: 18px;
|
||||
}
|
||||
|
||||
/* ---------- 上传进度 ---------- */
|
||||
.cb-upload {
|
||||
position: fixed;
|
||||
right: 22px;
|
||||
bottom: 22px;
|
||||
z-index: 900;
|
||||
width: min(86vw, 320px);
|
||||
padding: 14px 16px;
|
||||
border: 1px solid var(--line);
|
||||
border-radius: 16px;
|
||||
background: var(--paper);
|
||||
box-shadow: var(--shadow);
|
||||
animation: cb-rise .25s ease both;
|
||||
}
|
||||
.cb-upload[hidden] { display: none; }
|
||||
.cb-upload-head {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: 10px;
|
||||
color: var(--ink);
|
||||
font-size: 12.5px;
|
||||
font-weight: 600;
|
||||
}
|
||||
.cb-upload-close {
|
||||
width: 20px; height: 20px;
|
||||
border: 0;
|
||||
border-radius: 50%;
|
||||
background: transparent;
|
||||
color: var(--ink-faint);
|
||||
font-size: 14px;
|
||||
line-height: 1;
|
||||
cursor: pointer;
|
||||
}
|
||||
.cb-upload-close:hover { background: var(--paper-soft); color: var(--ink); }
|
||||
.cb-progress {
|
||||
height: 4px;
|
||||
margin-top: 12px;
|
||||
border-radius: 2px;
|
||||
background: var(--line);
|
||||
overflow: hidden;
|
||||
}
|
||||
.cb-progress i {
|
||||
display: block;
|
||||
width: 0;
|
||||
height: 100%;
|
||||
border-radius: 2px;
|
||||
background: linear-gradient(90deg, var(--gold), var(--accent));
|
||||
transition: width .2s ease;
|
||||
}
|
||||
.cb-upload-meta {
|
||||
margin: 8px 0 0;
|
||||
color: var(--ink-faint);
|
||||
font-size: 10.5px;
|
||||
line-height: 1.7;
|
||||
word-break: break-word;
|
||||
}
|
||||
|
||||
/* ---------- Toast ---------- */
|
||||
.cb-toast {
|
||||
position: fixed;
|
||||
left: 50%;
|
||||
bottom: 28px;
|
||||
z-index: 1100;
|
||||
transform: translateX(-50%);
|
||||
max-width: 86vw;
|
||||
padding: 9px 18px;
|
||||
border-radius: 999px;
|
||||
background: var(--ink);
|
||||
color: var(--bg);
|
||||
font-size: 12px;
|
||||
box-shadow: var(--shadow);
|
||||
pointer-events: none;
|
||||
animation: cb-fade .2s ease both;
|
||||
}
|
||||
.cb-toast[hidden] { display: none; }
|
||||
.cb-toast.is-error {
|
||||
background: var(--accent);
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
body.cb-modal-open,
|
||||
html.cb-modal-open { overflow: hidden; }
|
||||
|
||||
@keyframes cb-fade {
|
||||
from { opacity: 0; }
|
||||
to { opacity: 1; }
|
||||
}
|
||||
@keyframes cb-rise {
|
||||
from { opacity: 0; transform: translateY(10px); }
|
||||
to { opacity: 1; transform: none; }
|
||||
}
|
||||
|
||||
/* ---------- 响应式 ---------- */
|
||||
@media (max-width: 720px) {
|
||||
.cb-head h1 { font-size: 1.7rem; }
|
||||
.cb-bar { align-items: flex-start; }
|
||||
.cb-actions { width: 100%; }
|
||||
.cb-actions .cb-btn { flex: 1; justify-content: center; }
|
||||
.cb-lock { order: 5; }
|
||||
|
||||
.cb-file-head { display: none; }
|
||||
.cb-file {
|
||||
grid-template-columns: 24px minmax(0, 1fr) auto;
|
||||
grid-template-rows: auto auto;
|
||||
gap: 3px 10px;
|
||||
padding: 12px 14px;
|
||||
}
|
||||
.cb-file-icon { grid-row: 1 / span 2; align-self: center; }
|
||||
.cb-file-name { grid-column: 2; grid-row: 1; }
|
||||
.cb-file-size { grid-column: 2; grid-row: 2; font-size: 10.5px; }
|
||||
.cb-file-time { display: none; }
|
||||
.cb-download { grid-column: 3; grid-row: 1 / span 2; align-self: center; }
|
||||
}
|
||||
|
||||
@media (max-width: 520px) {
|
||||
.cb-folders { grid-template-columns: 1fr; }
|
||||
.cb-upload { right: 12px; bottom: 12px; }
|
||||
.cb-hint { margin-left: 0; width: 100%; }
|
||||
}
|
||||
|
||||
@media (prefers-reduced-motion: reduce) {
|
||||
.cb-modal, .cb-modal-card, .cb-upload, .cb-toast { animation: none; }
|
||||
}
|
||||
@@ -0,0 +1,487 @@
|
||||
/* =========================================================
|
||||
拾匣 · 交互
|
||||
浏览公开;下载 / 上传 / 新建目录需要密码解锁。
|
||||
========================================================= */
|
||||
(function () {
|
||||
'use strict';
|
||||
|
||||
var root = document.getElementById('cabinet');
|
||||
if (!root) return;
|
||||
|
||||
var csrfMeta = document.querySelector('meta[name="csrf-token"]');
|
||||
var CSRF = csrfMeta ? csrfMeta.getAttribute('content') || '' : '';
|
||||
|
||||
var el = {
|
||||
lockBadge: document.getElementById('cbLockBadge'),
|
||||
lockText: document.getElementById('cbLockText'),
|
||||
lockBtn: document.getElementById('cbLockBtn'),
|
||||
hint: document.getElementById('cbHint'),
|
||||
newFolder: document.getElementById('cbNewFolder'),
|
||||
upload: document.getElementById('cbUpload'),
|
||||
fileInput: document.getElementById('cbFileInput'),
|
||||
|
||||
passwordModal: document.getElementById('cbPasswordModal'),
|
||||
passwordForm: document.getElementById('cbPasswordForm'),
|
||||
passwordInput: document.getElementById('cbPasswordInput'),
|
||||
passwordError: document.getElementById('cbPasswordError'),
|
||||
passwordSubmit: document.getElementById('cbPasswordSubmit'),
|
||||
|
||||
folderModal: document.getElementById('cbFolderModal'),
|
||||
folderForm: document.getElementById('cbFolderForm'),
|
||||
folderName: document.getElementById('cbFolderName'),
|
||||
folderError: document.getElementById('cbFolderError'),
|
||||
folderSubmit: document.getElementById('cbFolderSubmit'),
|
||||
|
||||
uploadPanel: document.getElementById('cbUploadPanel'),
|
||||
uploadTitle: document.getElementById('cbUploadTitle'),
|
||||
uploadMeta: document.getElementById('cbUploadMeta'),
|
||||
progressBar: document.getElementById('cbProgressBar'),
|
||||
uploadClose: document.getElementById('cbUploadClose'),
|
||||
|
||||
toast: document.getElementById('cbToast')
|
||||
};
|
||||
|
||||
var state = {
|
||||
unlocked: root.getAttribute('data-unlocked') === 'true',
|
||||
folderId: root.getAttribute('data-folder-id') || '',
|
||||
maxMb: parseInt(root.getAttribute('data-max-mb'), 10) || 200,
|
||||
pendingAction: null,
|
||||
pendingFiles: null,
|
||||
uploading: false,
|
||||
toastTimer: null
|
||||
};
|
||||
|
||||
function setUnlocked(value) {
|
||||
state.unlocked = !!value;
|
||||
if (el.lockBadge) el.lockBadge.classList.toggle('is-open', state.unlocked);
|
||||
if (el.lockText) el.lockText.textContent = state.unlocked ? '已解锁' : '未解锁';
|
||||
if (el.lockBtn) {
|
||||
el.lockBtn.hidden = false;
|
||||
el.lockBtn.textContent = state.unlocked ? '锁定' : '解锁';
|
||||
}
|
||||
if (el.hint) el.hint.hidden = state.unlocked;
|
||||
}
|
||||
|
||||
function toast(message, isError) {
|
||||
if (!message) return;
|
||||
el.toast.textContent = message;
|
||||
el.toast.classList.toggle('is-error', !!isError);
|
||||
el.toast.hidden = false;
|
||||
window.clearTimeout(state.toastTimer);
|
||||
state.toastTimer = window.setTimeout(function () { el.toast.hidden = true; }, 2800);
|
||||
}
|
||||
|
||||
async function api(url, options) {
|
||||
options = options || {};
|
||||
var response = await fetch(url, {
|
||||
method: options.method || 'POST',
|
||||
credentials: 'same-origin',
|
||||
headers: Object.assign({
|
||||
'Accept': 'application/json',
|
||||
'X-CSRFToken': CSRF
|
||||
}, options.headers || {}),
|
||||
body: options.body
|
||||
});
|
||||
|
||||
var data = null;
|
||||
try { data = await response.json(); } catch (err) {}
|
||||
|
||||
if (!response.ok) {
|
||||
var error = new Error((data && data.error) || ('请求失败(HTTP ' + response.status + ')'));
|
||||
error.code = data && data.code;
|
||||
error.status = response.status;
|
||||
throw error;
|
||||
}
|
||||
return data || {};
|
||||
}
|
||||
|
||||
function postJson(url, payload) {
|
||||
return api(url, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(payload || {})
|
||||
});
|
||||
}
|
||||
|
||||
/* ---------- 弹窗 ---------- */
|
||||
function openModal(modal, focusEl) {
|
||||
if (!modal) return;
|
||||
[el.passwordModal, el.folderModal].forEach(function (item) {
|
||||
if (item && item !== modal) item.hidden = true;
|
||||
});
|
||||
modal.hidden = false;
|
||||
document.documentElement.classList.add('cb-modal-open');
|
||||
document.body.classList.add('cb-modal-open');
|
||||
if (focusEl) window.setTimeout(function () { focusEl.focus(); }, 30);
|
||||
}
|
||||
|
||||
function closeModals() {
|
||||
if (el.passwordModal) el.passwordModal.hidden = true;
|
||||
if (el.folderModal) el.folderModal.hidden = true;
|
||||
document.documentElement.classList.remove('cb-modal-open');
|
||||
document.body.classList.remove('cb-modal-open');
|
||||
}
|
||||
|
||||
function cancelModals() {
|
||||
closeModals();
|
||||
state.pendingAction = null;
|
||||
state.pendingFiles = null;
|
||||
}
|
||||
|
||||
function showPasswordError(message) {
|
||||
el.passwordError.textContent = message;
|
||||
el.passwordError.hidden = false;
|
||||
}
|
||||
|
||||
function showFolderError(message) {
|
||||
el.folderError.textContent = message;
|
||||
el.folderError.hidden = false;
|
||||
}
|
||||
|
||||
function requireUnlock(action) {
|
||||
if (state.unlocked) {
|
||||
action();
|
||||
return;
|
||||
}
|
||||
state.pendingAction = action;
|
||||
el.passwordError.hidden = true;
|
||||
openModal(el.passwordModal, el.passwordInput);
|
||||
}
|
||||
|
||||
/* ---------- 解锁 / 锁定 ---------- */
|
||||
el.passwordForm.addEventListener('submit', async function (event) {
|
||||
event.preventDefault();
|
||||
|
||||
var password = el.passwordInput.value;
|
||||
if (!password) {
|
||||
showPasswordError('请输入密码');
|
||||
return;
|
||||
}
|
||||
|
||||
el.passwordSubmit.disabled = true;
|
||||
el.passwordError.hidden = true;
|
||||
|
||||
try {
|
||||
var data = await postJson('/files/unlock/', { password: password });
|
||||
setUnlocked(true);
|
||||
closeModals();
|
||||
toast('已解锁,' + (data.minutes || 120) + ' 分钟内有效');
|
||||
|
||||
var action = state.pendingAction;
|
||||
state.pendingAction = null;
|
||||
el.passwordInput.value = '';
|
||||
if (action) action();
|
||||
} catch (error) {
|
||||
showPasswordError(error.message || '解锁失败');
|
||||
el.passwordInput.select();
|
||||
} finally {
|
||||
el.passwordSubmit.disabled = false;
|
||||
}
|
||||
});
|
||||
|
||||
function promptUnlock() {
|
||||
state.pendingAction = null;
|
||||
el.passwordError.hidden = true;
|
||||
el.passwordInput.value = '';
|
||||
openModal(el.passwordModal, el.passwordInput);
|
||||
}
|
||||
|
||||
el.lockBtn.addEventListener('click', async function () {
|
||||
if (!state.unlocked) {
|
||||
promptUnlock();
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await postJson('/files/lock/', {});
|
||||
setUnlocked(false);
|
||||
toast('已锁定');
|
||||
} catch (error) {
|
||||
toast(error.message || '锁定失败', true);
|
||||
}
|
||||
});
|
||||
|
||||
/* ---------- 下载 ---------- */
|
||||
function downloadFile(button) {
|
||||
if (button.disabled) return;
|
||||
|
||||
var fileId = button.getAttribute('data-file-id');
|
||||
var fileName = button.getAttribute('data-file-name') || 'download';
|
||||
var original = button.textContent;
|
||||
|
||||
button.disabled = true;
|
||||
button.textContent = '下载中…';
|
||||
|
||||
fetch('/files/download/' + fileId + '/', {
|
||||
credentials: 'same-origin',
|
||||
headers: { 'Accept': 'application/octet-stream' }
|
||||
}).then(function (response) {
|
||||
if (response.status === 403) {
|
||||
setUnlocked(false);
|
||||
state.pendingAction = function () { downloadFile(button); };
|
||||
el.passwordError.hidden = true;
|
||||
openModal(el.passwordModal, el.passwordInput);
|
||||
showPasswordError('登录已过期,请重新解锁');
|
||||
return null;
|
||||
}
|
||||
if (!response.ok) {
|
||||
return response.json().catch(function () { return {}; }).then(function (data) {
|
||||
throw new Error((data && data.error) || '下载失败');
|
||||
});
|
||||
}
|
||||
return response.blob();
|
||||
}).then(function (blob) {
|
||||
if (!blob) return;
|
||||
var url = URL.createObjectURL(blob);
|
||||
var link = document.createElement('a');
|
||||
link.href = url;
|
||||
link.download = fileName;
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
link.remove();
|
||||
window.setTimeout(function () { URL.revokeObjectURL(url); }, 4000);
|
||||
toast('开始下载');
|
||||
}).catch(function (error) {
|
||||
toast(error.message || '下载失败', true);
|
||||
}).finally(function () {
|
||||
button.disabled = false;
|
||||
button.textContent = original;
|
||||
});
|
||||
}
|
||||
|
||||
Array.prototype.forEach.call(document.querySelectorAll('.cb-download'), function (button) {
|
||||
button.addEventListener('click', function () {
|
||||
requireUnlock(function () { downloadFile(button); });
|
||||
});
|
||||
});
|
||||
|
||||
// 点击文件行(名称、图标、大小、时间)也触发下载;避免和按钮重复
|
||||
Array.prototype.forEach.call(document.querySelectorAll('.cb-file[data-file-id]'), function (row) {
|
||||
var button = row.querySelector('.cb-download');
|
||||
if (!button) return;
|
||||
|
||||
row.addEventListener('click', function (event) {
|
||||
if (event.target.closest('.cb-download')) return;
|
||||
if (window.getSelection && String(window.getSelection()).length) return;
|
||||
button.click();
|
||||
});
|
||||
});
|
||||
|
||||
/* ---------- 新建目录 ---------- */
|
||||
el.newFolder.addEventListener('click', function () {
|
||||
requireUnlock(function () {
|
||||
el.folderName.value = '';
|
||||
el.folderError.hidden = true;
|
||||
openModal(el.folderModal, el.folderName);
|
||||
});
|
||||
});
|
||||
|
||||
el.folderForm.addEventListener('submit', async function (event) {
|
||||
event.preventDefault();
|
||||
|
||||
var name = el.folderName.value.trim();
|
||||
if (!name) {
|
||||
showFolderError('给目录起个名字');
|
||||
return;
|
||||
}
|
||||
|
||||
el.folderSubmit.disabled = true;
|
||||
el.folderError.hidden = true;
|
||||
|
||||
try {
|
||||
await postJson('/files/folders/create/', {
|
||||
name: name,
|
||||
parent_id: state.folderId || null
|
||||
});
|
||||
closeModals();
|
||||
toast('目录已创建');
|
||||
window.setTimeout(function () { window.location.reload(); }, 450);
|
||||
} catch (error) {
|
||||
if (error.code === 'locked') {
|
||||
setUnlocked(false);
|
||||
closeModals();
|
||||
toast('登录已过期,请重新解锁', true);
|
||||
} else {
|
||||
showFolderError(error.message || '创建失败');
|
||||
}
|
||||
} finally {
|
||||
el.folderSubmit.disabled = false;
|
||||
}
|
||||
});
|
||||
|
||||
/* ---------- 上传 ---------- */
|
||||
el.upload.addEventListener('click', function () {
|
||||
// label 会原生打开文件选择;这里只清旧值,让同一文件也能再次触发 change
|
||||
el.fileInput.value = '';
|
||||
});
|
||||
|
||||
el.fileInput.addEventListener('change', function () {
|
||||
var files = Array.prototype.slice.call(el.fileInput.files || []);
|
||||
if (!files.length) return;
|
||||
|
||||
if (!state.unlocked) {
|
||||
state.pendingFiles = files;
|
||||
state.pendingAction = function () { uploadFiles(files); };
|
||||
el.passwordError.hidden = true;
|
||||
openModal(el.passwordModal, el.passwordInput);
|
||||
return;
|
||||
}
|
||||
|
||||
uploadFiles(files);
|
||||
});
|
||||
|
||||
var UPLOAD_BATCH = 10;
|
||||
|
||||
function uploadBatch(formData, onProgress) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.open('POST', '/files/upload/');
|
||||
xhr.setRequestHeader('X-CSRFToken', CSRF);
|
||||
xhr.setRequestHeader('Accept', 'application/json');
|
||||
|
||||
xhr.upload.onprogress = function (event) {
|
||||
if (event.lengthComputable) onProgress(event.loaded, event.total);
|
||||
};
|
||||
xhr.onload = function () {
|
||||
var data = null;
|
||||
try { data = JSON.parse(xhr.responseText); } catch (err) {}
|
||||
resolve({ status: xhr.status, data: data });
|
||||
};
|
||||
xhr.onerror = function () { reject(new Error('上传失败,请检查网络')); };
|
||||
xhr.send(formData);
|
||||
});
|
||||
}
|
||||
|
||||
async function uploadFiles(files) {
|
||||
if (state.uploading) {
|
||||
toast('正在上传,请稍候');
|
||||
return;
|
||||
}
|
||||
|
||||
var maxBytes = state.maxMb * 1024 * 1024;
|
||||
var accepted = files.filter(function (file) { return file.size <= maxBytes; });
|
||||
var skipped = files.length - accepted.length;
|
||||
|
||||
if (skipped) {
|
||||
toast('有 ' + skipped + ' 个文件超过 ' + state.maxMb + ' MB,已跳过', true);
|
||||
}
|
||||
if (!accepted.length) return;
|
||||
|
||||
state.uploading = true;
|
||||
el.uploadPanel.hidden = false;
|
||||
el.progressBar.style.width = '0%';
|
||||
el.uploadTitle.textContent = '准备上传 ' + accepted.length + ' 个文件';
|
||||
el.uploadMeta.textContent = accepted.map(function (file) { return file.name; }).join(' · ');
|
||||
|
||||
var totalBytes = accepted.reduce(function (sum, file) {
|
||||
return sum + (file.size || 0);
|
||||
}, 0) || 1;
|
||||
var sentBytes = 0;
|
||||
var uploaded = 0;
|
||||
var failed = 0;
|
||||
var firstError = '';
|
||||
|
||||
try {
|
||||
for (var offset = 0; offset < accepted.length; offset += UPLOAD_BATCH) {
|
||||
var batch = accepted.slice(offset, offset + UPLOAD_BATCH);
|
||||
var form = new FormData();
|
||||
if (state.folderId) form.append('folder_id', state.folderId);
|
||||
batch.forEach(function (file) { form.append('files', file); });
|
||||
|
||||
var batchBytes = batch.reduce(function (sum, file) {
|
||||
return sum + (file.size || 0);
|
||||
}, 0);
|
||||
|
||||
el.uploadTitle.textContent = '正在上传 · ' + (uploaded + failed) + '/' + accepted.length;
|
||||
|
||||
var result;
|
||||
try {
|
||||
result = await uploadBatch(form, function (loaded) {
|
||||
var percent = Math.round((sentBytes + loaded) / totalBytes * 100);
|
||||
el.progressBar.style.width = Math.min(percent, 99) + '%';
|
||||
});
|
||||
} catch (error) {
|
||||
failed += batch.length;
|
||||
firstError = firstError || error.message;
|
||||
sentBytes += batchBytes;
|
||||
el.uploadMeta.textContent = '已上传 ' + uploaded + ' 个,失败 ' + failed + ' 个';
|
||||
continue;
|
||||
}
|
||||
|
||||
sentBytes += batchBytes;
|
||||
|
||||
if (result.status === 403 && result.data && result.data.code === 'locked') {
|
||||
setUnlocked(false);
|
||||
el.uploadPanel.hidden = true;
|
||||
var remaining = accepted.slice(offset);
|
||||
state.pendingFiles = remaining;
|
||||
state.pendingAction = function () { uploadFiles(remaining); };
|
||||
el.passwordError.hidden = true;
|
||||
openModal(el.passwordModal, el.passwordInput);
|
||||
showPasswordError('登录已过期,请重新解锁');
|
||||
return;
|
||||
}
|
||||
|
||||
if (result.status >= 200 && result.status < 300) {
|
||||
var savedFiles = (result.data && result.data.files) || [];
|
||||
var errors = (result.data && result.data.errors) || [];
|
||||
uploaded += savedFiles.length;
|
||||
failed += Math.max(0, batch.length - savedFiles.length);
|
||||
if (errors.length && !firstError) firstError = errors[0].error || '';
|
||||
} else {
|
||||
failed += batch.length;
|
||||
firstError = firstError
|
||||
|| (result.data && (result.data.error
|
||||
|| (result.data.errors && result.data.errors[0] && result.data.errors[0].error)))
|
||||
|| '上传失败';
|
||||
}
|
||||
|
||||
el.uploadMeta.textContent = '已上传 ' + uploaded + ' 个,失败 ' + failed + ' 个';
|
||||
el.progressBar.style.width = Math.min(Math.round(sentBytes / totalBytes * 100), 99) + '%';
|
||||
}
|
||||
|
||||
el.progressBar.style.width = '100%';
|
||||
|
||||
if (uploaded > 0) {
|
||||
el.uploadTitle.textContent = '上传完成';
|
||||
toast('已上传 ' + uploaded + ' 个文件' + (failed ? ',' + failed + ' 个失败' : ''));
|
||||
window.setTimeout(function () { window.location.reload(); }, 800);
|
||||
} else {
|
||||
el.uploadTitle.textContent = '上传失败';
|
||||
toast(firstError || '上传失败', true);
|
||||
}
|
||||
} finally {
|
||||
state.uploading = false;
|
||||
}
|
||||
}
|
||||
|
||||
el.uploadClose.addEventListener('click', function () {
|
||||
el.uploadPanel.hidden = true;
|
||||
});
|
||||
|
||||
/* ---------- 弹窗关闭 ---------- */
|
||||
Array.prototype.forEach.call(document.querySelectorAll('[data-close-modal]'), function (button) {
|
||||
button.addEventListener('click', cancelModals);
|
||||
});
|
||||
|
||||
[el.passwordModal, el.folderModal].forEach(function (modal) {
|
||||
if (!modal) return;
|
||||
modal.addEventListener('click', function (event) {
|
||||
if (event.target === modal) cancelModals();
|
||||
});
|
||||
});
|
||||
|
||||
document.addEventListener('keydown', function (event) {
|
||||
if (event.key === 'Escape' && (!el.passwordModal.hidden || !el.folderModal.hidden)) {
|
||||
cancelModals();
|
||||
}
|
||||
});
|
||||
|
||||
/* ---------- 启动 ---------- */
|
||||
// 把浮层挪到 body 下,避免被 main 的层叠上下文/父级样式影响命中测试
|
||||
[el.passwordModal, el.folderModal, el.uploadPanel, el.toast].forEach(function (node) {
|
||||
if (node && node.parentNode !== document.body) document.body.appendChild(node);
|
||||
});
|
||||
|
||||
setUnlocked(state.unlocked);
|
||||
})();
|
||||
Reference in New Issue
Block a user