Compare commits

..

3 Commits

Author SHA1 Message Date
8287bdeedc
Fix compilation errors 2024-07-11 17:07:32 +02:00
d1fc44be60
User the good error type 2024-07-11 16:52:14 +02:00
bdb5148e0b
Eslint 2024-07-11 15:47:27 +02:00
20 changed files with 527 additions and 614 deletions

View File

@ -1,6 +0,0 @@
module.exports = {
extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended'],
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint'],
root: true,
};

View File

@ -0,0 +1,20 @@
// @ts-check
import eslint from '@eslint/js';
import tseslint from 'typescript-eslint';
export default tseslint.config(
eslint.configs.recommended,
...tseslint.configs.recommendedTypeChecked,
{
languageOptions: {
parserOptions: {
project: true,
tsconfigRootDir: import.meta.dirname,
}
}
},
{
ignores: ["dist/", "eslint.config.js"]
}
);

View File

@ -7,12 +7,11 @@
"build:watch": "tsc -w",
"build": "tsc --build --verbose",
"dev": "pnpm build && concurrently -n tsc,node \"pnpm build:watch\" \"pnpm serve:watch\"",
"lint": "tsc --noEmit && eslint \"**/*.{js,ts}\" --quiet --fix",
"lint": "tsc --noEmit && eslint --quiet --fix",
"serve:watch": "nodemon dist/server.js",
"serve": "node dist/server.js"
},
"dependencies": {
"@typescript-eslint/eslint-plugin": "^7.0.1",
"app-root-path": "3.1.0",
"cookie-parser": "1.4.6",
"cors": "^2.8.5",
@ -35,23 +34,24 @@
},
"packageManager": "pnpm@8.15.3",
"devDependencies": {
"@eslint/js": "^9.6.0",
"@tsconfig/recommended": "^1.0.3",
"@types/cookie-parser": "^1.4.6",
"@types/cors": "^2.8.17",
"@types/cron": "^2.4.0",
"@types/errorhandler": "^1.5.3",
"@types/eslint__js": "^8.42.3",
"@types/express": "^4.17.21",
"@types/figlet": "^1.5.8",
"@types/http-errors": "^2.0.4",
"@types/luxon": "^3.4.2",
"@types/morgan": "^1.9.9",
"@types/node": "^20.11.19",
"@typescript-eslint/parser": "^7.0.1",
"concurrently": "8.2.2",
"dotenv": "^16.4.4",
"eslint": "8.56.0",
"eslint": "^9.6.0",
"nodemon": "^3.0.3",
"ts-node": "^10.9.2",
"typescript": "^5.3.3"
"typescript": "^5.3.3",
"typescript-eslint": "8.0.0-alpha.41"
}
}
}

File diff suppressed because it is too large Load Diff

View File

@ -1,31 +1,26 @@
import cookieParser from "cookie-parser";
import express, { type NextFunction, type Request, type Response } from "express";
import cors from "cors";
import errorHandler from "errorhandler";
import express from "express";
import createError from "http-errors";
import morgan from "morgan";
import path from "path";
import { fileURLToPath } from "url";
import logger from "./config/winston.js";
import apiV2Router from "./routes/apiV2.js";
import cors from "cors"
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const app = express();
app.use(cors({
origin: true
}))
app.use(
cors({
origin: true,
})
);
app.set("port", process.env.PORT || 3000);
// app.use(
// logger("dev", {
// skip: function (req, res) {
// return (res.statusCode = 304);
// },
// })
// );
app.use(
morgan("dev", { stream: { write: (message) => logger.debug(message) } })
);
@ -46,15 +41,9 @@ app.use((req, res, next) => {
});
// error handler
app.use((err: any, req: Request, res: Response, next: NextFunction) => {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get("env") === "development" ? err : {};
// render the error page
res.status(err.status || 500);
res.render("error");
});
if (process.env.NODE_ENV === "development") {
// only use in development
app.use(errorHandler());
}
export default app;

View File

@ -1,10 +1,12 @@
import { existsSync, mkdirSync } from "fs";
import logger from "./config/winston.js";
import * as homeAssistantService from "./services/homeAssistantService.js";
import * as settingsTools from "./tools/settingsTools.js";
import * as statusTools from "./tools/status.js";
import kleur from "kleur";
import { checkWebdavLogin, createBackupFolder } from "./services/webdavService.js";
import {
checkWebdavLogin,
createBackupFolder,
} from "./services/webdavService.js";
import {
getWebdavConfig,
validateWebdavConfig,
@ -59,16 +61,15 @@ function postInit() {
}
);
},
(reason) => {
(reason: Error) => {
logger.error("Webdav config: " + kleur.red().bold("FAIL !"));
logger.error(reason);
messageManager.error("Invalid webdav config", reason.message);
}
);
settingsTools.check(settingsTools.getSettings(), true);
// settingsTools.check(settingsTools.getSettings(), true);
// cronTools.init();
}
export default postInit;

