2024-12-09 21:33:26 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
exports = module.exports = {
|
|
|
|
|
load,
|
|
|
|
|
|
|
|
|
|
list,
|
|
|
|
|
get,
|
2024-12-09 23:20:44 +01:00
|
|
|
getIcon,
|
|
|
|
|
del,
|
2024-12-09 21:33:26 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const assert = require('assert'),
|
2024-12-10 10:06:52 +01:00
|
|
|
archives = require('../archives.js'),
|
2024-12-09 21:33:26 +01:00
|
|
|
AuditSource = require('../auditsource.js'),
|
|
|
|
|
BoxError = require('../boxerror.js'),
|
|
|
|
|
HttpError = require('connect-lastmile').HttpError,
|
|
|
|
|
HttpSuccess = require('connect-lastmile').HttpSuccess,
|
|
|
|
|
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.resource = result;
|
|
|
|
|
|
|
|
|
|
next();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function list(req, res, next) {
|
|
|
|
|
const page = typeof req.query.page !== 'undefined' ? parseInt(req.query.page) : 1;
|
|
|
|
|
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 !== 'undefined'? parseInt(req.query.per_page) : 25;
|
|
|
|
|
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.resource));
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-09 23:20:44 +01:00
|
|
|
async function getIcon(req, res, next) {
|
|
|
|
|
assert.strictEqual(typeof req.app, 'object');
|
|
|
|
|
|
|
|
|
|
const [error, icon] = await safe(archives.getIcon(req.params.id, { original: req.query.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');
|
|
|
|
|
|
|
|
|
|
const [error] = await safe(archives.del(req.resource.id, AuditSource.fromRequest(req)));
|
|
|
|
|
if (error) return next(BoxError.toHttpError(error));
|
|
|
|
|
|
|
|
|
|
next(new HttpSuccess(204));
|
|
|
|
|
}
|