From 78bc163586bc376e620aa0e536683cb6830d7953 Mon Sep 17 00:00:00 2001 From: bill bittner Date: Mon, 27 Nov 2017 10:22:47 -0800 Subject: [PATCH] fixed claimId and show workflow for spee.ch/example --- controllers/serveController.js | 3 ++- helpers/serveHelpers.js | 8 ++++++-- models/claim.js | 6 +++--- routes/serve-routes.js | 15 ++++++++++----- 4 files changed, 21 insertions(+), 11 deletions(-) diff --git a/controllers/serveController.js b/controllers/serveController.js index d712819a..18ec4526 100644 --- a/controllers/serveController.js +++ b/controllers/serveController.js @@ -3,6 +3,7 @@ const logger = require('winston'); const DEFAULT_THUMBNAIL = 'https://spee.ch/assets/img/video_thumb_default.png'; const NO_CHANNEL = 'NO_CHANNEL'; +const NO_FILE = 'NO_FILE'; function chooseThumbnail (claimInfo, defaultThumbnail) { if (!claimInfo.thumbnail || claimInfo.thumbnail.trim() === '') { @@ -100,7 +101,7 @@ module.exports = { return db.File.findOne({where: {claimId, name}}) .then(file => { if (!file) { - return null; + return NO_FILE; } return file.dataValues; }); diff --git a/helpers/serveHelpers.js b/helpers/serveHelpers.js index 95ea5aab..4db1f376 100644 --- a/helpers/serveHelpers.js +++ b/helpers/serveHelpers.js @@ -16,8 +16,12 @@ function createOpenGraphInfo ({ claimId, name, fileExt }) { module.exports = { serveOrShowAsset (method, fileInfo, claimInfo, shortId, res) { // add file extension to the file info - claimInfo['fileExt'] = fileInfo.fileName.substring(fileInfo.fileName.lastIndexOf('.') + 1); - // serve or show + if (fileInfo.fileName) { + claimInfo['fileExt'] = fileInfo.fileName.substring(fileInfo.fileName.lastIndexOf('.') + 1); + } else { + claimInfo['fileExt'] = null; + } + // serve or show switch (method) { case SERVE: module.exports.serveFile(fileInfo, claimInfo, shortId, res); diff --git a/models/claim.js b/models/claim.js index 9027b087..c526e85f 100644 --- a/models/claim.js +++ b/models/claim.js @@ -159,7 +159,7 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, DECIMAL }) => { }; Claim.getShortClaimIdFromLongClaimId = function (claimId, claimName) { - logger.debug(`Claim.getShortClaimIdFromLongClaimId for ${claimId}#${claimId}`); + logger.debug(`Claim.getShortClaimIdFromLongClaimId for ${claimName}#${claimId}`); return new Promise((resolve, reject) => { this .findAll({ @@ -260,12 +260,12 @@ module.exports = (sequelize, { STRING, BOOLEAN, INTEGER, TEXT, DECIMAL }) => { order: [['effectiveAmount', 'DESC'], ['height', 'ASC']], // note: maybe height and effective amount need to switch? }) .then(result => { + logger.debug('length of result', result.length); switch (result.length) { case 0: return resolve(NO_CLAIM); default: - logger.debug('getTopFreeClaimIdByClaimName result:', result.dataValues); - return resolve(result[0].claimId); + return resolve(result[0].dataValues.claimId); } }) .catch(error => { diff --git a/routes/serve-routes.js b/routes/serve-routes.js index 82dfe569..38f97e99 100644 --- a/routes/serve-routes.js +++ b/routes/serve-routes.js @@ -228,17 +228,22 @@ module.exports = (app) => { getClaimId(null, null, name, null) .then(result => { logger.debug('getClaimId result:', result); - if (result === NO_CLAIM) { + if (result === NO_CLAIM || result === NO_CHANNEL) { + return result; + } + // check for local file info and resolve the claim + return Promise.all([getLocalFileRecord(result, name), getClaimRecord(result, name), db.Claim.getShortClaimIdFromLongClaimId(result, name)]); + }) + .then(result => { + if (result === NO_CLAIM || result === NO_CHANNEL) { res.status(200).render('noClaim'); return; } else if (result === NO_CHANNEL) { res.status(200).render('noChannel'); return; } - // check for local file info and resolve the claim - return Promise.all([getLocalFileRecord(result, name), getClaimRecord(result, name), db.Claim.getShortClaimIdFromLongClaimId(result, name)]); - }) - .then(([fileInfo, claimInfo, shortClaimId]) => { + let fileInfo, claimInfo, shortClaimId; + [fileInfo, claimInfo, shortClaimId] = result; logger.debug(`fileInfo:`, fileInfo); logger.debug('claimInfo:', claimInfo); logger.debug('shortClaimId:', shortClaimId);