Last active
June 25, 2019 19:43
-
-
Save jbis9051/a7239596d7eb1c5aa1f391078b2b1e52 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Download from './Download'; | |
import DownloadItem from './components/DownloadItem/DownloadItem.js'; | |
import React from "react"; | |
import Enum from './enum.js'; | |
const {DownloadStatus} = Enum; | |
const events = window.require('events'); | |
Date.prototype.print = function() { | |
return `${this.getUTCHours() || 0}h:${this.getUTCMinutes() || 0}m:${this.getUTCSeconds() || 0}s`; | |
}; | |
export default class DownloadCarrier extends events.EventEmitter { | |
constructor(url, name, headers,proxyOptions) { | |
super(); | |
this.url = url; | |
this.name = name; | |
this.customHeaders = (typeof headers === "string") ? DownloadCarrier.JSONparse(headers) : headers; | |
if(this.download){ | |
this.download.constructor(); // retry download, need to reset everything | |
} else { | |
this.download = new Download(); | |
} | |
this.headersExpanded = false; | |
this.status = DownloadStatus.AWAITING; | |
this.done = false; | |
this.stats = {}; | |
this.last_update = 0; | |
this.functions = { | |
cancel: this.cancel.bind(this), | |
remove: this.remove.bind(this), | |
retry: this.retry.bind(this), | |
toggleHeaders: this.toggleHeaders().bind(this), | |
}; | |
/* | |
if (window.localStorage.proxySettings === "auth") { | |
this.proxyOptions = { | |
hostname: window.localStorage.proxyHost, | |
port: window.localStorage.proxyPort, | |
auth: (window.localStorage.proxyRequiresCredentials === "true") ? { | |
username: window.localStorage.proxyUsername, | |
password: window.localStorage.proxyPassword, | |
} : false, | |
}; | |
} | |
*/ | |
} | |
async initiateDownload() { | |
this.emit("init"); | |
this.download.on('update', info => this.update(info)); | |
this.download.on('error', err => this.error(err)); | |
await this.download.init(this.url, this.name, window.localStorage.saveLocation, Number(window.localStorage.partsToCreate), this.customHeaders, this.proxyOptions || false); | |
} | |
async startDownload() { | |
await this.download.beginDownload(); | |
} | |
toggleHeaders() { | |
this.headersExpanded = !this.headersExpanded; | |
} | |
update(info) { | |
this.stats = Object.assign({}, this.stats, info); | |
if (Date.now() - this.last_update > 800 || info.done) { | |
this.last_update = Date.now(); | |
this.emit("update", this.stats); | |
} | |
} | |
error(err) { | |
this.emit("error", err); | |
} | |
static calculateSize(bytes) { | |
let output = bytes; | |
let steps = 0; | |
let units = []; | |
if (window.localStorage.getItem('preferredUnit') === "bin") { | |
units = ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"]; | |
while (output > 1024) { | |
output /= 1024; | |
steps++; | |
} | |
} else if (window.localStorage.getItem('preferredUnit') === "dec") { | |
units = ["B", "KB", "MB", "GB", "TB", "PB", "EB"]; | |
while (output > 1000) { | |
output /= 1000; | |
steps++; | |
} | |
} | |
return parseFloat(output).toFixed(2) + " " + units[steps]; | |
} | |
async cancel() { | |
if (this.download) { | |
await this.download.cancel(); | |
} | |
this.emit('cancel'); | |
} | |
remove() { | |
this.emit('remove'); | |
} | |
retry(){ | |
this.emit('retry'); | |
} | |
static JSONparse(str){ | |
str = str || "{}"; | |
try { | |
JSON.parse(str); | |
} catch (e) { | |
return false; | |
} | |
return JSON.parse(str); | |
} | |
formatProps() { | |
return { | |
percentage: Number(parseFloat(this.stats.percentage || 0).toFixed(7)), | |
progress: `${DownloadCarrier.calculateSize(this.stats.progress || 0)} / ${DownloadCarrier.calculateSize(this.stats.size || 0)}`, | |
size: DownloadCarrier.calculateSize(this.stats.size || 0), | |
speed: `${DownloadCarrier.calculateSize(this.stats.speed || 0)}/s`, | |
eta: `${new Date(this.stats.eta || Date.now()).toLocaleString()} (${new Date(this.stats.eta - Date.now()).print()})` || 0, | |
elapsedTime: this.stats.elapsedTime || 0, | |
parts: `${this.stats.chunks_done || 0} / ${this.stats.total_chunks || 0}`, | |
path: this.stats.path || "", | |
url: this.url || "", | |
headers: this.customHeaders || "", | |
headersExpanded: this.headersExpanded || false, | |
error: this.stats.error || "None", | |
}; | |
} | |
render(key) { | |
return <DownloadItem key={key} status={this.status} functions={this.functions} content={this.formatProps()}/> | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment