update download function

Rename it and turn it into async other than using callback
This commit is contained in:
myl7
2021-11-27 16:31:02 +08:00
parent 6c63aa90be
commit 1d6c40affe
2 changed files with 14 additions and 19 deletions
+3 -3
View File
@@ -242,7 +242,7 @@ const FileListing: FunctionComponent<{ query?: ParsedUrlQuery }> = ({ query }) =
} }
// Selected file download // Selected file download
const handleDownloadSelected = () => { const handleSelectedDownload = () => {
const folderName = path.substr(path.lastIndexOf('/') + 1) const folderName = path.substr(path.lastIndexOf('/') + 1)
const folder = folderName ? folderName : undefined const folder = folderName ? folderName : undefined
const files = getFiles() const files = getFiles()
@@ -257,7 +257,7 @@ const FileListing: FunctionComponent<{ query?: ParsedUrlQuery }> = ({ query }) =
el.remove() el.remove()
} else if (files.length > 1) { } else if (files.length > 1) {
setTotalGenerating(true) setTotalGenerating(true)
saveFiles(files, folder, () => setTotalGenerating(false)) saveFiles(files, folder).then(() => setTotalGenerating(false))
} }
} }
@@ -302,7 +302,7 @@ const FileListing: FunctionComponent<{ query?: ParsedUrlQuery }> = ({ query }) =
<span <span
title="Download selected files" title="Download selected files"
className="hover:bg-gray-300 dark:hover:bg-gray-600 p-2 rounded cursor-pointer" className="hover:bg-gray-300 dark:hover:bg-gray-600 p-2 rounded cursor-pointer"
onClick={handleDownloadSelected} onClick={handleSelectedDownload}
> >
<FontAwesomeIcon icon={['far', 'arrow-alt-circle-down']} /> <FontAwesomeIcon icon={['far', 'arrow-alt-circle-down']} />
</span> </span>
+2 -7
View File
@@ -129,15 +129,14 @@ export const matchProtectedRoute = (route: string) => {
* Download multiple files after compressing them into a zip * Download multiple files after compressing them into a zip
* @param files Files to be downloaded * @param files Files to be downloaded
* @param folder Optional folder name to hold files, otherwise flatten files in the zip * @param folder Optional folder name to hold files, otherwise flatten files in the zip
* @param onFinish Optional hook triggered after sending generated zip to browser to download
*/ */
export const saveFiles = (files: { name: string, url: string }[], folder?: string, onFinish?: () => void) => { export const saveFiles = async (files: { name: string, url: string }[], folder?: string) => {
const zip = new JSZip() const zip = new JSZip()
const dir = folder ? zip.folder(folder)! : zip const dir = folder ? zip.folder(folder)! : zip
files.forEach(({ name, url }) => { files.forEach(({ name, url }) => {
dir.file(name, fetch(url).then(r => r.blob())) dir.file(name, fetch(url).then(r => r.blob()))
}) })
dir.generateAsync({ type: 'blob' }).then(b => { const b = await dir.generateAsync({ type: 'blob' })
const el = document.createElement('a') const el = document.createElement('a')
el.style.display = 'none' el.style.display = 'none'
document.body.appendChild(el) document.body.appendChild(el)
@@ -147,8 +146,4 @@ export const saveFiles = (files: { name: string, url: string }[], folder?: strin
el.click() el.click()
window.URL.revokeObjectURL(bUrl) window.URL.revokeObjectURL(bUrl)
el.remove() el.remove()
if (onFinish) {
onFinish()
}
})
} }