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 db = require('../models');
const logger = require('winston'); const logger = require('winston');
const DEFAULT_THUMBNAIL = 'https://spee.ch/assets/img/video_thumb_default.png';
const NO_CHANNEL = 'NO_CHANNEL'; const NO_CHANNEL = 'NO_CHANNEL';
const NO_CLAIM = 'NO_CLAIM'; const NO_CLAIM = 'NO_CLAIM';
const NO_FILE = 'NO_FILE'; 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 = { module.exports = {
getClaimId (channelName, channelClaimId, name, claimId) { getClaimId (channelName, channelClaimId, name, claimId) {
if (channelName) { 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) => { return new Promise((resolve, reject) => {
db.Certificate.getLongChannelId(channelName, channelClaimId) // 1. get the long channel Id // 1. get the long channel Id (make sure channel exists)
.then(longChannelClaimId => { // 2. get all claims for that channel db.Certificate.getLongChannelId(channelName, channelClaimId)
.then(longChannelClaimId => {
if (!longChannelClaimId) { if (!longChannelClaimId) {
return [null, null, null]; 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)]); 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) { if (!longChannelClaimId) {
return resolve(NO_CHANNEL); return resolve(NO_CHANNEL);
} }
// 3. add url information to each claim
if (channelClaimsArray) { if (channelClaimsArray) {
channelClaimsArray.forEach(element => { channelClaimsArray.forEach(claim => {
const fileExtenstion = element.contentType.substring(element.contentType.lastIndexOf('/') + 1); return addUrlInformation(claim);
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);
}); });
} }
resolve({ // 4. return all the channel information and contents
channelName, resolve({ channelName, longChannelClaimId, shortChannelClaimId, claims: channelClaimsArray });
longChannelClaimId,
shortChannelClaimId,
claims: channelClaimsArray,
});
}) })
.catch(error => { .catch(error => {
reject(error); reject(error);
@ -97,38 +98,4 @@ module.exports = {
return file.dataValues; 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 logger = require('winston');
const { returnShortId } = require('../helpers/sequelizeHelpers.js'); 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 }) => { module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, DECIMAL }) => {
const Claim = sequelize.define( const Claim = sequelize.define(
@ -187,12 +212,17 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, DECIMAL }) => {
where: { certificateId: channelClaimId }, where: { certificateId: channelClaimId },
order: [['height', 'ASC']], order: [['height', 'ASC']],
}) })
.then(result => { .then(channelClaimsArray => {
switch (result.length) { switch (channelClaimsArray.length) {
case 0: case 0:
return resolve(null); return resolve(null);
default: 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 => { .catch(error => {
@ -313,6 +343,8 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, DECIMAL }) => {
}; };
switch (result.length) { switch (result.length) {
case 1: 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]); return resolve(result[0]);
default: default:
logger.warn(`more than one entry matches that name (${name}) and claimID (${claimId})`); logger.warn(`more than one entry matches that name (${name}) and claimID (${claimId})`);

View file

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