salix/back/methods/docuware/download.js

62 lines
2.0 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.remoteMethodCtx('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',
2023-01-09 14:11:53 +00:00
description: 'The file cabinet',
http: {source: 'path'}
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/:fileCabinet`,
2022-02-16 13:57:47 +00:00
verb: 'GET'
}
});
Self.download = async function(ctx, id, fileCabinet) {
const models = Self.app.models;
const docuwareFile = await models.Docuware.checkFile(ctx, id, fileCabinet);
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];
};
};