🔨 Add progress tracking on upload

This commit is contained in:
Sebastien Clement 2020-01-06 15:20:14 +01:00
parent 8cd2fb80c7
commit c403ec5079

View File

@ -6,18 +6,25 @@ const statusTools = require('./status');
const endpoint = "/remote.php/webdav" const endpoint = "/remote.php/webdav"
const configPath = "./webdav_conf.json" const configPath = "./webdav_conf.json"
const request = require('request');
class WebdavTools { class WebdavTools {
constructor() { constructor() {
this.client = null; this.client = null;
this.baseUrl = null;
this.username = null;
this.password = null;
} }
init(ssl, host, username, password) { init(ssl, host, username, password) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let status = statusTools.getStatus(); let status = statusTools.getStatus();
console.log("Initilizing and checking webdav client...") console.log("Initilizing and checking webdav client...")
let url = (ssl ? "https" : "http") + "://" + host + endpoint; this.baseUrl = (ssl ? "https" : "http") + "://" + host + endpoint;
this.username = username;
this.password = password;
try { try {
this.client = createClient(url, { username: username, password: password }); this.client = createClient(this.baseUrl, { username: username, password: password });
this.client.getDirectoryContents("/").then(() => { this.client.getDirectoryContents("/").then(() => {
if (status.error_code == 3) { if (status.error_code == 3) {
@ -138,34 +145,67 @@ class WebdavTools {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let status = statusTools.getStatus(); let status = statusTools.getStatus();
status.status = "upload"; status.status = "upload";
status.progress = -1; status.progress = 0;
status.message = null; status.message = null;
status.error_code = null; status.error_code = null;
statusTools.setStatus(status); statusTools.setStatus(status);
console.log('Uploading snap...'); console.log('Uploading snap...');
//TODO Change this, try with request (buid the webdav request manualy) to track progress (https://stackoverflow.com/questions/12098713/upload-progress-request) let fileSize = fs.statSync('./temp/' + id + '.tar').size;
this.client.putFileContents(path, fs.readFileSync('./temp/' + id + '.tar'), { maxContentLength: 1024 ** 3 }).then((result) => { let option = {
console.log("...Upload finish !"); url: this.baseUrl + encodeURI(path),
status.status = "idle"; auth: {
status.message = null; user: this.username,
status.error_code = null; pass: this.password
status.last_backup = moment().format('MMM D, YYYY HH:mm') },
body: fs.createReadStream('./temp/' + id + '.tar')
statusTools.setStatus(status);
fs.unlinkSync('./temp/' + id + '.tar') }
resolve(); let lastPercent = 0;
}).catch((err) => { let req = request.put(option)
status.status = "error"; .on('drain', () => {
status.error_code = 4; let percent = Math.floor((req.req.connection.bytesWritten / fileSize)*100);
status.message = "Fail to upload snapshot to nextcloud (" + err + ") !" if(lastPercent != percent){
statusTools.setStatus(status); lastPercent = percent;
console.error(status.message); status.progress = percent/100;
reject(status.message); statusTools.setStatus(status);
}); }
}).on('error', function(err) {
fs.unlinkSync('./temp/' + id + '.tar');
status.status = "error";
status.error_code = 4;
status.message = "Fail to upload snapshot to nextcloud (" + err + ") !"
statusTools.setStatus(status);
console.error(status.message);
reject(status.message);
}).on('response', (res) => {
if (res.statusCode != 204) {
status.status = "error";
status.error_code = 4;
status.message = "Fail to upload snapshot to nextcloud (Status code: " + res.statusCode + ") !"
statusTools.setStatus(status);
console.error(status.message);
fs.unlinkSync('./temp/' + id + '.tar')
reject(status.message);
}
else {
console.log("...Upload finish !");
status.status = "idle";
status.progress = -1;
status.message = null;
status.error_code = null;
status.last_backup = moment().format('MMM D, YYYY HH:mm')
statusTools.setStatus(status);
fs.unlinkSync('./temp/' + id + '.tar')
resolve();
}
})
}); });
} }
} }