salix/modules/ticket/back/methods/boxing/getVideo.js

89 lines
2.5 KiB
JavaScript
Raw Normal View History

2022-08-03 12:57:39 +00:00
const https = require('https');
2022-08-01 05:59:04 +00:00
module.exports = Self => {
Self.remoteMethod('getVideo', {
description: 'Get packing video of ticketId',
accessType: 'READ',
accepts: [
{
arg: 'id',
type: 'number',
2022-09-05 10:06:14 +00:00
required: true,
2022-08-01 05:59:04 +00:00
description: 'Ticket id'
},
{
2022-09-05 10:06:14 +00:00
arg: 'filename',
type: 'string',
required: true,
2022-08-01 05:59:04 +00:00
description: 'Time to add'
},
{
2022-08-03 12:57:39 +00:00
arg: 'req',
type: 'object',
http: {source: 'req'}
},
{
2022-08-03 12:57:39 +00:00
arg: 'res',
type: 'object',
http: {source: 'res'}
}
],
2022-08-01 05:59:04 +00:00
http: {
path: `/getVideo`,
verb: 'GET',
},
});
2022-09-05 10:06:14 +00:00
Self.getVideo = async(id, filename, req, res, options) => {
2022-08-01 05:59:04 +00:00
const models = Self.app.models;
const myOptions = {};
2022-09-05 10:06:14 +00:00
2022-08-01 05:59:04 +00:00
if (typeof options == 'object')
Object.assign(myOptions, options);
const packingSiteConfig = await models.PackingSiteConfig.findOne({}, myOptions);
2022-09-05 10:06:14 +00:00
const query = `
SELECT
e.id,
ps.monitorId,
e.created
FROM expedition e
JOIN host h ON Convert(h.code USING utf8mb3) COLLATE utf8mb3_unicode_ci = e.hostFk
JOIN packingSite ps ON ps.hostFk = h.id
WHERE e.id = ?;`;
const [expedition] = await models.Expedition.rawSql(query, [id]);
2022-09-06 13:08:28 +00:00
const monitorId = expedition.monitorId;
2022-08-03 12:57:39 +00:00
2022-09-05 10:06:14 +00:00
const videoUrl =
`/${packingSiteConfig.shinobiToken}/videos/${packingSiteConfig.shinobiGroupKey}/${monitorId}/${filename}`;
2022-08-03 12:57:39 +00:00
const headers = Object.assign({}, req.headers, {
host: 'shinobi.verdnatura.es'
});
2022-08-03 12:57:39 +00:00
const httpOptions = {
host: 'shinobi.verdnatura.es',
2022-09-05 10:06:14 +00:00
path: videoUrl,
2022-08-03 12:57:39 +00:00
port: 443,
headers
};
2022-08-03 12:57:39 +00:00
return new Promise((resolve, reject) => {
2022-09-05 10:06:14 +00:00
const req = https.request(httpOptions, shinobiRes => {
shinobiRes.pause();
res.writeHeader(shinobiRes.statusCode, shinobiRes.headers);
shinobiRes.pipe(res);
});
2022-09-06 13:08:28 +00:00
req.on('error', () => {
reject();
2022-09-05 10:06:14 +00:00
});
2022-09-06 13:08:28 +00:00
req.on('end', () => {
2022-09-05 10:06:14 +00:00
resolve();
});
2022-08-03 12:57:39 +00:00
req.end();
});
2022-08-01 05:59:04 +00:00
};
};