Files
cloudron-box/src/routes/settings.js
T
2023-08-03 08:29:12 +05:30

203 lines
8.6 KiB
JavaScript

'use strict';
exports = module.exports = {
set,
get,
// owner only settings
setBackupConfig,
};
const assert = require('assert'),
backups = require('../backups.js'),
BoxError = require('../boxerror.js'),
docker = require('../docker.js'),
HttpError = require('connect-lastmile').HttpError,
HttpSuccess = require('connect-lastmile').HttpSuccess,
safe = require('safetydance'),
settings = require('../settings.js');
async function getAutoupdatePattern(req, res, next) {
const [error, pattern] = await safe(settings.getAutoupdatePattern());
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, { pattern }));
}
async function setAutoupdatePattern(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
if (typeof req.body.pattern !== 'string') return next(new HttpError(400, 'pattern is required'));
const [error] = await safe(settings.setAutoupdatePattern(req.body.pattern));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, {}));
}
async function getTimeZone(req, res, next) {
const [error, timeZone] = await safe(settings.getTimeZone());
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, { timeZone }));
}
async function setTimeZone(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
if (typeof req.body.timeZone !== 'string') return next(new HttpError(400, 'timeZone is required'));
const [error] = await safe(settings.setTimeZone(req.body.timeZone));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, {}));
}
async function getBackupConfig(req, res, next) {
const [error, backupConfig] = await safe(settings.getBackupConfig());
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, backups.removePrivateFields(backupConfig)));
}
async function setBackupConfig(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
if (typeof req.body.provider !== 'string') return next(new HttpError(400, 'provider is required'));
if ('password' in req.body && typeof req.body.password !== 'string') return next(new HttpError(400, 'password must be a string'));
if ('encryptedFilenames' in req.body && typeof req.body.encryptedFilenames !== 'boolean') return next(new HttpError(400, 'encryptedFilenames must be a boolean'));
if (req.body.limits) {
if (typeof req.body.limits !== 'object') return next(new HttpError(400, 'limits must be an object'));
const limits = req.body;
if ('syncConcurrency' in limits) {
if (typeof limits.syncConcurrency !== 'number') return next(new HttpError(400, 'syncConcurrency must be a positive integer'));
if (limits.syncConcurrency < 1) return next(new HttpError(400, 'syncConcurrency must be a positive integer'));
}
if ('copyConcurrency' in limits) {
if (typeof limits.copyConcurrency !== 'number') return next(new HttpError(400, 'copyConcurrency must be a positive integer'));
if (limits.copyConcurrency < 1) return next(new HttpError(400, 'copyConcurrency must be a positive integer'));
}
if ('downloadConcurrency' in limits) {
if (typeof limits.downloadConcurrency !== 'number') return next(new HttpError(400, 'downloadConcurrency must be a positive integer'));
if (limits.downloadConcurrency < 1) return next(new HttpError(400, 'downloadConcurrency must be a positive integer'));
}
if ('deleteConcurrency' in limits) {
if (typeof limits.deleteConcurrency !== 'number') return next(new HttpError(400, 'deleteConcurrency must be a positive integer'));
if (limits.deleteConcurrency < 1) return next(new HttpError(400, 'deleteConcurrency must be a positive integer'));
}
if ('uploadPartSize' in limits) {
if (typeof limits.uploadPartSize !== 'number') return next(new HttpError(400, 'uploadPartSize must be a positive integer'));
if (limits.uploadPartSize < 1) return next(new HttpError(400, 'uploadPartSize must be a positive integer'));
}
if ('memoryLimit' in limits && typeof limits.memoryLimit !== 'number') return next(new HttpError(400, 'memoryLimit must be a positive integer'));
}
if (typeof req.body.format !== 'string') return next(new HttpError(400, 'format must be a string'));
if ('acceptSelfSignedCerts' in req.body && typeof req.body.acceptSelfSignedCerts !== 'boolean') return next(new HttpError(400, 'format must be a boolean'));
if ('mountOptions' in req.body && typeof req.body.mountOptions !== 'object') return next(new HttpError(400, 'mountOptions must be a object'));
// testing the backup using put/del takes a bit of time at times
req.clearTimeout();
const [error] = await safe(settings.setBackupConfig(req.body));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, {}));
}
async function getBackupPolicy(req, res, next) {
const [error, policy] = await safe(settings.getBackupPolicy());
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, { policy }));
}
async function setBackupPolicy(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
if (typeof req.body.schedule !== 'string') return next(new HttpError(400, 'schedule is required'));
if (!req.body.retention || typeof req.body.retention !== 'object') return next(new HttpError(400, 'retention is required'));
const [error] = await safe(settings.setBackupPolicy(req.body));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, {}));
}
async function getRegistryConfig(req, res, next) {
const [error, registryConfig] = await safe(settings.getRegistryConfig());
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, docker.removePrivateFields(registryConfig)));
}
async function setRegistryConfig(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
if (!req.body.provider || typeof req.body.provider !== 'string') return next(new HttpError(400, 'provider is required'));
if (req.body.provider !== 'noop') {
if (typeof req.body.serverAddress !== 'string') return next(new HttpError(400, 'serverAddress is required'));
if ('username' in req.body && typeof req.body.username !== 'string') return next(new HttpError(400, 'username is required'));
if ('email' in req.body && typeof req.body.email !== 'string') return next(new HttpError(400, 'email is required'));
if ('password' in req.body && typeof req.body.password !== 'string') return next(new HttpError(400, 'password is required'));
}
const [error] = await safe(settings.setRegistryConfig(req.body));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200));
}
async function getLanguage(req, res, next) {
const [error, language] = await safe(settings.getLanguage());
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, { language }));
}
async function setLanguage(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
if (!req.body.language || typeof req.body.language !== 'string') return next(new HttpError(400, 'language is required'));
const [error] = await safe(settings.setLanguage(req.body.language));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, {}));
}
function get(req, res, next) {
assert.strictEqual(typeof req.params.setting, 'string');
switch (req.params.setting) {
case settings.BACKUP_POLICY_KEY: return getBackupPolicy(req, res, next);
case settings.BACKUP_CONFIG_KEY: return getBackupConfig(req, res, next);
case settings.REGISTRY_CONFIG_KEY: return getRegistryConfig(req, res, next);
case settings.LANGUAGE_KEY: return getLanguage(req, res, next);
case settings.AUTOUPDATE_PATTERN_KEY: return getAutoupdatePattern(req, res, next);
case settings.TIME_ZONE_KEY: return getTimeZone(req, res, next);
default: return next(new HttpError(404, 'No such setting'));
}
}
function set(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
switch (req.params.setting) {
case settings.BACKUP_POLICY_KEY: return setBackupPolicy(req, res, next);
case settings.REGISTRY_CONFIG_KEY: return setRegistryConfig(req, res, next);
case settings.LANGUAGE_KEY: return setLanguage(req, res, next);
case settings.AUTOUPDATE_PATTERN_KEY: return setAutoupdatePattern(req, res, next);
case settings.TIME_ZONE_KEY: return setTimeZone(req, res, next);
default: return next(new HttpError(404, 'No such setting'));
}
}