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
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue