salix/back/methods/docuware/download.js

68 lines
2.1 KiB
JavaScript
Raw Normal View History

/* eslint max-len: ["error", { "code": 180 }]*/
2023-01-12 14:16:38 +00:00
const axios = require('axios');
const UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
Self.remoteMethod('download', {
2022-02-16 13:57:47 +00:00
description: 'Download an docuware PDF',
accessType: 'READ',
accepts: [
{
2022-02-16 13:57:47 +00:00
arg: 'id',
type: 'number',
2023-01-09 14:11:53 +00:00
description: 'The ticket id',
http: {source: 'path'}
},
{
arg: 'fileCabinet',
type: 'string',
description: 'The file cabinet'
},
{
arg: 'filter',
type: 'object',
description: 'The filter'
2022-02-16 13:57:47 +00:00
}
],
returns: [
{
arg: 'body',
type: 'file',
root: true
}, {
arg: 'Content-Type',
type: 'string',
2022-02-16 13:57:47 +00:00
http: {target: 'header'}
}, {
arg: 'Content-Disposition',
type: 'string',
2022-02-16 13:57:47 +00:00
http: {target: 'header'}
}
],
http: {
path: `/:id/download`,
2022-02-16 13:57:47 +00:00
verb: 'GET'
},
// accessScopes: ['read:multimedia']
});
Self.download = async function(id, fileCabinet, filter) {
const models = Self.app.models;
const docuwareFile = await models.Docuware.checkFile(id, fileCabinet, filter);
if (!docuwareFile) throw new UserError('The DOCUWARE PDF document does not exists');
const fileCabinetId = await Self.getFileCabinet(fileCabinet);
const options = await Self.getOptions();
2023-01-12 14:16:38 +00:00
options.headers.responseType = 'stream';
const fileName = `filename="${id}.pdf"`;
const contentType = 'application/pdf';
const downloadUri = `${options.url}/FileCabinets/${fileCabinetId}/Documents/${docuwareFile.id}/FileDownload?targetFileType=Auto&keepAnnotations=false`;
2023-01-12 14:16:38 +00:00
const stream = await axios.get(downloadUri, options.headers);
2022-02-16 13:57:47 +00:00
2023-01-12 14:16:38 +00:00
return [stream.data, contentType, fileName];
};
};