-
Notifications
You must be signed in to change notification settings - Fork 0
/
Download.jsx
75 lines (68 loc) · 2.32 KB
/
Download.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import React, { useState, useEffect, useContext } from 'react'
import { Button } from 'antd'
import JSZip from 'jszip'
import GitRepo from "./IGiS/lib/git/GitRepo";
import GitTree from "./IGiS/lib/git/GitTree";
import GitBlob from "./IGiS/lib/git/GitBlob";
import FileSaver from "file-saver";
import IPFSContext from './IPFSContext'
export default (props) => {
const [ipfs] = useContext(IPFSContext)
const { cid, onLoad } = props
const [processing, setProcessing] = useState(false)
const [current, setCurrent] = useState()
const [repo, setRepo] = useState()
useEffect(() => {
console.log('IPFS', ipfs)
if(ipfs) {
GitRepo.fetch(cid, ipfs).then(setRepo)
}
}, [cid, ipfs])
const populateTree = (zipDir, path, name, ipfs) => {
setCurrent((path || '').split('/').slice(1).join('/'))
console.debug('%crepo', 'color: orange', repo)
return repo.getObject(path, ipfs)
.then(data => {
console.debug('%cRepo Got', 'color: red', name)
if(data instanceof GitBlob) {
console.debug('storing', name)
zipDir.file(name, data.content, {base64: true})
return Promise.resolve()
.then(a => console.log('%cDone', 'color: yellow', name))
} else if(data instanceof GitTree) {
let promises = data.files.map(d => {
let child = zipDir
if(name !== '') {
child = zipDir.folder(name)
}
console.debug('%crecurse', 'color: red', path + '/' + d.name)
return populateTree(child, path + '/' + d.name + '/hash', d.name, ipfs)
.then(a => console.log('%cDone', 'color: green', d.name))
})
console.log('%cprep', 'color: red', path, name)
return Promise.allSettled(promises)
.then(a => console.log('Done', name))
}
})
}
const handleClick = () => {
setProcessing(true)
setCurrent('preparing')
let zip = new JSZip()
populateTree(zip, cid + '/refs/heads/master/tree', '', ipfs)
.then(() => {
setCurrent('finalizing')
zip.generateAsync({type: 'blob'})
.then(content => {
onLoad(URL.createObjectURL(content))
setProcessing(false)
//FileSaver.saveAs(content, 'repository.zip')
})
})
}
return (
<Button onClick={handleClick} disabled={!repo}>
{processing ? current : 'Download Zip'}
</Button>
)
}