mirror of
https://github.com/Sebclem/hassio-nextcloud-backup.git
synced 2024-11-25 10:32:58 +01:00
Add upload methods
This commit is contained in:
parent
7ce157efec
commit
a962827599
@ -79,13 +79,15 @@ router.get('/nextcloud-settings', function(req, res, next) {
|
||||
|
||||
router.post('/manual-backup', function(req, res, next) {
|
||||
let id = req.query.id;
|
||||
hassioApiTools.downloadSnapshot(id)
|
||||
.then(() => {
|
||||
let name = req.query.name;
|
||||
// hassioApiTools.downloadSnapshot(id)
|
||||
// .then(() => {
|
||||
webdav.uploadFile('8afb4728', '/Hassio Backup/Manual/' + '8afb4728' + '.tar');
|
||||
res.send(200);
|
||||
})
|
||||
.catch(() => {
|
||||
res.send(500);
|
||||
})
|
||||
// })
|
||||
// .catch(() => {
|
||||
// res.send(500);
|
||||
// })
|
||||
|
||||
});
|
||||
|
||||
|
@ -38,6 +38,7 @@ function getSnapshots() {
|
||||
status.message = "Fail to fetch Hassio snapshot (" + error + ")";
|
||||
status.error_code = 1;
|
||||
statusTools.setStatus(status);
|
||||
console.error(status.message);
|
||||
reject(error);
|
||||
}
|
||||
})
|
||||
@ -47,8 +48,10 @@ function getSnapshots() {
|
||||
|
||||
function downloadSnapshot(id) {
|
||||
return new Promise((resolve, reject) => {
|
||||
|
||||
let stream = fs.createWriteStream('./' + id + '.tar');
|
||||
console.log('Downloading snapshot ' + id + '...')
|
||||
if(!fs.existsSync('./temp/'))
|
||||
fs.mkdirSync('./temp/');
|
||||
let stream = fs.createWriteStream('./temp/' + id + '.tar');
|
||||
let token = process.env.HASSIO_TOKEN;
|
||||
if (token == null) {
|
||||
token = fallbackToken
|
||||
@ -64,27 +67,31 @@ function downloadSnapshot(id) {
|
||||
}
|
||||
progress(request(option))
|
||||
.on('progress', (state) => {
|
||||
// TODO Don't write progress to disk, preseve disk IO time
|
||||
status.progress = state.percent;
|
||||
statusTools.setStatus(status);
|
||||
})
|
||||
.on('error', (error) => {
|
||||
status.status = "error";
|
||||
status.message = "Fail to downloadw Hassio snapshot (" + error + ")";
|
||||
status.message = "Fail to download Hassio snapshot (" + error + ")";
|
||||
status.error_code = 4;
|
||||
statusTools.setStatus(status);
|
||||
console.error(status.message);
|
||||
reject(error);
|
||||
})
|
||||
.on('end', () => {
|
||||
console.log('end')
|
||||
console.log('Download success !')
|
||||
status.progress = 1;
|
||||
statusTools.setStatus(status);
|
||||
resolve();
|
||||
})
|
||||
.pipe(stream);
|
||||
}).catch(() => {
|
||||
status.status = "error";
|
||||
status.message = "Fail to downloadw Hassio snapshot";
|
||||
status.message = "Fail to download Hassio snapshot. Not found ?";
|
||||
status.error_code = 4;
|
||||
statusTools.setStatus(status);
|
||||
console.error(status.message);
|
||||
reject();
|
||||
});
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
const { createClient } = require("webdav");
|
||||
const fs = require("fs");
|
||||
|
||||
const statusTools = require('./status');
|
||||
const endpoint = "/remote.php/webdav"
|
||||
const configPath = "./webdav_conf.json"
|
||||
@ -16,6 +17,7 @@ class WebdavTools {
|
||||
let url = (ssl ? "https" : "http") + "://" + host + endpoint;
|
||||
try {
|
||||
this.client = createClient(url, { username: username, password: password });
|
||||
|
||||
this.client.getDirectoryContents("/").then(() => {
|
||||
if (status.error_code == 3) {
|
||||
status.status = "idle";
|
||||
@ -50,11 +52,11 @@ class WebdavTools {
|
||||
});
|
||||
}
|
||||
|
||||
initFolder(){
|
||||
return new Promise((resolve, reject) =>{
|
||||
this.client.createDirectory("/Hassio Backup").catch(()=>{}).then(()=>{
|
||||
this.client.createDirectory("/Hassio Backup/Auto").catch(()=>{}).then(()=>{
|
||||
this.client.createDirectory("/Hassio Backup/Manual").catch(()=>{}).then(()=>{
|
||||
initFolder() {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.client.createDirectory("/Hassio Backup").catch(() => { }).then(() => {
|
||||
this.client.createDirectory("/Hassio Backup/Auto").catch(() => { }).then(() => {
|
||||
this.client.createDirectory("/Hassio Backup/Manual").catch(() => { }).then(() => {
|
||||
resolve();
|
||||
})
|
||||
})
|
||||
@ -87,6 +89,7 @@ class WebdavTools {
|
||||
status.error_code = 2;
|
||||
status.message = "Nextcloud config invalid !"
|
||||
statusTools.setStatus(status);
|
||||
console.error(status.message);
|
||||
reject("Nextcloud config invalid !");
|
||||
}
|
||||
}
|
||||
@ -95,6 +98,7 @@ class WebdavTools {
|
||||
status.error_code = 2;
|
||||
status.message = "Nextcloud config not found !"
|
||||
statusTools.setStatus(status);
|
||||
console.error(status.message);
|
||||
reject("Nextcloud config not found !");
|
||||
}
|
||||
|
||||
@ -110,11 +114,53 @@ class WebdavTools {
|
||||
return null;
|
||||
}
|
||||
|
||||
setConf(conf){
|
||||
setConf(conf) {
|
||||
fs.writeFileSync(configPath, JSON.stringify(conf));
|
||||
}
|
||||
|
||||
|
||||
uploadFile(id, path) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (this.client == null) {
|
||||
this.confIsValid().then(() => {
|
||||
this._startUpload(id, path);
|
||||
}).catch((err) => {
|
||||
reject(err);
|
||||
})
|
||||
}
|
||||
else
|
||||
this._startUpload(id, path);
|
||||
});
|
||||
}
|
||||
|
||||
_startUpload(id, path) {
|
||||
return new Promise((resolve, reject) => {
|
||||
let status = statusTools.getStatus();
|
||||
status.status = "upload";
|
||||
status.progress = -1;
|
||||
status.message = null;
|
||||
status.error_code = null;
|
||||
statusTools.setStatus(status);
|
||||
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)
|
||||
this.client.putFileContents(path, fs.readFileSync('./temp/' + id + '.tar'), { maxContentLength: 1024 ** 3 }).then((result) => {
|
||||
console.log("...Upload finish !");
|
||||
status.status = "idle";
|
||||
status.message = null;
|
||||
status.error_code = null;
|
||||
statusTools.setStatus(status);
|
||||
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);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
@ -310,7 +310,8 @@
|
||||
|
||||
$('.manual-back-list').click(function(){
|
||||
let id = this.getAttribute('data-id');
|
||||
manualBackup(id);
|
||||
let name = this.getAttribute('data-name');
|
||||
manualBackup(id, name);
|
||||
})
|
||||
|
||||
});
|
||||
@ -329,6 +330,8 @@
|
||||
break;
|
||||
case "download":
|
||||
printStatusWithBar('Downloading Snapshot', data.progress);
|
||||
case "upload":
|
||||
printStatusWithBar('Uploading Snapshot', data.progress);
|
||||
}
|
||||
}
|
||||
|
||||
@ -408,8 +411,8 @@
|
||||
})
|
||||
}
|
||||
|
||||
function manualBackup(id){
|
||||
$.post('./api/manual-backup?id=' + id);
|
||||
function manualBackup(id, name){
|
||||
$.post('./api/manual-backup?id=' + id + '&name=' + name);
|
||||
|
||||
}
|
||||
|
||||
|
@ -3,7 +3,7 @@
|
||||
<% for(const index in snaps) { %>
|
||||
<a class="collection-item local-snap-listener modal-trigger" href="#modal-<%=snaps[index].slug%>"
|
||||
data-id="<%= snaps[index].slug %>">
|
||||
<div><%= snaps[index].name ? snaps[index].name : 'No name' %><div class="secondary-content">
|
||||
<div><%= snaps[index].name ? snaps[index].name : snaps[index].slug %><div class="secondary-content">
|
||||
<%= moment(snaps[index].date).format('lll') %></div>
|
||||
</div>
|
||||
</a>
|
||||
@ -22,7 +22,7 @@
|
||||
<div class="row">
|
||||
|
||||
<div class="input-field col s12">
|
||||
<input disabled type="text" id="name-<%=snaps[index].slug%>" value="<%= snaps[index].name ? snaps[index].name : 'No name' %>" />
|
||||
<input disabled type="text" id="name-<%=snaps[index].slug%>" value="<%= snaps[index].name ? snaps[index].name : snaps[index].slug %>" />
|
||||
<label for="name-<%=snaps[index].slug%>" class="white-text active">Name</label>
|
||||
</div>
|
||||
|
||||
@ -46,7 +46,7 @@
|
||||
</div>
|
||||
</div>
|
||||
<div class="modal-footer blue-grey darken-4">
|
||||
<a href="#!" class="waves-effect waves-green btn light-blue accent-4 manual-back-list modal-close" data-id="<%=snaps[index].slug%>">Backup now</a>
|
||||
<a href="#!" class="waves-effect waves-green btn light-blue accent-4 manual-back-list modal-close" data-id="<%=snaps[index].slug%>" data-name='<%= snaps[index].name ? snaps[index].name : snaps[index].slug %>'</a>Backup now</a>
|
||||
<a href="#!" class="modal-close waves-effect waves-green btn red">Close</a>
|
||||
|
||||
</div>
|
||||
|
Loading…
Reference in New Issue
Block a user