-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.tsx
320 lines (293 loc) · 9.96 KB
/
index.tsx
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/* eslint-disable one-var */
/* eslint-disable prefer-const */
import * as React from 'react'
import { Fragment, useEffect, useState, useRef } from 'react'
// eslint-disable-next-line no-unused-vars
import type WebTorrent from 'webtorrent'
import { GetTorrent, PromiseTorrent } from './getTorrent'
import {
DownloaderOuterProps,
ImageTorrent,
TorrentUpdates as tUp,
VideoTorProps,
VideoTorPropsWrap
} from './interfaces'
import ImageTest from './demo/ImageDownloadTime'
import VideoTest from './demo/VideoDownloadTime'
import { Downloader } from './downloader'
export type TorrentUpdates = tUp
export const ImageDemo = ImageTest
export const VideoDemo = VideoTest
export const getTorrent = GetTorrent
export const promiseTorrent = PromiseTorrent
import { ATorStreamVideo_2 } from './stream'
export const ATorStreamVideo = ATorStreamVideo_2
export const ImgATor = (props: ImageTorrent) => {
let { magnetURI, height, width, style, sizes, srcset, loading, showPrgrs } =
props
let [fileState, updateFile] = useState<WebTorrent.TorrentFile>(),
[urlState, updateUrl] = useState<string>(),
[downloadTime, updateTime] = useState<number>(0),
[peers, setPeers] = useState(0),
manageFile = (torrent: WebTorrent.Torrent) => {
// TODO: this better with more file types and upper and lower cases
let file = torrent.files[0],
timeStart = Date.now()
if (file) {
updateFile(file)
file.getBlobURL((err, url) => {
if (err) throw err
updateTime(Date.now() - timeStart)
setPeers(torrent.numPeers)
updateUrl(url)
})
}
}
useEffect(() => {
PromiseTorrent(magnetURI).then(manageFile)
return () => {}
}, [])
return (
<Fragment>
{urlState ? null : loading}
<img
src={urlState}
alt={fileState?.name}
width={width}
height={height}
sizes={sizes}
srcSet={srcset}
style={style}
/>
{!showPrgrs ? null : (
<p>
Time ms: {downloadTime} Peers: {peers}
</p>
)}
</Fragment>
)
}
export const VidATorWrapped = (props: VideoTorPropsWrap) => {
let {
ShowPrgrs,
loading,
width,
height,
type,
torrent,
dwnldSpeed,
downloaded,
progress,
peers
} = props,
[urlState, updateUrl] = useState<string>()
const videoElement = useRef(null)
useEffect(() => {
if (torrent) {
let file = torrent.files[0]
if (file) {
file.getBlobURL((err, url) => {
if (err) throw err
updateUrl(url)
})
}
}
return () => {}
}, [torrent])
return (
<div>
{urlState ? null : loading}
<video
width={width}
height={height}
controls
// autoPlay
muted
ref={videoElement}
src={urlState}
>
<source type={type} />
Your browser does not support the video tag.
</video>
{ShowPrgrs && (
<ShowPrgrs
dwnldSpeed={dwnldSpeed || 0}
downloaded={downloaded || 0}
progress={progress || 0}
peers={peers || 0}
other={{ urlState }}
/>
)}
</div>
)
}
export const VidStrmATor = (props: VideoTorProps) => {
let { magnetURI, height, width, ShowPrgrs, type, autoplay } = props,
[dwnldSpeed, updateDwnldSpeed] = useState<number>(0),
[progress, updateProgress] = useState<number>(0),
[peers, setPeers] = useState(0)
const videoElement = useRef(null)
const opts = {
autoplay: autoplay,
muted: true
}
let mngTor = (torrent: WebTorrent.Torrent) => {
torrent.on('download', () => {
let mbps = torrent.downloadSpeed * 1e-6
updateDwnldSpeed(mbps)
updateProgress(torrent.progress)
setPeers(torrent.numPeers)
})
torrent.files.forEach((file) => {
if (file.name.includes('.mp4')) {
// @ts-ignore: Object is possibly 'null'. // TODO: fix this
file.renderTo(videoElement.current, opts, function (err, elem) {
if (err) throw err // file failed to download or display in the DOM
console.log('New DOM node with the content', elem)
})
}
})
}
useEffect(() => {
PromiseTorrent(magnetURI).then(mngTor)
return () => {}
}, [])
return (
<div>
<video
width={width}
height={height}
controls
muted
// autoPlay
ref={videoElement}
>
<source type={type || 'video/mp4'} />
Your browser does not support the video tag.
</video>
{ShowPrgrs && (
<ShowPrgrs
dwnldSpeed={dwnldSpeed}
progress={progress}
peers={peers}
other={{}}
/>
)}
</div>
)
}
// TODO: torrent.on('wire', function (wire) {}) // for when peers connect
export const ATorWrap = (props: { children: any; magnetURI: string }) => {
const { children, magnetURI } = props
let [childElements, updateChildElements] = useState<any>(),
mngTor = (torrent: WebTorrent.Torrent) => {
const chldElements = React.Children.map(children, (child) => {
let { downloadSpeed, progress, numPeers, done, downloaded } =
torrent,
mbps = downloadSpeed * 1e-6
downloaded = downloaded * 1e-6
return React.cloneElement(child, {
torrent: torrent,
dwnldSpeed: mbps,
downloaded,
progress,
peers: numPeers,
done: done
})
})
updateChildElements(chldElements)
torrent.on('download', () => {
let { downloadSpeed, progress, numPeers, done, downloaded } =
torrent,
mbps = downloadSpeed * 1e-6
downloaded = downloaded * 1e-6
const chldElements = React.Children.map(children, (child) =>
React.cloneElement(child, {
torrent: torrent,
dwnldSpeed: mbps,
downloaded,
progress,
peers: numPeers,
done: done
})
)
updateChildElements(chldElements)
})
torrent.on('done', () => {
const chldElements = React.Children.map(children, (child) => {
let {
downloadSpeed,
progress,
numPeers,
done,
downloaded
} = torrent,
mbps = downloadSpeed * 1e-6
downloaded = downloaded * 1e-6
return React.cloneElement(child, {
torrent,
dwnldSpeed: mbps,
downloaded,
progress,
peers: numPeers,
done: done
})
})
updateChildElements(chldElements)
})
torrent.on('wire', function () {
const chldElements = React.Children.map(children, (child) => {
let {
downloadSpeed,
progress,
numPeers,
done,
downloaded
} = torrent,
mbps = downloadSpeed * 1e-6
downloaded = downloaded * 1e-6
return React.cloneElement(child, {
torrent,
dwnldSpeed: mbps,
downloaded,
progress,
peers: numPeers,
done: done
})
})
updateChildElements(chldElements)
})
}
useEffect(() => {
if (magnetURI) PromiseTorrent(magnetURI).then(mngTor)
return () => {}
}, [magnetURI])
return <Fragment>{childElements}</Fragment>
}
export const VidATor = (props: VideoTorProps) => {
let { ShowPrgrs, magnetURI, loading, width, height, type } = props
return (
<ATorWrap magnetURI={magnetURI}>
<VidATorWrapped
loading={loading}
width={width}
height={height}
type={type}
ShowPrgrs={ShowPrgrs}
/>
</ATorWrap>
)
}
export const ATorDownloader = (props: DownloaderOuterProps) => {
let { magnetURI, startDownload, ShowPrgrs } = props,
magLink = startDownload ? magnetURI : ''
return (
<ATorWrap magnetURI={magLink}>
<Downloader ShowPrgrs={ShowPrgrs} />
</ATorWrap>
)
}
export const ATorVid = VidATor
export const ATorVidStrm = VidStrmATor
export const ATorImg = ImgATor
// https://stackoverflow.com/questions/51657890/is-it-ok-to-use-a-wrapper-component-to-pass-props-in-react