2022-02-21 07:48:53 +00:00
|
|
|
/* eslint max-len: ["error", { "code": 180 }]*/
|
2022-02-15 15:48:37 +00:00
|
|
|
const got = require('got');
|
2023-01-04 14:03:45 +00:00
|
|
|
const axios = require('axios');
|
2022-02-15 15:48:37 +00:00
|
|
|
const UserError = require('vn-loopback/util/user-error');
|
2023-01-04 14:03:45 +00:00
|
|
|
const FormData = require('form-data');
|
|
|
|
const fs = require('fs-extra');
|
2022-02-15 15:48:37 +00:00
|
|
|
|
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethodCtx('download', {
|
2022-02-16 13:57:47 +00:00
|
|
|
description: 'Download an docuware PDF',
|
2022-02-15 15:48:37 +00:00
|
|
|
accessType: 'READ',
|
|
|
|
accepts: [
|
|
|
|
{
|
2022-02-16 13:57:47 +00:00
|
|
|
arg: 'id',
|
2022-02-15 15:48:37 +00:00
|
|
|
type: 'number',
|
2022-02-21 07:48:53 +00:00
|
|
|
description: 'The id',
|
|
|
|
http: {source: 'path'}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'fileCabinet',
|
|
|
|
type: 'string',
|
|
|
|
description: 'The id',
|
|
|
|
http: {source: 'path'}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'dialog',
|
|
|
|
type: 'string',
|
|
|
|
description: 'The id',
|
2022-02-16 13:57:47 +00:00
|
|
|
http: {source: 'path'}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
returns: [
|
|
|
|
{
|
|
|
|
arg: 'body',
|
|
|
|
type: 'file',
|
|
|
|
root: true
|
|
|
|
}, {
|
|
|
|
arg: 'Content-Type',
|
2022-02-21 07:48:53 +00:00
|
|
|
type: 'string',
|
2022-02-16 13:57:47 +00:00
|
|
|
http: {target: 'header'}
|
|
|
|
}, {
|
|
|
|
arg: 'Content-Disposition',
|
2022-02-21 07:48:53 +00:00
|
|
|
type: 'string',
|
2022-02-16 13:57:47 +00:00
|
|
|
http: {target: 'header'}
|
|
|
|
}
|
2022-02-15 15:48:37 +00:00
|
|
|
],
|
|
|
|
http: {
|
2022-02-21 07:48:53 +00:00
|
|
|
path: `/:id/download/:fileCabinet/:dialog`,
|
2022-02-16 13:57:47 +00:00
|
|
|
verb: 'GET'
|
2022-02-15 15:48:37 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-02-21 07:48:53 +00:00
|
|
|
Self.download = async function(ctx, id, fileCabinet, dialog) {
|
|
|
|
const myUserId = ctx.req.accessToken.userId;
|
2022-02-16 13:57:47 +00:00
|
|
|
if (!myUserId)
|
2022-02-21 07:48:53 +00:00
|
|
|
throw new UserError(`You don't have enough privileges`);
|
2022-02-16 13:57:47 +00:00
|
|
|
|
2022-02-15 15:48:37 +00:00
|
|
|
const models = Self.app.models;
|
2022-02-16 13:57:47 +00:00
|
|
|
const docuwareConfig = await models.DocuwareConfig.findOne();
|
2022-02-15 15:48:37 +00:00
|
|
|
const docuwareInfo = await models.Docuware.findOne({
|
|
|
|
where: {
|
2022-02-22 14:23:23 +00:00
|
|
|
code: fileCabinet,
|
2022-02-21 07:48:53 +00:00
|
|
|
dialogName: dialog
|
2022-02-15 15:48:37 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const docuwareUrl = docuwareConfig.url;
|
|
|
|
const cookie = docuwareConfig.token;
|
|
|
|
const fileCabinetName = docuwareInfo.fileCabinetName;
|
|
|
|
const find = docuwareInfo.find;
|
|
|
|
const options = {
|
|
|
|
'headers': {
|
|
|
|
'Accept': 'application/json',
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'Cookie': cookie
|
|
|
|
}
|
|
|
|
};
|
2022-02-28 12:27:03 +00:00
|
|
|
const searchFilter = {
|
2022-02-21 07:48:53 +00:00
|
|
|
condition: [
|
|
|
|
{
|
|
|
|
DBName: find,
|
|
|
|
Value: [id]
|
|
|
|
}
|
|
|
|
]
|
2022-02-15 15:48:37 +00:00
|
|
|
};
|
|
|
|
|
2022-02-16 13:57:47 +00:00
|
|
|
try {
|
2022-02-21 07:48:53 +00:00
|
|
|
// get fileCabinetId
|
|
|
|
const fileCabinetResponse = await got.get(`${docuwareUrl}/FileCabinets`, options);
|
|
|
|
const fileCabinetJson = JSON.parse(fileCabinetResponse.body).FileCabinet;
|
|
|
|
const fileCabinetId = fileCabinetJson.find(dialogs => dialogs.Name === fileCabinetName).Id;
|
|
|
|
|
|
|
|
// get dialog
|
|
|
|
const dialogResponse = await got.get(`${docuwareUrl}/FileCabinets/${fileCabinetId}/dialogs`, options);
|
2023-01-04 14:03:45 +00:00
|
|
|
console.log(`${docuwareUrl}/FileCabinets/${fileCabinetId}/dialogs`);
|
2022-02-21 07:48:53 +00:00
|
|
|
const dialogJson = JSON.parse(dialogResponse.body).Dialog;
|
|
|
|
const dialogId = dialogJson.find(dialogs => dialogs.DisplayName === 'find').Id;
|
|
|
|
|
|
|
|
// get docuwareID
|
2022-02-28 12:27:03 +00:00
|
|
|
Object.assign(options, {'body': JSON.stringify(searchFilter)});
|
2022-02-21 07:48:53 +00:00
|
|
|
const response = await got.post(`${docuwareUrl}/FileCabinets/${fileCabinetId}/Query/DialogExpression?dialogId=${dialogId}`, options);
|
|
|
|
const docuwareId = JSON.parse(response.body).Items[0].Id;
|
|
|
|
|
|
|
|
// download & save file
|
|
|
|
const fileName = `filename="${id}.pdf"`;
|
|
|
|
const contentType = 'application/pdf';
|
|
|
|
const downloadUri = `${docuwareUrl}/FileCabinets/${fileCabinetId}/Documents/${docuwareId}/FileDownload?targetFileType=Auto&keepAnnotations=false`;
|
|
|
|
const downloadOptions = {
|
|
|
|
'headers': {
|
|
|
|
'Cookie': cookie
|
|
|
|
}
|
2022-02-16 13:57:47 +00:00
|
|
|
};
|
|
|
|
|
2023-01-04 14:03:45 +00:00
|
|
|
// const stream = got.stream(downloadUri, downloadOptions);
|
2022-02-16 13:57:47 +00:00
|
|
|
|
2023-01-04 14:03:45 +00:00
|
|
|
const uploadUri = `${docuwareUrl}/FileCabinets/${fileCabinetId}/Documents?storeDialogId=1f665772-c936-4e13-aa2a-f209b1a7070e`;
|
|
|
|
const form = new FormData();
|
|
|
|
const file = await fs.readFile('back/methods/docuware/10.pdf');
|
|
|
|
form.append('file', file, '10.pdf');
|
|
|
|
const uploadOptions = {
|
|
|
|
'headers': {
|
|
|
|
'Cookie': cookie,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const fileData = {
|
|
|
|
formData: {
|
|
|
|
'document': {
|
|
|
|
value: JSON.stringify({Fields: {}}),
|
|
|
|
options: {
|
|
|
|
filename: 'document.json',
|
|
|
|
contentType: 'application/json',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
'file[]': {
|
|
|
|
value: fs.createReadStream('back/methods/docuware/10.pdf'),
|
|
|
|
options: {
|
|
|
|
filename: '10.pdf',
|
|
|
|
contentType: null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Object.assign(uploadOptions, fileData);
|
|
|
|
try {
|
|
|
|
const upload = await axios.post(uploadUri, file, uploadOptions);
|
|
|
|
console.log('UPLOAD FINISHED');
|
|
|
|
} catch (e) {
|
|
|
|
console.log('ERROR CATCHED:', e);
|
|
|
|
console.log('ERROR CATCHED');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// return [stream, contentType, fileName];
|
|
|
|
return;
|
2022-02-16 13:57:47 +00:00
|
|
|
} catch (error) {
|
|
|
|
if (error.code === 'ENOENT')
|
|
|
|
throw new UserError('The DOCUWARE PDF document does not exists');
|
|
|
|
|
|
|
|
throw error;
|
|
|
|
}
|
2022-02-15 15:48:37 +00:00
|
|
|
};
|
|
|
|
};
|