added fallback to serve image direct on show routes (backwards compatability
This commit is contained in:
parent
8c6d7613f8
commit
299eddcdad
2 changed files with 105 additions and 83 deletions
|
@ -35,84 +35,9 @@ function retrieveAssetShowInfo (name, claimId) {
|
||||||
return deferred;
|
return deferred;
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = (app) => {
|
function serveClaimByNameWrapper (name, headers, originalUrl, ip, res) {
|
||||||
// route to serve a specific asset
|
|
||||||
app.get('/:name/:claim_id', ({ headers, ip, originalUrl, params }, res) => {
|
|
||||||
// decide to serve or show
|
|
||||||
const dotIndex = params.claim_id.lastIndexOf('.');
|
|
||||||
if (dotIndex === 0) {
|
|
||||||
logger.error('a file extension with no name was submitted');
|
|
||||||
errorHandlers.handleRequestError('serve', originalUrl, ip, new Error('no claim id provided'), res);
|
|
||||||
// if an image extension was given, serve the image directly
|
|
||||||
} else if (dotIndex > 0) {
|
|
||||||
// google analytics
|
|
||||||
sendGoogleAnalytics('serve', headers, ip, originalUrl);
|
|
||||||
const fileExtension = params.claim_id.substring(dotIndex);
|
|
||||||
const claimId = params.claim_id.substring(0, dotIndex);
|
|
||||||
logger.debug('file extension is:', fileExtension);
|
|
||||||
// begin image-serve processes
|
// begin image-serve processes
|
||||||
retrieveAssetServeInfo(params.name, claimId)
|
serveClaimByName(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']) {
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.catch(error => {
|
|
||||||
errorHandlers.handleRequestError('serve', originalUrl, ip, error, res);
|
|
||||||
});
|
|
||||||
// if no image extension was given, show the asset with details
|
|
||||||
} else if (dotIndex === -1) {
|
|
||||||
// google analytics
|
|
||||||
sendGoogleAnalytics('show', headers, ip, originalUrl);
|
|
||||||
// begin image-show processes
|
|
||||||
retrieveAssetShowInfo(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
|
|
||||||
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) => {
|
|
||||||
// decide to serve or show
|
|
||||||
const dotIndex = params.name.lastIndexOf('.');
|
|
||||||
if (dotIndex === 0) {
|
|
||||||
logger.error('a file extension with no name was submitted');
|
|
||||||
errorHandlers.handleRequestError('serve', originalUrl, ip, new Error('no name provided'), res);
|
|
||||||
// if an image extension was given, serve the image directly
|
|
||||||
} else if (dotIndex > 0) {
|
|
||||||
// google analytics
|
|
||||||
sendGoogleAnalytics('serve', headers, ip, originalUrl);
|
|
||||||
const fileExtension = params.name.substring(dotIndex);
|
|
||||||
const claimName = params.name.substring(0, dotIndex);
|
|
||||||
logger.debug('file extension is:', fileExtension);
|
|
||||||
// begin image-serve processes
|
|
||||||
serveClaimByName(claimName)
|
|
||||||
.then(fileInfo => {
|
.then(fileInfo => {
|
||||||
// check to make sure a file was found
|
// check to make sure a file was found
|
||||||
if (!fileInfo) {
|
if (!fileInfo) {
|
||||||
|
@ -137,10 +62,104 @@ module.exports = (app) => {
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
errorHandlers.handleRequestError('serve', originalUrl, ip, error, res);
|
errorHandlers.handleRequestError('serve', originalUrl, ip, error, res);
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function serveAssetByClaimIdWrapper (name, claimId, headers, originalUrl, ip, res) {
|
||||||
|
retrieveAssetServeInfo(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);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
errorHandlers.handleRequestError('serve', originalUrl, ip, error, res);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = (app) => {
|
||||||
|
// route to serve a specific asset
|
||||||
|
app.get('/:name/:claim_id', ({ headers, ip, originalUrl, params }, res) => {
|
||||||
|
// decide to serve or show
|
||||||
|
const dotIndex = params.claim_id.lastIndexOf('.');
|
||||||
|
if (dotIndex === 0) {
|
||||||
|
logger.error('a file extension with no name was submitted');
|
||||||
|
errorHandlers.handleRequestError('serve', originalUrl, ip, new Error('no claim id provided'), res);
|
||||||
|
// if an image extension was given, serve the image directly
|
||||||
|
} else if (dotIndex > 0) {
|
||||||
|
// google analytics
|
||||||
|
sendGoogleAnalytics('serve', headers, ip, originalUrl);
|
||||||
|
// parse params
|
||||||
|
const fileExtension = params.claim_id.substring(dotIndex);
|
||||||
|
const claimId = params.claim_id.substring(0, dotIndex);
|
||||||
|
logger.debug('file extension is:', fileExtension);
|
||||||
|
// start image-serve processes
|
||||||
|
serveAssetByClaimIdWrapper(params.name, claimId, headers, originalUrl, ip, res);
|
||||||
// if no image extension was given, show the asset with details
|
// if no image extension was given, show the asset with details
|
||||||
} else if (dotIndex === -1) {
|
} else if (dotIndex === -1) {
|
||||||
// google analytics
|
// google analytics
|
||||||
sendGoogleAnalytics('show', headers, ip, originalUrl);
|
sendGoogleAnalytics('show', headers, ip, originalUrl);
|
||||||
|
// for backwards compatability: make sure the client can acept html, if not serve the file.
|
||||||
|
if (headers['accept'] && headers['accept'].split(',').includes('text/html')) {
|
||||||
|
// begin image-show processes
|
||||||
|
retrieveAssetShowInfo(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
|
||||||
|
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);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
// start image-serve processes
|
||||||
|
serveAssetByClaimIdWrapper(params.name, params.claim_id, headers, originalUrl, ip, res);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
// route to serve the winning asset at a claim
|
||||||
|
app.get('/:name', ({ headers, ip, originalUrl, params }, res) => {
|
||||||
|
// decide to serve or show
|
||||||
|
const dotIndex = params.name.lastIndexOf('.');
|
||||||
|
if (dotIndex === 0) {
|
||||||
|
logger.error('a file extension with no name was submitted');
|
||||||
|
errorHandlers.handleRequestError('serve', originalUrl, ip, new Error('no name provided'), res);
|
||||||
|
// if an image extension was given, serve the image directly
|
||||||
|
} else if (dotIndex > 0) {
|
||||||
|
// google analytics
|
||||||
|
sendGoogleAnalytics('serve', headers, ip, originalUrl);
|
||||||
|
// parse name param
|
||||||
|
const fileExtension = params.name.substring(dotIndex);
|
||||||
|
const claimName = params.name.substring(0, dotIndex);
|
||||||
|
logger.debug('file extension is:', fileExtension);
|
||||||
|
// start image-serve processes
|
||||||
|
serveClaimByNameWrapper(claimName, headers, originalUrl, ip, res);
|
||||||
|
// if no image extension was given, show the asset with details
|
||||||
|
} else if (dotIndex === -1) {
|
||||||
|
// google analytics
|
||||||
|
sendGoogleAnalytics('show', headers, ip, originalUrl);
|
||||||
|
// for backwards compatability: make sure the client can receive text/html, or else serve the asset directly
|
||||||
|
if (headers['accept'] && headers['accept'].split(',').includes('text/html')) {
|
||||||
// begin image-show process
|
// begin image-show process
|
||||||
showClaimByName(params.name)
|
showClaimByName(params.name)
|
||||||
.then(fileInfo => {
|
.then(fileInfo => {
|
||||||
|
@ -156,6 +175,10 @@ module.exports = (app) => {
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
errorHandlers.handleRequestError('show', originalUrl, ip, error, res);
|
errorHandlers.handleRequestError('show', originalUrl, ip, error, res);
|
||||||
});
|
});
|
||||||
|
} else {
|
||||||
|
// start image serve process
|
||||||
|
serveClaimByNameWrapper(params.name, headers, originalUrl, ip, res);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
|
@ -3,5 +3,4 @@
|
||||||
<h1 id="title"><a href="/">Spee.ch</a></h1><span >(beta)</span>
|
<h1 id="title"><a href="/">Spee.ch</a></h1><span >(beta)</span>
|
||||||
<a href="https://github.com/lbryio/spee.ch" target="_blank" class="top-bar-right">contribute</a>
|
<a href="https://github.com/lbryio/spee.ch" target="_blank" class="top-bar-right">contribute</a>
|
||||||
<a href="/about" class="top-bar-right">help</a>
|
<a href="/about" class="top-bar-right">help</a>
|
||||||
|
|
||||||
</div>
|
</div>
|
Loading…
Add table
Reference in a new issue