ledown
Version:
A library for nodejs to download http large file in pauseable and resumable way
82 lines (70 loc) • 1.92 kB
JavaScript
/** @flow */
import ByteSheet, { type ByteRange } from './ByteSheet'
export default class TaskMeta {
url: string = ''
createDate: Date = null
mimeType: string = ''
downloadPath: string = ''
totalBytes: number = 0
resumable: boolean = false
byteSheet: ByteSheet = null
constructor (param: {
url: string,
mimeType: string,
downloadPath: string,
totalBytes: number,
resumable: boolean,
createDate?: number,
refererUrl?: string,
byteSheet?: Array<ByteRange[]>
}) {
Object.assign(this, param)
const { createDate, byteSheet } = param
if (createDate) {
this.createDate = new Date(createDate)
} else {
this.createDate = new Date()
}
if (byteSheet && this.resumable) {
this.byteSheet = new ByteSheet(byteSheet)
} else {
this.byteSheet = new ByteSheet(param.totalBytes)
}
}
allocRange (size: number): ByteRange {
if (!this.resumable) {
size = this.totalBytes
}
return this.byteSheet.allocRangeToDownload(size)
}
completeRange (range: ByteRange): void {
return this.byteSheet.setRangeAsCompleted(range)
}
resetDownloadingRange (): void {
return this.byteSheet.resetDownloading()
}
get progress (): number {
return this.completedByteCount / this.totalBytes
}
get remainingByteCount (): number {
return this.byteSheet.remainingByteCount
}
get completedByteCount (): number {
return this.byteSheet.completedByteCount
}
get downloadingByteCount (): number {
return this.byteSheet.downloadingByteCount
}
toJSONObject (): Object {
return {
url: this.url,
refererUrl: this.refererUrl,
mimeType: this.mimeType,
resumable: this.resumable,
downloadPath: this.downloadPath,
totalBytes: this.totalBytes,
createDate: this.createDate.valueOf(),
byteSheet: this.byteSheet.toJSONObject()
}
}
}