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

58 lines
1.7 KiB
JavaScript

const JSZip = require('jszip');
module.exports = Self => {
Self.remoteMethodCtx('downloadZip', {
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: {
path: '/downloadZip',
verb: 'GET'
},
accessScopes: ['DEFAULT', 'read:multimedia']
});
Self.downloadZip = async function(ctx, ids, options) {
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);
const [data] = await models.Cmr.print(ctx, myOptions);
zip.file(`${id}.pdf`, data, {binary: true});
};
ids = ids.split(',');
const promises = ids.map(id => downloadAddZip(id));
await Promise.all(promises);
const zipStream = zip.generateNodeStream({streamFiles: true});
return [zipStream, 'application/zip', `filename="cmrs.zip"`];
};
};