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

83 lines
2.7 KiB
JavaScript
Raw Normal View History

2022-08-01 05:59:04 +00:00
const axios = require('axios');
module.exports = Self => {
Self.remoteMethod('getVideo', {
description: 'Get packing video of ticketId',
accessType: 'READ',
accepts: [
{
arg: 'id',
type: 'number',
2022-08-01 13:03:17 +00:00
required: false,
2022-08-01 05:59:04 +00:00
description: 'Ticket id'
},
{
arg: 'time',
type: 'number',
2022-08-01 13:03:17 +00:00
required: false,
2022-08-01 05:59:04 +00:00
description: 'Time to add'
}
],
returns: [
{
arg: 'body',
type: 'file',
root: true
},
{
arg: 'Content-Type',
type: 'String',
http: {target: 'header'}
},
{
arg: 'Content-Disposition',
type: 'String',
http: {target: 'header'}
}
],
2022-08-01 05:59:04 +00:00
http: {
path: `/getVideo`,
verb: 'GET',
},
});
Self.getVideo = async(id, time, options) => {
const models = Self.app.models;
const myOptions = {};
2022-08-01 13:03:17 +00:00
console.log('ENTRY');
2022-08-01 05:59:04 +00:00
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 = 'VbiUcajdaT';
console.log(packingSiteConfig);
2022-08-01 05:59:04 +00:00
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.toISOString().split('.')[0]);
console.log(end.toISOString().split('.')[0]);
2022-08-01 05:59:04 +00:00
const videoUrl = `/${packingSiteConfig.shinobiToken}/videos/${packingSiteConfig.shinobiGroupKey}/${monitorId}`;
const timeUrl = `?start=${start.toISOString().split('.')[0]}&end=${end.toISOString().split('.')[0]}`;
const url = `${packingSiteConfig.shinobiUrl}${videoUrl}${timeUrl}&endIsStartTo`;
2022-08-01 05:59:04 +00:00
console.log(url);
const videoList = await axios.post(url);
console.log(videoList.data);
const lastVideo = videoList.data.videos[videoList.data.videos.length - 1].href;
console.log(lastVideo);
const stream = await axios({
2022-08-01 13:03:17 +00:00
method: 'get',
url: `${packingSiteConfig.shinobiUrl}${lastVideo}`,
2022-08-01 13:03:17 +00:00
responseType: 'stream'
}).then(response => {
return response.data;
});
return [stream, 'video/mp4', `filename="${id}.mp4"`];
2022-08-01 05:59:04 +00:00
};
};