🔨 retrive backup form webdav

This commit is contained in:
SebClem 2022-09-30 23:52:27 +02:00
parent 52131e1011
commit b1f4d45eb5
Signed by: sebclem
GPG Key ID: 5A4308F6A359EA50
6 changed files with 139 additions and 0 deletions

View File

@ -19,6 +19,7 @@
"debug": "4.3.4", "debug": "4.3.4",
"errorhandler": "^1.5.1", "errorhandler": "^1.5.1",
"express": "4.18.1", "express": "4.18.1",
"fast-xml-parser": "^4.0.10",
"figlet": "^1.5.2", "figlet": "^1.5.2",
"form-data": "4.0.0", "form-data": "4.0.0",
"got": "12.3.0", "got": "12.3.0",

View File

@ -22,6 +22,7 @@ specifiers:
errorhandler: ^1.5.1 errorhandler: ^1.5.1
eslint: 7.19.0 eslint: 7.19.0
express: 4.18.1 express: 4.18.1
fast-xml-parser: ^4.0.10
figlet: ^1.5.2 figlet: ^1.5.2
form-data: 4.0.0 form-data: 4.0.0
got: 12.3.0 got: 12.3.0
@ -45,6 +46,7 @@ dependencies:
debug: 4.3.4 debug: 4.3.4
errorhandler: 1.5.1 errorhandler: 1.5.1
express: 4.18.1 express: 4.18.1
fast-xml-parser: 4.0.10
figlet: 1.5.2 figlet: 1.5.2
form-data: 4.0.0 form-data: 4.0.0
got: 12.3.0 got: 12.3.0
@ -1184,6 +1186,13 @@ packages:
strnum: 1.0.5 strnum: 1.0.5
dev: false dev: false
/fast-xml-parser/4.0.10:
resolution: {integrity: sha512-mYMMIk7Ho1QOiedyvafdyPamn1Vlda+5n95lcn0g79UiCQoLQ2xfPQ8m3pcxBMpVaftYXtoIE2wrNTjmLQnnkg==}
hasBin: true
dependencies:
strnum: 1.0.5
dev: false
/fastq/1.13.0: /fastq/1.13.0:
resolution: {integrity: sha512-YpkpUnK8od0o1hmeSc7UUs/eB/vIPWJYjKck2QKIzAf71Vm1AAQ3EbuZB3g2JIy+pg+ERD0vqI79KyZiB2e2Nw==} resolution: {integrity: sha512-YpkpUnK8od0o1hmeSc7UUs/eB/vIPWJYjKck2QKIzAf71Vm1AAQ3EbuZB3g2JIy+pg+ERD0vqI79KyZiB2e2Nw==}
dependencies: dependencies:

View File

@ -1,11 +1,13 @@
import express from "express" import express from "express"
import configRouter from "./config.js"; import configRouter from "./config.js";
import homeAssistant from "./homeAssistant.js" import homeAssistant from "./homeAssistant.js"
import webdavRouter from "./webdav.js";
const router = express.Router(); const router = express.Router();
router.use("/homeAssistant", homeAssistant) router.use("/homeAssistant", homeAssistant)
router.use("/config", configRouter); router.use("/config", configRouter);
router.use("/webdav", webdavRouter);
export default router; export default router;

View File

@ -0,0 +1,51 @@
import express from "express";
import {
getWebdavConfig,
validateWebdavConfig,
} from "../services/webdavConfigService.js";
import * as webdavService from "../services/webdavService.js";
import * as pathTools from "../tools/pathTools.js";
const webdavRouter = express.Router();
webdavRouter.get("/backup/auto", (req, res, next) => {
const config = getWebdavConfig();
validateWebdavConfig(config)
.then(() => {
webdavService
.getBackups(pathTools.auto, config)
.then((value) => {
res.json(value);
})
.catch((reason) => {
res.status(500);
res.json(reason);
});
})
.catch((reason) => {
res.status(500);
res.json(reason);
});
});
webdavRouter.get("/backup/manual", (req, res, next) => {
const config = getWebdavConfig();
validateWebdavConfig(config)
.then(() => {
webdavService
.getBackups(pathTools.manual, config)
.then((value) => {
res.json(value);
})
.catch((reason) => {
res.status(500);
res.json(reason);
});
})
.catch((reason) => {
res.status(500);
res.json(reason);
});
});
export default webdavRouter;

View File

@ -4,6 +4,21 @@ import messageManager from "../tools/messageManager.js";
import { WebdavConfig } from "../types/services/webdavConfig.js"; import { WebdavConfig } from "../types/services/webdavConfig.js";
import { getEndpoint } from "./webdavConfigService.js"; import { getEndpoint } from "./webdavConfigService.js";
import * as pathTools from "../tools/pathTools.js"; import * as pathTools from "../tools/pathTools.js";
import { XMLParser } from "fast-xml-parser";
import { WebdavBackup } from "../types/services/webdav.js";
import { DateTime } from "luxon";
const PROPFIND_BODY =
'<?xml version="1.0" encoding="utf-8" ?>\
<d:propfind xmlns:d="DAV:">\
<d:prop>\
<d:getlastmodified />\
<d:getetag />\
<d:getcontenttype />\
<d:resourcetype />\
<d:getcontentlength />\
</d:prop>\
</d:propfind>';
export function checkWebdavLogin(config: WebdavConfig) { export function checkWebdavLogin(config: WebdavConfig) {
const endpoint = getEndpoint(config); const endpoint = getEndpoint(config);
@ -77,6 +92,59 @@ function createDirectory(path: string, config: WebdavConfig) {
}); });
} }
export function getBackups(folder: string, config: WebdavConfig) {
const endpoint = getEndpoint(config);
return got(config.url + endpoint + config.backupDir + folder, {
method: "PROPFIND" as Method,
headers: {
authorization:
"Basic " +
Buffer.from(config.username + ":" + config.password).toString("base64"),
Depth: "1",
},
body: PROPFIND_BODY,
}).then(
(value) => {
return parseXmlBackupData(value.body);
},
(reason) => {
messageManager.error(
`Fail to retrive webdav backups in ${folder} folder`
);
logger.error(`Fail to retrive webdav backups in ${folder} folder`);
logger.error(reason);
return Promise.reject(reason);
}
);
}
function parseXmlBackupData(body: string){
const parser = new XMLParser();
const data = parser.parse(body);
const multistatus = data["d:multistatus"];
const backups: WebdavBackup[] = [];
if(Array.isArray(multistatus["d:response"])){
for(const elem of multistatus["d:response"]){
// If array -> base folder, ignoring it
if(!Array.isArray(elem["d:propstat"])){
const propstat = elem["d:propstat"];
const id = propstat["d:prop"]["d:getetag"].replaceAll("\"", "");
const href = decodeURI(elem["d:href"]);
const name = href.split("/").slice(-1)[0];
const lastEdit = DateTime.fromHTTP(propstat["d:prop"]["d:getlastmodified"]);
backups.push({
id: id,
lastEdit: lastEdit,
size: propstat["d:prop"]["d:getcontentlenght"],
name: name
})
}
}
}
return backups;
}
// import fs from "fs"; // import fs from "fs";
// import got from "got"; // import got from "got";
// import https from "https"; // import https from "https";

View File

@ -0,0 +1,8 @@
import { DateTime } from "luxon";
export interface WebdavBackup {
id: string;
name: string;
size: number;
lastEdit: DateTime;
}