pushed thumbnail sanitizing down to the model layer

This commit is contained in:
bill bittner 2017-12-06 11:53:31 -08:00
parent 17ead09747
commit e9647d053d
3 changed files with 60 additions and 61 deletions

View file

@ -1,11 +1,18 @@
const db = require('../models');
const logger = require('winston');
const DEFAULT_THUMBNAIL = 'https://spee.ch/assets/img/video_thumb_default.png';
const NO_CHANNEL = 'NO_CHANNEL';
const NO_CLAIM = 'NO_CLAIM';
const NO_FILE = 'NO_FILE';
function addUrlInformation (claim, channelName, longChannelClaimId, shortChannelClaimId, name, fileExtension) {
claim['showUrlLong'] = `/${channelName}:${longChannelClaimId}/${name}`;
claim['directUrlLong'] = `/${channelName}:${longChannelClaimId}/${name}.${fileExtension}`;
claim['showUrlShort'] = `/${channelName}:${shortChannelClaimId}/${name}`;
claim['directUrlShort'] = `/${channelName}:${shortChannelClaimId}/${name}.${fileExtension}`;
return claim;
}
module.exports = {
getClaimId (channelName, channelClaimId, name, claimId) {
if (channelName) {
@ -53,35 +60,29 @@ module.exports = {
});
});
},
getChannelContents (channelName, channelClaimId) {
getChannelInfoAndContent (channelName, channelClaimId) { // note: move down to model layer?
return new Promise((resolve, reject) => {
db.Certificate.getLongChannelId(channelName, channelClaimId) // 1. get the long channel Id
.then(longChannelClaimId => { // 2. get all claims for that channel
// 1. get the long channel Id (make sure channel exists)
db.Certificate.getLongChannelId(channelName, channelClaimId)
.then(longChannelClaimId => {
if (!longChannelClaimId) {
return [null, null, null];
}
// 2. get the short ID and all claims for that channel
return Promise.all([longChannelClaimId, db.Certificate.getShortChannelIdFromLongChannelId(longChannelClaimId, channelName), db.Claim.getAllChannelClaims(longChannelClaimId)]);
})
.then(([longChannelClaimId, shortChannelClaimId, channelClaimsArray]) => { // 4. add extra data not available from Claim table
.then(([longChannelClaimId, shortChannelClaimId, channelClaimsArray]) => {
if (!longChannelClaimId) {
return resolve(NO_CHANNEL);
}
// 3. add url information to each claim
if (channelClaimsArray) {
channelClaimsArray.forEach(element => {
const fileExtenstion = element.contentType.substring(element.contentType.lastIndexOf('/') + 1);
element['showUrlLong'] = `/${channelName}:${longChannelClaimId}/${element.name}`;
element['directUrlLong'] = `/${channelName}:${longChannelClaimId}/${element.name}.${fileExtenstion}`;
element['showUrlShort'] = `/${channelName}:${shortChannelClaimId}/${element.name}`;
element['directUrlShort'] = `/${channelName}:${shortChannelClaimId}/${element.name}.${fileExtenstion}`;
element['thumbnail'] = module.exports.chooseThumbnail(element, DEFAULT_THUMBNAIL);
channelClaimsArray.forEach(claim => {
return addUrlInformation(claim);
});
}
resolve({
channelName,
longChannelClaimId,
shortChannelClaimId,
claims: channelClaimsArray,
});
// 4. return all the channel information and contents
resolve({ channelName, longChannelClaimId, shortChannelClaimId, claims: channelClaimsArray });
})
.catch(error => {
reject(error);
@ -97,38 +98,4 @@ module.exports = {
return file.dataValues;
});
},
getClaimRecord (claimId, name) {
return db.Claim.findOne({where: {claimId, name}})
.then(claim => {
if (!claim) {
throw new Error('no record found in Claim table');
}
claim.dataValues.thumbnail = module.exports.chooseThumbnail(claim.dataValues, DEFAULT_THUMBNAIL);
claim.dataValues.fileExt = module.exports.determineFileExtensionFromContentType(claim.dataValues.contentType);
return claim.dataValues;
});
},
determineFileExtensionFromContentType (contentType) {
switch (contentType) {
case 'image/jpeg':
return 'jpeg';
case 'image/jpg':
return 'jpg';
case 'image/png':
return 'png';
case 'image/gif':
return 'gif';
case 'video/mp4':
return 'mp4';
default:
logger.info('showing unknown file type as image/jpeg');
return 'jpeg';
}
},
chooseThumbnail (claimInfo, defaultThumbnail) {
if (!claimInfo.thumbnail || claimInfo.thumbnail.trim() === '') {
return defaultThumbnail;
}
return claimInfo.thumbnail;
},
};

View file

@ -1,5 +1,30 @@
const logger = require('winston');
const { returnShortId } = require('../helpers/sequelizeHelpers.js');
const DEFAULT_THUMBNAIL = 'https://spee.ch/assets/img/video_thumb_default.png';
function determineFileExtensionFromContentType (contentType) {
switch (contentType) {
case 'image/jpeg':
case 'image/jpg':
return 'jpg';
case 'image/png':
return 'png';
case 'image/gif':
return 'gif';
case 'video/mp4':
return 'mp4';
default:
logger.info('setting unknown file type as file extension jpg');
return 'jpg';
}
};
function determineThumbnail (storedThumbnail, defaultThumbnail) {
if (storedThumbnail === '') {
return defaultThumbnail;
}
return storedThumbnail;
};
module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, DECIMAL }) => {
const Claim = sequelize.define(
@ -187,12 +212,17 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, DECIMAL }) => {
where: { certificateId: channelClaimId },
order: [['height', 'ASC']],
})
.then(result => {
switch (result.length) {
.then(channelClaimsArray => {
switch (channelClaimsArray.length) {
case 0:
return resolve(null);
default:
return resolve(result);
channelClaimsArray.forEach(claim => {
claim['fileExt'] = determineFileExtensionFromContentType(claim.contentType);
claim['thumbnail'] = determineThumbnail(claim.thumbnail, DEFAULT_THUMBNAIL);
return claim;
});
return resolve(channelClaimsArray);
}
})
.catch(error => {
@ -313,6 +343,8 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, DECIMAL }) => {
};
switch (result.length) {
case 1:
result[0].dataValues.thumbnail = determineThumbnail(result[0].dataValues.thumbnail, DEFAULT_THUMBNAIL);
result[0].dataValues.fileExt = determineFileExtensionFromContentType(result[0].dataValues.contentType);
return resolve(result[0]);
default:
logger.warn(`more than one entry matches that name (${name}) and claimID (${claimId})`);

View file

@ -1,5 +1,5 @@
const logger = require('winston');
const { getClaimId, getChannelContents, getLocalFileRecord, getClaimRecord } = require('../controllers/serveController.js');
const { getClaimId, getChannelInfoAndContent, getLocalFileRecord } = require('../controllers/serveController.js');
const serveHelpers = require('../helpers/serveHelpers.js');
const { handleRequestError } = require('../helpers/errorHandlers.js');
const db = require('../models');
@ -119,7 +119,7 @@ function returnOptionsForChannelPageRendering (result, query) {
return options;
}
function sendChannelContentsToClient (result, query, res) {
function sendChannelInfoAndContentToClient (result, query, res) {
if (result === NO_CHANNEL) { // (a) no channel found
res.status(200).render('noChannel');
} else { // (b) channel found
@ -134,9 +134,9 @@ function showChannelPageToClient (uri, originalUrl, ip, query, res) {
let channelClaimId = returnChannelIdFromUri(uri);
logger.debug('channel Id =', channelClaimId);
// 1. retrieve the channel contents
getChannelContents(channelName, channelClaimId)
getChannelInfoAndContent(channelName, channelClaimId)
.then(result => {
sendChannelContentsToClient(result, query, res);
sendChannelInfoAndContentToClient(result, query, res);
})
.catch(error => {
handleRequestError('serve', originalUrl, ip, error, res);
@ -173,7 +173,7 @@ function determineName (uri) {
function showAssetToClient (claimId, name, res) {
return Promise
.all([getClaimRecord(claimId, name), db.Claim.getShortClaimIdFromLongClaimId(claimId, name)])
.all([db.Claim.resolveClaim(name, claimId), db.Claim.getShortClaimIdFromLongClaimId(claimId, name)])
.then(([claimInfo, shortClaimId]) => {
logger.debug('claimInfo:', claimInfo);
logger.debug('shortClaimId:', shortClaimId);
@ -186,7 +186,7 @@ function showAssetToClient (claimId, name, res) {
function showPlainAssetToClient (claimId, name, res) {
return Promise
.all([getClaimRecord(claimId, name), db.Claim.getShortClaimIdFromLongClaimId(claimId, name)])
.all([db.Claim.resolveClaim(claimId, name), db.Claim.getShortClaimIdFromLongClaimId(claimId, name)])
.then(([claimInfo, shortClaimId]) => {
logger.debug('claimInfo:', claimInfo);
logger.debug('shortClaimId:', shortClaimId);