-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackblaze.js
More file actions
136 lines (114 loc) · 4.73 KB
/
backblaze.js
File metadata and controls
136 lines (114 loc) · 4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
(() => {
const notificationArea = document.getElementById('notificationArea')
const progressArea = document.getElementById('progressArea')
const downloadButton = document.getElementById('downloadButton')
downloadButton.onclick = startDownload
function startDownload () {
const urlInput = document.getElementById('filepath').value
const threadsInput = document.getElementById('threads')
const chunkSizeInput = document.getElementById('chunkSize')
const retriesInput = document.getElementById('retries')
if (urlInput) {
const fileUrl = urlInput
const threads = parseInt(threadsInput.value)
const chunkSize = parseInt(chunkSizeInput.value) * 1024 * 1024
const retries = parseInt(retriesInput.value)
const fileName = fileUrl.substring(fileUrl.lastIndexOf('/') + 1);
downloadFile({fileUrl, threads, chunkSize, retries, fileName})
}
}
function removeAllChildren (element) {
while (element.firstChild) {
element.removeChild(element.firstChild)
}
}
function downloadFile (options) {
// Remove any children in the DOM from previous downloads
removeAllChildren(notificationArea)
removeAllChildren(progressArea)
// Change "Download" button text & function to "Cancel"
downloadButton.innerText = 'Cancel'
document.getElementById("downloadButton").style.background='#d02e31';
downloadButton.onclick = () => {
multiThread.cancel()
// Switch back to download again
downloadButton.innerText = 'Retry'
document.getElementById("downloadButton").style.background='#d0ba3e'
downloadButton.onclick = startDownload
}
let completedChunks = 0
let totalChunks = 0
let progressElements = []
const notification = document.createElement('blockquote')
// These are the main "thread" handlers
options.onStart = ({contentLength, chunks}) => {
notificationArea.appendChild(notification)
totalChunks = chunks
}
options.onFinish = () => {
notification.innerText += '\nFinished successfully!'
downloadButton.innerText = 'Download'
document.getElementById("downloadButton").style.background='#1e9b2d'
downloadButton.onclick = startDownload
}
options.onError = ({error}) => {
console.error(error)
}
options.onProgress = ({contentLength, loaded}) => {
const bytesToMb = bytes => {
return bytes / 1024 / 1024
}
// handle divide-by-zero edge case when Content-Length=0
const percent = contentLength ? Math.round(loaded / contentLength * 100) : 1
loaded = bytesToMb(loaded).toFixed(1)
contentLength = bytesToMb(contentLength).toFixed(1)
notification.innerText = `${percent}% of File Downloaded ${completedChunks}/${totalChunks-1} Chunks Completed ${loaded}/${contentLength} MB`
}
// These are the individual chunk handlers
options.onChunkStart = ({id}) => {
if (!progressElements[id]) {
const bg = document.createElement('div')
bg.classList.add('progress-background')
const fill = document.createElement('span')
fill.classList.add('progress-fill')
fill.style.width = '0%'
bg.appendChild(fill)
progressArea.prepend(bg)
progressElements[id] = {bg, fill}
progressElements[id].fill.classList.add('downloading')
} else {
progressElements[id].fill.classList.remove('downloading')
progressElements[id].fill.classList.remove('error')
progressElements[id].fill.classList.add('warning')
}
}
options.onChunkFinish = ({id}) => {
completedChunks += 1
progressElements[id].fill.classList.remove('error')
progressElements[id].fill.classList.remove('warning')
progressElements[id].fill.classList.remove('downloading')
progressElements[id].fill.classList.add('finished')
}
options.onChunkError = ({id, error}) => {
progressElements[id].fill.classList.remove('downloading')
progressElements[id].fill.classList.remove('warning')
progressElements[id].fill.classList.add('error')
console.warn(`Chunk ${id}:`, error)
}
options.onChunkProgress = ({contentLength, loaded, id}) => {
if (!progressElements[id]) {
options.onChunkStart({id})
} else {
if (progressElements[id].fill.classList.contains('warning')) {
progressElements[id].fill.classList.remove('warning')
progressElements[id].fill.classList.add('downloading')
}
// handle divide-by-zero edge case when Content-Length=0
const percent = contentLength ? loaded / contentLength : 1
progressElements[id].fill.style.width = `${percent * 100}%`
}
}
options.url = `${options.fileUrl}`
const multiThread = new MultiThread(options)
}
})()