|
40 | 40 |
|
41 | 41 | function getDownloadUrl(item) { |
42 | 42 | const path = `/api/items/${item.id}/download`; |
43 | | - if (state.role === 'admin' && state.shareInfo?.url) { |
44 | | - return state.shareInfo.url.replace(/\/client\.html$/, path); |
| 43 | + if (state.shareInfo?.url) { |
| 44 | + return new URL(path, new URL(state.shareInfo.url).origin).toString(); |
45 | 45 | } |
46 | 46 | return new URL(path, window.location.origin).toString(); |
47 | 47 | } |
|
563 | 563 | const pickAdminFilesButton = $('pickAdminFilesButton'); |
564 | 564 | const adminDropZones = Array.from(document.querySelectorAll('.admin-file-drop')); |
565 | 565 | if (fileForm && fileInput) { |
566 | | - const updateFileHint = () => { |
| 566 | + let uploadingFiles = false; |
| 567 | + |
| 568 | + const updateFileHint = (message) => { |
| 569 | + if (!fileHint) return; |
| 570 | + if (message) { |
| 571 | + fileHint.textContent = message; |
| 572 | + return; |
| 573 | + } |
567 | 574 | const count = fileInput.files.length; |
568 | | - fileHint.textContent = count ? `已选择 ${count} 个文件,上传中会自动开始` : '支持多文件上传'; |
| 575 | + fileHint.textContent = count ? `已选择 ${count} 个文件,正在准备上传` : '选择后自动上传'; |
569 | 576 | }; |
570 | 577 |
|
571 | 578 | const uploadSelectedFiles = async () => { |
572 | | - if (!fileInput.files.length) return; |
573 | | - updateFileHint(); |
| 579 | + if (!fileInput.files.length || uploadingFiles) return; |
| 580 | + const files = Array.from(fileInput.files); |
| 581 | + uploadingFiles = true; |
| 582 | + updateFileHint(`正在上传 ${files.length} 个文件,请保持页面打开`); |
574 | 583 | if (dropZone) { |
575 | 584 | dropZone.classList.add('uploading'); |
576 | 585 | } |
577 | | - const data = new FormData(); |
578 | | - data.append('source', state.role); |
579 | | - for (const file of Array.from(fileInput.files)) { |
580 | | - data.append('file', file, file.name); |
581 | | - } |
582 | | - await request('/api/upload', { method: 'POST', body: data }); |
583 | | - fileForm.reset(); |
584 | | - updateFileHint(); |
585 | | - if (dropZone) { |
586 | | - dropZone.classList.remove('uploading'); |
| 586 | + try { |
| 587 | + const data = new FormData(); |
| 588 | + data.append('source', state.role); |
| 589 | + for (const file of files) { |
| 590 | + data.append('file', file, file.name); |
| 591 | + } |
| 592 | + await request('/api/upload', { method: 'POST', body: data }); |
| 593 | + fileForm.reset(); |
| 594 | + updateFileHint('上传完成'); |
| 595 | + window.setTimeout(() => updateFileHint(), 900); |
| 596 | + fileForm.closest('dialog')?.close(); |
| 597 | + } finally { |
| 598 | + uploadingFiles = false; |
| 599 | + if (dropZone) { |
| 600 | + dropZone.classList.remove('uploading'); |
| 601 | + } |
587 | 602 | } |
588 | | - fileForm.closest('dialog')?.close(); |
589 | 603 | }; |
590 | 604 |
|
591 | 605 | fileInput.addEventListener('change', () => { |
592 | 606 | if (!fileInput.files.length) return; |
593 | 607 | updateFileHint(); |
594 | 608 | uploadSelectedFiles().catch((error) => { |
| 609 | + uploadingFiles = false; |
595 | 610 | if (dropZone) { |
596 | 611 | dropZone.classList.remove('uploading'); |
597 | 612 | } |
| 613 | + updateFileHint('上传失败,请重新选择'); |
598 | 614 | alert(error.message); |
599 | 615 | }); |
600 | 616 | }); |
|
0 commit comments