salix/back/methods/docuware/download.js

61 lines
1.9 KiB
JavaScript
Raw Normal View History

/* eslint max-len: ["error", { "code": 180 }]*/
const got = require('got');
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();
const fileName = `filename="${id}.pdf"`;
const contentType = 'application/pdf';
const downloadUri = `${options.url}/FileCabinets/${fileCabinetId}/Documents/${docuwareFile.id}/FileDownload?targetFileType=Auto&keepAnnotations=false`;
const stream = got.stream(downloadUri, options.headers);
2022-02-16 13:57:47 +00:00
return [stream, contentType, fileName];
};
};