mirror of https://github.com/kern/filepizza
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
770 B
JavaScript
47 lines
770 B
JavaScript
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')
|
|
a.download = this.name
|
|
a.href = url
|
|
a.click()
|
|
URL.revokeObjectURL(url)
|
|
}
|
|
|
|
toJSON() {
|
|
return {
|
|
name: this.name,
|
|
size: this.size,
|
|
type: this.type
|
|
}
|
|
}
|
|
|
|
}
|