Commit afef50b3 authored by Anton's avatar Anton

Add metadata responder

parent 7b02c46e
const
Memcached = require('memcached'),
{ ObjectUtil } = require('mbjs-utils'),
getMetaData = require('mbjs-media/src/util/get-metadata'),
cote = require('cote')
class MetadataService {
constructor (config = {}) {
this._responder = new cote.Responder({ name: 'Metadata Service', key: 'service.metadata' })
this._memcached = config.memcachedURL ? new Memcached(config.memcachedURL) : undefined
this._responder.on('service.metadata:get', (req, res) => this.get(req, res))
}
async get (req, cb) {
const
{ url } = req,
key = `metadata_${ObjectUtil.slug(req.url)}`
let metadata
if (!url) return cb(null)
if (this._memcached) {
metadata = await new Promise((resolve, reject) => {
this._memcached.get(key, (err, data) => err ? reject(err) : resolve(data))
})
}
if ((!metadata || !Object.keys(metadata).length)) {
metadata = await getMetaData(req.url)
if (this._memcached && metadata && Object.keys(metadata).length) {
await new Promise((resolve, reject) => {
this._memcached.set(
key,
metadata,
parseInt(config.lifetime.toString()), err => err ? reject(err) : resolve())
})
}
}
if (metadata && Object.keys(metadata).length) cb(metadata)
else cb(null)
}
}
module.exports = MetadataService
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