added /api/get route

This commit is contained in:
bill bittner 2017-11-20 15:51:05 -08:00
parent ab3d6243e8
commit 859654e6e4
7 changed files with 132 additions and 37 deletions

View file

@ -6,11 +6,10 @@ function handleLbrynetResponse ({ data }, resolve, reject) {
if (data.result) {
// check for an error
if (data.result.error) {
logger.debug('Lbrynet api error:', data.result.error);
logger.warn('Lbrynet api error:', data.result.error);
reject(data.result.error);
return;
};
// logger.debug('data.result', data.result);
resolve(data.result);
return;
}

View file

@ -4,18 +4,15 @@ const config = require('../config/speechConfig.js');
const multipartMiddleware = multipart({uploadDir: config.files.uploadDirectory});
const db = require('../models');
const { publish } = require('../controllers/publishController.js');
const { getClaimList, resolveUri } = require('../helpers/lbryApi.js');
const { getClaimList, resolveUri, getClaim } = require('../helpers/lbryApi.js');
const { createPublishParams, validateApiPublishRequest, validatePublishSubmission, cleanseChannelName, checkClaimNameAvailability, checkChannelAvailability } = require('../helpers/publishHelpers.js');
const errorHandlers = require('../helpers/errorHandlers.js');
const { postToStats, sendGoogleAnalytics } = require('../controllers/statsController.js');
const { postToStats } = require('../controllers/statsController.js');
const { authenticateOrSkip } = require('../auth/authentication.js');
module.exports = (app) => {
// route to run a claim_list request on the daemon
app.get('/api/claim_list/:name', ({ headers, ip, originalUrl, params }, res) => {
// google analytics
sendGoogleAnalytics('SERVE', headers, ip, originalUrl);
// serve the content
app.get('/api/claim_list/:name', ({ ip, originalUrl, params }, res) => {
getClaimList(params.name)
.then(claimsList => {
postToStats('serve', originalUrl, ip, null, null, 'success');
@ -25,9 +22,22 @@ module.exports = (app) => {
errorHandlers.handleApiError('claim_list', originalUrl, ip, error, res);
});
});
// route to get an asset
app.get('/api/get/:name/:claimId', ({ ip, originalUrl, params }, res) => {
if (!params.name || !params.claimId) {
res.status(400).json({success: false, message: 'provide a claimId and/or a name'});
}
getClaim(`${params.name}#${params.claimId}`)
.then(result => {
res.status(200).json({status: 'success', message: result});
})
.catch(error => {
errorHandlers.handleApiError('get', originalUrl, ip, error, res);
});
});
// route to check whether spee.ch has published to a claim
app.get('/api/isClaimAvailable/:name', ({ params }, res) => {
// send response
checkClaimNameAvailability(params.name)
.then(result => {
if (result === true) {
@ -59,9 +69,6 @@ module.exports = (app) => {
});
// route to run a resolve request on the daemon
app.get('/api/resolve/:uri', ({ headers, ip, originalUrl, params }, res) => {
// google analytics
sendGoogleAnalytics('SERVE', headers, ip, originalUrl);
// serve content
resolveUri(params.uri)
.then(resolvedUri => {
postToStats('serve', originalUrl, ip, null, null, 'success');
@ -161,8 +168,7 @@ module.exports = (app) => {
});
// route to get a short claim id from long claim Id
app.get('/api/shortClaimId/:longId/:name', ({ originalUrl, ip, params }, res) => {
// serve content
app.get('/api/shortClaimId/:longId/:name', ({ params }, res) => {
db.Claim.getShortClaimIdFromLongClaimId(params.longId, params.name)
.then(shortId => {
res.status(200).json(shortId);
@ -174,7 +180,6 @@ module.exports = (app) => {
});
// route to get a short channel id from long channel Id
app.get('/api/shortChannelId/:longId/:name', ({ ip, originalUrl, params }, res) => {
// serve content
db.Certificate.getShortChannelIdFromLongChannelId(params.longId, params.name)
.then(shortId => {
logger.debug('sending back short channel id', shortId);

View file

@ -17,7 +17,7 @@
{{ googleAnalytics }}
</head>
<body id="showlite-body">
{{{ body }}}
{{> getAsset}}
<script src="/assets/js/showFunctions.js"></script>
</body>
</html>

View file

@ -0,0 +1,91 @@
<div id="asset-holder">
<div id="peer-info">
<p id="peer-search-placeholder">Searching for peers for this asset...</p>
<p id="peer-search-success" hidden="true">Number of peers with content:<span id="peer-number">?</span></p>
<p id="peer-search-failure" hidden="true">Unfortunately, no peers could be located with that asset</p>
</div>
<div id="asset">
<div id="video-display" hidden="true">
</div>
<div id="gifv-display" hidden="true">
</div>
<div id="gif-display" hidden="true">
</div>
<div id="static-image-display" hidden="true">
</div>
</div>
</div>
<script type="text/javascript">
const getAssetFunctions = {
showPeerListSuccess: function (numberOfPeers) {
const peerSearchPlaceholder = document.getElementById('peer-search-placeholder');
const peerSearchSuccess = document.getElementById('peer-search-success');
const peerSearchNumber = document.getElementById('peer-search-number');
peerSearchPlaceholder.hidden = true;
peerSearchSuccess.hidden = false;
peerSearchNumber.innerText = numberOfPeers;
},
showPeerListFailure: function () {
const peerSearchPlaceholder = document.getElementById('peer-search-placeholder');
const peerSearchFailure = document.getElementById('peer-search-failure');
peerSearchPlaceholder.hidden = true;
peerSearchFailure.hidden = false;
},
showAsset: function (claimName, claimId) {
},
showFailureMessage: function (msg) {
console.log(msg);
},
getAsset: function(claimName, claimId) {
console.log(`getting ${claimName}#${claimId}}`)
var uri = `/api/get/${claimName}/${claimId}`;
var xhr = new XMLHttpRequest();
xhr.open("GET", uri, true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
console.log('get returned successfully')
this.showAsset(claimName, claimId);
} else {
console.log('get failed:', xhr.response);
}
} else {
console.log('xhr.readyState', xhr.readyState);
this.showFailureMessage(xhr.readyState.message);
}
};
// Initiate a multipart/form-data upload
xhr.send();
},
getPeerList: function(claimName, claimId) {
var uri = `/api/peer_list/${claimName}/${claimId}`;
var xhr = new XMLHttpRequest();
xhr.open("GET", uri, true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
console.log('peer list retrieved:', JSON.parse(xhr.response).message);
this.showPeerListSuccess(JSON.parse(xhr.response).message);
this.getAsset(claimName, claimId);
} else {
console.log(xhr.response);
console.log('unfortunately no peers could be found');
this.showPeerListFailure(JSON.parse(xhr.response).message);
}
} else {
console.log('xhr.readyState', xhr.readyState);
}
};
// Initiate a multipart/form-data upload
xhr.send();
}
}
getAssetFunctions.getPeerList({{fileInfo.claimName}}, {{fileInfo.claimId}})
</script>

View file

@ -0,0 +1,17 @@
<div class="row row--tall row--padded">
<div class="column column--10">
<!-- title -->
<span class="text--large">{{fileInfo.title}}</span>
</div>
<div class="column column--5 column--sml-10 align-content-top">
<!-- asset -->
<div class="row row--padded">
{{> getAsset}}
</div>
</div><div class="column column--5 column--sml-10 align-content-top">
<!-- details -->
<div class="row row--padded">
{{> assetInfo}}
</div>
</div>
</div>

View file

@ -0,0 +1,2 @@
{{> getAsset }}
<a class="link--primary fine-print" href="/{{fileInfo.claimId}}/{{fileInfo.name}}">hosted via spee&lt;h</a>

View file

@ -1,21 +1,2 @@
{{#ifConditional fileInfo.fileType '===' 'video/mp4'}}
{{#ifConditional fileInfo.fileExt '===' '.gifv'}}
<video class="showlite-asset" autoplay loop muted>
<source src="/{{fileInfo.claimId}}/{{fileInfo.name}}.{{fileInfo.fileExt}}">
{{!--fallback--}}
Your browser does not support the <code>video</code> element.
</video>
{{else}}
<video class="showlite-asset" controls id="video-player">
<source src="/{{fileInfo.claimId}}/{{fileInfo.name}}.{{fileInfo.fileExt}}">
{{!--fallback--}}
Your browser does not support the <code>video</code> element.
</video>
{{/ifConditional}}
<br/>
{{> asset }}
<a class="link--primary fine-print" href="/{{fileInfo.claimId}}/{{fileInfo.name}}">hosted via spee&lt;h</a>
{{else}}
<a href="/{{fileInfo.claimId}}/{{fileInfo.name}}">
<img class="showlite-asset" src="/{{fileInfo.claimId}}/{{fileInfo.name}}.{{fileInfo.fileExt}}" alt="{{fileInfo.fileName}}"/>
</a>
{{/ifConditional}}