2023-08-03 14:26:41 +05:30
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
exports = module.exports = {
|
|
|
|
|
getAutoupdatePattern,
|
|
|
|
|
setAutoupdatePattern,
|
|
|
|
|
|
2025-06-26 15:19:28 +02:00
|
|
|
getBoxUpdate,
|
|
|
|
|
checkBoxUpdate,
|
|
|
|
|
|
|
|
|
|
updateBox,
|
2023-08-03 14:26:41 +05:30
|
|
|
};
|
|
|
|
|
|
2025-08-14 11:17:38 +05:30
|
|
|
const assert = require('node:assert'),
|
2023-08-03 14:26:41 +05:30
|
|
|
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,
|
2023-08-03 14:26:41 +05:30
|
|
|
safe = require('safetydance'),
|
2025-06-26 13:41:09 +02:00
|
|
|
updater = require('../updater.js');
|
2023-08-03 14:26:41 +05:30
|
|
|
|
|
|
|
|
async function getAutoupdatePattern(req, res, next) {
|
|
|
|
|
const [error, pattern] = await safe(updater.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(updater.setAutoupdatePattern(req.body.pattern));
|
|
|
|
|
if (error) return next(BoxError.toHttpError(error));
|
|
|
|
|
|
|
|
|
|
next(new HttpSuccess(200, {}));
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-26 15:19:28 +02:00
|
|
|
async function updateBox(req, res, next) {
|
2023-08-03 14:26:41 +05:30
|
|
|
if ('skipBackup' in req.body && typeof req.body.skipBackup !== 'boolean') return next(new HttpError(400, 'skipBackup must be a boolean'));
|
|
|
|
|
|
|
|
|
|
// this only initiates the update, progress can be checked via the progress route
|
2025-06-20 19:04:55 +02:00
|
|
|
const [error, taskId] = await safe(updater.startBoxUpdateTask(req.body, AuditSource.fromRequest(req)));
|
2023-08-03 14:26:41 +05:30
|
|
|
if (error && error.reason === BoxError.NOT_FOUND) return next(new HttpError(422, error.message));
|
|
|
|
|
if (error && error.reason === BoxError.BAD_STATE) return next(new HttpError(409, error.message));
|
|
|
|
|
if (error) return next(new HttpError(500, error));
|
|
|
|
|
|
|
|
|
|
next(new HttpSuccess(202, { taskId }));
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-26 15:19:28 +02:00
|
|
|
async function getBoxUpdate(req, res, next) {
|
|
|
|
|
const [error, boxUpdateInfo] = await safe(updater.getBoxUpdate());
|
|
|
|
|
if (error) return next(new HttpError(500, error));
|
|
|
|
|
next(new HttpSuccess(200, { update: boxUpdateInfo }));
|
2023-08-03 14:26:41 +05:30
|
|
|
}
|
|
|
|
|
|
2025-06-26 15:19:28 +02:00
|
|
|
async function checkBoxUpdate(req, res, next) {
|
2023-08-03 14:26:41 +05:30
|
|
|
// it can take a while sometimes to get all the app updates one by one
|
|
|
|
|
req.clearTimeout();
|
|
|
|
|
|
2025-06-26 15:19:28 +02:00
|
|
|
const [error, result ] = await safe(updater.checkBoxUpdate({ stableOnly: false }));
|
2025-06-26 13:41:09 +02:00
|
|
|
if (error) return next(BoxError.toHttpError(error));
|
|
|
|
|
|
|
|
|
|
next(new HttpSuccess(200, { update: result }));
|
2023-08-03 14:26:41 +05:30
|
|
|
}
|