Files
cloudron-box/src/routes/archives.js

99 lines
3.7 KiB
JavaScript
Raw Normal View History

2024-12-09 21:33:26 +01:00
'use strict';
exports = module.exports = {
load,
list,
get,
getIcon,
del,
unarchive
2024-12-09 21:33:26 +01:00
};
2024-12-10 17:19:12 +01:00
const apps = require('../apps.js'),
assert = require('node:assert'),
archives = require('../archives.js'),
2024-12-09 21:33:26 +01:00
AuditSource = require('../auditsource.js'),
BoxError = require('../boxerror.js'),
2025-07-10 11:00:31 +02:00
HttpError = require('@cloudron/connect-lastmile').HttpError,
HttpSuccess = require('@cloudron/connect-lastmile').HttpSuccess,
2024-12-09 21:33:26 +01:00
safe = require('safetydance');
async function load(req, res, next) {
assert.strictEqual(typeof req.params.id, 'string');
const [error, result] = await safe(archives.get(req.params.id));
if (error) return next(BoxError.toHttpError(error));
if (!result) return next(new HttpError(404, 'Backup not found'));
req.resources.archive = result;
2024-12-09 21:33:26 +01:00
next();
}
async function list(req, res, next) {
const page = typeof req.query.page === 'string' ? parseInt(req.query.page) : 1;
2024-12-09 21:33:26 +01:00
if (!page || page < 0) return next(new HttpError(400, 'page query param has to be a postive number'));
const perPage = typeof req.query.per_page === 'string'? parseInt(req.query.per_page) : 25;
2024-12-09 21:33:26 +01:00
if (!perPage || perPage < 0) return next(new HttpError(400, 'per_page query param has to be a postive number'));
const [error, result] = await safe(archives.list(page, perPage));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, { archives: result }));
}
async function get(req, res, next) {
assert.strictEqual(typeof req.params.id, 'string');
next(new HttpSuccess(200, req.resources.archive));
2024-12-09 21:33:26 +01:00
}
async function getIcon(req, res, next) {
2024-12-10 11:53:29 +01:00
assert.strictEqual(typeof req.params.id, 'string');
assert.strictEqual(typeof req.resources.archive, 'object');
const original = typeof req.query.original === 'string' ? (req.query.original === '1' || req.query.original === 'true') : false;
const [error, icon] = await safe(archives.getIcon(req.params.id, { original }));
if (error) return next(BoxError.toHttpError(error));
res.send(icon);
}
2024-12-09 21:33:26 +01:00
async function del(req, res, next) {
assert.strictEqual(typeof req.params.id, 'string');
assert.strictEqual(typeof req.resources.archive, 'object');
2024-12-09 21:33:26 +01:00
const [error] = await safe(archives.del(req.resources.archive, AuditSource.fromRequest(req)));
2024-12-09 21:33:26 +01:00
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(204));
}
async function unarchive(req, res, next) {
assert.strictEqual(typeof req.params.id, 'string');
assert.strictEqual(typeof req.resources.archive, 'object');
assert.strictEqual(typeof req.body, 'object');
const data = req.body;
// required
if (typeof data.subdomain !== 'string') return next(new HttpError(400, 'subdomain is required'));
if (typeof data.domain !== 'string') return next(new HttpError(400, 'domain is required'));
// optional
if (('ports' in data) && typeof data.ports !== 'object') return next(new HttpError(400, 'ports must be an object'));
if ('secondaryDomains' in data) {
if (!data.secondaryDomains || typeof data.secondaryDomains !== 'object') return next(new HttpError(400, 'secondaryDomains must be an object'));
if (Object.keys(data.secondaryDomains).some(function (key) { return typeof data.secondaryDomains[key].domain !== 'string' || typeof data.secondaryDomains[key].subdomain !== 'string'; })) return next(new HttpError(400, 'secondaryDomain object must contain domain and subdomain strings'));
}
const [error, result] = await safe(apps.unarchive(req.resources.archive, req.body, AuditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202, { id: result.id, taskId: result.taskId }));
}