Commit 716b4bf1 authored by A. Koch's avatar A. Koch

transcoder is now an external service

parent 3c8a0e00
This diff is collapsed.
{
"api": {
"uriBase": "https://app.motionbank.org",
"apiHost": "https://api.motionbank.org"
"apiHost": "https://api.motionbank.org",
"transcoderHost": "https://transcoder.motionbank.org"
},
"http": {
"host": "0.0.0.0",
......
{
"api": {
"uriBase": "https://app.motionbank.org",
"apiHost": "http://localhost:3030"
"apiHost": "http://localhost:3030",
"transcoderHost": "http://localhost:4040"
},
"http": {
"host": "localhost",
......
......@@ -31,15 +31,6 @@ const setup = async function () {
// const documents = new Service('documents', api._app, models.Document, api._logger, api._acl)
// documents.on('message', message => api._sockets.write(message))
/**
* Configure metadata
*/
const
Metadata = require('./lib/metadata'),
metadata = new Metadata(api._app, annotations)
// metadata.on('message', message => api._sockets.write(message))
/**
* Configure sessions
*/
......
const
config = require('config'),
send = require('@polka/send-type'),
TinyEmitter = require('tiny-emitter'),
Memcached = require('memcached'),
{ ObjectUtil } = require('mbjs-utils'),
{ getMetaData } = require('mbjs-media/src/util/metadata')
const fetchMetaData = async (annotation, user, annotationsService) => {
let meta
try {
meta = await getMetaData(annotation, async query => {
const results = await annotationsService.findHandler({
query: {
query: JSON.stringify(query)
},
user
})
return results.data
}, config.apiKeys)
}
catch (e) { console.error('fetchMetaData', e.message) }
return meta
}
class Metadata extends TinyEmitter {
constructor (app, annotationsService) {
super()
this._annotations = annotationsService
if (config.metadata.memcachedURL) this._memcached = new Memcached(config.metadata.memcachedURL)
const _this = this
app.get('/metadata/:id', async (req, res) => {
const result = await _this._annotations.getHandler(req)
const annotation = result.data
const key = `metadata_${ObjectUtil.slug(annotation.body.source.id)}`
if (!annotation) return _this._errorResponse(res, 404)
let metadata
if (_this._memcached) {
metadata = await new Promise((resolve, reject) => {
_this._memcached.get(key, function (err, data) {
if (err) console.error('failed to get metadata from cache', err.message)
resolve(data)
})
})
}
if (!metadata) {
metadata = await fetchMetaData(annotation, req.user, _this._annotations)
if (_this._memcached && metadata) {
await new Promise((resolve, reject) => {
_this._memcached.set(key, metadata, parseInt(config.metadata.lifetime.toString()), err => {
if (err) console.error('failed to store metadata in cache', err.message)
resolve()
})
})
}
}
if (!metadata) return _this._errorResponse(res, 404)
_this._response(req, res, metadata)
})
}
_response (req, res, data = {}) {
this.emit('message', { method: req.method, id: data.uuid })
if (typeof res === 'function') res({ data })
else if (typeof res === 'undefined') return Promise.resolve({ data })
else send(res, 200, data)
}
_errorResponse (res, code, message = undefined) {
if (typeof res === 'function') res({ error: true, code })
else if (typeof res === 'undefined') return Promise.resolve({ error: true, code })
else send(res, code, message)
}
}
module.exports = Metadata
......@@ -5,7 +5,7 @@ const
constants = require('mbjs-data-models/src/constants'),
{ ObjectUtil } = require('mbjs-utils'),
{ parseSelector, Sorting } = require('mbjs-data-models/src/lib'),
{ getMetaData } = require('mbjs-media/src/util/metadata'),
axios = require('axios'),
{ DateTime } = require('luxon')
class SessionHelpers {
......@@ -35,26 +35,22 @@ const resurrectAnnotation = function (annotation) {
return annotation
}
const fetchMetaData = async (videos, user, annotationsService) => {
const fetchMetaData = async (videos, req) => {
for (let v of videos) {
try {
const meta = await getMetaData(v.annotation, async query => {
const results = await annotationsService.findHandler({
query: {
query: JSON.stringify(query)
},
user
})
return results.data
}, config.apiKeys)
Object.assign(v.meta, meta)
const meta = await axios.get(`${config.api.transcoderHost}/metadata/${v.annotation.uuid}`, {
headers: {
Authorization: req.headers.authorization
}
})
Object.assign(v.meta, meta.data)
}
catch (e) { console.error('fetchMetaData', e.message, e.stack) }
}
return videos
}
const groupBySessions = async function (annotations, user, annotationsService, secondsDist = constants.SESSION_DISTANCE_SECONDS) {
const groupBySessions = async function (annotations, req, secondsDist = constants.SESSION_DISTANCE_SECONDS) {
let millisDist = secondsDist * 1000
annotations = annotations.map(annotation => resurrectAnnotation(annotation)).sort(Sorting.sortOnTarget)
const videos = annotations.filter(anno => { return anno.body.type === 'Video' })
......@@ -64,7 +60,7 @@ const groupBySessions = async function (annotations, user, annotationsService, s
annotation: annotation
}
})
await fetchMetaData(videos, user, annotationsService)
await fetchMetaData(videos, req)
annotations = annotations.filter(anno => { return anno.body.type === 'TextualBody' })
const sessions = []
const defaultSession = { start: undefined, end: undefined, duration: undefined, annotations: [] }
......@@ -127,7 +123,7 @@ class Sessions extends TinyEmitter {
headers: req.headers
})
let annotations = results.data.items
const sessions = await groupBySessions(annotations, req.user, _this._annotations)
const sessions = await groupBySessions(annotations, req)
_this._response(req, res, sessions)
})
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment