fix(downloadAll): 防止未解密文件导致的下载失败 优化目录选择框的弹出 优化权限请求

This commit is contained in:
awalol
2025-06-17 18:40:58 +08:00
committed by awalol
parent b493391371
commit 2c461df5fc

View File

@@ -1,23 +1,43 @@
import { DecryptedAudioFile, selectFiles } from '~/features/file-listing/fileListingSlice'; import { DecryptedAudioFile, ProcessState, selectFiles } from '~/features/file-listing/fileListingSlice';
import { FaDownload } from 'react-icons/fa'; import { FaDownload } from 'react-icons/fa';
import { useAppSelector } from '~/hooks'; import { useAppSelector } from '~/hooks';
import { toast } from 'react-toastify'; import { toast } from 'react-toastify';
export function DownloadAll() { export function DownloadAll() {
const files = useAppSelector(selectFiles); const files = useAppSelector(selectFiles);
const filesLength = Object.keys(files).length;
const onClickDownloadAll = async () => { const onClickDownloadAll = async () => {
if (Object.keys(files).length === 0) {
toast.warning('未添加文件');
return;
}
//判断所有文件是否处理完成
const allComplete = Object.values(files).every((file) => file.state !== ProcessState.PROCESSING);
if (!allComplete) {
toast.warning('请等待所有文件解密完成');
return;
}
//过滤处理失败的文件
const completeFiles = Object.values(files).filter((file) => file.state === ProcessState.COMPLETE);
const filesLength = Object.keys(completeFiles).length;
//开始下载
let dir: FileSystemDirectoryHandle | undefined; let dir: FileSystemDirectoryHandle | undefined;
let success = 0; let success = 0;
try { try {
dir = await window.showDirectoryPicker(); dir = await window.showDirectoryPicker({ mode: 'readwrite' });
} catch (e) { } catch (e) {
console.error(e); console.error(e);
if (e instanceof Error && e.name === 'AbortError') { if (e instanceof Error && e.name === 'AbortError') {
return; return;
} }
} }
for (const [_, file] of Object.entries(files)) { toast.warning('开始下载,请稍候');
for (const [_, file] of Object.entries(completeFiles)) {
if (file.state !== ProcessState.COMPLETE) {
return;
}
try { try {
if (dir) { if (dir) {
await DownloadNew(dir, file); await DownloadNew(dir, file);
@@ -33,7 +53,7 @@ export function DownloadAll() {
if (success === filesLength) { if (success === filesLength) {
toast.success(`成功下载: ${success}/${filesLength}`); toast.success(`成功下载: ${success}/${filesLength}`);
} else { } else {
toast.error(`成功下载: ${success}/${filesLength}`); toast.warning(`成功下载: ${success}/${filesLength}`);
} }
}; };