Files
cloudron-box/src/routes/branding.js
T
2023-08-02 23:02:40 +05:30

78 lines
2.4 KiB
JavaScript

'use strict';
exports = module.exports = {
getCloudronName,
setCloudronName,
getCloudronAvatar,
setCloudronAvatar,
getFooter,
setFooter,
};
const assert = require('assert'),
BoxError = require('../boxerror.js'),
branding = require('../branding.js'),
HttpError = require('connect-lastmile').HttpError,
HttpSuccess = require('connect-lastmile').HttpSuccess,
safe = require('safetydance');
async function getFooter(req, res, next) {
const [error, footer] = await safe(branding.getFooter());
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, { footer }));
}
async function setFooter(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
if (typeof req.body.footer !== 'string') return next(new HttpError(400, 'footer is required'));
const [error] = await safe(branding.setFooter(req.body.footer));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, {}));
}
async function setCloudronName(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
if (typeof req.body.name !== 'string') return next(new HttpError(400, 'name is required'));
const [error] = await safe(branding.setCloudronName(req.body.name));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, {}));
}
async function getCloudronName(req, res, next) {
const [error, name] = await safe(branding.getCloudronName());
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, { name }));
}
async function setCloudronAvatar(req, res, next) {
assert.strictEqual(typeof req.files, 'object');
if (!req.files.avatar) return next(new HttpError(400, 'avatar must be provided'));
const avatar = safe.fs.readFileSync(req.files.avatar.path);
if (!avatar) return next(500, safe.error.message);
const [error] = await safe(branding.setCloudronAvatar(avatar));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, {}));
}
async function getCloudronAvatar(req, res, next) {
const [error, avatar] = await safe(branding.getCloudronAvatar());
if (error) return next(BoxError.toHttpError(error));
// avoid caching the avatar on the client to see avatar changes immediately
res.set('Cache-Control', 'no-cache');
res.set('Content-Type', 'image/png');
res.status(200).send(avatar);
}