完成ali-oss文件管理
This commit is contained in:
@@ -0,0 +1,204 @@
|
||||
'use client';
|
||||
|
||||
import { Button } from "@/components/ui/button"
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogDescription,
|
||||
DialogFooter,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
DialogTrigger,
|
||||
} from "@/components/ui/dialog"
|
||||
import OSS from "ali-oss";
|
||||
import { ArrowUpFromLine, Check, CloudUpload, ClubIcon, Coins, File, Files, X } from "lucide-react";
|
||||
import React, { useRef, useState } from "react";
|
||||
import { Progress } from '@/components/ui/progress';
|
||||
import { cn } from "@/lib/utils";
|
||||
import { toast } from "sonner";
|
||||
|
||||
interface UploadManagerProps {
|
||||
children: React.ReactNode;
|
||||
store?: OSS;
|
||||
basePath?: string;
|
||||
handleRefreshFileList?: () => void;
|
||||
}
|
||||
|
||||
interface UploadFileItem {
|
||||
id: string;
|
||||
file: File;
|
||||
status: 'ready' | 'uploading' | 'finish' | 'failed';
|
||||
progress: number;// 0 ~ 100
|
||||
}
|
||||
|
||||
export function UploadManager({ children, store, basePath, handleRefreshFileList }: UploadManagerProps) {
|
||||
const [filesList, setFileList] = useState<UploadFileItem[]>([]);
|
||||
|
||||
const handleFileSelect = (fileList: FileList) => {
|
||||
setFileList(currentFileList => {
|
||||
const newFiles: UploadFileItem[] = [];
|
||||
for (let file of fileList) {
|
||||
const repeatFile = currentFileList.find(f =>
|
||||
f.file.name === file.name &&
|
||||
f.file.size === file.size &&
|
||||
f.file.lastModified === file.lastModified
|
||||
);
|
||||
if (!repeatFile) {
|
||||
newFiles.push({
|
||||
id: `${Math.random()}${Date.now()}`,
|
||||
file: file,
|
||||
progress: 0,
|
||||
status: 'ready',
|
||||
})
|
||||
}
|
||||
}
|
||||
return [...currentFileList, ...newFiles];
|
||||
})
|
||||
}
|
||||
|
||||
const handleFileDelete = (fileItemId: string) => {
|
||||
if (isUploading) return toast.warning('上传过程暂时无法删除');
|
||||
setFileList(currentFileList => currentFileList.filter(i => i.id !== fileItemId))
|
||||
}
|
||||
|
||||
const inputFileRef = useRef<HTMLInputElement>(null);
|
||||
const handleFileInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
if (!e.target.files) return;
|
||||
handleFileSelect(e.target.files);
|
||||
}
|
||||
|
||||
const [fileDroping, setFileDroping] = useState(false);
|
||||
const handleFileOnDropOver = (e: React.DragEvent<HTMLDivElement>) => {
|
||||
e.preventDefault();
|
||||
setFileDroping(true);
|
||||
}
|
||||
|
||||
const handleFileOnDrop = (e: React.DragEvent<HTMLDivElement>) => {
|
||||
e.preventDefault();
|
||||
setFileDroping(false);
|
||||
if (e.dataTransfer.files && e.dataTransfer.files.length > 0) {
|
||||
handleFileSelect(e.dataTransfer.files);
|
||||
}
|
||||
}
|
||||
|
||||
const [isUploading, setIsUploading] = useState(false);
|
||||
const handleUpload = async () => {
|
||||
const needUploadFiles = filesList.filter(f => f.status !== 'finish');
|
||||
if (needUploadFiles.length === 0) return toast.info('请选择需要上传的文件');
|
||||
|
||||
let failCount = 0;
|
||||
setIsUploading(true);
|
||||
for (const fileItem of needUploadFiles) {
|
||||
fileItem.status = 'uploading';
|
||||
await startUploadFile(fileItem).catch(e => { fileItem.status = 'failed'; failCount++; });
|
||||
fileItem.status = 'finish';
|
||||
}
|
||||
setIsUploading(false);
|
||||
|
||||
if (failCount > 0) {
|
||||
toast.warning(`上传完成,本次共有${failCount}个文件上传失败`);
|
||||
} else {
|
||||
toast.success(`上传完成,共上传了${needUploadFiles.length}个文件`)
|
||||
}
|
||||
handleRefreshFileList?.();
|
||||
}
|
||||
|
||||
// 开始上传文件
|
||||
const startUploadFile = async (fileItem: UploadFileItem) => {
|
||||
if (!store || !basePath) return;
|
||||
|
||||
let checkpoint: any;
|
||||
await store.multipartUpload(`${basePath}/${fileItem.file.name}`, fileItem.file, {
|
||||
checkpoint: checkpoint,
|
||||
progress: (p, cpt, res) => {
|
||||
setFileList(currentFileList => {
|
||||
return currentFileList.map(f => {
|
||||
if (f.id == fileItem.id) {
|
||||
f.progress = p * 100;
|
||||
}
|
||||
return f;
|
||||
})
|
||||
});
|
||||
checkpoint = cpt;
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
return (
|
||||
<Dialog>
|
||||
<DialogTrigger asChild>
|
||||
{children}
|
||||
</DialogTrigger>
|
||||
<DialogContent className="sm:max-w-[425px]">
|
||||
<DialogHeader>
|
||||
<DialogTitle>上传文件</DialogTitle>
|
||||
<DialogDescription>
|
||||
请点击选择文件按钮或拖动文件到指定位置
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
<div>
|
||||
<div
|
||||
onDrop={handleFileOnDrop}
|
||||
onDragOver={handleFileOnDropOver}
|
||||
onClick={() => inputFileRef.current?.click()}
|
||||
className="flex items-center justify-center border shadow rounded select-none cursor-pointer relative">
|
||||
<div className={cn('flex flex-col items-center py-6 text-zinc-500 text-sm text-center', fileDroping && 'invisible')}>
|
||||
<div className="flex items-center gap-3">
|
||||
<CloudUpload className="size-10 text-zinc-400" />
|
||||
<span>请拖动文件到此处 或 点击此处</span>
|
||||
</div>
|
||||
<div className="w-full px-5 mt-3 text-xs">
|
||||
<p>支持同时选择多个文件,文件区分大小写,若存在同名文件,新文件将被覆;支持超过5GB的文件,上传过程请不要关闭对话框</p>
|
||||
</div>
|
||||
</div>
|
||||
<div className={cn('absolute flex items-center text-zinc-600 gap-1 invisible', fileDroping && 'visible')}>
|
||||
<Files className="size-6 text-zinc-500" />
|
||||
<span className="text-sm">请拖放至此处,并松开鼠标</span>
|
||||
</div>
|
||||
</div>
|
||||
<input
|
||||
type="file"
|
||||
multiple
|
||||
ref={inputFileRef}
|
||||
className="hidden"
|
||||
onChange={handleFileInputChange} />
|
||||
</div>
|
||||
<div>
|
||||
{
|
||||
filesList.map(f => (
|
||||
<div
|
||||
key={f.id}
|
||||
className="group hover:bg-zinc-100 duration-300 rounded px-1 py-1">
|
||||
<div className="flex justify-between items-center">
|
||||
<div className="flex items-center gap-1">
|
||||
<File className="size-4 text-zinc-500" />
|
||||
{/* 存在一个bug,文本省略无法自动适配容器宽度 */}
|
||||
<div className="text-sm text-zinc-800 overflow-hidden text-nowrap text-ellipsis max-w-60">
|
||||
{f.file.name}
|
||||
</div>
|
||||
</div>
|
||||
<div>
|
||||
{f.status === 'finish' && <Check className="size-4 text-green-600 group-hover:hidden" />}
|
||||
<X
|
||||
onClick={() => handleFileDelete(f.id)}
|
||||
className="size-4 text-zinc-500 cursor-pointer hover:text-zinc-600 hidden group-hover:block" />
|
||||
</div>
|
||||
</div>
|
||||
<div className="h-0 w-full relative">
|
||||
{f.status === 'uploading' && <Progress value={f.progress} className="h-1" />}
|
||||
</div>
|
||||
</div>
|
||||
))
|
||||
}
|
||||
</div>
|
||||
<DialogFooter>
|
||||
<Button
|
||||
type="button"
|
||||
className="cursor-pointer"
|
||||
onClick={handleUpload}
|
||||
disabled={isUploading}>开始上传</Button>
|
||||
</DialogFooter>
|
||||
</DialogContent>
|
||||
</Dialog >
|
||||
)
|
||||
}
|
||||
@@ -1,5 +1,196 @@
|
||||
'use client';
|
||||
|
||||
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 { useOssStore } from '@/hooks/admin/web/blog/use-oss-store';
|
||||
import { ObjectMeta } from 'ali-oss';
|
||||
import { Delete, Download, Edit, RefreshCcw, Upload } from 'lucide-react';
|
||||
import { useEffect, useMemo, useState } from 'react';
|
||||
import { toast } from 'sonner';
|
||||
import {
|
||||
DropdownMenu,
|
||||
DropdownMenuContent,
|
||||
DropdownMenuItem,
|
||||
DropdownMenuTrigger,
|
||||
} from "@/components/ui/dropdown-menu"
|
||||
import { UploadManager } from './components/UploadManager';
|
||||
|
||||
|
||||
const formatSizeNumber = (n: number) => {
|
||||
const unit = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB'];
|
||||
for (const [i, u] of unit.entries()) {
|
||||
if (n < 1024 ** (i + 1)) {
|
||||
if (i <= 0) {
|
||||
return `${(n)}${unit[i]}`;
|
||||
} else {
|
||||
return `${(n / 1024 ** i).toFixed(2)}${unit[i]}`;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
interface FileItem {
|
||||
id: string;
|
||||
name: string;
|
||||
size: number;// Byte
|
||||
lastModified: Date;
|
||||
isChecked: boolean;
|
||||
};
|
||||
|
||||
export default function Page() {
|
||||
const { stsTokenData, isLoading, error, store } = useOssStore();
|
||||
|
||||
const [fileList, setFileList] = useState<null | FileItem[]>(null);
|
||||
|
||||
const handleRefreshFileList = async () => {
|
||||
if (!store || !stsTokenData) return toast.error('初始化失败,请刷新界面重试');
|
||||
setFileList(null);
|
||||
const res = await store.listV2({ prefix: `tone-page/${stsTokenData.userId}` }, {}).catch(e => {
|
||||
toast.error('文件列表加载失败');
|
||||
});
|
||||
|
||||
if (res && res.objects) {
|
||||
setFileList(res.objects.map(v => ({
|
||||
id: v.name,
|
||||
name: v.name.replace(`tone-page/${stsTokenData.userId}/`, ''),
|
||||
size: v.size,
|
||||
lastModified: new Date(v.lastModified),
|
||||
isChecked: false,
|
||||
})));
|
||||
}
|
||||
}
|
||||
|
||||
const handleCheckboxChange = (id: string, checked: boolean) => {
|
||||
setFileList((prevFileList) => {
|
||||
if (!prevFileList) return null;
|
||||
|
||||
return prevFileList.map(item => {
|
||||
if (item.id === id) {
|
||||
return { ...item, isChecked: checked };
|
||||
}
|
||||
return item;
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
store && stsTokenData && handleRefreshFileList();
|
||||
}, [stsTokenData]);
|
||||
|
||||
const checkedFileIds = useMemo(() => {
|
||||
if (!fileList) return null;
|
||||
return fileList.filter(i => i.isChecked).map(i => i.id);
|
||||
}, [fileList])
|
||||
|
||||
|
||||
const handleDeleteCheckedFiles = async () => {
|
||||
if (!store || !stsTokenData) return toast.error('初始化未完成或失败,请等待或重新加载');
|
||||
const deleteFiles = (fileList || []).filter(i => i.isChecked);
|
||||
if (!deleteFiles) return toast.error('请选择需要删除的文件');
|
||||
|
||||
let failedCount = 0;
|
||||
for (let item of deleteFiles) {
|
||||
await deleteFile(item.name).catch(e => { failedCount++; return e; });
|
||||
}
|
||||
|
||||
if (failedCount > 0) {
|
||||
toast.warning(`删除完成,共有${failedCount}个文件删除失败`)
|
||||
} else {
|
||||
toast.success(`${deleteFiles.length}个文件删除完成`)
|
||||
}
|
||||
handleRefreshFileList();
|
||||
}
|
||||
|
||||
const handleDeleteFile = async (fileItem: FileItem) => {
|
||||
await deleteFile(fileItem.name)
|
||||
.then(() => toast.success('删除完成'))
|
||||
.catch(() => toast.error('删除失败'));
|
||||
handleRefreshFileList();
|
||||
}
|
||||
|
||||
const deleteFile = async (localFilename: string) => {
|
||||
if (!store) throw new Error('store未初始化');
|
||||
if (!stsTokenData) throw new Error('sts服务未初始化');
|
||||
return store.delete(`/tone-page/${stsTokenData.userId}/${localFilename}`);
|
||||
}
|
||||
|
||||
const downloadFile = (localFilename: string) => {
|
||||
if (!store) throw new Error('store未初始化');
|
||||
if (!stsTokenData) throw new Error('sts服务未初始化');
|
||||
const url = store.signatureUrl(`/tone-page/${stsTokenData.userId}/${localFilename}`);
|
||||
const a = document.createElement('a');
|
||||
a.href = url;
|
||||
a.download = localFilename;
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
document.body.removeChild(a);
|
||||
}
|
||||
|
||||
const handleDownloadFile = async (fileItem: FileItem) => {
|
||||
downloadFile(fileItem.name);
|
||||
}
|
||||
|
||||
return (
|
||||
<div>storage</div>
|
||||
<div>
|
||||
<div>
|
||||
<Button variant='secondary' size='icon' className='cursor-pointer' onClick={() => handleRefreshFileList()}><RefreshCcw /></Button>
|
||||
</div>
|
||||
<div className='mt-1 flex gap-2'>
|
||||
<UploadManager
|
||||
store={store}
|
||||
basePath={stsTokenData && `/tone-page/${stsTokenData.userId}`}
|
||||
handleRefreshFileList={handleRefreshFileList}
|
||||
>
|
||||
<Button variant='default' className='cursor-pointer'><Upload />上传</Button>
|
||||
</UploadManager>
|
||||
<Button
|
||||
variant='destructive'
|
||||
className='cursor-pointer'
|
||||
disabled={(checkedFileIds?.length || 0) <= 0}
|
||||
onClick={() => handleDeleteCheckedFiles()}
|
||||
><Delete />删除</Button>
|
||||
</div>
|
||||
<Table>
|
||||
<TableCaption>
|
||||
{(isLoading || (fileList == null && !error)) && <div>加载中...</div>}
|
||||
{error && <div>{`${error}`}</div>}
|
||||
{fileList && fileList.length === 0 && <div>暂无文件</div>}
|
||||
</TableCaption>
|
||||
<TableHeader>
|
||||
<TableRow>
|
||||
<TableHead className='w-10'></TableHead>
|
||||
<TableHead>文件名</TableHead>
|
||||
<TableHead>文件大小</TableHead>
|
||||
<TableHead>上次修改时间</TableHead>
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
{
|
||||
fileList && fileList.map(d => (
|
||||
<TableRow key={d.name} >
|
||||
<TableCell>
|
||||
<Checkbox checked={d.isChecked} onCheckedChange={v => handleCheckboxChange(d.id, Boolean(v))} />
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<DropdownMenu>
|
||||
<DropdownMenuTrigger className='whitespace-normal break-all cursor-pointer text-left'>
|
||||
{d.name}
|
||||
</DropdownMenuTrigger>
|
||||
<DropdownMenuContent>
|
||||
<DropdownMenuItem onClick={() => handleDownloadFile(d)}><Download />下载</DropdownMenuItem>
|
||||
{/* <DropdownMenuItem><Edit />编辑</DropdownMenuItem> */}
|
||||
<DropdownMenuItem onClick={() => handleDeleteFile(d)}><Delete />删除</DropdownMenuItem>
|
||||
</DropdownMenuContent>
|
||||
</DropdownMenu>
|
||||
</TableCell>
|
||||
<TableCell>{formatSizeNumber(d.size)}</TableCell>
|
||||
<TableCell className='whitespace-normal break-words'>{d.lastModified.toLocaleString()}</TableCell>
|
||||
</TableRow>
|
||||
))
|
||||
}
|
||||
</TableBody>
|
||||
</Table>
|
||||
</div >
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user