fixed show route get process and api routes

This commit is contained in:
bill bittner 2017-11-27 16:53:32 -08:00
parent dbb552e662
commit 0fd7dd6b08
4 changed files with 10 additions and 15 deletions

View file

@ -13,12 +13,7 @@ module.exports = {
}, },
}; };
// send the file // send the file
if (filePath) { res.status(200).sendFile(filePath, options);
res.status(200).sendFile(filePath, options);
} else {
// res.status(307).redirect(`/api/get/${name}/${claimId}`);
res.status(400).json({success: false, message: 'that claim is not hosted locally by Spee<ch yet'});
}
}, },
showFile (claimInfo, shortId, res) { showFile (claimInfo, shortId, res) {
const openGraphInfo = module.exports.createOpenGraphInfo(claimInfo); const openGraphInfo = module.exports.createOpenGraphInfo(claimInfo);

View file

@ -43,7 +43,7 @@ module.exports = (app) => {
}); });
}); });
// route to see if asset is available locally // route to see if asset is available locally
app.get('/api/local/:name/:claimId', ({ ip, originalUrl, params }, res) => { app.get('/api/check_local_claim/:name/:claimId', ({ ip, originalUrl, params }, res) => {
const name = params.name; const name = params.name;
const claimId = params.claimId; const claimId = params.claimId;
let isLocalFileAvailable = false; let isLocalFileAvailable = false;
@ -52,14 +52,14 @@ module.exports = (app) => {
if (result) { if (result) {
isLocalFileAvailable = true; isLocalFileAvailable = true;
} }
res.status(200).json({status: 'success', isLocalFileAvailable}); res.status(200).json({status: 'success', message: isLocalFileAvailable});
}) })
.catch(error => { .catch(error => {
errorHandlers.handleApiError('get', originalUrl, ip, error, res); errorHandlers.handleApiError('get', originalUrl, ip, error, res);
}); });
}); });
// route to get an asset // route to get an asset
app.get('/api/get/:name/:claimId', ({ ip, originalUrl, params }, res) => { app.get('/api/get_claim/:name/:claimId', ({ ip, originalUrl, params }, res) => {
let fileRecord; let fileRecord;
// resolve and get the claim // resolve and get the claim
db.Claim.resolveClaim(params.name, params.claimId) db.Claim.resolveClaim(params.name, params.claimId)

View file

@ -207,7 +207,7 @@ function serveAssetToClient (claimId, name, res) {
.then(fileInfo => { .then(fileInfo => {
logger.debug('fileInfo:', fileInfo); logger.debug('fileInfo:', fileInfo);
if (fileInfo === NO_FILE) { if (fileInfo === NO_FILE) {
return res.status(307).json({status: 'success', message: 'resource temporarily unavailable'}); res.status(307).redirect(`/api/get_claim/${name}/${claimId}`);
} else { } else {
return serveHelpers.serveFile(fileInfo, claimId, name, res); return serveHelpers.serveFile(fileInfo, claimId, name, res);
} }

View file

@ -49,8 +49,8 @@
failureMessage.innerText = msg; failureMessage.innerText = msg;
}, },
checkClaimAvailability: function (claimName, claimId) { checkClaimAvailability: function (claimName, claimId) {
console.log(`getting ${claimName}#${claimId}}`) console.log(`getting ${claimName}#${claimId}`)
var uri = `/api/local/${claimName}/${claimId}`; var uri = `/api/check_local_claim/${claimName}/${claimId}`;
var xhr = new XMLHttpRequest(); var xhr = new XMLHttpRequest();
var that = this; var that = this;
xhr.open("GET", uri, true); xhr.open("GET", uri, true);
@ -77,8 +77,8 @@
xhr.send(); xhr.send();
}, },
getAsset: function(claimName, claimId) { getAsset: function(claimName, claimId) {
console.log(`getting ${claimName}#${claimId}}`) console.log(`getting ${claimName}#${claimId}`)
var uri = `/api/get/${claimName}/${claimId}`; var uri = `/api/get_claim/${claimName}/${claimId}`;
var xhr = new XMLHttpRequest(); var xhr = new XMLHttpRequest();
var that = this; var that = this;
xhr.open("GET", uri, true); xhr.open("GET", uri, true);
@ -90,7 +90,7 @@
that.showAsset(); that.showAsset();
} else { } else {
console.log('get failed:', response); console.log('get failed:', response);
that.showFailureMessage('Hmmm, looks like no peers have your content. How anti-social!'); that.showFailureMessage(`${response.message}`);
} }
} else { } else {
console.log('xhr.readyState', xhr.readyState); console.log('xhr.readyState', xhr.readyState);