Instantly share code, notes, and snippets.

@sudipp sudipp/mediaController.js
Last active Oct 9, 2018

Embed
What would you like to do?
let _ = require('lodash');
const uriTokenizer = require('../utils/uriTokenizer');
const tokenizer = new uriTokenizer( { algorithm: 'RS256', expiresIn : '15m' }, { algorithms: ['RS256'] });
const media = require('../Model/Media');
async function buildMediaFileDownloadUrl(req, mediafile){
let uri= req.protocol + '://' + req.get('host') + '/file/' + (mediafile || "").replace(/^\//, '');
return await tokenizer.signUri(uri, { divison : "Honda"});
}
router.get('/:fsno', (req,res,next) =>
{
if(req.params.fsno){
media.getMediaFileByFsNo(req.params.fsno, function(err,rows){
if(err) next(err);
else
{
let filesPromises = _.map(rows, async function(value){
return {
url: await buildMediaFileDownloadUrl(req, value.DownloadPath),
size: parseInt(value.Size),
destinationFolder: value.RelativeDestFolder,
};
});
Promise.all(filesPromises).then(
function(result){
var firmwareFilesData = { results:{
totalSize: _.sumBy(rows, function(o) { return o.Size; }),
folderStruct : _.map(_.uniq(_.filter(rows, function(o) { return o.RelativeDestFolder && o.RelativeDestFolder.length !=''; })),
function(value){
return value.RelativeDestFolder
}
),
files : result
}};
res.json(firmwareFilesData);
},
function error(err){ next(err); }
);
}
});
}
});
module.exports = router;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment