Add download zip file after click download
This commit is contained in:
parent
adefe31090
commit
5cf54b76ab
3 changed files with 189 additions and 7 deletions
|
|
@ -5,6 +5,7 @@ import { ReloadIcon } from '@radix-ui/react-icons'
|
|||
import { LinuxFileType, type Adb } from '@yume-chan/adb'
|
||||
import { WritableStream } from '@yume-chan/stream-extra'
|
||||
import { useCallback, useState } from 'react'
|
||||
import JSZip from 'jszip'
|
||||
|
||||
interface FileManagerTabProps {
|
||||
adb: Adb | undefined
|
||||
|
|
@ -22,12 +23,48 @@ export const FileManagerTab: React.FC<FileManagerTabProps> = ({ adb }) => {
|
|||
|
||||
const [files, setFiles] = useState<filesType>()
|
||||
|
||||
const zipFiles = (files: filesType) => {
|
||||
const zip = new JSZip()
|
||||
|
||||
const addFiles = (folder: filesType, parent: JSZip | null) => {
|
||||
folder.files?.forEach(file => {
|
||||
if (file.files) {
|
||||
let newFolder
|
||||
if (parent) {
|
||||
newFolder = parent.folder(file.file)
|
||||
} else {
|
||||
newFolder = zip.folder(file.file)
|
||||
}
|
||||
addFiles(file, newFolder)
|
||||
} else {
|
||||
if (!file.blob) {
|
||||
return
|
||||
}
|
||||
if (parent) {
|
||||
parent.file(file.file, file.blob)
|
||||
} else {
|
||||
zip.file(file.file, file.blob)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
addFiles(files, zip.folder(files.file))
|
||||
zip.generateAsync({ type: 'blob' }).then(content => {
|
||||
const url = URL.createObjectURL(content)
|
||||
const a = document.createElement('a')
|
||||
a.href = url
|
||||
a.download = files.file + '.zip'
|
||||
a.click()
|
||||
})
|
||||
}
|
||||
|
||||
const download = useCallback(() => {
|
||||
const readFiles = async (adb: Adb) => {
|
||||
const sync = await adb.sync()
|
||||
try {
|
||||
const folder: filesType = {
|
||||
file: 'test-pull-taobin_project',
|
||||
file: path.split('/').pop() || 'scannedFiles',
|
||||
indent: 0,
|
||||
files: []
|
||||
}
|
||||
|
|
@ -82,6 +119,9 @@ export const FileManagerTab: React.FC<FileManagerTabProps> = ({ adb }) => {
|
|||
console.log('Scanned files')
|
||||
setFiles(folder)
|
||||
console.log(folder)
|
||||
|
||||
// Zip files
|
||||
zipFiles(folder)
|
||||
} finally {
|
||||
await sync.dispose()
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue