mirror of https://github.com/kern/filepizza
Migrate to WebTorrent.
parent
9a6b1b8f8e
commit
75e646c4b9
@ -1,25 +0,0 @@
|
|||||||
const rankSize = 16
|
|
||||||
|
|
||||||
function blobLength(b) {
|
|
||||||
if (typeof b.byteLength !== 'undefined') return b.byteLength
|
|
||||||
if (typeof b.size !== 'undefined') return b.size
|
|
||||||
return b.length
|
|
||||||
}
|
|
||||||
|
|
||||||
export default class ChunkedBlob {
|
|
||||||
|
|
||||||
constructor() {
|
|
||||||
this.size = 0
|
|
||||||
this.ranks = []
|
|
||||||
}
|
|
||||||
|
|
||||||
add(b) {
|
|
||||||
this.size += blobLength(b)
|
|
||||||
this.ranks.push(b)
|
|
||||||
}
|
|
||||||
|
|
||||||
toBlob() {
|
|
||||||
return new Blob(this.ranks)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -1,54 +0,0 @@
|
|||||||
import ChunkedBlob from './ChunkedBlob'
|
|
||||||
|
|
||||||
export default class DownloadFile {
|
|
||||||
|
|
||||||
constructor(name, size, type) {
|
|
||||||
this.name = name
|
|
||||||
this.size = size
|
|
||||||
this.type = type
|
|
||||||
this.chunks = new ChunkedBlob()
|
|
||||||
}
|
|
||||||
|
|
||||||
addChunk(b) {
|
|
||||||
this.chunks.add(b)
|
|
||||||
}
|
|
||||||
|
|
||||||
clearChunks() {
|
|
||||||
this.chunks = new ChunkedBlob()
|
|
||||||
}
|
|
||||||
|
|
||||||
isComplete() {
|
|
||||||
return this.getProgress() === 1
|
|
||||||
}
|
|
||||||
|
|
||||||
getProgress() {
|
|
||||||
return this.chunks.size / this.size
|
|
||||||
}
|
|
||||||
|
|
||||||
download() {
|
|
||||||
|
|
||||||
let blob = this.chunks.toBlob()
|
|
||||||
let url = URL.createObjectURL(blob)
|
|
||||||
|
|
||||||
let a = document.createElement('a')
|
|
||||||
document.body.appendChild(a)
|
|
||||||
a.download = this.name
|
|
||||||
a.href = url
|
|
||||||
a.click()
|
|
||||||
|
|
||||||
setTimeout(() => {
|
|
||||||
URL.revokeObjectURL(url)
|
|
||||||
document.body.removeChild(a)
|
|
||||||
}, 0)
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
toJSON() {
|
|
||||||
return {
|
|
||||||
name: this.name,
|
|
||||||
size: this.size,
|
|
||||||
type: this.type
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -1,33 +0,0 @@
|
|||||||
const chunkSize = 256 * 1024
|
|
||||||
|
|
||||||
export default class UploadFile {
|
|
||||||
|
|
||||||
constructor(file) {
|
|
||||||
this.name = file.name
|
|
||||||
this.size = file.size
|
|
||||||
this.type = file.type
|
|
||||||
this.blob = file
|
|
||||||
}
|
|
||||||
|
|
||||||
countChunks() {
|
|
||||||
return Math.ceil(this.size / chunkSize)
|
|
||||||
}
|
|
||||||
|
|
||||||
getChunk(i) {
|
|
||||||
if (i < 0 || i >= this.countChunks())
|
|
||||||
throw new Error('Chunk out of bounds')
|
|
||||||
|
|
||||||
let start = i * chunkSize
|
|
||||||
let end = Math.min(start + chunkSize, this.size)
|
|
||||||
return this.blob.slice(start, end)
|
|
||||||
}
|
|
||||||
|
|
||||||
toJSON() {
|
|
||||||
return {
|
|
||||||
name: this.name,
|
|
||||||
size: this.size,
|
|
||||||
type: this.type
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -1,71 +1,43 @@
|
|||||||
import UploadActions from '../actions/UploadActions'
|
import UploadActions from '../actions/UploadActions'
|
||||||
import UploadFile from '../UploadFile'
|
|
||||||
import alt from '../alt'
|
import alt from '../alt'
|
||||||
import peer from 'filepizza-peerjs'
|
|
||||||
import socket from 'filepizza-socket'
|
import socket from 'filepizza-socket'
|
||||||
|
import WebTorrent from 'webtorrent'
|
||||||
|
|
||||||
export default alt.createStore(class UploadStore {
|
export default alt.createStore(class UploadStore {
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.bindActions(UploadActions)
|
this.bindActions(UploadActions)
|
||||||
|
|
||||||
|
this.fileName = ''
|
||||||
|
this.fileSize = 0
|
||||||
|
this.fileType = ''
|
||||||
this.status = 'ready'
|
this.status = 'ready'
|
||||||
this.token = null
|
this.token = null
|
||||||
this.file = null
|
this.infoHash = null
|
||||||
this.peerProgress = {}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
onUploadFile(file) {
|
onUploadFile(file) {
|
||||||
if (this.status !== 'ready') return
|
if (this.status !== 'ready') return
|
||||||
this.status = 'processing'
|
this.status = 'processing'
|
||||||
this.file = new UploadFile(file)
|
|
||||||
|
|
||||||
socket.emit('upload', {
|
const client = new WebTorrent()
|
||||||
name: this.file.name,
|
client.seed(file, (torrent) => {
|
||||||
size: this.file.size,
|
socket.emit('upload', {
|
||||||
type: this.file.type
|
fileName: file.name,
|
||||||
}, (token) => {
|
fileSize: file.size,
|
||||||
this.setState({
|
fileType: file.type,
|
||||||
status: 'uploading',
|
infoHash: torrent.infoHash
|
||||||
token: token
|
}, (token) => {
|
||||||
|
this.setState({
|
||||||
|
status: 'uploading',
|
||||||
|
token: token,
|
||||||
|
fileName: file.name,
|
||||||
|
fileSize: file.size,
|
||||||
|
fileType: file.type,
|
||||||
|
infoHash: torrent.infoHash
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
onSendToDownloader(peerID) {
|
|
||||||
if (this.status !== 'uploading') return
|
|
||||||
|
|
||||||
let conn = peer.connect(peerID, {
|
|
||||||
reliable: true
|
|
||||||
})
|
|
||||||
|
|
||||||
let totalChunks = this.file.countChunks()
|
|
||||||
let i = 0
|
|
||||||
|
|
||||||
let sendNextChunk = () => {
|
|
||||||
if (i === totalChunks) return
|
|
||||||
let packet = this.file.getChunk(i)
|
|
||||||
conn.send(packet)
|
|
||||||
i++
|
|
||||||
this.peerProgress[peerID] = i/totalChunks
|
|
||||||
}
|
|
||||||
|
|
||||||
conn.on('open', () => {
|
|
||||||
sendNextChunk()
|
|
||||||
this.setState({ peerProgress: this.peerProgress })
|
|
||||||
})
|
|
||||||
|
|
||||||
conn.on('data', (data) => {
|
|
||||||
if (data === 'more') sendNextChunk()
|
|
||||||
this.setState({ peerProgress: this.peerProgress })
|
|
||||||
})
|
|
||||||
|
|
||||||
conn.on('close', () => {
|
|
||||||
if (this.peerProgress[peerID] < 1) {
|
|
||||||
this.peerProgress[peerID] = -1
|
|
||||||
}
|
|
||||||
this.setState({ peerProgress: this.peerProgress })
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
}, 'UploadStore')
|
}, 'UploadStore')
|
||||||
|
|||||||
Loading…
Reference in New Issue