salix/modules/route/back/methods/cmr/downloadZip.js

58 lines
1.7 KiB
JavaScript
Raw Normal View History

2023-09-01 10:10:39 +00:00
const JSZip = require('jszip');
module.exports = Self => {
2024-11-12 07:25:23 +00:00
Self.remoteMethodCtx('downloadZip', {
2023-09-01 10:10:39 +00:00
description: 'Download a zip file with multiple cmrs pdfs',
accessType: 'READ',
accepts: [
{
arg: 'ids',
type: 'string',
description: 'The cmrs ids',
}
],
returns: [
{
arg: 'body',
type: 'file',
root: true
}, {
arg: 'Content-Type',
type: 'string',
http: {target: 'header'}
}, {
arg: 'Content-Disposition',
type: 'string',
http: {target: 'header'}
}
],
http: {
2024-11-12 07:25:23 +00:00
path: '/downloadZip',
2023-09-01 10:10:39 +00:00
verb: 'GET'
},
accessScopes: ['DEFAULT', 'read:multimedia']
2023-09-01 10:10:39 +00:00
});
2024-11-12 07:25:23 +00:00
Self.downloadZip = async function(ctx, ids, options) {
2023-09-01 10:10:39 +00:00
const models = Self.app.models;
const myOptions = {};
const zip = new JSZip();
if (typeof options == 'object')
Object.assign(myOptions, options);
const downloadAddZip = async id => {
ctx.args = ctx.args || {};
ctx.args.id = Number(id);
2024-11-12 07:25:23 +00:00
const [data] = await models.Cmr.print(ctx, myOptions);
2024-01-30 08:44:10 +00:00
zip.file(`${id}.pdf`, data, {binary: true});
};
ids = ids.split(',');
const promises = ids.map(id => downloadAddZip(id));
await Promise.all(promises);
2024-01-17 12:07:26 +00:00
const zipStream = zip.generateNodeStream({streamFiles: true});
return [zipStream, 'application/zip', `filename="cmrs.zip"`];
2023-09-01 10:10:39 +00:00
};
};