combined show and serve routes into one, and routed based on file extension presence
This commit is contained in:
parent
cece85f9e7
commit
bec1dd3f02
5 changed files with 120 additions and 101 deletions
|
@ -2,8 +2,10 @@ const { serveClaimByName, serveClaimByClaimId, serveClaimByShortUrl } = require(
|
|||
const { postToStats, sendGoogleAnalytics } = require('../controllers/statsController.js');
|
||||
const errorHandlers = require('../helpers/libraries/errorHandlers.js');
|
||||
const { serveFile } = require('../helpers/libraries/serveHelpers.js');
|
||||
const { showClaimByName, showClaimByClaimId, showClaimByShortUrl } = require('../controllers/showController.js');
|
||||
const logger = require('winston');
|
||||
|
||||
function retrieveAssetInfo (name, claimId) {
|
||||
function retrieveAssetServeInfo (name, claimId) {
|
||||
const deferred = new Promise((resolve, reject) => {
|
||||
// if claim id is full 40 chars, retrieve the shortest possible url
|
||||
if (claimId.length === 40) {
|
||||
|
@ -18,67 +20,136 @@ function retrieveAssetInfo (name, claimId) {
|
|||
return deferred;
|
||||
}
|
||||
|
||||
function retrieveAssetShowInfo (name, claimId) {
|
||||
const deferred = new Promise((resolve, reject) => {
|
||||
// if claim id is full 40 chars, retrieve the shortest possible url
|
||||
if (claimId.length === 40) {
|
||||
resolve(showClaimByClaimId(name, claimId));
|
||||
// if the claim id is shorter than 40, retrieve the full claim id & shortest possible url
|
||||
} else if (claimId.length < 40) {
|
||||
resolve(showClaimByShortUrl(name, claimId));
|
||||
} else {
|
||||
reject(new Error('That Claim Id is longer than 40 characters.'));
|
||||
}
|
||||
});
|
||||
return deferred;
|
||||
}
|
||||
|
||||
module.exports = (app) => {
|
||||
// route to serve a specific asset
|
||||
app.get('/:name/:claim_id', ({ headers, ip, originalUrl, params }, res) => {
|
||||
// google analytics
|
||||
sendGoogleAnalytics('serve', headers, ip, originalUrl);
|
||||
// begin image-serve processes
|
||||
retrieveAssetInfo(params.name, params.claim_id)
|
||||
.then((fileInfo) => {
|
||||
// check to make sure a file was found
|
||||
if (!fileInfo) {
|
||||
res.status(307).render('noClaims');
|
||||
return;
|
||||
}
|
||||
// serve the file or the show route
|
||||
if (headers['accept']) {
|
||||
const mimetypes = headers['accept'].split(',');
|
||||
if (mimetypes.includes('text/html')) {
|
||||
postToStats('show', originalUrl, ip, fileInfo.name, fileInfo.claimId, 'success');
|
||||
res.status(200).render('showLite', { layout: 'show', fileInfo });
|
||||
// decide to serve or show
|
||||
const dotIndex = params.claim_id.lastIndexOf('.');
|
||||
if (dotIndex === 0) {
|
||||
logger.error('only an extension was submitted');
|
||||
// if an image extension was given, serve the image directly
|
||||
} else if (dotIndex > 0) {
|
||||
const fileExtension = params.claim_id.substring(dotIndex);
|
||||
const claimId = params.claim_id.substring(0, dotIndex - 1);
|
||||
logger.debug('file extension is:', fileExtension);
|
||||
retrieveAssetServeInfo(params.name, claimId)
|
||||
.then((fileInfo) => {
|
||||
// check to make sure a file was found
|
||||
if (!fileInfo) {
|
||||
res.status(307).render('noClaims');
|
||||
return;
|
||||
}
|
||||
// serve the file or the show route
|
||||
if (headers['accept']) {
|
||||
const mimetypes = headers['accept'].split(',');
|
||||
if (mimetypes.includes('text/html')) {
|
||||
postToStats('show', originalUrl, ip, fileInfo.name, fileInfo.claimId, 'success');
|
||||
res.status(200).render('showLite', { layout: 'show', fileInfo });
|
||||
} else {
|
||||
postToStats('serve', originalUrl, ip, fileInfo.name, fileInfo.claimId, 'success');
|
||||
serveFile(fileInfo, res);
|
||||
}
|
||||
} else {
|
||||
postToStats('serve', originalUrl, ip, fileInfo.name, fileInfo.claimId, 'success');
|
||||
serveFile(fileInfo, res);
|
||||
}
|
||||
} else {
|
||||
postToStats('serve', originalUrl, ip, fileInfo.name, fileInfo.claimId, 'success');
|
||||
serveFile(fileInfo, res);
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
errorHandlers.handleRequestError('serve', originalUrl, ip, error, res);
|
||||
});
|
||||
})
|
||||
.catch(error => {
|
||||
errorHandlers.handleRequestError('serve', originalUrl, ip, error, res);
|
||||
});
|
||||
// if no image extension was given, show the asset with details
|
||||
} else if (dotIndex === -1) {
|
||||
// begin image-serve processes
|
||||
retrieveAssetShowInfo(params.name, params.claim_id)
|
||||
.then((fileInfo) => {
|
||||
console.log('SHORT URL:', fileInfo.shortUrl);
|
||||
// check to make sure a file was found
|
||||
if (!fileInfo) {
|
||||
res.status(307).render('noClaims');
|
||||
return;
|
||||
}
|
||||
// serve the file or the show route
|
||||
postToStats('show', originalUrl, ip, fileInfo.name, fileInfo.claimId, 'success');
|
||||
res.status(200).render('show', { layout: 'show', fileInfo });
|
||||
})
|
||||
.catch(error => {
|
||||
errorHandlers.handleRequestError('show', originalUrl, ip, error, res);
|
||||
});
|
||||
}
|
||||
});
|
||||
// route to serve the winning asset at a claim
|
||||
app.get('/:name', ({ headers, ip, originalUrl, params }, res) => {
|
||||
// google analytics
|
||||
sendGoogleAnalytics('serve', headers, ip, originalUrl);
|
||||
// begin image-serve processes
|
||||
serveClaimByName(params.name)
|
||||
.then(fileInfo => {
|
||||
// check to make sure a file was found
|
||||
if (!fileInfo) {
|
||||
res.status(307).render('noClaims');
|
||||
return;
|
||||
}
|
||||
// serve the file or the show route
|
||||
if (headers['accept']) { // note: added b/c some requests errored out due to no accept param in header
|
||||
const mimetypes = headers['accept'].split(',');
|
||||
if (mimetypes.includes('text/html')) {
|
||||
postToStats('show', originalUrl, ip, fileInfo.name, fileInfo.claimId, 'success');
|
||||
res.status(200).render('showLite', { layout: 'show', fileInfo });
|
||||
// decide to serve or show
|
||||
const dotIndex = params.name.lastIndexOf('.');
|
||||
if (dotIndex === 0) {
|
||||
logger.error('only an extension was submitted');
|
||||
// if an image extension was given, serve the image directly
|
||||
} else if (dotIndex > 0) {
|
||||
const fileExtension = params.name.substring(dotIndex);
|
||||
const claimName = params.name.substring(0, dotIndex - 1);
|
||||
logger.debug('file extension is:', fileExtension);
|
||||
// begin image-serve processes
|
||||
serveClaimByName(claimName)
|
||||
.then(fileInfo => {
|
||||
// check to make sure a file was found
|
||||
if (!fileInfo) {
|
||||
res.status(307).render('noClaims');
|
||||
return;
|
||||
}
|
||||
// serve the file or the show route
|
||||
if (headers['accept']) { // note: added b/c some requests errored out due to no accept param in header
|
||||
const mimetypes = headers['accept'].split(',');
|
||||
if (mimetypes.includes('text/html')) {
|
||||
postToStats('show', originalUrl, ip, fileInfo.name, fileInfo.claimId, 'success');
|
||||
res.status(200).render('showLite', { layout: 'show', fileInfo });
|
||||
} else {
|
||||
postToStats('serve', originalUrl, ip, fileInfo.name, fileInfo.claimId, 'success');
|
||||
serveFile(fileInfo, res);
|
||||
}
|
||||
} else {
|
||||
postToStats('serve', originalUrl, ip, fileInfo.name, fileInfo.claimId, 'success');
|
||||
serveFile(fileInfo, res);
|
||||
}
|
||||
} else {
|
||||
postToStats('serve', originalUrl, ip, fileInfo.name, fileInfo.claimId, 'success');
|
||||
serveFile(fileInfo, res);
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
errorHandlers.handleRequestError('serve', originalUrl, ip, error, res);
|
||||
});
|
||||
})
|
||||
.catch(error => {
|
||||
errorHandlers.handleRequestError('serve', originalUrl, ip, error, res);
|
||||
});
|
||||
// if no image extension was given, show the asset with details
|
||||
} else if (dotIndex === -1) {
|
||||
// get and render the content
|
||||
showClaimByName(params.name)
|
||||
.then(fileInfo => {
|
||||
// check to make sure a file was found
|
||||
if (!fileInfo) {
|
||||
res.status(307).render('noClaims');
|
||||
return;
|
||||
}
|
||||
// serve the show route
|
||||
postToStats('show', originalUrl, ip, fileInfo.name, fileInfo.claimId, 'success');
|
||||
res.status(200).render('show', { layout: 'show', fileInfo });
|
||||
})
|
||||
.catch(error => {
|
||||
errorHandlers.handleRequestError('show', originalUrl, ip, error, res);
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
|
|
@ -1,22 +1,7 @@
|
|||
const errorHandlers = require('../helpers/libraries/errorHandlers.js');
|
||||
const { showClaimByName, showClaimByClaimId, showClaimByShortUrl, showAllClaims } = require('../controllers/showController.js');
|
||||
const { showAllClaims } = require('../controllers/showController.js');
|
||||
const { postToStats, getStatsSummary, getTrendingClaims } = require('../controllers/statsController.js');
|
||||
|
||||
function retrieveAssetInfo (name, claimId) {
|
||||
const deferred = new Promise((resolve, reject) => {
|
||||
// if claim id is full 40 chars, retrieve the shortest possible url
|
||||
if (claimId.length === 40) {
|
||||
resolve(showClaimByClaimId(name, claimId));
|
||||
// if the claim id is shorter than 40, retrieve the full claim id & shortest possible url
|
||||
} else if (claimId.length < 40) {
|
||||
resolve(showClaimByShortUrl(name, claimId));
|
||||
} else {
|
||||
reject(new Error('That Claim Id is longer than 40 characters.'));
|
||||
}
|
||||
});
|
||||
return deferred;
|
||||
}
|
||||
|
||||
module.exports = (app) => {
|
||||
// route to show 'about' page for spee.ch
|
||||
app.get('/about', ({ ip, originalUrl }, res) => {
|
||||
|
@ -66,41 +51,4 @@ module.exports = (app) => {
|
|||
errorHandlers.handleRequestError('show', originalUrl, ip, error, res);
|
||||
});
|
||||
});
|
||||
// route to show a specific asset
|
||||
app.get('/show/:name/:claim_id', ({ ip, originalUrl, params }, res) => {
|
||||
// begin image-serve processes
|
||||
retrieveAssetInfo(params.name, params.claim_id)
|
||||
.then((fileInfo) => {
|
||||
console.log('SHORT URL:', fileInfo.shortUrl);
|
||||
// check to make sure a file was found
|
||||
if (!fileInfo) {
|
||||
res.status(307).render('noClaims');
|
||||
return;
|
||||
}
|
||||
// serve the file or the show route
|
||||
postToStats('show', originalUrl, ip, fileInfo.name, fileInfo.claimId, 'success');
|
||||
res.status(200).render('show', { layout: 'show', fileInfo });
|
||||
})
|
||||
.catch(error => {
|
||||
errorHandlers.handleRequestError('show', originalUrl, ip, error, res);
|
||||
});
|
||||
});
|
||||
// route to show the winning free, public claim
|
||||
app.get('/show/:name', ({ ip, originalUrl, params }, res) => {
|
||||
// get and render the content
|
||||
showClaimByName(params.name)
|
||||
.then(fileInfo => {
|
||||
// check to make sure a file was found
|
||||
if (!fileInfo) {
|
||||
res.status(307).render('noClaims');
|
||||
return;
|
||||
}
|
||||
// serve the show route
|
||||
postToStats('show', originalUrl, ip, fileInfo.name, fileInfo.claimId, 'success');
|
||||
res.status(200).render('show', { layout: 'show', fileInfo });
|
||||
})
|
||||
.catch(error => {
|
||||
errorHandlers.handleRequestError('show', originalUrl, ip, error, res);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
|
|
@ -50,7 +50,7 @@
|
|||
});
|
||||
socket.on('publish-complete', function(msg){
|
||||
var publishResults;
|
||||
var showUrl = '/show/' + msg.name + '/' + msg.result.claim_id;
|
||||
var showUrl = msg.name + '/' + msg.result.claim_id;
|
||||
// build new publish area
|
||||
publishResults = '<p>Your publish is complete! You are being redirected to it now.</p>';
|
||||
publishResults += '<p><a target="_blank" href="' + showUrl + '">If you do not get redirected, click here.</a></p>';
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
<div class="grid" data-masonry='{ "itemSelector": ".grid-item" }'>
|
||||
{{#each trendingAssets}}
|
||||
{{#unless this.nsfw}}
|
||||
<a href="/show/{{this.name}}/{{this.claimId}}">
|
||||
<a href="/{{this.name}}/{{this.claimId}}">
|
||||
{{#ifConditional this.fileType '===' 'video/mp4'}}
|
||||
<video class="grid-item trending-video" controls>
|
||||
<source src="/api/streamFile/{{this.fileName}}">
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
<div id="asset-placeholder">
|
||||
<a href="/show/{{fileInfo.name}}/{{fileInfo.claimId}}">
|
||||
<a href="/{{fileInfo.name}}/{{fileInfo.claimId}}">
|
||||
{{#ifConditional fileInfo.fileType '===' 'video/mp4'}}
|
||||
<video class="show-asset-lite" autoplay controls>
|
||||
<source src="/api/streamFile/{{fileInfo.fileName}}">
|
||||
|
|
Loading…
Reference in a new issue