2021-06-19 15:47:10 +03:00
|
|
|
/**
|
|
|
|
* This file exports a function, used by the write config endpoint.
|
|
|
|
* It will make a backup of the users conf.yml file
|
|
|
|
* and then write their new config into the main conf.yml file.
|
|
|
|
* Finally, it will call a function with the status message
|
|
|
|
*/
|
|
|
|
const fsPromises = require('fs').promises;
|
2023-09-22 17:27:31 +03:00
|
|
|
const path = require('path');
|
2021-06-19 15:47:10 +03:00
|
|
|
|
|
|
|
module.exports = async (newConfig, render) => {
|
2022-05-02 00:26:55 +03:00
|
|
|
/* Either returns nothing (if using default path), or strips navigational characters from path */
|
|
|
|
const makeSafeFileName = (configObj) => {
|
|
|
|
if (!configObj || !configObj.filename) return undefined;
|
|
|
|
return configObj.filename.replaceAll('/', '').replaceAll('..', '');
|
|
|
|
};
|
|
|
|
|
2024-04-21 16:45:52 +03:00
|
|
|
// Path to config file (with navigational characters stripped)
|
2022-05-02 00:26:55 +03:00
|
|
|
const usersFileName = makeSafeFileName(newConfig);
|
|
|
|
|
2024-04-21 16:45:52 +03:00
|
|
|
// Path to user data directory
|
|
|
|
const userDataDirectory = process.env.USER_DATA_DIR || './user-data/';
|
|
|
|
|
2021-06-18 20:01:42 +03:00
|
|
|
// Define constants for the config file
|
|
|
|
const settings = {
|
2024-04-21 16:45:52 +03:00
|
|
|
defaultLocation: userDataDirectory,
|
|
|
|
backupLocation: process.env.BACKUP_DIR || path.join(userDataDirectory, 'config-backups'),
|
2021-06-18 20:01:42 +03:00
|
|
|
defaultFile: 'conf.yml',
|
|
|
|
filename: 'conf',
|
|
|
|
backupDenominator: '.backup.yml',
|
|
|
|
};
|
|
|
|
|
|
|
|
// Make the full file name and path to save the backup config file
|
2024-04-21 16:45:52 +03:00
|
|
|
const backupFilePath = `${path.normalize(settings.backupLocation)
|
2024-04-08 22:36:35 +03:00
|
|
|
}/${usersFileName || settings.filename}-`
|
2021-06-18 20:01:42 +03:00
|
|
|
+ `${Math.round(new Date() / 1000)}${settings.backupDenominator}`;
|
|
|
|
|
|
|
|
// The path where the main conf.yml should be read and saved to
|
2022-05-02 00:26:55 +03:00
|
|
|
const defaultFilePath = settings.defaultLocation + (usersFileName || settings.defaultFile);
|
2021-06-18 20:01:42 +03:00
|
|
|
|
|
|
|
// Returns a string confirming successful job
|
|
|
|
const getSuccessMessage = () => `Successfully backed up ${settings.defaultFile} to`
|
|
|
|
+ ` ${backupFilePath}, and updated the contents of ${defaultFilePath}`;
|
|
|
|
|
2021-06-19 15:47:10 +03:00
|
|
|
// Encoding options for writing to conf file
|
|
|
|
const writeFileOptions = { encoding: 'utf8' };
|
|
|
|
|
2021-06-18 20:01:42 +03:00
|
|
|
// Prepare the response returned by the API
|
|
|
|
const getRenderMessage = (success, errorMsg) => JSON.stringify({
|
|
|
|
success,
|
|
|
|
message: !success ? errorMsg : getSuccessMessage(),
|
|
|
|
});
|
|
|
|
|
2024-04-21 16:45:52 +03:00
|
|
|
// Create a backup of current config, and if backup dir doesn't yet exist, create it
|
2022-04-24 18:02:14 +03:00
|
|
|
await fsPromises
|
2024-04-21 16:45:52 +03:00
|
|
|
.mkdir(settings.backupLocation, { recursive: true })
|
|
|
|
.then(() => fsPromises.copyFile(defaultFilePath, backupFilePath))
|
|
|
|
.catch((error) => render(
|
|
|
|
getRenderMessage(false, `Unable to backup ${settings.defaultFile}: ${error}`),
|
|
|
|
));
|
2021-06-19 15:47:10 +03:00
|
|
|
|
|
|
|
// Writes the new content to the conf.yml file
|
2022-04-24 18:02:14 +03:00
|
|
|
await fsPromises
|
|
|
|
.writeFile(defaultFilePath, newConfig.config.toString(), writeFileOptions)
|
2024-04-21 16:45:52 +03:00
|
|
|
.catch((error) => render(
|
|
|
|
getRenderMessage(false, `Unable to write to ${settings.defaultFile}: ${error}`),
|
|
|
|
));
|
2021-06-18 20:01:42 +03:00
|
|
|
|
2021-06-19 15:47:10 +03:00
|
|
|
// If successful, then render hasn't yet been called- call it
|
|
|
|
await render(getRenderMessage(true));
|
2021-06-18 20:01:42 +03:00
|
|
|
};
|