Commit 1386de5c authored by A. Koch's avatar A. Koch

add metadata route, close #2

parent 1e81927d
......@@ -2244,9 +2244,9 @@
}
},
"mbjs-media": {
"version": "0.0.9",
"resolved": "https://registry.npmjs.org/mbjs-media/-/mbjs-media-0.0.9.tgz",
"integrity": "sha512-ye67oQpxczrdLLRnERdFbIM+nJUvp/GFagBeAO9MHn4zVs6ga1TA0SXd2HCRdaNf+WvRNR+XqoZzBUhRVneUiQ==",
"version": "0.0.10",
"resolved": "https://registry.npmjs.org/mbjs-media/-/mbjs-media-0.0.10.tgz",
"integrity": "sha512-ryabEdi9G+lrdQiFg3WF3G/5Rn+Qs71RcMbgQ1+HekpUvunoirdVngCpUtygv/IW5/RxqLfBEkDNfIiQeeL2aw==",
"requires": {
"axios": "^0.18.0",
"bluebird": "^3.5.1",
......
......@@ -107,6 +107,15 @@ const setup = async function () {
const documents = new Service('documents', app, models.Document, winston, acl)
documents.on('message', message => primus.write(message))
/**
* Configure metadata
*/
const
Metadata = require('./metadata'),
metadata = new Metadata(app, annotations)
metadata.on('message', message => primus.write(message))
/**
* Configure sessions
*/
......
const
config = require('config'),
send = require('@polka/send-type'),
TinyEmitter = require('tiny-emitter'),
{ 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
const _this = this
app.get('/metadata/:id', async (req, res) => {
const result = await _this._annotations.getHandler(req)
const annotation = result.data
if (!annotation) return _this._errorResponse(res, 404)
const metadata = await fetchMetaData(annotation, req.user, _this._annotations)
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
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