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

94 lines
3.2 KiB
JavaScript

const axios = require('axios');
const https = require('https');
module.exports = Self => {
Self.remoteMethod('getVideo', {
description: 'Get packing video of ticketId',
accessType: 'READ',
accepts: [
{
arg: 'id',
type: 'number',
required: false,
description: 'Ticket id'
},
{
arg: 'time',
type: 'number',
required: false,
description: 'Time to add'
},
{
arg: 'req',
type: 'object',
http: {source: 'req'}
},
{
arg: 'res',
type: 'object',
http: {source: 'res'}
}
],
http: {
path: `/getVideo`,
verb: 'GET',
},
});
Self.getVideo = async(id, time, req, res, options) => {
const models = Self.app.models;
const myOptions = {};
// console.log('ENTRY');
if (typeof options == 'object')
Object.assign(myOptions, options);
const expedition = await models.Expedition.findById(id, null, myOptions);
const packingSiteConfig = await models.PackingSiteConfig.findOne({}, myOptions);
const monitorId = 'Xme2hiqz1f';
// console.log(packingSiteConfig);
const addTime = (time ? time : packingSiteConfig.avgBoxingTime) * 1000;
const start = new Date(expedition.created);// '2022-07-27T09:00:00';// new Date('2022-07-27'); //
const end = new Date(start.getTime() + addTime);
// console.log(addTime);
// console.log(start.toString().split('.')[0]);
// console.log(end.toString().split('.')[0]);
const videoUrl = `/${packingSiteConfig.shinobiToken}/videos/${packingSiteConfig.shinobiGroupKey}/${monitorId}`;
const timeUrl = `?start=${start.toString().split('.')[0]}&end=${end.toString().split('.')[0]}`;
const url = `${packingSiteConfig.shinobiUrl}${videoUrl}${timeUrl}&endIsStartTo`;
// console.log(url);
const videoList = await axios.post(url);
// console.log('videoList.data', videoList.data);
// console.log(videoList.data.videos);
// console.log(videoList.data.videos.length);
const length = videoList.data.videos.length;
// console.log(length);
if (!length) return [];
const lastVideo = videoList.data.videos[length - 1].href;
const headers = Object.assign({}, req.headers, {
host: 'shinobi.verdnatura.es'
});
const httpOptions = {
host: 'shinobi.verdnatura.es',
path: '/BW4o7FZyJ85she1hlRDe6QnwW3jwEP/videos/xVqft9LFXg/Xme2hiqz1f/2022-08-02T11-45-01.mp4',
port: 443,
headers
};
return new Promise((resolve, reject) => {
const req = https.request(httpOptions,
shinobiRes => {
shinobiRes.pause();
res.writeHeader(shinobiRes.statusCode, shinobiRes.headers);
shinobiRes.pipe(res);
resolve();
});
req.end();
});
};
};