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'
|
2024-02-26 05:57:17 +00:00
|
|
|
},
|
2024-04-19 08:49:57 +00:00
|
|
|
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);
|
|
|
|
|
2024-06-17 11:03:02 +00:00
|
|
|
const downloadAddZip = async id => {
|
2024-01-23 10:05:39 +00:00
|
|
|
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});
|
2024-06-17 11:03:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
};
|
|
|
|
};
|