-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChunkyDownloader.js
More file actions
45 lines (38 loc) · 1.47 KB
/
Copy pathChunkyDownloader.js
File metadata and controls
45 lines (38 loc) · 1.47 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
/*
This script downloads a large file in manageable chunks.
Yes, it's not bulletproof but worked for what I needed it.
Saving separately for future reference, might be useful.
by gib
*/
const keyboard = require('keyboard');
const storage = require('storage');
const dialog = require('dialog');
const wifi = require('wifi');
function downloadFile(url, destination, displayDialog){
if(destination === undefined) destination = "/" + url.split('/').pop();
if(displayDialog === undefined) displayDialog = false;
var headResp = wifi.httpFetch(url, {method: "HEAD"});
var isOk = to_string(headResp.status);
if(isOk.indexOf("2") === 0){
try{storage.remove(destination);}
catch(erdf){}
if(displayDialog) dialog.info("Downloading " + size + " bytes");
var size = Number(headResp.headers["Content-Length"]);
var chunkSize = 64 * 1024;
var offset = 0;
while(offset < size){
var end = Math.min(offset + chunkSize - 1, size - 1);
var chunkResp = wifi.httpFetch(url, {headers: {Connection: "Keep-Alive", Range: "bytes=" + offset + "-" + end}});
storage.write({fs: "sd", path: destination}, chunkResp.body, "append");
offset += chunkSize;
if(displayDialog){
var percent = Math.floor(offset / size * 100);
percent = percent < 100 ? percent : 100;
dialog.info("Downloading: " + percent + "%");
}
}
return true;
}
else return false;
}
downloadFile("https://sample-files.com/downloads/images/jpg/exif_rich_3000x2000_3.81mb.jpg", "/downloaded.jpg", true);