const axios = require('axios'); 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' } ], returns: [ { arg: 'body', type: 'file', root: true }, { arg: 'Content-Type', type: 'String', http: {target: 'header'} }, { arg: 'Content-Disposition', type: 'String', http: {target: 'header'} } ], http: { path: `/getVideo`, verb: 'GET', }, }); Self.getVideo = async(id, time, 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 = 'VbiUcajdaT'; 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.toISOString().split('.')[0]); console.log(end.toISOString().split('.')[0]); 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`; 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({ method: 'get', url: `${packingSiteConfig.shinobiUrl}${lastVideo}`, responseType: 'stream' }).then(response => { return response.data; }); return [stream, 'video/mp4', `filename="${id}.mp4"`]; }; };