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-10 14:46:30 +01:00
unarchive
2024-12-09 21:33:26 +01:00
} ;
2024-12-10 17:19:12 +01:00
const apps = require ( '../apps.js' ) ,
2025-08-14 11:17:38 +05:30
assert = require ( 'node: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' ) ,
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' ) ) ;
2025-06-10 11:02:41 +02:00
req . resources . archive = result ;
2024-12-09 21:33:26 +01:00
next ( ) ;
}
async function list ( req , res , next ) {
2025-07-13 13:14:32 +02:00
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' ) ) ;
2025-07-13 13:14:32 +02:00
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' ) ;
2025-06-10 11:02:41 +02:00
next ( new HttpSuccess ( 200 , req . resources . archive ) ) ;
2024-12-09 21:33:26 +01:00
}
2024-12-09 23:20:44 +01:00
async function getIcon ( req , res , next ) {
2024-12-10 11:53:29 +01:00
assert . strictEqual ( typeof req . params . id , 'string' ) ;
2025-06-10 11:02:41 +02:00
assert . strictEqual ( typeof req . resources . archive , 'object' ) ;
2024-12-09 23:20:44 +01:00
2025-07-13 13:14:32 +02:00
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 } ) ) ;
2024-12-09 23:20:44 +01:00
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' ) ;
2025-06-10 11:02:41 +02:00
assert . strictEqual ( typeof req . resources . archive , 'object' ) ;
2024-12-09 21:33:26 +01:00
2025-06-10 11:02:41 +02: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 ) ) ;
}
2024-12-10 14:46:30 +01:00
async function unarchive ( req , res , next ) {
assert . strictEqual ( typeof req . params . id , 'string' ) ;
2025-06-10 11:02:41 +02:00
assert . strictEqual ( typeof req . resources . archive , 'object' ) ;
2024-12-10 14:46:30 +01:00
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' ) ) ;
}
2025-06-10 11:02:41 +02:00
const [ error , result ] = await safe ( apps . unarchive ( req . resources . archive , req . body , AuditSource . fromRequest ( req ) ) ) ;
2024-12-10 14:46:30 +01:00
if ( error ) return next ( BoxError . toHttpError ( error ) ) ;
next ( new HttpSuccess ( 202 , { id : result . id , taskId : result . taskId } ) ) ;
}