仰望星辰工作室

miaopanel

miaopanel/ web/src/views/Files.vue 35.3 KB · 1,209 行 原始文件
1<script setup lang="ts">
2import { computed, inject, onMounted, onUnmounted, reactive, ref } from 'vue'
3import { api, getCsrf } from '../api'
4import { clearLeaveGuard, setLeaveGuard } from '../guard'
5import { bytes, time } from '../format'
6import type { DirListing, FileEntry } from '../types'
7import ConfirmDialog from '../components/ConfirmDialog.vue'
8import DirPicker from '../components/DirPicker.vue'
9
10interface TrashItem {
11 id: string
12 name: string
13 original: string
14 dir: boolean
15 size: number
16 deleted_at: number
17}
18
19interface UploadItem {
20 id: number
21 name: string
22 size: number
23 percent: number
24 status: 'waiting' | 'uploading' | 'done' | 'error' | 'canceled'
25 error?: string
26 file: File
27 xhr?: XMLHttpRequest
28}
29
30const notify = inject<(text: string, kind?: 'ok' | 'error' | 'warn') => void>('notify')!
31
32const path = ref('/')
33const parent = ref('/')
34const root = ref('')
35const entries = ref<FileEntry[]>([])
36const loading = ref(false)
37const query = ref('')
38const searching = ref(false)
39const results = ref<FileEntry[]>([])
40const selection = ref<string[]>([])
41
42const editor = reactive({ open: false, path: '', content: '', original: '', busy: false })
43const trash = reactive({ open: false, items: [] as TrashItem[], busy: false })
44const menu = reactive({ open: false, x: 0, y: 0, target: null as FileEntry | null })
45const rename = reactive({ open: false, target: null as FileEntry | null, value: '', busy: false })
46const chmod = reactive({
47 open: false,
48 target: null as FileEntry | null,
49 // index 0 is unused, 1..9 are rwx for owner/group/other
50 bits: [false, ...Array(9).fill(false)] as boolean[],
51 busy: false,
52})
53const preview = reactive({ open: false, target: null as FileEntry | null })
54const deleteTarget = ref<FileEntry | null>(null)
55const bulkDelete = ref(false)
56const deleteBusy = ref(false)
57const deleteError = ref('')
58
59const picker = reactive({
60 open: false,
61 mode: '' as 'copy' | 'move' | 'extract' | 'bulk-move',
62 target: null as FileEntry | null,
63 files: [] as string[],
64 dir: '',
65})
66const namePrompt = reactive({ open: false, value: '', label: '', busy: false })
67
68const uploads = ref<UploadItem[]>([])
69const dragActive = ref(false)
70const uploadInput = ref<HTMLInputElement | null>(null)
71let uploadSeq = 0
72let uploading = false
73
74const TEXT_EXT =
75 /\.(txt|md|markdown|json|json5|ya?ml|conf|cfg|ini|log|html?|css|scss|less|js|mjs|cjs|ts|tsx|jsx|vue|php|py|rb|go|rs|java|sh|bash|zsh|env|sql|xml|csv|tsv|toml|lock|htaccess|service|gitignore|dockerfile)$/i
76const IMAGE_EXT = /\.(png|jpe?g|gif|webp|svg|bmp|ico|avif)$/i
77const ARCHIVE_EXT = /\.(zip|tar\.gz|tgz)$/i
78const BINARY_EXT = /\.(exe|bin|so|a|dll|deb|rpm|iso|gz|bz2|xz|7z|rar|mp4|mov|mkv|mp3|wav|flac|woff2?|ttf|otf|eot|pdf|sqlite3?|db)$/i
79
80const crumbs = computed(() => {
81 const parts = path.value.split('/').filter(Boolean)
82 const out = [{ name: '根目录', path: '/' }]
83 let acc = ''
84 for (const p of parts) {
85 acc += '/' + p
86 out.push({ name: p, path: acc })
87 }
88 return out
89})
90
91const visible = computed(() => (searching.value ? results.value : entries.value))
92const allSelected = computed(
93 () => visible.value.length > 0 && visible.value.every((e) => selection.value.includes(e.path)),
94)
95
96function kindOf(entry: FileEntry): 'dir' | 'image' | 'archive' | 'text' | 'binary' {
97 if (entry.dir) return 'dir'
98 if (IMAGE_EXT.test(entry.name)) return 'image'
99 if (ARCHIVE_EXT.test(entry.name)) return 'archive'
100 if (BINARY_EXT.test(entry.name)) return 'binary'
101 if (TEXT_EXT.test(entry.name)) return 'text'
102 return entry.size <= 2 << 20 ? 'text' : 'binary'
103}
104
105function canEdit(entry: FileEntry) {
106 return kindOf(entry) === 'text' && entry.size <= 2 << 20
107}
108
109function downloadURL(entry: FileEntry) {
110 return 'api/v1/files/download?path=' + encodeURIComponent(entry.path)
111}
112
113async function load(p = path.value) {
114 loading.value = true
115 try {
116 const res = await api.get<DirListing>('api/v1/files?path=' + encodeURIComponent(p))
117 path.value = res.path
118 parent.value = res.parent
119 root.value = res.root
120 entries.value = res.entries ?? []
121 searching.value = false
122 results.value = []
123 selection.value = []
124 } catch (e) {
125 notify((e as Error).message, 'error')
126 } finally {
127 loading.value = false
128 }
129}
130
131function openEntry(entry: FileEntry) {
132 if (entry.dir) {
133 load(entry.path)
134 return
135 }
136 const kind = kindOf(entry)
137 if (kind === 'image') {
138 preview.target = entry
139 preview.open = true
140 return
141 }
142 if (kind === 'text' && entry.size <= 2 << 20) {
143 openEditor(entry)
144 return
145 }
146 download(entry)
147}
148
149function download(entry: FileEntry) {
150 const a = document.createElement('a')
151 a.href = downloadURL(entry)
152 a.download = entry.name
153 document.body.appendChild(a)
154 a.click()
155 a.remove()
156}
157
158async function openEditor(entry: FileEntry) {
159 editor.busy = true
160 try {
161 const res = await api.get<{ content: string }>('api/v1/files/read?path=' + encodeURIComponent(entry.path))
162 editor.path = entry.path
163 editor.content = res.content
164 editor.original = res.content
165 editor.open = true
166 } catch (e) {
167 notify((e as Error).message, 'error')
168 } finally {
169 editor.busy = false
170 }
171}
172
173function closeEditor() {
174 if (editor.content !== editor.original && !window.confirm('关闭编辑器将丢弃未保存的修改,确定?')) return
175 editor.open = false
176}
177
178async function saveEditor() {
179 editor.busy = true
180 try {
181 await api.put('api/v1/files/write', { path: editor.path, content: editor.content })
182 editor.original = editor.content
183 notify('已保存')
184 editor.open = false
185 await load()
186 } catch (e) {
187 notify((e as Error).message, 'error')
188 } finally {
189 editor.busy = false
190 }
191}
192
193function toggleSelect(entry: FileEntry) {
194 const i = selection.value.indexOf(entry.path)
195 if (i >= 0) selection.value.splice(i, 1)
196 else selection.value.push(entry.path)
197}
198
199function toggleAll() {
200 if (allSelected.value) {
201 selection.value = selection.value.filter((p) => !visible.value.some((e) => e.path === p))
202 } else {
203 selection.value = Array.from(new Set([...selection.value, ...visible.value.map((e) => e.path)]))
204 }
205}
206
207function openMenu(entry: FileEntry, ev: MouseEvent) {
208 const rect = (ev.currentTarget as HTMLElement).getBoundingClientRect()
209 menu.target = entry
210 menu.x = Math.min(window.innerWidth - 200, Math.max(8, rect.right - 180))
211 menu.y = Math.min(window.innerHeight - 300, rect.bottom + 4)
212 menu.open = true
213}
214
215function closeMenu() {
216 menu.open = false
217}
218
219function askRename(entry: FileEntry) {
220 closeMenu()
221 rename.target = entry
222 rename.value = entry.name
223 rename.open = true
224}
225
226async function submitRename() {
227 const entry = rename.target
228 const name = rename.value.trim()
229 if (!name) return
230 rename.busy = true
231 try {
232 if (!entry) {
233 const target = path.value === '/' ? '/' + name : path.value + '/' + name
234 await api.post('api/v1/files/mkdir', { path: target })
235 notify('文件夹已创建')
236 } else {
237 if (name === entry.name) {
238 rename.open = false
239 return
240 }
241 await api.post('api/v1/files/rename', { path: entry.path, name })
242 notify('已重命名')
243 }
244 rename.open = false
245 await load()
246 } catch (e) {
247 notify((e as Error).message, 'error')
248 } finally {
249 rename.busy = false
250 }
251}
252
253function askChmod(entry: FileEntry) {
254 closeMenu()
255 chmod.target = entry
256 const mode = (entry.mode || '0644').padStart(4, '0')
257 const digits = mode.slice(-3).split('').map((d) => parseInt(d, 8) || 0)
258 const bits = [false]
259 for (const d of digits) {
260 bits.push((d & 4) !== 0, (d & 2) !== 0, (d & 1) !== 0)
261 }
262 chmod.bits = bits
263 chmod.open = true
264}
265
266const chmodMode = computed(() => {
267 let out = ''
268 for (let i = 0; i < 3; i++) {
269 const b = chmod.bits.slice(1 + i * 3, 4 + i * 3)
270 out += String((b[0] ? 4 : 0) + (b[1] ? 2 : 0) + (b[2] ? 1 : 0))
271 }
272 return out
273})
274
275function setPreset(mode: string) {
276 const digits = mode.split('').map((d) => parseInt(d, 8) || 0)
277 const bits = [false]
278 for (const d of digits) bits.push((d & 4) !== 0, (d & 2) !== 0, (d & 1) !== 0)
279 chmod.bits = bits
280}
281
282async function submitChmod() {
283 const entry = chmod.target
284 if (!entry) return
285 chmod.busy = true
286 try {
287 await api.post('api/v1/files/chmod', { path: entry.path, mode: chmodMode.value })
288 notify(`权限已改为 ${chmodMode.value}`)
289 chmod.open = false
290 await load()
291 } catch (e) {
292 notify((e as Error).message, 'error')
293 } finally {
294 chmod.busy = false
295 }
296}
297
298function compress(entry: FileEntry, format: 'zip' | 'tar.gz') {
299 closeMenu()
300 api
301 .post<{ path: string }>('api/v1/files/compress', { path: entry.path, format })
302 .then((res) => {
303 notify('已生成 ' + res.path)
304 return load()
305 })
306 .catch((e: Error) => notify(e.message, 'error'))
307}
308
309function openPicker(mode: typeof picker.mode, target: FileEntry | null, initial: string) {
310 picker.mode = mode
311 picker.target = target
312 picker.dir = initial
313 picker.open = true
314 closeMenu()
315}
316
317function onPickDir(dir: string) {
318 picker.open = false
319 picker.dir = dir
320 const target = picker.target
321 if (picker.mode === 'extract') {
322 extractTo(dir)
323 return
324 }
325 if (picker.mode === 'bulk-move') {
326 void bulkMove(dir)
327 return
328 }
329 if (!target) return
330 namePrompt.label = `目标目录 ${dir},文件名`
331 namePrompt.value =
332 picker.mode === 'copy' ? copyName(target.name) : target.name
333 namePrompt.open = true
334}
335
336function copyName(name: string) {
337 const dot = name.lastIndexOf('.')
338 if (dot <= 0) return name + '-copy'
339 return name.slice(0, dot) + '-copy' + name.slice(dot)
340}
341
342async function submitNamePrompt() {
343 const target = picker.target
344 const name = namePrompt.value.trim()
345 if (!target || !name) return
346 namePrompt.busy = true
347 const dst = (picker.dir === '/' ? '' : picker.dir) + '/' + name
348 try {
349 if (picker.mode === 'copy') {
350 await api.post('api/v1/files/copy', { src: target.path, dst })
351 notify('复制完成')
352 } else {
353 await api.post('api/v1/files/move', { src: target.path, dst })
354 notify('移动完成')
355 }
356 namePrompt.open = false
357 await load()
358 } catch (e) {
359 notify((e as Error).message, 'error')
360 } finally {
361 namePrompt.busy = false
362 }
363}
364
365function extractEntry(entry: FileEntry) {
366 const dest = entry.path.slice(0, entry.path.lastIndexOf('/')) || '/'
367 openPicker('extract', entry, dest)
368}
369
370async function extractTo(dest: string) {
371 const target = picker.target
372 if (!target) return
373 try {
374 await api.post('api/v1/files/extract', { path: target.path, dest: dest === '/' ? '/' : dest })
375 notify('解压完成')
376 await load()
377 } catch (e) {
378 notify((e as Error).message, 'error')
379 }
380}
381
382async function bulkMove(dest: string) {
383 const list = [...selection.value]
384 const failed: string[] = []
385 for (const src of list) {
386 const name = src.slice(src.lastIndexOf('/') + 1)
387 try {
388 await api.post('api/v1/files/move', { src, dst: (dest === '/' ? '' : dest) + '/' + name })
389 } catch {
390 failed.push(name)
391 }
392 }
393 selection.value = []
394 notify(
395 failed.length ? `已移动 ${list.length - failed.length} 项,失败 ${failed.length} 项` : `已移动 ${list.length} 项`,
396 failed.length ? 'warn' : 'ok',
397 )
398 await load()
399}
400
401/* ---------------- delete ---------------- */
402async function confirmSingleDelete() {
403 const entry = deleteTarget.value
404 if (!entry) return
405 deleteBusy.value = true
406 deleteError.value = ''
407 try {
408 await api.del('api/v1/files?path=' + encodeURIComponent(entry.path))
409 notify('已移入回收站')
410 deleteTarget.value = null
411 await load()
412 } catch (e) {
413 deleteError.value = (e as Error).message
414 } finally {
415 deleteBusy.value = false
416 }
417}
418
419async function confirmBulkDelete() {
420 deleteBusy.value = true
421 deleteError.value = ''
422 const list = [...selection.value]
423 let failed = 0
424 for (const p of list) {
425 try {
426 await api.del('api/v1/files?path=' + encodeURIComponent(p))
427 } catch {
428 failed++
429 }
430 }
431 deleteBusy.value = false
432 bulkDelete.value = false
433 selection.value = []
434 notify(failed ? `已删除 ${list.length - failed} 项,失败 ${failed} 项` : `已移入回收站 ${list.length} 项`, failed ? 'warn' : 'ok')
435 await load()
436}
437
438/* ---------------- upload ---------------- */
439function pickFiles() {
440 uploadInput.value?.click()
441}
442
443function onUploadInput(ev: Event) {
444 const input = ev.target as HTMLInputElement
445 const files = Array.from(input.files ?? [])
446 input.value = ''
447 enqueue(files)
448}
449
450function enqueue(files: File[]) {
451 for (const file of files) {
452 if (file.size > 512 << 20) {
453 notify(`${file.name} 超过 512 MB 上限`, 'warn')
454 continue
455 }
456 uploads.value.push({
457 id: ++uploadSeq,
458 name: file.name,
459 size: file.size,
460 percent: 0,
461 status: 'waiting',
462 file,
463 })
464 }
465 void pump()
466}
467
468function pump() {
469 if (uploading) return
470 const next = uploads.value.find((u) => u.status === 'waiting')
471 if (!next) {
472 if (uploads.value.some((u) => u.status === 'done')) void load()
473 return
474 }
475 uploading = true
476 const dir = path.value
477 const xhr = new XMLHttpRequest()
478 next.xhr = xhr
479 next.status = 'uploading'
480 xhr.open('POST', 'api/v1/files/upload?dir=' + encodeURIComponent(dir))
481 xhr.setRequestHeader('X-CSRF-Token', getCsrf())
482 xhr.withCredentials = true
483 xhr.upload.onprogress = (e) => {
484 if (e.lengthComputable) next.percent = Math.round((e.loaded / e.total) * 100)
485 }
486 xhr.onload = () => {
487 uploading = false
488 try {
489 const env = JSON.parse(xhr.responseText || '{}')
490 if (xhr.status >= 200 && xhr.status < 300 && env.code === 0) {
491 next.status = 'done'
492 next.percent = 100
493 } else {
494 next.status = 'error'
495 next.error = env.msg || `上传失败 (HTTP ${xhr.status})`
496 }
497 } catch {
498 next.status = 'error'
499 next.error = '服务器返回异常'
500 }
501 pump()
502 }
503 xhr.onerror = () => {
504 uploading = false
505 next.status = 'error'
506 next.error = '网络中断'
507 pump()
508 }
509 xhr.onabort = () => {
510 uploading = false
511 next.status = 'canceled'
512 pump()
513 }
514 const fd = new FormData()
515 fd.append('file', next.file)
516 xhr.send(fd)
517}
518
519function cancelUpload(item: UploadItem) {
520 if (item.status === 'uploading') {
521 item.xhr?.abort()
522 } else if (item.status === 'waiting') {
523 item.status = 'canceled'
524 }
525}
526
527function clearFinished() {
528 uploads.value = uploads.value.filter((u) => u.status === 'uploading' || u.status === 'waiting')
529}
530
531function closeUploadPanel() {
532 for (const u of uploads.value) {
533 if (u.status === 'uploading') {
534 u.status = 'canceled'
535 u.xhr?.abort()
536 } else if (u.status === 'waiting') {
537 u.status = 'canceled'
538 }
539 }
540 uploads.value = []
541}
542
543function onDrop(ev: DragEvent) {
544 dragActive.value = false
545 const files = Array.from(ev.dataTransfer?.files ?? [])
546 if (files.length) enqueue(files)
547}
548
549/* ---------------- search / trash ---------------- */
550async function search() {
551 const q = query.value.trim()
552 if (!q) {
553 searching.value = false
554 return
555 }
556 loading.value = true
557 try {
558 results.value = await api.get<FileEntry[]>(
559 'api/v1/files/search?q=' + encodeURIComponent(q) + '&path=' + encodeURIComponent(path.value),
560 )
561 searching.value = true
562 } catch (e) {
563 notify((e as Error).message, 'error')
564 } finally {
565 loading.value = false
566 }
567}
568
569function clearSearch() {
570 query.value = ''
571 searching.value = false
572 results.value = []
573}
574
575function gotoEntryDir(entry: FileEntry) {
576 const dir = entry.path.slice(0, entry.path.lastIndexOf('/')) || '/'
577 clearSearch()
578 load(dir)
579}
580
581async function openTrash() {
582 trash.open = true
583 trash.busy = true
584 try {
585 trash.items = await api.get<TrashItem[]>('api/v1/files/trash')
586 } catch (e) {
587 notify((e as Error).message, 'error')
588 } finally {
589 trash.busy = false
590 }
591}
592
593async function restoreTrash(item: TrashItem) {
594 try {
595 await api.post('api/v1/files/trash/restore', { id: item.id })
596 notify('已恢复 ' + item.name)
597 trash.items = await api.get<TrashItem[]>('api/v1/files/trash')
598 await load()
599 } catch (e) {
600 notify((e as Error).message, 'error')
601 }
602}
603
604async function purgeTrash(item?: TrashItem) {
605 const msg = item ? `彻底删除「${item.name}」?` : '清空回收站?此操作不可恢复。'
606 if (!window.confirm(msg)) return
607 try {
608 await api.del('api/v1/files/trash' + (item ? '?id=' + encodeURIComponent(item.id) : ''))
609 trash.items = await api.get<TrashItem[]>('api/v1/files/trash')
610 notify('已删除')
611 } catch (e) {
612 notify((e as Error).message, 'error')
613 }
614}
615
616const uploadingAny = () =>
617 uploads.value.some((u) => u.status === 'uploading' || u.status === 'waiting')
618const uploadGuard = () =>
619 !uploadingAny() || window.confirm('有文件正在上传,离开页面会中断上传,确定离开?')
620
621onMounted(() => {
622 load()
623 setLeaveGuard(uploadGuard, uploadingAny)
624})
625onUnmounted(() => {
626 clearLeaveGuard(uploadGuard)
627 for (const u of uploads.value) u.xhr?.abort()
628})
629</script>
630
631<template>
632 <div
633 class="card files-card"
634 :class="{ dragging: dragActive }"
635 @dragover.prevent="dragActive = true"
636 @dragleave.self="dragActive = false"
637 @drop.prevent="onDrop"
638 >
639 <div class="row files-head">
640 <div>
641 <h2>文件管理</h2>
642 <p class="hint">根目录 {{ root || '/www/wwwroot' }} · 删除的文件进入回收站,可随时恢复或彻底删除</p>
643 </div>
644 <div class="spacer"></div>
645 <div class="row files-actions">
646 <input
647 v-model="query"
648 class="input"
649 style="width: 170px"
650 placeholder="搜索名称"
651 @keyup.enter="search"
652 />
653 <button class="btn btn-ghost btn-sm" @click="search">搜索</button>
654 <button v-if="searching" class="btn btn-ghost btn-sm" @click="clearSearch">清空</button>
655 <button class="btn btn-ghost btn-sm" @click="openTrash">回收站</button>
656 <button class="btn btn-ghost btn-sm" :disabled="loading" @click="load()">刷新</button>
657 <button class="btn btn-ghost btn-sm" @click="pickFiles">上传</button>
658 <button class="btn btn-primary btn-sm" @click="rename.target = null; rename.value = ''; rename.open = true">
659 新建文件夹
660 </button>
661 <input ref="uploadInput" type="file" multiple hidden @change="onUploadInput" />
662 </div>
663 </div>
664
665 <div class="crumbs">
666 <button class="btn btn-ghost btn-sm" :disabled="path === '/'" @click="load(parent)">上一级</button>
667 <template v-for="(c, i) in crumbs" :key="c.path">
668 <button class="crumb" @click="load(c.path)">{{ c.name }}</button>
669 <span v-if="i < crumbs.length - 1" class="crumb-sep">/</span>
670 </template>
671 <span v-if="searching" class="badge" style="margin-left: 8px">搜索结果 · {{ results.length }}</span>
672 </div>
673
674 <div v-if="selection.length" class="bulk-bar">
675 <b>已选 {{ selection.length }} 项</b>
676 <button class="btn btn-ghost btn-sm" @click="openPicker('bulk-move', null, path)">移动到…</button>
677 <button class="btn btn-danger btn-sm" @click="bulkDelete = true">移入回收站</button>
678 <button class="btn btn-ghost btn-sm" @click="selection = []">取消选择</button>
679 </div>
680
681 <div class="table-wrap">
682 <table class="table">
683 <thead>
684 <tr>
685 <th style="width: 34px">
686 <input type="checkbox" :checked="allSelected" aria-label="全选" @change="toggleAll" />
687 </th>
688 <th>名称</th>
689 <th style="width: 100px">大小</th>
690 <th style="width: 165px">修改时间</th>
691 <th style="width: 78px">权限</th>
692 <th class="ops-col">操作</th>
693 </tr>
694 </thead>
695 <tbody>
696 <tr v-for="entry in visible" :key="entry.path">
697 <td>
698 <input
699 type="checkbox"
700 :checked="selection.includes(entry.path)"
701 :aria-label="`选择 ${entry.name}`"
702 @change="toggleSelect(entry)"
703 />
704 </td>
705 <td>
706 <button class="file-name" @click="openEntry(entry)">
707 <span class="file-icon" :class="{ dir: entry.dir }">{{ entry.dir ? '▸' : '·' }}</span>
708 {{ entry.name }}
709 <span v-if="entry.symlink" class="badge off" style="margin-left: 6px">链接</span>
710 </button>
711 <div v-if="searching" class="mono muted tiny clickable" @click="gotoEntryDir(entry)">
712 {{ entry.path }}
713 </div>
714 </td>
715 <td>{{ entry.dir ? '—' : bytes(entry.size) }}</td>
716 <td class="mono">{{ time(entry.mod_time) }}</td>
717 <td class="mono">{{ entry.mode }}</td>
718 <td>
719 <div class="row ops">
720 <button v-if="entry.dir" class="btn btn-sm btn-ghost" @click="load(entry.path)">打开</button>
721 <button v-else-if="canEdit(entry)" class="btn btn-sm btn-ghost" @click="openEditor(entry)">
722 编辑
723 </button>
724 <button v-else-if="kindOf(entry) === 'image'" class="btn btn-sm btn-ghost" @click="openEntry(entry)">
725 预览
726 </button>
727 <button v-else class="btn btn-sm btn-ghost" @click="download(entry)">下载</button>
728 <button v-if="!entry.dir" class="btn btn-sm btn-ghost" @click="download(entry)">下载</button>
729 <button class="btn btn-sm btn-ghost" @click="openMenu(entry, $event)">更多</button>
730 </div>
731 </td>
732 </tr>
733 <tr v-if="visible.length === 0">
734 <td colspan="6">
735 <div class="empty">
736 {{ loading ? '加载中…' : searching ? '没有匹配的文件' : '空目录,拖拽文件到窗口中即可上传' }}
737 </div>
738 </td>
739 </tr>
740 </tbody>
741 </table>
742 </div>
743
744 <div v-if="uploads.length" class="upload-panel">
745 <div class="row upload-head">
746 <b>上传队列({{ uploads.length }})</b>
747 <div class="spacer"></div>
748 <button class="btn btn-ghost btn-sm" @click="clearFinished">清除已完成</button>
749 <button class="icon-btn" title="取消并关闭" @click="closeUploadPanel">×</button>
750 </div>
751 <div v-for="u in uploads" :key="u.id" class="upload-row">
752 <span class="mono upload-name">{{ u.name }}</span>
753 <div class="bar"><i :style="{ width: u.percent + '%' }"></i></div>
754 <span class="tiny muted upload-pct">
755 {{ u.status === 'done' ? '完成' : u.status === 'error' ? u.error : u.status === 'canceled' ? '已取消' : u.percent + '%' }}
756 </span>
757 <button
758 v-if="u.status === 'uploading' || u.status === 'waiting'"
759 class="icon-btn"
760 title="取消"
761 @click="cancelUpload(u)"
762 >
763 ×
764 </button>
765 </div>
766 </div>
767
768 <div v-if="dragActive" class="drop-hint">松开鼠标即可上传到当前目录</div>
769 </div>
770
771 <!-- editor -->
772 <div v-if="editor.open" class="overlay" @click.self="closeEditor">
773 <div class="modal wide">
774 <div class="modal-head">
775 <div>
776 <h3>{{ editor.path }}</h3>
777 <div class="muted mono" style="font-size: 12px">
778 {{ editor.content.length }} 字符 · 最大 2 MB
779 <span v-if="editor.content !== editor.original" class="unsaved">· 未保存</span>
780 </div>
781 </div>
782 <button class="icon-btn" @click="closeEditor">×</button>
783 </div>
784 <textarea v-model="editor.content" class="textarea code-editor" spellcheck="false"></textarea>
785 <div class="row end" style="margin-top: 12px">
786 <button class="btn btn-ghost" @click="closeEditor">取消</button>
787 <button class="btn btn-primary" :disabled="editor.busy" @click="saveEditor">
788 <span v-if="editor.busy" class="spin"></span>
789 保存
790 </button>
791 </div>
792 </div>
793 </div>
794
795 <!-- per-entry menu -->
796 <div v-if="menu.open && menu.target" class="menu-backdrop" @click="closeMenu" @contextmenu.prevent="closeMenu"></div>
797 <div v-if="menu.open && menu.target" class="menu-pop" :style="{ left: menu.x + 'px', top: menu.y + 'px' }">
798 <div class="menu-title mono">{{ menu.target.name }}</div>
799 <button v-if="canEdit(menu.target)" @click="openEditor(menu.target); closeMenu()">编辑</button>
800 <button v-if="!menu.target.dir" @click="download(menu.target); closeMenu()">下载</button>
801 <button @click="askRename(menu.target)">重命名</button>
802 <button @click="openPicker('copy', menu.target, path)">复制到…</button>
803 <button @click="openPicker('move', menu.target, path)">移动到…</button>
804 <button @click="compress(menu.target, 'zip')">压缩为 zip</button>
805 <button @click="compress(menu.target, 'tar.gz')">压缩为 tar.gz</button>
806 <button v-if="kindOf(menu.target) === 'archive'" @click="extractEntry(menu.target); closeMenu()">解压到…</button>
807 <button @click="askChmod(menu.target)">修改权限</button>
808 <div class="menu-sep"></div>
809 <button class="danger" @click="deleteTarget = menu.target; closeMenu()">移入回收站</button>
810 </div>
811
812 <!-- rename / new folder -->
813 <div v-if="rename.open" class="overlay" @click.self="rename.open = false">
814 <div class="modal" style="width: min(480px, 100%)">
815 <div class="modal-head">
816 <h3>{{ rename.target ? '重命名' : '新建文件夹' }}</h3>
817 <button class="icon-btn" @click="rename.open = false">×</button>
818 </div>
819 <label class="field">
820 {{ rename.target ? '新名称' : `在 ${path} 下创建文件夹` }}
821 <input v-model="rename.value" class="input mono" autofocus @keyup.enter="submitRename" />
822 </label>
823 <div class="row end" style="margin-top: 16px">
824 <button class="btn btn-ghost" @click="rename.open = false">取消</button>
825 <button class="btn btn-primary" :disabled="rename.busy" @click="submitRename">
826 <span v-if="rename.busy" class="spin"></span>
827 {{ rename.target ? '确定' : '创建' }}
828 </button>
829 </div>
830 </div>
831 </div>
832
833 <!-- chmod -->
834 <div v-if="chmod.open" class="overlay" @click.self="chmod.open = false">
835 <div class="modal" style="width: min(480px, 100%)">
836 <div class="modal-head">
837 <h3>修改权限 · {{ chmod.target?.name }}</h3>
838 <button class="icon-btn" @click="chmod.open = false">×</button>
839 </div>
840 <div class="chmod-grid">
841 <span></span><span>读</span><span>写</span><span>执行</span>
842 <template v-for="(role, i) in ['所有者', '所属组', '其他']" :key="role">
843 <span class="muted">{{ role }}</span>
844 <label v-for="j in 3" :key="j" class="check chmod-cell">
845 <input v-model="chmod.bits[1 + i * 3 + (j - 1)]" type="checkbox" />
846 </label>
847 </template>
848 </div>
849 <div class="row" style="gap: 8px; margin-top: 10px">
850 <span class="muted">当前:<b class="mono">{{ chmodMode }}</b></span>
851 <div class="spacer"></div>
852 <button class="btn btn-ghost btn-sm" @click="setPreset('644')">644</button>
853 <button class="btn btn-ghost btn-sm" @click="setPreset('755')">755</button>
854 <button class="btn btn-ghost btn-sm" @click="setPreset('600')">600</button>
855 </div>
856 <div class="row end" style="margin-top: 16px">
857 <button class="btn btn-ghost" @click="chmod.open = false">取消</button>
858 <button class="btn btn-primary" :disabled="chmod.busy" @click="submitChmod">保存</button>
859 </div>
860 </div>
861 </div>
862
863 <!-- name prompt for copy / move -->
864 <div v-if="namePrompt.open" class="overlay" @click.self="namePrompt.open = false">
865 <div class="modal" style="width: min(480px, 100%)">
866 <div class="modal-head">
867 <h3>{{ picker.mode === 'copy' ? '复制' : '移动' }}</h3>
868 <button class="icon-btn" @click="namePrompt.open = false">×</button>
869 </div>
870 <label class="field">
871 {{ namePrompt.label }}
872 <input v-model="namePrompt.value" class="input mono" autofocus @keyup.enter="submitNamePrompt" />
873 </label>
874 <div class="row end" style="margin-top: 16px">
875 <button class="btn btn-ghost" @click="namePrompt.open = false">取消</button>
876 <button class="btn btn-primary" :disabled="namePrompt.busy" @click="submitNamePrompt">
877 {{ picker.mode === 'copy' ? '复制' : '移动' }}
878 </button>
879 </div>
880 </div>
881 </div>
882
883 <!-- image preview -->
884 <div v-if="preview.open && preview.target" class="overlay" @click.self="preview.open = false">
885 <div class="modal preview-modal">
886 <div class="modal-head">
887 <h3 class="mono">{{ preview.target.name }}</h3>
888 <button class="icon-btn" @click="preview.open = false">×</button>
889 </div>
890 <div class="preview-box">
891 <img :src="downloadURL(preview.target)" :alt="preview.target.name" />
892 </div>
893 <div class="row end">
894 <button class="btn btn-ghost" @click="download(preview.target)">下载</button>
895 <button class="btn btn-primary" @click="preview.open = false">关闭</button>
896 </div>
897 </div>
898 </div>
899
900 <!-- recycle bin -->
901 <div v-if="trash.open" class="overlay" @click.self="trash.open = false">
902 <div class="modal wide">
903 <div class="modal-head">
904 <h3>回收站</h3>
905 <button class="icon-btn" @click="trash.open = false">×</button>
906 </div>
907 <table v-if="trash.items.length" class="table">
908 <thead>
909 <tr>
910 <th>名称</th>
911 <th>原位置</th>
912 <th style="width: 90px">大小</th>
913 <th style="width: 170px">删除时间</th>
914 <th style="width: 160px">操作</th>
915 </tr>
916 </thead>
917 <tbody>
918 <tr v-for="item in trash.items" :key="item.id">
919 <td>{{ item.name }}</td>
920 <td class="mono muted">{{ item.original }}</td>
921 <td>{{ item.dir ? '目录' : bytes(item.size) }}</td>
922 <td class="mono">{{ time(item.deleted_at) }}</td>
923 <td>
924 <div class="row ops">
925 <button class="btn btn-sm btn-ghost" @click="restoreTrash(item)">恢复</button>
926 <button class="btn btn-sm btn-danger" @click="purgeTrash(item)">删除</button>
927 </div>
928 </td>
929 </tr>
930 </tbody>
931 </table>
932 <div v-else class="empty">{{ trash.busy ? '加载中…' : '回收站是空的' }}</div>
933 <div class="modal-foot">
934 <button class="btn btn-danger btn-sm" :disabled="!trash.items.length" @click="purgeTrash()">清空回收站</button>
935 <div class="spacer"></div>
936 <button class="btn btn-ghost" @click="trash.open = false">关闭</button>
937 </div>
938 </div>
939 </div>
940
941 <DirPicker
942 v-if="picker.open"
943 :title="picker.mode === 'extract' ? '解压到' : picker.mode === 'bulk-move' ? '批量移动到' : '选择目标目录'"
944 :initial="picker.dir"
945 :select-label="picker.mode === 'extract' ? '解压到此目录' : '选择此目录'"
946 @close="picker.open = false"
947 @select="onPickDir"
948 />
949
950 <ConfirmDialog
951 v-if="deleteTarget"
952 :title="`移入回收站:${deleteTarget.name}`"
953 message="删除的文件会进入回收站,可以随时恢复。"
954 confirm-text="移入回收站"
955 :busy="deleteBusy"
956 :error="deleteError"
957 @close="deleteTarget = null"
958 @confirm="confirmSingleDelete"
959 />
960
961 <ConfirmDialog
962 v-if="bulkDelete"
963 :title="`移入回收站:${selection.length} 项`"
964 message="选中的文件和目录都会进入回收站,之后可以恢复。"
965 confirm-text="全部移入回收站"
966 :busy="deleteBusy"
967 :error="deleteError"
968 @close="bulkDelete = false"
969 @confirm="confirmBulkDelete"
970 />
971</template>
972
973<style scoped>
974.files-card {
975 position: relative;
976}
977.files-card.dragging {
978 outline: 2px dashed rgba(52, 211, 153, 0.8);
979 outline-offset: -6px;
980}
981.files-head {
982 flex-wrap: wrap;
983 gap: 10px;
984}
985.files-actions {
986 flex-wrap: wrap;
987 gap: 6px;
988}
989.crumbs {
990 display: flex;
991 align-items: center;
992 gap: 2px;
993 flex-wrap: wrap;
994 margin: 6px 0 12px;
995 font-size: 13px;
996}
997.crumb {
998 border: none;
999 background: none;
1000 color: #0f766e;
1001 cursor: pointer;
1002 padding: 3px 6px;
1003 border-radius: 6px;
1004}
1005.crumb:hover {
1006 background: rgba(52, 211, 153, 0.12);
1007}
1008.crumb-sep {
1009 color: #b6cdd2;
1010}
1011.bulk-bar {
1012 display: flex;
1013 align-items: center;
1014 gap: 8px;
1015 flex-wrap: wrap;
1016 margin-bottom: 10px;
1017 padding: 8px 12px;
1018 border-radius: 10px;
1019 background: rgba(52, 211, 153, 0.1);
1020 border: 1px solid rgba(52, 211, 153, 0.3);
1021 font-size: 12.5px;
1022}
1023.file-name {
1024 display: flex;
1025 align-items: center;
1026 gap: 8px;
1027 border: none;
1028 background: none;
1029 color: inherit;
1030 cursor: pointer;
1031 padding: 0;
1032 text-align: left;
1033}
1034.file-name:hover {
1035 color: #0f766e;
1036}
1037.file-icon {
1038 display: inline-grid;
1039 place-items: center;
1040 width: 20px;
1041 height: 20px;
1042 border-radius: 6px;
1043 background: #eef7f7;
1044 color: #7b98a1;
1045 font-size: 12px;
1046}
1047.file-icon.dir {
1048 background: rgba(56, 189, 248, 0.14);
1049 color: #0284c7;
1050}
1051.tiny {
1052 font-size: 11.5px;
1053}
1054.clickable {
1055 cursor: pointer;
1056}
1057.clickable:hover {
1058 color: #0f766e;
1059 text-decoration: underline;
1060}
1061.ops {
1062 gap: 4px;
1063 flex-wrap: wrap;
1064}
1065.ops-col {
1066 width: 200px;
1067}
1068.code-editor {
1069 min-height: 420px;
1070 font-size: 13px;
1071 background: #f7fcfb;
1072}
1073.unsaved {
1074 color: #d97706;
1075}
1076.menu-backdrop {
1077 position: fixed;
1078 inset: 0;
1079 z-index: 44;
1080}
1081.menu-pop {
1082 position: fixed;
1083 z-index: 45;
1084 width: 186px;
1085 padding: 6px;
1086 border-radius: 12px;
1087 border: 1px solid var(--line);
1088 background: #fff;
1089 box-shadow: 0 18px 40px rgba(23, 58, 67, 0.2);
1090 display: flex;
1091 flex-direction: column;
1092}
1093.menu-pop button {
1094 text-align: left;
1095 padding: 7px 9px;
1096 border: none;
1097 border-radius: 7px;
1098 background: none;
1099 cursor: pointer;
1100 font: inherit;
1101 color: var(--text);
1102}
1103.menu-pop button:hover {
1104 background: #f1f9f8;
1105}
1106.menu-pop button.danger {
1107 color: #dc2626;
1108}
1109.menu-title {
1110 padding: 4px 9px 6px;
1111 color: var(--muted);
1112 overflow: hidden;
1113 text-overflow: ellipsis;
1114 white-space: nowrap;
1115}
1116.menu-sep {
1117 height: 1px;
1118 margin: 4px 2px;
1119 background: var(--line);
1120}
1121.chmod-grid {
1122 display: grid;
1123 grid-template-columns: 90px repeat(3, 1fr);
1124 gap: 8px;
1125 align-items: center;
1126 padding: 10px 12px;
1127 border: 1px solid var(--line);
1128 border-radius: 10px;
1129 background: #fbfefd;
1130}
1131.chmod-cell {
1132 justify-content: center;
1133}
1134.upload-panel {
1135 position: fixed;
1136 right: 20px;
1137 bottom: 76px;
1138 z-index: 30;
1139 width: min(360px, calc(100vw - 40px));
1140 max-height: 40vh;
1141 overflow: auto;
1142 padding: 12px;
1143 border-radius: 14px;
1144 border: 1px solid var(--line);
1145 background: #fff;
1146 box-shadow: 0 18px 44px rgba(23, 58, 67, 0.18);
1147}
1148.upload-head {
1149 margin-bottom: 8px;
1150 align-items: center;
1151}
1152.upload-row {
1153 display: grid;
1154 grid-template-columns: minmax(0, 1fr) 90px 62px 24px;
1155 align-items: center;
1156 gap: 8px;
1157 padding: 4px 0;
1158}
1159.upload-name {
1160 overflow: hidden;
1161 text-overflow: ellipsis;
1162 white-space: nowrap;
1163 font-size: 12px;
1164}
1165.upload-pct {
1166 text-align: right;
1167 overflow: hidden;
1168 text-overflow: ellipsis;
1169 white-space: nowrap;
1170}
1171.drop-hint {
1172 position: absolute;
1173 inset: 10px;
1174 display: grid;
1175 place-items: center;
1176 border-radius: 14px;
1177 border: 2px dashed rgba(16, 185, 129, 0.7);
1178 background: rgba(240, 253, 247, 0.92);
1179 color: #047857;
1180 font-weight: 600;
1181 pointer-events: none;
1182 z-index: 6;
1183}
1184.preview-modal {
1185 width: min(900px, 100%);
1186}
1187.preview-box {
1188 display: grid;
1189 place-items: center;
1190 max-height: 62vh;
1191 overflow: auto;
1192 margin-bottom: 12px;
1193 border-radius: 12px;
1194 background: #f7fcfb;
1195 padding: 12px;
1196}
1197.preview-box img {
1198 max-width: 100%;
1199 max-height: 58vh;
1200}
1201@media (max-width: 720px) {
1202 .files-actions {
1203 width: 100%;
1204 }
1205 .files-actions .input {
1206 flex: 1;
1207 }
1208}
1209</style>