mirror of
https://github.com/Sebclem/hassio-nextcloud-backup.git
synced 2024-11-04 16:42:58 +01:00
[back] Change when we send message to front
This commit is contained in:
parent
10fcb94962
commit
99471114ed
@ -68,11 +68,6 @@ function postInit() {
|
|||||||
|
|
||||||
settingsTools.check(settingsTools.getSettings(), true);
|
settingsTools.check(settingsTools.getSettings(), true);
|
||||||
// cronTools.init();
|
// cronTools.init();
|
||||||
|
|
||||||
messageManager.error("this is error");
|
|
||||||
messageManager.info("This is info");
|
|
||||||
messageManager.warn("re gerg rg ergrge r ");
|
|
||||||
messageManager.success("zefzegz gze gerg erg zegfze gerg erg aeferg erg erg er");
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,11 @@
|
|||||||
import { XMLParser } from "fast-xml-parser";
|
import { XMLParser } from "fast-xml-parser";
|
||||||
import { createReadStream, statSync, unlinkSync } from "fs";
|
import { createReadStream, stat, statSync, unlinkSync } from "fs";
|
||||||
import got, { HTTPError, type Method } from "got";
|
import got, {
|
||||||
|
HTTPError,
|
||||||
|
RequestError,
|
||||||
|
type Method,
|
||||||
|
type PlainResponse,
|
||||||
|
} from "got";
|
||||||
import { DateTime } from "luxon";
|
import { DateTime } from "luxon";
|
||||||
import logger from "../config/winston.js";
|
import logger from "../config/winston.js";
|
||||||
import messageManager from "../tools/messageManager.js";
|
import messageManager from "../tools/messageManager.js";
|
||||||
@ -10,7 +15,7 @@ import type { WebdavBackup } from "../types/services/webdav.js";
|
|||||||
import type { WebdavConfig } from "../types/services/webdavConfig.js";
|
import type { WebdavConfig } from "../types/services/webdavConfig.js";
|
||||||
import { templateToRegexp } from "./backupConfigService.js";
|
import { templateToRegexp } from "./backupConfigService.js";
|
||||||
import { getEndpoint } from "./webdavConfigService.js";
|
import { getEndpoint } from "./webdavConfigService.js";
|
||||||
import { WebdabStatus } from "../types/status.js";
|
import { States } from "../types/status.js";
|
||||||
|
|
||||||
const PROPFIND_BODY =
|
const PROPFIND_BODY =
|
||||||
'<?xml version="1.0" encoding="utf-8" ?>\
|
'<?xml version="1.0" encoding="utf-8" ?>\
|
||||||
@ -24,7 +29,10 @@ const PROPFIND_BODY =
|
|||||||
</d:prop>\
|
</d:prop>\
|
||||||
</d:propfind>';
|
</d:propfind>';
|
||||||
|
|
||||||
export function checkWebdavLogin(config: WebdavConfig) {
|
export function checkWebdavLogin(
|
||||||
|
config: WebdavConfig,
|
||||||
|
silent: boolean = false
|
||||||
|
) {
|
||||||
const endpoint = getEndpoint(config);
|
const endpoint = getEndpoint(config);
|
||||||
return got(config.url + endpoint, {
|
return got(config.url + endpoint, {
|
||||||
method: "OPTIONS",
|
method: "OPTIONS",
|
||||||
@ -35,16 +43,21 @@ export function checkWebdavLogin(config: WebdavConfig) {
|
|||||||
},
|
},
|
||||||
}).then(
|
}).then(
|
||||||
(response) => {
|
(response) => {
|
||||||
|
const status = statusTools.getStatus();
|
||||||
|
status.webdav.logged_in = true;
|
||||||
|
status.webdav.last_check = DateTime.now();
|
||||||
return response;
|
return response;
|
||||||
},
|
},
|
||||||
(reason) => {
|
(reason) => {
|
||||||
messageManager.error("Fail to connect to Webdav", reason?.message);
|
if (!silent) {
|
||||||
|
messageManager.error("Fail to connect to Webdav", reason?.message);
|
||||||
|
}
|
||||||
const status = statusTools.getStatus();
|
const status = statusTools.getStatus();
|
||||||
status.webdav = {
|
status.webdav = {
|
||||||
state: WebdabStatus.LOGIN_FAIL,
|
logged_in: false,
|
||||||
blocked: true,
|
folder_created: status.webdav.folder_created,
|
||||||
last_check: DateTime.now()
|
last_check: DateTime.now(),
|
||||||
}
|
};
|
||||||
statusTools.setStatus(status);
|
statusTools.setStatus(status);
|
||||||
logger.error(`Fail to connect to Webdav`);
|
logger.error(`Fail to connect to Webdav`);
|
||||||
logger.error(reason);
|
logger.error(reason);
|
||||||
@ -70,11 +83,8 @@ export async function createBackupFolder(conf: WebdavConfig) {
|
|||||||
logger.error("Fail to create webdav root folder");
|
logger.error("Fail to create webdav root folder");
|
||||||
logger.error(error);
|
logger.error(error);
|
||||||
const status = statusTools.getStatus();
|
const status = statusTools.getStatus();
|
||||||
status.webdav = {
|
status.webdav.folder_created = false;
|
||||||
state: WebdabStatus.MK_FOLDER_FAIL,
|
status.webdav.last_check = DateTime.now();
|
||||||
blocked: true,
|
|
||||||
last_check: DateTime.now()
|
|
||||||
}
|
|
||||||
statusTools.setStatus(status);
|
statusTools.setStatus(status);
|
||||||
return Promise.reject(error);
|
return Promise.reject(error);
|
||||||
}
|
}
|
||||||
@ -92,10 +102,18 @@ export async function createBackupFolder(conf: WebdavConfig) {
|
|||||||
messageManager.error("Fail to create webdav root folder");
|
messageManager.error("Fail to create webdav root folder");
|
||||||
logger.error("Fail to create webdav root folder");
|
logger.error("Fail to create webdav root folder");
|
||||||
logger.error(error);
|
logger.error(error);
|
||||||
|
const status = statusTools.getStatus();
|
||||||
|
status.webdav.folder_created = false;
|
||||||
|
status.webdav.last_check = DateTime.now();
|
||||||
|
statusTools.setStatus(status);
|
||||||
return Promise.reject(error);
|
return Promise.reject(error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
const status = statusTools.getStatus();
|
||||||
|
status.webdav.folder_created = true;
|
||||||
|
status.webdav.last_check = DateTime.now();
|
||||||
|
statusTools.setStatus(status);
|
||||||
}
|
}
|
||||||
|
|
||||||
function createDirectory(path: string, config: WebdavConfig) {
|
function createDirectory(path: string, config: WebdavConfig) {
|
||||||
@ -115,6 +133,10 @@ export function getBackups(
|
|||||||
config: WebdavConfig,
|
config: WebdavConfig,
|
||||||
nameTemplate: string
|
nameTemplate: string
|
||||||
) {
|
) {
|
||||||
|
const status = statusTools.getStatus();
|
||||||
|
if (!status.webdav.logged_in && !status.webdav.folder_created) {
|
||||||
|
return Promise.reject("Not logged in");
|
||||||
|
}
|
||||||
const endpoint = getEndpoint(config);
|
const endpoint = getEndpoint(config);
|
||||||
return got(config.url + endpoint + config.backupDir + folder, {
|
return got(config.url + endpoint + config.backupDir + folder, {
|
||||||
method: "PROPFIND" as Method,
|
method: "PROPFIND" as Method,
|
||||||
@ -160,8 +182,8 @@ function extractBackupInfo(backups: WebdavBackup[], template: string) {
|
|||||||
|
|
||||||
elem.creationDate = DateTime.fromFormat(date, format);
|
elem.creationDate = DateTime.fromFormat(date, format);
|
||||||
}
|
}
|
||||||
if(match?.groups?.version){
|
if (match?.groups?.version) {
|
||||||
elem.haVersion = match.groups.version
|
elem.haVersion = match.groups.version;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return backups;
|
return backups;
|
||||||
@ -224,13 +246,14 @@ function parseXmlBackupData(body: string, config: WebdavConfig) {
|
|||||||
return backups;
|
return backups;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function webdabUploadFile(
|
export function webdavUploadFile(
|
||||||
localPath: string,
|
localPath: string,
|
||||||
webdavPath: string,
|
webdavPath: string,
|
||||||
config: WebdavConfig
|
config: WebdavConfig
|
||||||
) {
|
) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
logger.info(`Uploading ${localPath} to webdav...`);
|
logger.info(`Uploading ${localPath} to webdav...`);
|
||||||
|
|
||||||
const stats = statSync(localPath);
|
const stats = statSync(localPath);
|
||||||
const stream = createReadStream(localPath);
|
const stream = createReadStream(localPath);
|
||||||
const options = {
|
const options = {
|
||||||
@ -251,6 +274,9 @@ export function webdabUploadFile(
|
|||||||
logger.debug(`...URI: ${encodeURI(url)}`);
|
logger.debug(`...URI: ${encodeURI(url)}`);
|
||||||
logger.debug(`...rejectUnauthorized: ${options.https?.rejectUnauthorized}`);
|
logger.debug(`...rejectUnauthorized: ${options.https?.rejectUnauthorized}`);
|
||||||
const status = statusTools.getStatus();
|
const status = statusTools.getStatus();
|
||||||
|
status.status = States.BKUP_UPLOAD_CLOUD;
|
||||||
|
status.progress = 0;
|
||||||
|
statusTools.setStatus(status);
|
||||||
got.stream
|
got.stream
|
||||||
.put(encodeURI(url), options)
|
.put(encodeURI(url), options)
|
||||||
.on("uploadProgress", (e) => {
|
.on("uploadProgress", (e) => {
|
||||||
@ -263,16 +289,19 @@ export function webdabUploadFile(
|
|||||||
logger.info("Upload done...");
|
logger.info("Upload done...");
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.on("response", (res) => {
|
.on("response", (res: PlainResponse) => {
|
||||||
|
const status = statusTools.getStatus();
|
||||||
|
status.status = States.IDLE;
|
||||||
|
status.progress = undefined;
|
||||||
|
statusTools.setStatus(status);
|
||||||
if (res.statusCode != 201 && res.statusCode != 204) {
|
if (res.statusCode != 201 && res.statusCode != 204) {
|
||||||
messageManager.error(
|
messageManager.error(
|
||||||
"Fail to upload file to webdav.",
|
"Fail to upload file to Cloud.",
|
||||||
`Status code: ${res.statusCode}`
|
`Code: ${res.statusCode} Body: ${res.body}`
|
||||||
);
|
);
|
||||||
logger.error(
|
logger.error(`Fail to upload file to Cloud`);
|
||||||
`Fail to upload file to webdav, Status code: ${res.statusCode}`
|
logger.error(`Code: ${res.statusCode}`);
|
||||||
);
|
logger.error(`Body: ${res.body}`);
|
||||||
logger.error(status.message);
|
|
||||||
unlinkSync(localPath);
|
unlinkSync(localPath);
|
||||||
reject(res);
|
reject(res);
|
||||||
} else {
|
} else {
|
||||||
@ -281,10 +310,16 @@ export function webdabUploadFile(
|
|||||||
resolve(undefined);
|
resolve(undefined);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.on("error", (err) => {
|
.on("error", (err: RequestError) => {
|
||||||
logger.error(status.message);
|
const status = statusTools.getStatus();
|
||||||
logger.error(err.stack);
|
status.status = States.IDLE;
|
||||||
reject(status.message);
|
status.progress = undefined;
|
||||||
|
statusTools.setStatus(status);
|
||||||
|
messageManager.error("Fail to upload backup to Cloud", err.message);
|
||||||
|
logger.error("Fail to upload backup to Cloud");
|
||||||
|
logger.error(err);
|
||||||
|
unlinkSync(localPath);
|
||||||
|
reject(err);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user