'use strict'; const Joi = require('joi'); const Boom = require('@hapi/boom'); const Rfr = require('rfr'); const ComSrv = Rfr('/server/comsrv'); const Calls = Rfr('/server/sections/calls'); const getRecordingInfo = async (uid) => { let res = { file: uid + '.' + process.env.RECORD_EXT }; res.name = res.file; if (process.env.RECORD_PRETTY_NAMES) { try { res.name = (await Calls.getCdrById(uid)).date + '.' + process.env.RECORD_EXT } catch (e) { console.warn(`Failed to get CDR info by ID: ${e}`); } } return res; }; const recordingHandler = async (request, h) => { const recording = await getRecordingInfo(request.params.id); ComSrv.debug(`Serving recording: ${recording.name} as ${request.query.asFile ? 'file' : 'inline'}`); return h.file(recording.file, { confine: process.env.RECORD_DIR ?? true, mode: request.query.asFile ? 'attachment' : 'inline', filename: recording.name }); } module.exports = { routes: [{ method: 'GET', path: '/api/recording/{id}', handler: recordingHandler, options: { validate: { params: Joi.object({ id: Joi.string().min(1).max(10000) }), query: Joi.object({ asFile: Joi.boolean().optional().default(true) }) } } }], config: Joi.object({ RECORD_DIR: Joi.string().min(1).required(), RECORD_EXT: Joi.string().min(1).optional().default('ogg'), RECORD_PRETTY_NAMES: Joi.boolean().optional().default(true) }) };