-
Notifications
You must be signed in to change notification settings - Fork 1
/
terminal.jsx
268 lines (249 loc) · 6.88 KB
/
terminal.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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import AttachAddon from './addon-zmodem'
export class Term extends Component {
componentDidMount () {
this.zmodemAddon = new AddonZmodem()
term.loadAddon(this.zmodemAddon)
}
onzmodemRetract = () => {
log.debug('zmodemRetract')
}
writeBanner = (type) => {
this.term.write(`\x1b[32mZMODEM::${type}::START\x1b[0m\r\n\r\n`)
}
onReceiveZmodemSession = async () => {
const savePath = await this.openSaveFolderSelect()
this.zsession.on('offer', this.onOfferReceive)
this.zsession.start()
this.term.write('\r\n\x1b[2A\r\n')
if (!savePath) {
return this.onZmodemEnd()
}
this.writeBanner('RECEIVE')
this.zmodemSavePath = savePath
return new Promise((resolve) => {
this.zsession.on('session_end', resolve)
})
.then(this.onZmodemEnd)
.catch(this.onZmodemCatch)
}
initZmodemDownload = async (name, size) => {
if (!this.zmodemSavePath) {
return
}
let pth = window.pre.resolve(
this.zmodemSavePath, name
)
const exist = await fs.exists(pth).catch(() => false)
if (exist) {
pth = pth + '.' + generate()
}
const fd = await fs.open(pth, 'w').catch(this.onZmodemEnd)
this.downloadFd = fd
this.downloadPath = pth
this.downloadCount = 0
this.zmodemStartTime = Date.now()
this.downloadSize = size
this.updateZmodemProgress(
0, pth, size, transferTypeMap.download
)
return fd
}
onOfferReceive = async (xfer) => {
const {
name,
size
} = xfer.get_details()
if (!this.downloadFd) {
await this.initZmodemDownload(name, size)
}
xfer.on('input', this.onZmodemDownload)
this.xfer = xfer
await xfer.accept()
.then(this.finishZmodemTransfer)
.catch(this.onZmodemEnd)
}
onZmodemDownload = async payload => {
console.log('onZmodemDownload', payload)
if (this.onCanceling || !this.downloadFd) {
return
}
this.downloadCount += payload.length
await fs.write(this.downloadFd, new Uint8Array(payload))
this.updateZmodemProgress(
this.downloadCount,
this.downloadPath,
this.downloadSize,
transferTypeMap.download
)
}
updateZmodemProgress = throttle((start, name, size, type) => {
this.zmodemTransfer = {
type,
start,
name,
size
}
this.writeZmodemProgress()
}, 500)
finishZmodemTransfer = () => {
this.zmodemTransfer = {
...this.zmodemTransfer,
start: this.zmodemTransfer.size
}
this.writeZmodemProgress()
}
writeZmodemProgress = () => {
if (this.onCanceling) {
return
}
const {
size, start, name
} = this.zmodemTransfer
const speed = size > 0 ? formatBytes(start * 1000 / 1024 / (Date.now() - this.zmodemStartTime)) : 0
const percent = size > 0 ? Math.floor(start * 100 / size) : 100
const str = `\x1b[32m${name}\x1b[0m::${percent}%,${start}/${size},${speed}/s`
this.term.write('\r\n\x1b[2A' + str + '\n')
}
zmodemTransferFile = async (file, filesRemaining, sizeRemaining) => {
const offer = {
obj: file,
name: file.name,
size: file.size,
files_remaining: filesRemaining,
bytes_remaining: sizeRemaining
}
const xfer = await this.zsession.send_offer(offer)
if (!xfer) {
this.onZmodemEnd()
return window.store.onError(new Error('Transfer cancelled, maybe file already exists'))
}
this.zmodemStartTime = Date.now()
const fd = await fs.open(file.filePath, 'r')
let start = 0
const { size } = file
let inited = false
while (start < size || !inited) {
const rest = size - start
const len = rest > zmodemTransferPackSize ? zmodemTransferPackSize : rest
const buffer = new Uint8Array(len)
const newArr = await fs.read(fd, buffer, 0, len, null)
const n = newArr.length
await xfer.send(newArr)
start = start + n
inited = true
this.updateZmodemProgress(start, file.name, size, transferTypeMap.upload)
if (n < zmodemTransferPackSize || start >= file.size || this.onCanceling) {
break
}
}
await fs.close(fd)
this.finishZmodemTransfer()
await xfer.end()
}
openFileSelect = async () => {
const properties = [
'openFile',
'multiSelections',
'showHiddenFiles',
'noResolveAliases',
'treatPackageAsDirectory',
'dontAddToRecent'
]
const files = await window.api.openDialog({
title: 'Choose some files to send',
message: 'Choose some files to send',
properties
}).catch(() => false)
if (!files || !files.length) {
return this.onZmodemEnd()
}
const r = []
for (const filePath of files) {
const stat = await getLocalFileInfo(filePath)
r.push({ ...stat, filePath })
}
return r
}
openSaveFolderSelect = async () => {
const savePaths = await window.api.openDialog({
title: 'Choose a folder to save file(s)',
message: 'Choose a folder to save file(s)',
properties: [
'openDirectory',
'showHiddenFiles',
'createDirectory',
'noResolveAliases',
'treatPackageAsDirectory',
'dontAddToRecent'
]
}).catch(() => false)
if (!savePaths || !savePaths.length) {
return false
}
return savePaths[0]
}
beforeZmodemUpload = async (files) => {
if (!files || !files.length) {
return false
}
this.writeBanner('SEND')
let filesRemaining = files.length
let sizeRemaining = files.reduce((a, b) => a + b.size, 0)
for (const f of files) {
await this.zmodemTransferFile(f, filesRemaining, sizeRemaining)
filesRemaining = filesRemaining - 1
sizeRemaining = sizeRemaining - f.size
}
this.onZmodemEnd()
}
onSendZmodemSession = async () => {
this.term.write('\r\n\x1b[2A\n')
const files = await this.openFileSelect()
this.beforeZmodemUpload(files)
}
onZmodemEnd = async () => {
delete this.zmodemSavePath
this.onCanceling = true
if (this.downloadFd) {
await fs.close(this.downloadFd)
}
if (this.xfer && this.xfer.end) {
await this.xfer.end().catch(
console.error
)
}
delete this.xfer
if (this.zsession && this.zsession.close) {
await this.zsession.close().catch(
console.error
)
}
delete this.zsession
this.term.focus()
this.term.write('\r\n')
this.onZmodem = false
delete this.downloadFd
delete this.downloadPath
delete this.downloadCount
delete this.downloadSize
delete this.DownloadCache
}
onZmodemCatch = (e) => {
this.onZmodemEnd()
}
onZmodemDetect = detection => {
this.onCanceling = false
this.term.blur()
this.onZmodem = true
const zsession = detection.confirm()
this.zsession = zsession
if (zsession.type === 'receive') {
this.onReceiveZmodemSession()
} else {
this.onSendZmodemSession()
}
}
render () {
return null
}
}