From f550a08e372f8bf59b2db8187937d710abc46b86 Mon Sep 17 00:00:00 2001 From: myl7 Date: Thu, 16 Dec 2021 21:57:04 +0800 Subject: [PATCH] update function names and comments according to upstream --- components/FileListing.tsx | 10 ++++++---- utils/tools.ts | 15 +++++++++++---- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/components/FileListing.tsx b/components/FileListing.tsx index 5910cc8..c7b2f92 100644 --- a/components/FileListing.tsx +++ b/components/FileListing.tsx @@ -12,7 +12,9 @@ import dynamic from 'next/dynamic' import { getExtension, getFileIcon, hasKey } from '../utils/getFileIcon' import { extensions, preview } from '../utils/getPreviewType' -import { getBaseUrl, treeList, downloadMultipleFiles, useProtectedSWRInfinite, saveTreeFiles } from '../utils/tools' +import { + getBaseUrl, traverseFolder, downloadMultipleFiles, useProtectedSWRInfinite, downloadTreelikeMultipleFiles +} from '../utils/tools' import { VideoPreview } from './previews/VideoPreview' import { AudioPreview } from './previews/AudioPreview' @@ -305,7 +307,7 @@ const FileListing: FunctionComponent<{ query?: ParsedUrlQuery }> = ({ query }) = // Folder recursive download const handleFolderDownload = (path: string, id: string, name?: string) => () => { const files = (async function* () { - for await (const { meta: c, path: p, isFolder } of treeList(path)) { + for await (const { meta: c, path: p, isFolder } of traverseFolder(path)) { yield { name: c?.name, url: c ? c['@microsoft.graph.downloadUrl'] : undefined, @@ -315,8 +317,8 @@ const FileListing: FunctionComponent<{ query?: ParsedUrlQuery }> = ({ query }) = } })() setFolderGenerating({ ...folderGenerating, [id]: true }) - const toastId = toast.loading('Downloading folder. This may be slow...') - saveTreeFiles(files, name).then(() => { + const toastId = toast.loading('Downloading folder. Refresh to cancel, this may take some time...') + downloadTreelikeMultipleFiles(files, name).then(() => { setFolderGenerating({ ...folderGenerating, [id]: false }) toast.dismiss(toastId) toast.success('Finished to download folder.') diff --git a/utils/tools.ts b/utils/tools.ts index 002e475..c76c475 100644 --- a/utils/tools.ts +++ b/utils/tools.ts @@ -141,12 +141,15 @@ export const downloadMultipleFiles = async (files: { name: string; url: string } fetch(url).then(r => r.blob()) ) }) + + // Create zip file and download it const b = await zip.generateAsync({ type: 'blob' }) downloadBlob(b, folder ? folder + '.zip' : 'download.zip') } // Blob download helper const downloadBlob = (b: Blob, name: string) => { + // Prepare for download const el = document.createElement('a') el.style.display = 'none' document.body.appendChild(el) @@ -160,10 +163,10 @@ const downloadBlob = (b: Blob, name: string) => { el.remove() } -// One-shot recursive tree-like listing for the folder. -// Due to react hook limit, we cannot reuse SWR utils for recursive listing. +// One-shot DFS file traversing for the folder. +// Due to react hook limit, we cannot reuse SWR utils for recursive actions. // Only root dir meta, without returning from API, will be undefined. -export async function* treeList(path: string) { +export async function* traverseFolder(path: string) { const hashedToken = getStoredToken(path) const root = new PathNode(path) const loader = async (path: string) => { @@ -214,12 +217,14 @@ class PathNode { * @param folder Optional folder name to hold files, otherwise flatten files in the zip. * Root folder name passed in files param is not unused, on the contrary use this param as top-level folder name. */ -export const saveTreeFiles = async ( +export const downloadTreelikeMultipleFiles = async ( files: AsyncGenerator<{ name: string, url?: string, path: string, isFolder: boolean }>, folder?: string, ) => { const zip = new JSZip() const root = folder ? zip.folder(folder)! : zip const map = [{ path: '/', dir: root }] // Root path will be set later in looping + + // Add selected file blobs to zip according to its path for await (const { name, url, path, isFolder } of files) { if (name === undefined) { map[0].path = path @@ -236,6 +241,8 @@ export const saveTreeFiles = async ( dir.file(name, fetch(url!).then(r => r.blob())) } } + + // Create zip file and download it const b = await zip.generateAsync({ type: 'blob' }) downloadBlob(b, folder ? folder + '.zip' : 'download.zip') }