From 0142c168aee80da336cb2fac2866b77f123d0806 Mon Sep 17 00:00:00 2001 From: bill bittner Date: Mon, 27 Nov 2017 14:52:46 -0800 Subject: [PATCH] fixed missing file extension --- controllers/serveController.js | 35 +++++++++++++----- routes/serve-routes.js | 66 +++++++++++++++++----------------- 2 files changed, 59 insertions(+), 42 deletions(-) diff --git a/controllers/serveController.js b/controllers/serveController.js index 18ec4526..fdbfcd81 100644 --- a/controllers/serveController.js +++ b/controllers/serveController.js @@ -5,13 +5,6 @@ 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() === '') { - return defaultThumbnail; - } - return claimInfo.thumbnail; -} - module.exports = { getClaimId (channelName, channelId, name, claimId) { if (channelName) { @@ -82,7 +75,7 @@ module.exports = { element['directUrlLong'] = `/${channelName}:${longChannelId}/${element.name}.${fileExtenstion}`; element['showUrlShort'] = `/${channelName}:${shortChannelId}/${element.name}`; element['directUrlShort'] = `/${channelName}:${shortChannelId}/${element.name}.${fileExtenstion}`; - element['thumbnail'] = chooseThumbnail(element, DEFAULT_THUMBNAIL); + element['thumbnail'] = module.exports.chooseThumbnail(element, DEFAULT_THUMBNAIL); }); } resolve({ @@ -112,8 +105,32 @@ module.exports = { if (!claim) { throw new Error('no record found in Claim table'); } - claim.dataValues.thumbnail = chooseThumbnail(claim.dataValues.thumbnail, DEFAULT_THUMBNAIL); + claim.dataValues.thumbnail = module.exports.chooseThumbnail(claim.dataValues.thumbnail, 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; + }, }; diff --git a/routes/serve-routes.js b/routes/serve-routes.js index 41cea422..6f8cebcf 100644 --- a/routes/serve-routes.js +++ b/routes/serve-routes.js @@ -177,43 +177,43 @@ function determineName (uri) { function showAssetToClient (claimId, name, res) { // check for local file info, resolve the claim, and get the short return Promise - .all([getClaimRecord(claimId, name), db.Claim.getShortClaimIdFromLongClaimId(claimId, name)]) - .then(([claimInfo, shortClaimId]) => { - logger.debug('claimInfo:', claimInfo); - logger.debug('shortClaimId:', shortClaimId); - return serveHelpers.showFile(claimInfo, shortClaimId, res); - }) - .catch(error => { - throw error; - }); + .all([getClaimRecord(claimId, name), db.Claim.getShortClaimIdFromLongClaimId(claimId, name)]) + .then(([claimInfo, shortClaimId]) => { + logger.debug('claimInfo:', claimInfo); + logger.debug('shortClaimId:', shortClaimId); + return serveHelpers.showFile(claimInfo, shortClaimId, res); + }) + .catch(error => { + throw error; + }); } function showPlainAssetToClient (claimId, name, res) { return Promise - .all([getClaimRecord(claimId, name), db.Claim.getShortClaimIdFromLongClaimId(claimId, name)]) - .then(([claimInfo, shortClaimId]) => { - logger.debug('claimInfo:', claimInfo); - logger.debug('shortClaimId:', shortClaimId); - return serveHelpers.showFileLite(claimInfo, shortClaimId, res); - }) - .catch(error => { - throw error; - }); + .all([getClaimRecord(claimId, name), db.Claim.getShortClaimIdFromLongClaimId(claimId, name)]) + .then(([claimInfo, shortClaimId]) => { + logger.debug('claimInfo:', claimInfo); + logger.debug('shortClaimId:', shortClaimId); + return serveHelpers.showFileLite(claimInfo, shortClaimId, res); + }) + .catch(error => { + throw error; + }); } function serveAssetToClient (claimId, name, res) { return getLocalFileRecord(claimId, name) - .then(fileInfo => { - logger.debug('fileInfo:', fileInfo); - if (fileInfo) { - return serveHelpers.serveFile(fileInfo, res); - } else { - return res.status(307).json({status: 'success', message: 'resource temporarily unavailable'}); - } - }) - .catch(error => { - throw error; - }); + .then(fileInfo => { + logger.debug('fileInfo:', fileInfo); + if (fileInfo) { + return serveHelpers.serveFile(fileInfo, res); + } else { + return res.status(307).json({status: 'success', message: 'resource temporarily unavailable'}); + } + }) + .catch(error => { + throw error; + }); } module.exports = (app) => { @@ -259,11 +259,11 @@ module.exports = (app) => { } // show, showlite, or serve switch (responseType) { - case SERVE: + case SHOW: return showAssetToClient(claimId, name, res); case SHOWLITE: return showPlainAssetToClient(claimId, name, res); - case SHOW: + case SERVE: return serveAssetToClient(claimId, name, res); default: break; @@ -303,11 +303,11 @@ module.exports = (app) => { } // show, showlite, or serve switch (responseType) { - case SERVE: + case SHOW: return showAssetToClient(claimId, name, res); case SHOWLITE: return showPlainAssetToClient(claimId, name, res); - case SHOW: + case SERVE: return serveAssetToClient(claimId, name, res); default: break;