Updated home.js: Live upload speed

Previously it calculated average upload speed instead
This should be more accurate/reliable
This commit is contained in:
Bobby Wibowo 2020-04-21 17:05:20 +07:00
parent 3b1779e492
commit 7cd9e5b40b
No known key found for this signature in database
GPG Key ID: 51C3A1E1E22D26CF
4 changed files with 20 additions and 11 deletions

2
dist/js/home.js vendored

File diff suppressed because one or more lines are too long

2
dist/js/home.js.map vendored

File diff suppressed because one or more lines are too long

View File

@ -339,9 +339,12 @@ page.prepareDropzone = () => {
page.dropzone._handleUploadError(instances, xhr, 'Connection timed out. Try to reduce upload chunk size.')
}
// Add start timestamp of upload attempt
if (xhr._start === undefined)
xhr._start = Date.now()
// Attach necessary data for initial upload speed calculation
if (xhr._upSpeedCalc === undefined)
xhr._upSpeedCalc = {
bytes: 0,
timestamp: Date.now()
}
// If not chunked uploads, add extra headers
if (!file.upload.chunked) {
@ -381,15 +384,21 @@ page.prepareDropzone = () => {
prefix = `Uploading chunk ${chunkIndex}/${file.upload.totalChunkCount}\u2026`
}
let prettyBytesPerSec
// Upload speed calculation
let bytesPerSec
if (!skipProgress) {
const elapsed = (Date.now() - xhr._start) / 1000
const bytesPerSec = elapsed ? (upl.bytesSent / elapsed) : 0
prettyBytesPerSec = page.getPrettyBytes(bytesPerSec)
const now = Date.now()
const elapsedSecs = (now - xhr._upSpeedCalc.timestamp) / 1000
const bytesSent = upl.bytesSent - xhr._upSpeedCalc.bytes
bytesPerSec = elapsedSecs ? (bytesSent / elapsedSecs) : 0
// Update data for next upload speed calculation
xhr._upSpeedCalc.bytes = upl.bytesSent
xhr._upSpeedCalc.timestamp = now
}
file.previewElement.querySelector('.descriptive-progress').innerHTML =
`${prefix} ${percentage}%${prettyBytesPerSec ? ` at ~${prettyBytesPerSec}/s` : ''}`
`${prefix} ${percentage}%${bytesPerSec ? ` at ${page.getPrettyBytes(bytesPerSec)}/s` : ''}`
})
this.on('success', (file, data) => {

View File

@ -1,5 +1,5 @@
{
"1": "1587322292",
"1": "1587463408",
"2": "1581416390",
"3": "1581416390",
"4": "1581416390",