View File

@ -10,7 +10,7 @@ actionRouter.post("/backup", (req, res) => {
.then(() => {
logger.info("All good !");
})
.catch((reason) => {
.catch(() => {
logger.error("Something wrong !");
});
res.statusCode = 200;

View File

@ -4,45 +4,56 @@ import {
saveBackupConfig,
validateBackupConfig,
} from "../services/backupConfigService.js";
import { getWebdavConfig, saveWebdavConfig, validateWebdavConfig } from "../services/webdavConfigService.js";
import {
getWebdavConfig,
saveWebdavConfig,
validateWebdavConfig,
} from "../services/webdavConfigService.js";
import { checkWebdavLogin } from "../services/webdavService.js";
import type { BackupConfig } from "../types/services/backupConfig.js";
import type { ValidationError } from "joi";
import type { WebdavConfig } from "../types/services/webdavConfig.js";
const configRouter = express.Router();
configRouter.get("/backup", (req, res, next) => {
configRouter.get("/backup", (req, res) => {
res.json(getBackupConfig());
});
configRouter.put("/backup", (req, res, next) => {
validateBackupConfig(req.body)
configRouter.put("/backup", (req, res) => {
validateBackupConfig(req.body as BackupConfig)
.then(() => {
saveBackupConfig(req.body);
saveBackupConfig(req.body as BackupConfig);
res.status(204);
res.send();
})
.catch((error) => {
.catch((error: ValidationError) => {
res.status(400);
res.json(error.details);
});
});
configRouter.get("/webdav", (req, res, next) => {
configRouter.get("/webdav", (req, res) => {
res.json(getWebdavConfig());
});
configRouter.put("/webdav", (req, res, next) => {
validateWebdavConfig(req.body)
configRouter.put("/webdav", (req, res) => {
validateWebdavConfig(req.body as WebdavConfig)
.then(() => {
return checkWebdavLogin(req.body, true)
return checkWebdavLogin(req.body as WebdavConfig, true);
})
.then(() => {
saveWebdavConfig(req.body);
saveWebdavConfig(req.body as WebdavConfig);
res.status(204);
res.send();
})
.catch((error) => {
.catch((error: ValidationError) => {
res.status(400);
res.json(error.details ? error.details : error);
if (error.details) {
res.json(error.details);
} else {
res.json(error);
}
});
});

View File

@ -3,11 +3,15 @@ import * as haOsService from "../services/homeAssistantService.js";
const homeAssistantRouter = express.Router();
homeAssistantRouter.get("/backups/", (req, res, next) => {
homeAssistantRouter.get("/backups/", (req, res) => {
haOsService
.getBackups()
.then((value) => {
res.json(value.body.data.backups.sort((a, b)=> Date.parse(b.date) - Date.parse(a.date)));
res.json(
value.body.data.backups.sort(
(a, b) => Date.parse(b.date) - Date.parse(a.date)
)
);
})
.catch((reason) => {
res.status(500);
@ -15,7 +19,7 @@ homeAssistantRouter.get("/backups/", (req, res, next) => {
});
});
homeAssistantRouter.get("/backup/:slug", (req, res, next) => {
homeAssistantRouter.get("/backup/:slug", (req, res) => {
haOsService
.getBackupInfo(req.params.slug)
.then((value) => {
@ -27,7 +31,7 @@ homeAssistantRouter.get("/backup/:slug", (req, res, next) => {
});
});
homeAssistantRouter.get("/addons", (req, res, next) => {
homeAssistantRouter.get("/addons", (req, res) => {
haOsService
.getAddonList()
.then((value) => {
@ -39,7 +43,7 @@ homeAssistantRouter.get("/addons", (req, res, next) => {
});
});
homeAssistantRouter.get("/folders", (req, res, next) => {
homeAssistantRouter.get("/folders", (req, res) => {
res.json(haOsService.getFolderList());
});

View File

@ -12,7 +12,7 @@ import { WebdavDeleteValidation } from "../types/services/webdavValidation.js";
const webdavRouter = express.Router();
webdavRouter.get("/backup/auto", (req, res, next) => {
webdavRouter.get("/backup/auto", (req, res) => {
const config = getWebdavConfig();
const backupConf = getBackupConfig();
validateWebdavConfig(config)
@ -20,8 +20,11 @@ webdavRouter.get("/backup/auto", (req, res, next) => {
return webdavService.checkWebdavLogin(config);
})
.then(async () => {
const value = await webdavService
.getBackups(pathTools.auto, config, backupConf.nameTemplate);
const value = await webdavService.getBackups(
pathTools.auto,
config,
backupConf.nameTemplate
);
res.json(value);
})
.catch((reason) => {
@ -30,7 +33,7 @@ webdavRouter.get("/backup/auto", (req, res, next) => {
});
});
webdavRouter.get("/backup/manual", (req, res, next) => {
webdavRouter.get("/backup/manual", (req, res) => {
const config = getWebdavConfig();
const backupConf = getBackupConfig();
validateWebdavConfig(config)
@ -38,8 +41,11 @@ webdavRouter.get("/backup/manual", (req, res, next) => {
return webdavService.checkWebdavLogin(config);
})
.then(async () => {
const value = await webdavService
.getBackups(pathTools.manual, config, backupConf.nameTemplate);
const value = await webdavService.getBackups(
pathTools.manual,
config,
backupConf.nameTemplate
);
res.json(value);
})
.catch((reason) => {
@ -48,28 +54,30 @@ webdavRouter.get("/backup/manual", (req, res, next) => {
});
});
webdavRouter.delete("/", (req, res, next) => {
const body: WebdavDelete = req.body;
webdavRouter.delete("/", (req, res) => {
const body = req.body as WebdavDelete;
const validator = Joi.object(WebdavDeleteValidation);
const config = getWebdavConfig();
validateWebdavConfig(config).then(() => {
validator
.validateAsync(body)
.then(() => {
return webdavService.checkWebdavLogin(config);
})
.then(() => {
webdavService.deleteBackup(body.path, config)
.then(()=>{
res.status(201).send();
}).catch((reason)=>{
res.status(500).json(reason);
});
})
.catch((reason) => {
res.status(400).json(reason);
});
});
validateWebdavConfig(config)
.then(() => {
return validator.validateAsync(body);
})
.then(() => {
return webdavService.checkWebdavLogin(config);
})
.then(() => {
webdavService
.deleteBackup(body.path, config)
.then(() => {
res.status(201).send();
})
.catch((reason) => {
res.status(500).json(reason);
});
})
.catch((reason) => {
res.status(400).json(reason);
});
});
export default webdavRouter;

View File

@ -4,27 +4,29 @@ import app from "./app.js";
import logger from "./config/winston.js";
import postInit from "./postInit.js";
import figlet from "figlet";
import kleur from 'kleur';
import kleur from "kleur";
/**
* Error Handler. Provides full stack
*/
if (process.env.NODE_ENV === "development") {
app.use(errorHandler());
app.use(errorHandler());
}
/**
* Start Express server.
*/
const server = app.listen(app.get("port"), () => {
console.log(kleur.yellow().bold(figlet.textSync("NC Backup")))
logger.info(
`App is running at ` + kleur.green().bold(`http://localhost:${app.get("port")}`) + " in " + kleur.green().bold(app.get("env")) + " mode"
);
logger.info(kleur.red().bold("Press CTRL-C to stop"));
postInit();
console.log(kleur.yellow().bold(figlet.textSync("NC Backup")));
logger.info(
`App is running at ` +
kleur.green().bold(`http://localhost:${app.get("port")}`) +
" in " +
kleur.green().bold(app.get("env") as string) +
" mode"
);
logger.info(kleur.red().bold("Press CTRL-C to stop"));
postInit();
});
export default server;
export default server;

View File

@ -29,7 +29,9 @@ export function getBackupConfig(): BackupConfig {
saveBackupConfig(defaultConfig);
return defaultConfig;
} else {
return JSON.parse(fs.readFileSync(backupConfigPath).toString());
return JSON.parse(
fs.readFileSync(backupConfigPath).toString()
) as BackupConfig;
}
}

View File

@ -5,27 +5,26 @@ import got, {
RequestError,
type OptionsOfJSONResponseBody,
type PlainResponse,
type Progress,
type Response,
} from "got";
import { DateTime } from "luxon";
import stream from "stream";
import { promisify } from "util";
import logger from "../config/winston.js";
import messageManager from "../tools/messageManager.js";
import * as settingsTools from "../tools/settingsTools.js";
import * as statusTools from "../tools/status.js";
import { BackupType } from "../types/services/backupConfig.js";
import type { NewBackupPayload } from "../types/services/ha_os_payload.js";
import type {
AddonData,
AddonModel,
BackupData,
BackupDetailModel,
BackupModel,
CoreInfoBody,
SupervisorResponse,
} from "../types/services/ha_os_response.js";
import { States, type Status } from "../types/status.js";
import { DateTime } from "luxon";
import { BackupType } from "../types/services/backupConfig.js";
import { States } from "../types/status.js";
const pipeline = promisify(stream.pipeline);
@ -44,7 +43,7 @@ function getVersion(): Promise<Response<SupervisorResponse<CoreInfoBody>>> {
(result) => {
return result;
},
(error) => {
(error: Error) => {
messageManager.error(
"Fail to fetch Home Assistant version",
error?.message
@ -73,7 +72,7 @@ function getAddonList(): Promise<Response<SupervisorResponse<AddonData>>> {
});
return result;
},
(error) => {
(error: RequestError) => {
messageManager.error("Fail to fetch addons list", error?.message);
logger.error(`Fail to fetch addons list (${error?.message})`);
logger.error(error);
@ -98,7 +97,7 @@ function getBackups(): Promise<Response<SupervisorResponse<BackupData>>> {
statusTools.setStatus(status);
return result;
},
(error) => {
(error: RequestError) => {
const status = statusTools.getStatus();
status.hass.ok = false;
status.hass.last_check = DateTime.now();
@ -127,7 +126,7 @@ function downloadSnapshot(id: string): Promise<string> {
return pipeline(
got.stream
.get(`http://hassio/backups/${id}/download`, option)
.on("downloadProgress", (e) => {
.on("downloadProgress", (e: Progress) => {
const percent = Math.round(e.percent * 100) / 100;
if (status.progress !== percent) {
status.progress = percent;
@ -147,7 +146,7 @@ function downloadSnapshot(id: string): Promise<string> {
);
return tmp_file;
},
(reason) => {
(reason: RequestError) => {
fs.unlinkSync(tmp_file);
messageManager.error(
"Fail to download Home Assistant backup",
@ -170,7 +169,7 @@ function delSnap(id: string) {
(result) => {
return result;
},
(reason) => {
(reason: RequestError) => {
messageManager.error(
"Fail to delete Homme assistant backup detail.",
reason.message
@ -196,7 +195,7 @@ function getBackupInfo(id: string) {
logger.debug(`Backup size: ${result.body.data.size}`);
return result;
},
(reason) => {
(reason: RequestError) => {
messageManager.error(
"Fail to retrive Homme assistant backup detail.",
reason.message
@ -250,7 +249,7 @@ function createNewBackup(
statusTools.setStatus(status);
return result;
},
(reason) => {
(reason: RequestError) => {
messageManager.error("Fail to create new backup.", reason.message);
logger.error("Fail to create new backup");
logger.error(reason);
@ -263,19 +262,15 @@ function createNewBackup(
);
}
function clean(backups: BackupModel[]) {
function clean(backups: BackupModel[], numberToKeep: number) {
const promises = [];
let limit = settingsTools.getSettings().auto_clean_local_keep;
if (limit == null) {
limit = 5;
}
if (backups.length < limit) {
if (backups.length < numberToKeep) {
return;
}
backups.sort((a, b) => {
return Date.parse(b.date) - Date.parse(a.date);
});
const toDel = backups.slice(limit);
const toDel = backups.slice(numberToKeep);
for (const i of toDel) {
promises.push(delSnap(i.slug));
}
@ -284,6 +279,7 @@ function clean(backups: BackupModel[]) {
let errors = false;
for (const val of values) {
if (val.status == "rejected") {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
messageManager.error("Fail to delete backup", val.reason);
logger.error("Fail to delete backup");
logger.error(val.reason);
@ -293,7 +289,7 @@ function clean(backups: BackupModel[]) {
if (errors) {
messageManager.error("Fail to clean backups in Home Assistant");
logger.error("Fail to clean backups in Home Assistant");
return Promise.reject();
return Promise.reject(new Error());
}
});
}
@ -315,7 +311,7 @@ function uploadSnapshot(path: string) {
};
got.stream
.post(`http://hassio/backups/new/upload`, options)
.on("uploadProgress", (e) => {
.on("uploadProgress", (e: Progress) => {
const percent = e.percent;
if (status.progress !== percent) {
status.progress = percent;
@ -329,13 +325,13 @@ function uploadSnapshot(path: string) {
if (res.statusCode !== 200) {
messageManager.error(
"Fail to upload backup to Home Assistant",
`Code: ${res.statusCode} Body: ${res.body}`
`Code: ${res.statusCode} Body: ${res.body as string}`
);
logger.error("Fail to upload backup to Home Assistant");
logger.error(`Code: ${res.statusCode}`);
logger.error(`Body: ${res.body}`);
logger.error(`Body: ${res.body as string}`);
fs.unlinkSync(path);
reject(res.statusCode);
reject(new Error(res.statusCode.toString()));
} else {
logger.info(`...Upload finish ! (status: ${res.statusCode})`);
const status = statusTools.getStatus();
@ -388,17 +384,17 @@ function stopAddons(addonSlugs: string[]) {
statusTools.setStatus(status);
for (const val of values) {
if (val.status == "rejected") {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
messageManager.error("Fail to stop addon", val.reason);
logger.error("Fail to stop addon");
logger.error(val.reason);
logger.error;
errors = true;
}
}
if (errors) {
messageManager.error("Fail to stop addon");
logger.error("Fail to stop addon");
return Promise.reject();
return Promise.reject(new Error());
}
});
}
@ -428,6 +424,7 @@ function startAddons(addonSlugs: string[]) {
let errors = false;
for (const val of values) {
if (val.status == "rejected") {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
messageManager.error("Fail to start addon", val.reason);
logger.error("Fail to start addon");
logger.error(val.reason);
@ -437,7 +434,7 @@ function startAddons(addonSlugs: string[]) {
if (errors) {
messageManager.error("Fail to start addon");
logger.error("Fail to start addon");
return Promise.reject();
return Promise.reject(new Error());
}
});
}
@ -467,7 +464,7 @@ export function getFolderList() {
];
}
function publish_state(state: Status) {
function publish_state() {
// let data_error_sensor = {
// state: state.status == "error" ? "on" : "off",
// attributes: {
@ -532,15 +529,15 @@ function publish_state(state: Status) {
}
export {
getVersion,
getAddonList,
getBackups,
downloadSnapshot,
createNewBackup,
uploadSnapshot,
stopAddons,
startAddons,
clean,
publish_state,
createNewBackup,
downloadSnapshot,
getAddonList,
getBackupInfo,
getBackups,
getVersion,
publish_state,
startAddons,
stopAddons,
uploadSnapshot,
};

View File

@ -1,15 +1,15 @@
import { unlinkSync } from "fs";
import { DateTime } from "luxon";
import logger from "../config/winston.js";
import messageManager from "../tools/messageManager.js";
import * as statusTools from "../tools/status.js";
import { BackupType } from "../types/services/backupConfig.js";
import type { AddonModel } from "../types/services/ha_os_response.js";
import { WorkflowType } from "../types/services/orchecstrator.js";
import * as backupConfigService from "./backupConfigService.js";
import * as homeAssistantService from "./homeAssistantService.js";
import { getBackupFolder, getWebdavConfig } from "./webdavConfigService.js";
import * as webDavService from "./webdavService.js";
import * as statusTools from "../tools/status.js";
import { stat, unlinkSync } from "fs";
import logger from "../config/winston.js";
import { BackupType } from "../types/services/backupConfig.js";
import { DateTime } from "luxon";
import messageManager from "../tools/messageManager.js";
export function doBackupWorkflow(type: WorkflowType) {
let name = "";
@ -38,7 +38,7 @@ export function doBackupWorkflow(type: WorkflowType) {
.then(() => {
return homeAssistantService.stopAddons(addonsToStartStop);
})
.then((response) => {
.then(() => {
if (backupConfig.backupType == BackupType.FULL) {
return homeAssistantService.createNewBackup(
name,
@ -66,21 +66,23 @@ export function doBackupWorkflow(type: WorkflowType) {
}
})
.then((response) => {
response.body.data.slug;
return homeAssistantService.downloadSnapshot(response.body.data.slug);
})
.then((tmpFile) => {
tmpBackupFile = tmpFile;
return webDavService.chunkedUpload(
tmpFile,
getBackupFolder(type, webdavConfig) + name,
webdavConfig
);
// return webDavService.webdavUploadFile(
// tmpFile,
// getBackupFolder(type, webdavConfig) + name,
// webdavConfig
// );
if (webdavConfig.chunckedUpload) {
return webDavService.chunkedUpload(
tmpFile,
getBackupFolder(type, webdavConfig) + name,
webdavConfig
);
} else {
return webDavService.webdavUploadFile(
tmpFile,
getBackupFolder(type, webdavConfig) + name,
webdavConfig
);
}
})
.then(() => {
logger.info("Backup workflow finished successfully !");
@ -96,7 +98,7 @@ export function doBackupWorkflow(type: WorkflowType) {
if (tmpBackupFile != "") {
unlinkSync(tmpBackupFile);
}
return Promise.reject();
return Promise.reject(new Error());
});
}

View File

@ -1,16 +1,14 @@
import fs from "fs";
import Joi from "joi";
import logger from "../config/winston.js";
import * as pathTools from "../tools/pathTools.js";
import { default_root } from "../tools/pathTools.js";
import { WorkflowType } from "../types/services/orchecstrator.js";
import {
type WebdavConfig,
WebdavEndpointType,
} from "../types/services/webdavConfig.js";
import WebdavConfigValidation from "../types/services/webdavConfigValidation.js";
import { BackupType } from "../types/services/backupConfig.js";
import * as pathTools from "../tools/pathTools.js";
import { WorkflowType } from "../types/services/orchecstrator.js";
import e from "express";
const webdavConfigPath = "/data/webdavConfigV2.json";
const NEXTCLOUD_ENDPOINT = "/remote.php/dav/files/$username";
@ -34,7 +32,9 @@ export function getWebdavConfig(): WebdavConfig {
saveWebdavConfig(defaultConfig);
return defaultConfig;
} else {
return JSON.parse(fs.readFileSync(webdavConfigPath).toString());
return JSON.parse(
fs.readFileSync(webdavConfigPath).toString()
) as WebdavConfig;
}
}

View File

@ -1,3 +1,8 @@
/* eslint-disable @typescript-eslint/no-unsafe-argument */
/* eslint-disable @typescript-eslint/no-unsafe-call */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
import { randomUUID } from "crypto";
import { XMLParser } from "fast-xml-parser";
import fs from "fs";
import got, {
@ -13,10 +18,9 @@ import * as pathTools from "../tools/pathTools.js";
import * as statusTools from "../tools/status.js";
import type { WebdavBackup } from "../types/services/webdav.js";
import type { WebdavConfig } from "../types/services/webdavConfig.js";
import { States } from "../types/status.js";
import { templateToRegexp } from "./backupConfigService.js";
import { getChunkEndpoint, getEndpoint } from "./webdavConfigService.js";
import { States } from "../types/status.js";
import { randomUUID } from "crypto";
const CHUNK_SIZE = 5 * 1024 * 1024; // 5MiB Same as desktop client
const CHUNK_NUMBER_SIZE = 5; // To add landing "0"
@ -52,9 +56,9 @@ export function checkWebdavLogin(
status.webdav.last_check = DateTime.now();
return response;
},
(reason) => {
(reason: RequestError) => {
if (!silent) {
messageManager.error("Fail to connect to Webdav", reason?.message);
messageManager.error("Fail to connect to Webdav", reason.message);
}
const status = statusTools.getStatus();
status.webdav = {
@ -90,7 +94,7 @@ export async function createBackupFolder(conf: WebdavConfig) {
status.webdav.folder_created = false;
status.webdav.last_check = DateTime.now();
statusTools.setStatus(status);
return Promise.reject(error);
return Promise.reject(error as Error);
}
}
}
@ -110,7 +114,7 @@ export async function createBackupFolder(conf: WebdavConfig) {
status.webdav.folder_created = false;
status.webdav.last_check = DateTime.now();
statusTools.setStatus(status);
return Promise.reject(error);
return Promise.reject(error as Error);
}
}
}
@ -139,7 +143,7 @@ export function getBackups(
) {
const status = statusTools.getStatus();
if (!status.webdav.logged_in && !status.webdav.folder_created) {
return Promise.reject("Not logged in");
return Promise.reject(new Error("Not logged in"));
}
const endpoint = getEndpoint(config);
return got(config.url + endpoint + config.backupDir + folder, {
@ -159,7 +163,7 @@ export function getBackups(
);
return extractBackupInfo(data, nameTemplate);
},
(reason) => {
(reason: RequestError) => {
messageManager.error(
`Fail to retrive webdav backups in ${folder} folder`
);
@ -211,11 +215,8 @@ export function deleteBackup(path: string, config: WebdavConfig) {
(response) => {
return response;
},
(reason) => {
messageManager.error(
"Fail to delete backup in webdav",
reason?.message
);
(reason: RequestError) => {
messageManager.error("Fail to delete backup in webdav", reason.message);
logger.error(`Fail to delete backup in Cloud`);
logger.error(reason);
return Promise.reject(reason);
@ -302,13 +303,13 @@ export function webdavUploadFile(
if (res.statusCode != 201 && res.statusCode != 204) {
messageManager.error(
"Fail to upload file to Cloud.",
`Code: ${res.statusCode} Body: ${res.body}`
`Code: ${res.statusCode} Body: ${res.body as string}`
);
logger.error(`Fail to upload file to Cloud`);
logger.error(`Code: ${res.statusCode}`);
logger.error(`Body: ${res.body}`);
logger.error(`Body: ${res.body as string}`);
fs.unlinkSync(localPath);
reject(res);
reject(new Error(res.statusCode.toString()));
} else {
logger.info(`...Upload finish ! (status: ${res.statusCode})`);
fs.unlinkSync(localPath);
@ -340,6 +341,10 @@ export async function chunkedUpload(
const chunkEndpoint = getChunkEndpoint(config);
const chunkedUrl = config.url + chunkEndpoint + uuid;
const finalDestination = config.url + getEndpoint(config) + webdavPath;
const status = statusTools.getStatus();
status.status = States.BKUP_UPLOAD_CLOUD;
status.progress = 0;
statusTools.setStatus(status);
try {
await initChunkedUpload(chunkedUrl, finalDestination, config);
} catch (err) {
@ -366,7 +371,7 @@ export async function chunkedUpload(
let start = 0;
let end = fileSize > CHUNK_SIZE ? CHUNK_SIZE : fileSize;
let current_size = end;
let uploadedBytes = 0;
// const uploadedBytes = 0;
let i = 0;
while (start < fileSize) {
@ -396,12 +401,12 @@ export async function chunkedUpload(
messageManager.error(
"Fail to upload file to Cloud.",
`Code: ${(error as PlainResponse).statusCode} Body: ${
(error as PlainResponse).body
(error as PlainResponse).body as string
}`
);
logger.error(`Fail to upload file to Cloud`);
logger.error(`Code: ${(error as PlainResponse).statusCode}`);
logger.error(`Body: ${(error as PlainResponse).body}`);
logger.error(`Body: ${(error as PlainResponse).body as string}`);
}
throw error;
}
@ -462,7 +467,7 @@ export function uploadChunk(
resolve(res);
} else {
logger.error(`Fail to upload chunk: ${res.statusCode}`);
reject(res);
reject(new Error(res.statusCode.toString()));
}
})
.on("error", (err) => {

View File

@ -1,213 +0,0 @@
import { CronJob } from "cron";
import fs from "fs";
import { DateTime } from "luxon";
import logger from "../config/winston.js";
import type { Settings } from "../types/settings.js";
const settingsPath = "/data/backup_conf.json";
function check_cron(conf: Settings) {
if (conf.cron_base != null) {
if (
conf.cron_base == "1" ||
conf.cron_base == "2" ||
conf.cron_base == "3"
) {
if (conf.cron_hour != null && conf.cron_hour.match(/\d\d:\d\d/)) {
if (conf.cron_base === "1") return true;
} else return false;
}
if (conf.cron_base === "2") {
return (
conf.cron_weekday != null &&
conf.cron_weekday >= 0 &&
conf.cron_weekday <= 6
);
}
if (conf.cron_base === "3") {
return (
conf.cron_month_day != null &&
conf.cron_month_day >= 1 &&
conf.cron_month_day <= 28
);
}
if (conf.cron_base === "4") {
if (conf.cron_custom != null) {
try {
// TODO Need to be destroy
new CronJob(conf.cron_custom, () => {
//Do nothing
});
return true;
} catch (e) {
return false;
}
} else return false;
}
if (conf.cron_base === "0") return true;
} else return false;
return false;
}
function check(conf: Settings, fallback = false) {
let needSave = false;
if (!check_cron(conf)) {
if (fallback) {
logger.warn("Bad value for cron settings, fallback to default ");
conf.cron_base = "0";
conf.cron_hour = "00:00";
conf.cron_weekday = 0;
conf.cron_month_day = 1;
conf.cron_custom = "5 4 * * *";
} else {
logger.error("Bad value for cron settings");
return [false, "Bad cron settings"];
}
}
if (!conf.name_template) {
if (fallback) {
logger.warn("Bad value for 'name_template', fallback to default ");
conf.name_template = "{type}-{ha_version}-{date}_{hour}";
} else {
logger.error("Bad value for 'name_template'");
return [false, "Bad value for 'name_template'"];
}
}
if (
conf.auto_clean_local_keep == undefined ||
!/^\d+$/.test(conf.auto_clean_local_keep)
) {
if (fallback) {
logger.warn("Bad value for 'auto_clean_local_keep', fallback to 5 ");
conf.auto_clean_local_keep = "5";
} else {
logger.error("Bad value for 'auto_clean_local_keep'");
return [false, "Bad value for 'auto_clean_local_keep'"];
}
}
if (
conf.auto_clean_backup_keep == undefined ||
!/^\d+$/.test(conf.auto_clean_backup_keep)
) {
if (fallback) {
logger.warn("Bad value for 'auto_clean_backup_keep', fallback to 5 ");
conf.auto_clean_backup_keep = "5";
} else {
logger.error("Bad value for 'auto_clean_backup_keep'");
return [false, "Bad value for 'auto_clean_backup_keep'"];
}
}
if (conf.auto_clean_local == undefined) {
if (fallback) {
logger.warn("Bad value for 'auto_clean_local', fallback to false ");
conf.auto_clean_local = "false";
} else {
logger.error("Bad value for 'auto_clean_local'");
return [false, "Bad value for 'auto_clean_local'"];
}
}
if (conf.auto_clean_backup == undefined) {
if (fallback) {
logger.warn("Bad value for 'auto_clean_backup', fallback to false ");
conf.auto_clean_backup = "false";
} else {
logger.error("Bad value for 'auto_clean_backup'");
return [false, "Bad value for 'auto_clean_backup'"];
}
}
if (conf.exclude_addon == undefined) {
if (fallback) {
logger.warn("Bad value for 'exclude_addon', fallback to [] ");
conf.exclude_addon = [];
} else {
logger.error("Bad value for 'exclude_addon'");
return [false, "Bad value for 'exclude_addon'"];
}
}
if (conf.exclude_folder == undefined) {
if (fallback) {
logger.warn("Bad value for 'exclude_folder', fallback to [] ");
conf.exclude_folder = [];
} else {
logger.error("Bad value for 'exclude_folder'");
return [false, "Bad value for 'exclude_folder'"];
}
}
if (conf.auto_stop_addon == undefined) {
if (fallback) {
logger.warn("Bad value for 'auto_stop_addon', fallback to [] ");
conf.auto_stop_addon = [];
} else {
logger.error("Bad value for 'auto_stop_addon'");
return [false, "Bad value for 'auto_stop_addon'"];
}
}
if (!Array.isArray(conf.exclude_folder)) {
logger.debug("exclude_folder is not array (Empty value), reset...");
conf.exclude_folder = [];
needSave = true;
}
if (!Array.isArray(conf.exclude_addon)) {
logger.debug("exclude_addon is not array (Empty value), reset...");
conf.exclude_addon = [];
needSave = true;
}
if (conf.password_protected == undefined) {
if (fallback) {
logger.warn("Bad value for 'password_protected', fallback to false ");
conf.password_protected = "false";
} else {
logger.error("Bad value for 'password_protect_value'");
return [false, "Bad value for 'password_protect_value'"];
}
}
if (conf.password_protect_value == null) {
if (fallback) {
logger.warn("Bad value for 'password_protect_value', fallback to '' ");
conf.password_protect_value = "";
} else {
logger.error("Bad value for 'password_protect_value'");
return [false, "Bad value for 'password_protect_value'"];
}
}
if (fallback || needSave) {
setSettings(conf);
}
return [true, null];
}
function getFormatedName(is_manual: boolean, ha_version: string) {
const setting = getSettings();
let template = setting.name_template;
template = template.replace("{type_low}", is_manual ? "manual" : "auto");
template = template.replace("{type}", is_manual ? "Manual" : "Auto");
template = template.replace("{ha_version}", ha_version);
const now = DateTime.now().setLocale("en");
template = template.replace("{hour_12}", now.toFormat("hhmma"));
template = template.replace("{hour}", now.toFormat("HHmm"));
template = template.replace("{date}", now.toFormat("yyyy-MM-dd"));
return template;
}
function getSettings() {
if (!fs.existsSync(settingsPath)) {
return {};
} else {
return JSON.parse(fs.readFileSync(settingsPath).toString());
}
}
function setSettings(settings: Settings) {
fs.writeFileSync(settingsPath, JSON.stringify(settings));
}
export { getSettings, setSettings, check, check_cron, getFormatedName };

View File

@ -1,4 +1,3 @@
import { publish_state } from "../services/homeAssistantService.js";
import { States, type Status } from "../types/status.js";
import { DateTime } from "luxon";
@ -33,6 +32,6 @@ export function setStatus(new_state: Status) {
const old_state_str = JSON.stringify(status);
if (old_state_str !== JSON.stringify(new_state)) {
status = new_state;
publish_state(status);
// publish_state(status);
}
}

View File

@ -1,7 +1,6 @@
import Joi from "joi";
import { WebdavEndpointType } from "./webdavConfig.js";
const WebdavConfigValidation = {
url: Joi.string().not().empty().uri().required().label("Url"),
username: Joi.string().not().empty().label("Username"),
@ -10,18 +9,22 @@ const WebdavConfigValidation = {
allowSelfSignedCerts: Joi.boolean().label("Allow self signed certificate"),
chunckedUpload: Joi.boolean().required().label("Chuncked upload"),
webdavEndpoint: Joi.object({
type: Joi.string().valid(WebdavEndpointType.CUSTOM, WebdavEndpointType.NEXTCLOUD).required(),
type: Joi.string()
.valid(WebdavEndpointType.CUSTOM, WebdavEndpointType.NEXTCLOUD)
.required(),
customEndpoint: Joi.alternatives().conditional("type", {
is: WebdavEndpointType.CUSTOM,
then: Joi.string().not().empty().required,
otherwise: Joi.disallow()
then: Joi.string().not().empty().required(),
otherwise: Joi.disallow(),
}),
customChunkEndpoint: Joi.alternatives().conditional("type", {
is: WebdavEndpointType.CUSTOM,
then: Joi.string().not().empty().required,
otherwise: Joi.disallow()
})
}).required().label("Webdav endpoint"),
}
then: Joi.string().not().empty().required(),
otherwise: Joi.disallow(),
}),
})
.required()
.label("Webdav endpoint"),
};
export default WebdavConfigValidation;
export default WebdavConfigValidation;

View File

@ -3,12 +3,12 @@
"compilerOptions": {
"allowJs": true,
"outDir": "./dist",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"target": "es6",
"module": "nodenext",
"moduleResolution": "nodenext",
"target": "ES2022",
"sourceMap": true,
"verbatimModuleSyntax": true,
"strict": true,
"strict": true
},
"include": ["src/**/*"]
}
}