fleet/ee/bulk-operations-dashboard/api/controllers/scripts/download-script.js
Eric debb2d1790
Add app to manage scripts and profiles. (#21450)
Related to: #20296 

Changes:
- Added `ee/bulk-operations-dashboard`, a Sails.js app that lets users
manage configuration profiles and scripts across multiple teams on a
Fleet instance.
- Added a Github workflow to deploy the app to Heroku
- Added a Github workflow to test changes to the bulk operations
dashboard.
2024-08-22 14:59:15 -06:00

76 lines
2 KiB
JavaScript

module.exports = {
friendlyName: 'Download script',
description: 'Download script file (returning a stream).',
inputs: {
fleetApid: {
type: 'number',
description: 'The Fleet API ID of the script to download.',
},
id: {
type: 'number',
description: 'The database ID of the undeployed script to download'
}
},
exits: {
success: {
outputFriendlyName: 'File',
outputDescription: 'The streaming bytes of the file.',
outputType: 'ref'
},
notFound: {
description: 'No script exists with the specified ID or UUID.',
responseType: 'notFound'
},
},
fn: async function ({fleetApid, id}) {
if(!fleetApid && !id){
return this.res.badRequest();
}
let filename;
let download;
if(id){
let datePrefix = new Date().toISOString();
datePrefix = datePrefix.split('T')[0] +'_';
let scriptToDownload = await UndeployedScript.findOne({id: id});
filename = datePrefix + scriptToDownload.name;
let scriptContents = scriptToDownload.scriptContents;
if(scriptToDownload.scriptType === '.sh'){
this.res.type('application/x-apple-aspen-config');
} else {
this.res.type('application/octet-stream');
}
download = scriptContents;
} else {
let scriptDownloadResponse = await sails.helpers.http.sendHttpRequest.with({
method: 'GET',
url: `${sails.config.custom.fleetBaseUrl}/api/v1/fleet/scripts/${fleetApid}?alt=media`,
headers: {
Authorization: `Bearer ${sails.config.custom.fleetApiToken}`
}
});
let contentDispositionHeader = scriptDownloadResponse.headers['content-disposition'];
let filenameMatch = contentDispositionHeader.replace(/^attachment;filename="(.+?)"$/, '$1');
filename = filenameMatch;
let contentType = scriptDownloadResponse.headers['content-type'];
download = scriptDownloadResponse.body;
this.res.type(contentType);
}
this.res.attachment(filename);
// All done.
return download;
}
};