Merge pull request '3951-invoiceOut.index_downloadZip' (!1215) from 3951-invoiceOut.index_downloadZip into dev
gitea/salix/pipeline/head This commit looks good
Details
gitea/salix/pipeline/head This commit looks good
Details
Reviewed-on: #1215 Reviewed-by: Joan Sanchez <joan@verdnatura.es>
This commit is contained in:
commit
144a1cb105
|
@ -9,31 +9,43 @@ module.exports = Self => {
|
|||
accepts: [
|
||||
{
|
||||
arg: 'ids',
|
||||
type: ['number'],
|
||||
description: 'The invoice ids'
|
||||
type: 'string',
|
||||
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: {
|
||||
path: '/downloadZip',
|
||||
verb: 'POST'
|
||||
verb: 'GET'
|
||||
}
|
||||
});
|
||||
|
||||
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 zip = new JSZip();
|
||||
let totalSize = 0;
|
||||
const zipConfig = await models.ZipConfig.findOne(null, myOptions);
|
||||
let totalSize = 0;
|
||||
ids = ids.split(',');
|
||||
|
||||
for (let id of ids) {
|
||||
if (zipConfig && totalSize > zipConfig.maxSize) throw new UserError('Files are too large');
|
||||
const invoiceOutPdf = await models.InvoiceOut.download(ctx, id, myOptions);
|
||||
|
@ -44,8 +56,10 @@ module.exports = Self => {
|
|||
totalSize += sizeInMegabytes;
|
||||
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) {
|
||||
|
|
|
@ -3,7 +3,7 @@ const UserError = require('vn-loopback/util/user-error');
|
|||
|
||||
describe('InvoiceOut downloadZip()', () => {
|
||||
const userId = 9;
|
||||
const invoiceIds = [1, 2];
|
||||
const invoiceIds = '1,2';
|
||||
const ctx = {
|
||||
req: {
|
||||
|
||||
|
|
|
@ -29,13 +29,13 @@ export default class Controller extends Section {
|
|||
window.open(url, '_blank');
|
||||
} else {
|
||||
const invoiceOutIds = this.checked;
|
||||
const params = {
|
||||
ids: invoiceOutIds
|
||||
};
|
||||
this.$http.post(`InvoiceOuts/downloadZip`, params)
|
||||
.then(res => {
|
||||
location.href = 'data:application/zip;base64,' + res.data;
|
||||
});
|
||||
const invoicesIds = invoiceOutIds.join(',');
|
||||
const serializedParams = this.$httpParamSerializer({
|
||||
access_token: this.vnToken.token,
|
||||
ids: invoicesIds
|
||||
});
|
||||
const url = `api/InvoiceOuts/downloadZip?${serializedParams}`;
|
||||
window.open(url, '_blank');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue