/* jslint node:true */ 'use strict'; exports = module.exports = { getAutoupdatePattern: getAutoupdatePattern, setAutoupdatePattern: setAutoupdatePattern, getCloudronName: getCloudronName, setCloudronName: setCloudronName, getCloudronAvatar: getCloudronAvatar, setCloudronAvatar: setCloudronAvatar, getDnsConfig: getDnsConfig, setDnsConfig: setDnsConfig, setCertificate: setCertificate }; var assert = require('assert'), HttpError = require('connect-lastmile').HttpError, HttpSuccess = require('connect-lastmile').HttpSuccess, safe = require('safetydance'), settings = require('../settings.js'), SettingsError = settings.SettingsError; function getAutoupdatePattern(req, res, next) { settings.getAutoupdatePattern(function (error, pattern) { if (error) return next(new HttpError(500, error)); next(new HttpSuccess(200, { pattern: pattern })); }); } 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')); settings.setAutoupdatePattern(req.body.pattern, function (error) { if (error && error.reason === SettingsError.BAD_FIELD) return next(new HttpError(400, 'Invalid pattern')); if (error) return next(new HttpError(500, error)); next(new HttpSuccess(200)); }); } 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')); settings.setCloudronName(req.body.name, function (error) { if (error && error.reason === SettingsError.BAD_FIELD) return next(new HttpError(400, 'Invalid name')); if (error) return next(new HttpError(500, error)); next(new HttpSuccess(200)); }); } function getCloudronName(req, res, next) { settings.getCloudronName(function (error, name) { if (error) return next(new HttpError(500, error)); next(new HttpSuccess(200, { name: name })); }); } function setCloudronAvatar(req, res, next) { assert.strictEqual(typeof req.files, 'object'); if (!req.files.avatar) return next(new HttpError(400, 'avatar must be provided')); var avatar = safe.fs.readFileSync(req.files.avatar.path); settings.setCloudronAvatar(avatar, function (error) { if (error) return next(new HttpError(500, error)); next(new HttpSuccess(202, {})); }); } function getCloudronAvatar(req, res, next) { settings.getCloudronAvatar(function (error, avatar) { if (error) return next(new HttpError(500, 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); }); } function getDnsConfig(req, res, next) { settings.getDnsConfig(function (error, config) { if (error) return next(new HttpError(500, error)); next(new HttpSuccess(200, config)); }); } function setDnsConfig(req, res, next) { assert.strictEqual(typeof req.body, 'object'); if (req.body.provider !== 'route53') return next(new HttpError(400, 'provider is required')); if (typeof req.body.accessKeyId !== 'string') return next(new HttpError(400, 'accessKeyId is required')); if (typeof req.body.secretAccessKey !== 'string') return next(new HttpError(400, 'secretAccessKey is required')); settings.setDnsConfig(req.body, function (error) { if (error && error.reason === SettingsError.BAD_FIELD) return next(new HttpError(400, error.message)); if (error) return next(new HttpError(500, error)); next(new HttpSuccess(200)); }); } function setCertificate(req, res, next) { assert.strictEqual(typeof req.files, 'object'); if (!req.files.certificate) return next(new HttpError(400, 'certificate must be provided')); var certificate = safe.fs.readFileSync(req.files.certificate.path, 'utf8'); if (!req.files.key) return next(new HttpError(400, 'key must be provided')); var key = safe.fs.readFileSync(req.files.key.path, 'utf8'); settings.setCertificate(certificate, key, function (error) { if (error) return next(new HttpError(500, error)); next(new HttpSuccess(202, {})); }); }