mirror of
https://github.com/Nezumi-2711/onedrive-vercel-index.git
synced 2026-09-22 13:38:45 +00:00
update function names and comments according to upstream
This commit is contained in:
@@ -12,7 +12,9 @@ import dynamic from 'next/dynamic'
|
|||||||
|
|
||||||
import { getExtension, getFileIcon, hasKey } from '../utils/getFileIcon'
|
import { getExtension, getFileIcon, hasKey } from '../utils/getFileIcon'
|
||||||
import { extensions, preview } from '../utils/getPreviewType'
|
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 { VideoPreview } from './previews/VideoPreview'
|
||||||
import { AudioPreview } from './previews/AudioPreview'
|
import { AudioPreview } from './previews/AudioPreview'
|
||||||
@@ -305,7 +307,7 @@ const FileListing: FunctionComponent<{ query?: ParsedUrlQuery }> = ({ query }) =
|
|||||||
// Folder recursive download
|
// Folder recursive download
|
||||||
const handleFolderDownload = (path: string, id: string, name?: string) => () => {
|
const handleFolderDownload = (path: string, id: string, name?: string) => () => {
|
||||||
const files = (async function* () {
|
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 {
|
yield {
|
||||||
name: c?.name,
|
name: c?.name,
|
||||||
url: c ? c['@microsoft.graph.downloadUrl'] : undefined,
|
url: c ? c['@microsoft.graph.downloadUrl'] : undefined,
|
||||||
@@ -315,8 +317,8 @@ const FileListing: FunctionComponent<{ query?: ParsedUrlQuery }> = ({ query }) =
|
|||||||
}
|
}
|
||||||
})()
|
})()
|
||||||
setFolderGenerating({ ...folderGenerating, [id]: true })
|
setFolderGenerating({ ...folderGenerating, [id]: true })
|
||||||
const toastId = toast.loading('Downloading folder. This may be slow...')
|
const toastId = toast.loading('Downloading folder. Refresh to cancel, this may take some time...')
|
||||||
saveTreeFiles(files, name).then(() => {
|
downloadTreelikeMultipleFiles(files, name).then(() => {
|
||||||
setFolderGenerating({ ...folderGenerating, [id]: false })
|
setFolderGenerating({ ...folderGenerating, [id]: false })
|
||||||
toast.dismiss(toastId)
|
toast.dismiss(toastId)
|
||||||
toast.success('Finished to download folder.')
|
toast.success('Finished to download folder.')
|
||||||
|
|||||||
+11
-4
@@ -141,12 +141,15 @@ export const downloadMultipleFiles = async (files: { name: string; url: string }
|
|||||||
fetch(url).then(r => r.blob())
|
fetch(url).then(r => r.blob())
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Create zip file and download it
|
||||||
const b = await zip.generateAsync({ type: 'blob' })
|
const b = await zip.generateAsync({ type: 'blob' })
|
||||||
downloadBlob(b, folder ? folder + '.zip' : 'download.zip')
|
downloadBlob(b, folder ? folder + '.zip' : 'download.zip')
|
||||||
}
|
}
|
||||||
|
|
||||||
// Blob download helper
|
// Blob download helper
|
||||||
const downloadBlob = (b: Blob, name: string) => {
|
const downloadBlob = (b: Blob, name: string) => {
|
||||||
|
// Prepare for download
|
||||||
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)
|
||||||
@@ -160,10 +163,10 @@ const downloadBlob = (b: Blob, name: string) => {
|
|||||||
el.remove()
|
el.remove()
|
||||||
}
|
}
|
||||||
|
|
||||||
// One-shot recursive tree-like listing for the folder.
|
// One-shot DFS file traversing for the folder.
|
||||||
// Due to react hook limit, we cannot reuse SWR utils for recursive listing.
|
// Due to react hook limit, we cannot reuse SWR utils for recursive actions.
|
||||||
// Only root dir meta, without returning from API, will be undefined.
|
// 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 hashedToken = getStoredToken(path)
|
||||||
const root = new PathNode(path)
|
const root = new PathNode(path)
|
||||||
const loader = async (path: string) => {
|
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.
|
* @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.
|
* 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,
|
files: AsyncGenerator<{ name: string, url?: string, path: string, isFolder: boolean }>, folder?: string,
|
||||||
) => {
|
) => {
|
||||||
const zip = new JSZip()
|
const zip = new JSZip()
|
||||||
const root = folder ? zip.folder(folder)! : zip
|
const root = folder ? zip.folder(folder)! : zip
|
||||||
const map = [{ path: '/', dir: root }] // Root path will be set later in looping
|
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) {
|
for await (const { name, url, path, isFolder } of files) {
|
||||||
if (name === undefined) {
|
if (name === undefined) {
|
||||||
map[0].path = path
|
map[0].path = path
|
||||||
@@ -236,6 +241,8 @@ export const saveTreeFiles = async (
|
|||||||
dir.file(name, fetch(url!).then(r => r.blob()))
|
dir.file(name, fetch(url!).then(r => r.blob()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create zip file and download it
|
||||||
const b = await zip.generateAsync({ type: 'blob' })
|
const b = await zip.generateAsync({ type: 'blob' })
|
||||||
downloadBlob(b, folder ? folder + '.zip' : 'download.zip')
|
downloadBlob(b, folder ? folder + '.zip' : 'download.zip')
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user