3951-invoiceOut.index_downloadZip #1215

Merged
vicent merged 4 commits from 3951-invoiceOut.index_downloadZip into dev 2022-12-21 07:12:17 +00:00
2 changed files with 33 additions and 19 deletions
Showing only changes of commit 63343998d8 - Show all commits

View File

@ -9,31 +9,43 @@ module.exports = Self => {
accepts: [ accepts: [
{ {
arg: 'ids', arg: 'ids',
type: ['number'], type: 'string',
description: 'The invoice ids' description: 'The invoices ids',
}
],
returns: [
{
arg: 'body',
type: 'file',
root: true
}, {
arg: 'Content-Type',
type: 'string',
http: {target: 'header'}
}, {
arg: 'Content-Disposition',
type: 'string',
http: {target: 'header'}
} }
], ],
returns: {
arg: 'base64',
type: 'string',
root: true
},
http: { http: {
path: '/downloadZip', path: '/downloadZip',
verb: 'POST' verb: 'GET'
} }
}); });
Self.downloadZip = async function(ctx, ids, options) { Self.downloadZip = async function(ctx, ids, options) {
const models = Self.app.models; const models = Self.app.models;
const myOptions = {}; const myOptions = {};
const zipConfig = await models.ZipConfig.findOne(null, myOptions);
const zip = new JSZip();
if (typeof options == 'object') if (typeof options == 'object')
Object.assign(myOptions, options); Object.assign(myOptions, options);
const zip = new JSZip();
let totalSize = 0; let totalSize = 0;
const zipConfig = await models.ZipConfig.findOne(null, myOptions); ids = ids.split(',');
for (let id of ids) { for (let id of ids) {
if (zipConfig && totalSize > zipConfig.maxSize) throw new UserError('Files are too large'); if (zipConfig && totalSize > zipConfig.maxSize) throw new UserError('Files are too large');
const invoiceOutPdf = await models.InvoiceOut.download(ctx, id, myOptions); const invoiceOutPdf = await models.InvoiceOut.download(ctx, id, myOptions);
@ -44,8 +56,10 @@ module.exports = Self => {
totalSize += sizeInMegabytes; totalSize += sizeInMegabytes;
zip.file(fileName, body); zip.file(fileName, body);
} }
const base64 = await zip.generateAsync({type: 'base64'});
return base64; const stream = zip.generateNodeStream({streamFiles: true});
return [stream, 'application/zip', `filename="download.zip"`];
}; };
function extractFileName(str) { function extractFileName(str) {

View File

@ -29,13 +29,13 @@ export default class Controller extends Section {
window.open(url, '_blank'); window.open(url, '_blank');
} else { } else {
const invoiceOutIds = this.checked; const invoiceOutIds = this.checked;
const params = { const invoicesIds = invoiceOutIds.join(',');
ids: invoiceOutIds const serializedParams = this.$httpParamSerializer({
}; access_token: this.vnToken.token,
this.$http.post(`InvoiceOuts/downloadZip`, params) ids: invoicesIds
.then(res => { });
location.href = 'data:application/zip;base64,' + res.data; const url = `api/InvoiceOuts/downloadZip?${serializedParams}`;
}); window.open(url, '_blank');
} }
} }
} }