mirror of
https://github.com/Sebclem/hassio-nextcloud-backup.git
synced 2024-11-22 17:22:58 +01:00
🔨 Add progress tracking on upload
This commit is contained in:
parent
8cd2fb80c7
commit
c403ec5079
@ -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,15 +145,54 @@ 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 = {
|
||||||
|
url: this.baseUrl + encodeURI(path),
|
||||||
|
auth: {
|
||||||
|
user: this.username,
|
||||||
|
pass: this.password
|
||||||
|
},
|
||||||
|
body: fs.createReadStream('./temp/' + id + '.tar')
|
||||||
|
|
||||||
|
}
|
||||||
|
let lastPercent = 0;
|
||||||
|
let req = request.put(option)
|
||||||
|
.on('drain', () => {
|
||||||
|
let percent = Math.floor((req.req.connection.bytesWritten / fileSize)*100);
|
||||||
|
if(lastPercent != percent){
|
||||||
|
lastPercent = percent;
|
||||||
|
status.progress = percent/100;
|
||||||
|
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 !");
|
console.log("...Upload finish !");
|
||||||
status.status = "idle";
|
status.status = "idle";
|
||||||
|
status.progress = -1;
|
||||||
status.message = null;
|
status.message = null;
|
||||||
status.error_code = null;
|
status.error_code = null;
|
||||||
status.last_backup = moment().format('MMM D, YYYY HH:mm')
|
status.last_backup = moment().format('MMM D, YYYY HH:mm')
|
||||||
@ -154,14 +200,8 @@ class WebdavTools {
|
|||||||
statusTools.setStatus(status);
|
statusTools.setStatus(status);
|
||||||
fs.unlinkSync('./temp/' + id + '.tar')
|
fs.unlinkSync('./temp/' + id + '.tar')
|
||||||
resolve();
|
resolve();
|
||||||
}).catch((err) => {
|
}
|
||||||
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);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user