重构ossStore
This commit is contained in:
@@ -16,7 +16,7 @@ import { Progress } from '@/components/ui/progress';
|
||||
import { cn } from "@/lib/utils";
|
||||
import { toast } from "sonner";
|
||||
import { OssStore } from "@/lib/oss/OssStore";
|
||||
import { Checkpoint } from "ali-oss";
|
||||
import OSS, { Checkpoint } from "ali-oss";
|
||||
|
||||
interface UploadManagerProps {
|
||||
children: React.ReactNode;
|
||||
@@ -82,7 +82,12 @@ export function UploadManager({ children, ossStore, handleRefreshFileList }: Upl
|
||||
}
|
||||
|
||||
const [isUploading, setIsUploading] = useState(false);
|
||||
/** 开始上传文件 */
|
||||
const handleUpload = async () => {
|
||||
if (!ossStore) return;
|
||||
let store: OSS;
|
||||
try { store = ossStore.getStore(); } catch { return toast.error(`初始化失败`) };
|
||||
|
||||
const needUploadFiles = filesList.filter(f => f.status !== 'finish');
|
||||
if (needUploadFiles.length === 0) return toast.info('请选择需要上传的文件');
|
||||
|
||||
@@ -90,7 +95,7 @@ export function UploadManager({ children, ossStore, handleRefreshFileList }: Upl
|
||||
setIsUploading(true);
|
||||
for (const fileItem of needUploadFiles) {
|
||||
fileItem.status = 'uploading';
|
||||
await startUploadFile(fileItem).catch(() => { fileItem.status = 'failed'; failCount++; });
|
||||
await startUploadFile(store, fileItem, ossStore.getWorkDir()).catch(() => { fileItem.status = 'failed'; failCount++; });
|
||||
fileItem.status = 'finish';
|
||||
}
|
||||
setIsUploading(false);
|
||||
@@ -105,12 +110,11 @@ export function UploadManager({ children, ossStore, handleRefreshFileList }: Upl
|
||||
handleRefreshFileList?.();
|
||||
}
|
||||
|
||||
// 开始上传文件
|
||||
const startUploadFile = async (fileItem: UploadFileItem) => {
|
||||
if (!ossStore) return;
|
||||
|
||||
// 上传单个文件
|
||||
const startUploadFile = async (store: OSS, fileItem: UploadFileItem, workDir?: string) => {
|
||||
let checkpoint: Checkpoint | undefined;
|
||||
await ossStore.storeMeta.store?.multipartUpload(`${ossStore.getWorkDir()}/${fileItem.file.name}`, fileItem.file, {
|
||||
|
||||
await store.multipartUpload(`${workDir ? `${workDir}/` : ''}${fileItem.file.name}`, fileItem.file, {
|
||||
checkpoint: checkpoint,
|
||||
progress: (p, cpt) => {
|
||||
setFileList(currentFileList => {
|
||||
|
||||
@@ -4,7 +4,7 @@ import { Button } from '@/components/ui/button';
|
||||
import { Checkbox } from '@/components/ui/checkbox';
|
||||
import { Table, TableBody, TableCaption, TableCell, TableHead, TableHeader, TableRow } from '@/components/ui/table';
|
||||
import { Delete, Download, RefreshCcw, Upload } from 'lucide-react';
|
||||
import { useMemo, useState } from 'react';
|
||||
import { useEffect, useMemo, useState } from 'react';
|
||||
import { toast } from 'sonner';
|
||||
import {
|
||||
DropdownMenu,
|
||||
@@ -14,6 +14,8 @@ import {
|
||||
} from "@/components/ui/dropdown-menu"
|
||||
import { UploadManager } from './components/UploadManager';
|
||||
import { OssObjectItem, OssObjectList, OssStore } from '@/lib/oss/OssStore';
|
||||
import { useOssStore } from '@/hooks/admin/web/blog/use-oss-store';
|
||||
import OSS from 'ali-oss';
|
||||
|
||||
|
||||
const formatSizeNumber = (n: number) => {
|
||||
@@ -29,17 +31,41 @@ const formatSizeNumber = (n: number) => {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
const ossStore = new OssStore();
|
||||
|
||||
export default function Page() {
|
||||
const [objectList, setObjectList] = useState<OssObjectList>(null)
|
||||
|
||||
const ossStore = new OssStore({
|
||||
prefix: 'tone-page',
|
||||
prefixAddUserId: true,
|
||||
objectList: () => objectList,
|
||||
setObjectList,
|
||||
});
|
||||
ossStore.setSetObjectList(setObjectList);
|
||||
|
||||
const storeMeta = useOssStore();
|
||||
|
||||
useEffect(() => {
|
||||
const data = storeMeta.stsTokenData;
|
||||
if (!data) return;
|
||||
|
||||
const store = new OSS({
|
||||
region: 'oss-cn-chengdu',
|
||||
bucket: 'tone-personal',
|
||||
accessKeyId: data.AccessKeyId,
|
||||
accessKeySecret: data.AccessKeySecret,
|
||||
stsToken: data.SecurityToken,
|
||||
refreshSTSToken: async () => {
|
||||
await storeMeta.refresh();
|
||||
if (!storeMeta.stsTokenData) throw new Error();
|
||||
const { AccessKeyId, AccessKeySecret, SecurityToken } = storeMeta.stsTokenData;
|
||||
return {
|
||||
accessKeyId: AccessKeyId,
|
||||
accessKeySecret: AccessKeySecret,
|
||||
stsToken: SecurityToken,
|
||||
};
|
||||
},
|
||||
})
|
||||
|
||||
ossStore.setStore(store);
|
||||
ossStore.setWorkDir(`tone-page/${data.userId}`)
|
||||
ossStore.loadObjectList();
|
||||
}, [storeMeta.stsTokenData]);
|
||||
|
||||
const handleRefreshFileList = async () => ossStore.loadObjectList().catch(e => toast.error(e.message));
|
||||
const handleCheckboxChange = ossStore.handleObjectCheckedStateChanged.bind(ossStore);
|
||||
|
||||
Reference in New Issue
Block a user