-
Notifications
You must be signed in to change notification settings - Fork 3
/
UploadFile.js
209 lines (180 loc) · 5.81 KB
/
UploadFile.js
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import React from 'react'
import PropTypes from 'prop-types'
import fileReader from 'filereader-stream'
import pretty from 'pretty-bytes'
import speedometer from 'speedometer'
import through from 'through2'
import pump from 'pump'
import { prom } from '@archipel/common/util/async'
import { Button, Heading, List } from '@archipel/ui'
import { withApi } from '../../lib/api.js'
function updateAt (arr, i, put) {
return arr.map((item, j) => j === i ? Object.assign({}, item, put) : item)
}
const FileListItem = (file) => {
let { name, size, pending, done, written, speed } = file
let status = 'Waiting'
if (pending) status = 'Uploading'
if (done) status = 'Done'
let percent = written ? Math.round((written / size) * 100) : 0
size = pretty(size)
written = pretty(written)
speed = pretty(speed)
return (
<div className='flex'>
<div className='w-1/2 flex-none truncate'><strong>{name}</strong><br />{size}</div>
<div className='w-1/4 flex-none text-sm'>
<em>{status}</em><br />
<span className='text-blue'>{percent}%</span>
</div>
<div className='w-1/4 flex-none text-sm'>
<span className='text-blue'>{speed}/s</span><br />
<span className='text-green'>{written}</span>
</div>
</div>
)
}
class UploadFile extends React.Component {
constructor (props) {
super()
this.uploadRef = React.createRef()
this.state = {
pending: false,
result: null,
status: [],
files: [],
uploadDir: false
}
this.onUpload = this.onUpload.bind(this)
this.onChange = this.onChange.bind(this)
this.setUploadDir = this.setUploadDir.bind(this)
}
async setUploadDir (e) {
this.setState({ uploadDir: e.target.checked })
}
async onChange (e) {
const fileList = e.target.files
const files = []
let name
for (let i = 0; i < fileList.length; i++) {
if (this.state.uploadDir) {
name = fileList[i].webkitRelativePath
} else {
name = fileList[i].name
}
files.push({
idx: i,
name: name,
size: fileList[i].size,
pending: false,
done: false,
written: 0,
speed: 0
})
}
this.setState({ files })
}
async onUpload () {
if (this.uploadRef.current.files.length) {
this.setState({ pending: true })
const fileList = this.uploadRef.current.files
const files = []
for (let i = 0; i < fileList.length; i++) {
files.push(fileList[i])
}
// const uploads = files.map((file, i) => this.uploadFile(file, i))
// console.log('uploads', uploads)
// Promise.all(uploads)
// .then(() => this.setState({ pending: false }))
// .catch(e => this.setState({ error: e }))
this.seqRunUploads(files).then(
this.setState({ pending: false, done: true })
)
}
}
async seqRunUploads (files) {
for (let i = 0; i < files.length; i++) {
try {
await this.uploadFile(files[i], i)
} catch (e) {
this.setState({ error: e })
}
}
}
async uploadFile (file, i) {
const [promise, done] = prom()
const updateState = newState => this.setState(state => (
{ files: updateAt(state.files, i, newState) }
))
updateState({ pending: true })
let { path, api, archive } = this.props
const key = archive
let { name, webkitRelativePath } = file
if (this.state.uploadDir) name = webkitRelativePath
path = (path === '/' ? '' : path) + '/' + name
const speedo = speedometer()
let speed = 0
let written = 0
// Set up a passthrough stream to track stats.
const passthrough = through(function (chunk, enc, next) {
written += chunk.length
speed = speedo(chunk.length)
this.push(chunk)
next()
})
// The actual streaming file reader.
const reader = fileReader(file)
// The write stream to the backend.
const ws = await api.hyperdrive.createWriteStream(key, path)
// Pump it all toghether.
pump(reader, passthrough, ws)
// Update the state every 200ms.
const update = () => updateState({ written, speed })
const debounce = setInterval(update, 200)
// Cleanup when done.
ws.on('finish', () => {
clearInterval(debounce)
updateState({ pending: false, done: true, written, speed })
done()
})
return promise
// todo: is this clean enough?
// core.getStore('fs').fetchStats({ archive: key, path: this.props.path})
}
render () {
const { path, archive } = this.props
return (
<>
<Heading className='text-pink'>Upload file</Heading>
{path !== '/' && <div className='mb-2'>Parent folder: <strong>{path}</strong></div>}
<div className='p-4 bg-grey-lighter '>
<div className='flex mb-4'>
<input type='checkbox' name='uploadDir' onChange={this.setUploadDir} />
<label htmlFor='uploadDir'>Upload Directory</label>
</div>
{ this.state.uploadDir ?
<div className='flex mb-2'>
<input type='file' webkitdirectory='foo' multiple
onChange={this.onChange} ref={this.uploadRef} />
</div> :
<div className='flex mb-2'>
<input type='file' multiple
onChange={this.onChange} ref={this.uploadRef} />
</div> }
<Button onClick={this.onUpload}>Upload</Button>
</div>
<div className='pt-1'>
{ this.state.pending && <em>Status: Uploading...</em> }
{ this.state.done && <em>Status: Done.</em> }
{ this.state.res && <strong>Status: { this.state.res }</strong>}
<List items={this.state.files} renderItem={(file) => FileListItem(file)} />
</div>
</>
)
}
}
UploadFile.propTypes = {
archive: PropTypes.string,
path: PropTypes.string
}
export default withApi(UploadFile)