|
|
|
@ -3,6 +3,8 @@ import WebTorrent from 'webtorrent'
|
|
|
|
import alt from '../alt'
|
|
|
|
import alt from '../alt'
|
|
|
|
import socket from 'filepizza-socket'
|
|
|
|
import socket from 'filepizza-socket'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const SPEED_REFRESH_TIME = 2000
|
|
|
|
|
|
|
|
|
|
|
|
function downloadBlobURL(name, blobURL) {
|
|
|
|
function downloadBlobURL(name, blobURL) {
|
|
|
|
let a = document.createElement('a')
|
|
|
|
let a = document.createElement('a')
|
|
|
|
document.body.appendChild(a)
|
|
|
|
document.body.appendChild(a)
|
|
|
|
@ -19,10 +21,13 @@ export default alt.createStore(class DownloadStore {
|
|
|
|
this.fileName = ''
|
|
|
|
this.fileName = ''
|
|
|
|
this.fileSize = 0
|
|
|
|
this.fileSize = 0
|
|
|
|
this.fileType = ''
|
|
|
|
this.fileType = ''
|
|
|
|
|
|
|
|
this.infoHash = null
|
|
|
|
|
|
|
|
this.peers = 0
|
|
|
|
this.progress = 0
|
|
|
|
this.progress = 0
|
|
|
|
|
|
|
|
this.speedDown = 0
|
|
|
|
|
|
|
|
this.speedUp = 0
|
|
|
|
this.status = 'uninitialized'
|
|
|
|
this.status = 'uninitialized'
|
|
|
|
this.token = null
|
|
|
|
this.token = null
|
|
|
|
this.infoHash = null
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
onRequestDownload() {
|
|
|
|
onRequestDownload() {
|
|
|
|
@ -33,22 +38,31 @@ export default alt.createStore(class DownloadStore {
|
|
|
|
client.download(this.infoHash, (torrent) => {
|
|
|
|
client.download(this.infoHash, (torrent) => {
|
|
|
|
this.setState({ status: 'downloading' })
|
|
|
|
this.setState({ status: 'downloading' })
|
|
|
|
|
|
|
|
|
|
|
|
let downloaded = 0
|
|
|
|
const updateSpeed = () => {
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
|
|
|
|
speedUp: torrent.swarm.uploadSpeed(),
|
|
|
|
|
|
|
|
speedDown: torrent.swarm.downloadSpeed(),
|
|
|
|
|
|
|
|
peers: torrent.swarm.wires.length
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
torrent.on('upload', updateSpeed)
|
|
|
|
|
|
|
|
torrent.on('download', updateSpeed)
|
|
|
|
|
|
|
|
setInterval(updateSpeed, SPEED_REFRESH_TIME)
|
|
|
|
|
|
|
|
|
|
|
|
const file = torrent.files[0]
|
|
|
|
const file = torrent.files[0]
|
|
|
|
const stream = file.createReadStream()
|
|
|
|
const stream = file.createReadStream()
|
|
|
|
stream.on('data', (chunk) => {
|
|
|
|
stream.on('data', (chunk) => {
|
|
|
|
if (this.status !== 'downloading') return
|
|
|
|
if (this.status !== 'downloading') return
|
|
|
|
|
|
|
|
|
|
|
|
downloaded += chunk.length
|
|
|
|
if (torrent.progress === 1) {
|
|
|
|
|
|
|
|
|
|
|
|
if (downloaded === file.length) {
|
|
|
|
|
|
|
|
this.setState({ status: 'done', progress: 1 })
|
|
|
|
this.setState({ status: 'done', progress: 1 })
|
|
|
|
file.getBlobURL((err, blobURL) => {
|
|
|
|
file.getBlobURL((err, blobURL) => {
|
|
|
|
if (err) throw err
|
|
|
|
if (err) throw err
|
|
|
|
downloadBlobURL(this.fileName, blobURL)
|
|
|
|
downloadBlobURL(this.fileName, blobURL)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
this.setState({ progress: downloaded / file.length })
|
|
|
|
this.setState({ progress: torrent.progress })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|