Skip to content

Commit 5199c1e

Browse files
committed
feat: FilesDownloadDrawer 下载支持 blob,例如解压场景,isSelectFolder 为 true 时,可以直接写入 blob ,不用 fetch
1 parent 5857b41 commit 5199c1e

4 files changed

Lines changed: 29 additions & 14 deletions

File tree

src/components/FilesDownloadDrawer.tsx

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,12 @@ function FilesDownloadDrawer({
101101
return
102102
}
103103
try {
104-
const fileBlob = await downloadResourceAsBlob(item.url!)
104+
let fileBlob: Blob
105+
if (item.url instanceof Blob) {
106+
fileBlob = item.url
107+
} else {
108+
fileBlob = await downloadResourceAsBlob(item.url!)
109+
}
105110
if (item.folderPath === '/') {
106111
const fileHandle = await directoryHandle.current.getFileHandle(item.name, { create: true })
107112
const writable = await fileHandle.createWritable()
@@ -125,11 +130,15 @@ function FilesDownloadDrawer({
125130

126131
const downloadFile = async (item: DrawerFileItemType) => {
127132
console.log('downloadFile', item)
128-
const isCross = isCrossOrigin(item.url!)
129-
let url = item.url!
130-
if (isCross) {
133+
const isCross = item.url instanceof Blob ? false : isCrossOrigin(item.url!)
134+
let url: string = ''
135+
if (item.url instanceof Blob) {
136+
url = URL.createObjectURL(item.url)
137+
} else if (isCross) {
131138
const blob = await downloadResourceAsBlob(item.url!)
132139
url = URL.createObjectURL(blob)
140+
} else {
141+
url = item.url!
133142
}
134143
const a = document.createElement('a')
135144
a.href = url
@@ -138,7 +147,7 @@ function FilesDownloadDrawer({
138147
document.body.appendChild(a)
139148
a.click()
140149
document.body.removeChild(a)
141-
if (isCross) URL.revokeObjectURL(url)
150+
if (item.url instanceof Blob || isCross) URL.revokeObjectURL(url)
142151
}
143152
if (item.url) {
144153
if (canSelectFolder) {

src/components/FilesDrawer.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ function FilesDrawer({
8282
const fileListRef = useRef<HTMLUListElement>(null)
8383
const [fileList, updateFileList] = useImmer<DrawerFileItemType[]>([])
8484
const [loading, setLoading] = useState<boolean>(false)
85+
const [drawerLoading, setDrawerLoading] = useState<boolean>(false)
8586

8687
const doneFileFn = useCallback(async (item: DrawerFileItemType) => {
8788
setLoading(true)
@@ -169,6 +170,12 @@ function FilesDrawer({
169170
useEffect(() => {
170171
if (!open) return
171172
if (loading) return
173+
if (open && fileList.length === 0) {
174+
// eslint-disable-next-line react-hooks/set-state-in-effect
175+
setDrawerLoading(true)
176+
} else {
177+
setDrawerLoading(false)
178+
}
172179

173180
if (fileList.length > 0 && fileList.every((item) => item.percentStatus === 'success')) {
174181
setOpen(false)
@@ -244,6 +251,7 @@ function FilesDrawer({
244251
title={title}
245252
open={open}
246253
size='large'
254+
loading={drawerLoading}
247255
onClose={doCloseFn}
248256
maskClosable={false}
249257
classNames={{

src/mdx/FilesZipReader/FilesZipReader.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -216,16 +216,14 @@ function FilesZipReader() {
216216
type: 'file',
217217
filePath: [oldFileName.current, ...parts.slice(0)].join('/'),
218218
folderPath: [oldFileName.current, ...parts.slice(0, -1)].join('/'),
219-
url: URL.createObjectURL(blobData),
219+
url: blobData,
220220
})
221221
}
222222
setLoading(false)
223223
setFileList(allFileList)
224224
}
225225
function onSuccessFn(list: DrawerFileItemType[]) {
226-
list.forEach(item => {
227-
if (item.url) URL.revokeObjectURL(item.url)
228-
})
226+
console.log('onSuccessFn', list)
229227
}
230228
async function downloadCurrentFolder() {
231229
setDrawerOpen(true)
@@ -336,7 +334,7 @@ function FilesZipReader() {
336334
pagination={false} />
337335
<FilesDownloadDrawer
338336
targetType='list'
339-
title='下载文件/文件夹'
337+
title='解压文件/文件夹'
340338
open={drawerOpen}
341339
list={fileList}
342340
setOpen={setDrawerOpen}

src/types/files.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ export type FileItemType = {
3232
*/
3333
folderPath: string
3434
/**
35-
* @description 文件 URL。|| File URL.
35+
* @description 文件 URL 或者Blob对象。|| File URL or Blob object.
3636
*/
37-
url?: string
37+
url?: string | Blob
3838
}
3939

4040
export type FileTreeItem = FileItemType & {
@@ -51,7 +51,7 @@ export type TargetType = 'tree' | 'list'
5151

5252
export type FileDownloadItemType = Omit<FileItemType, 'file' | 'url'> & {
5353
/**
54-
* @description 文件 URL。|| File URL.
54+
* @description 文件 URL 或者Blob对象。|| File URL or Blob object.
5555
*/
56-
url: string
56+
url: string | Blob
5757
}

0 commit comments

Comments
 (0)