updated api/get_claim route to return message and status

This commit is contained in:
bill bittner 2017-11-28 12:17:22 -08:00
parent fe49f7e7de
commit 8400ca5110
4 changed files with 55 additions and 33 deletions

View file

@ -10,13 +10,13 @@ const errorHandlers = require('../helpers/errorHandlers.js');
const { postToStats } = require('../controllers/statsController.js');
const { authenticateOrSkip } = require('../auth/authentication.js');
function addGetResultsToFileRecord (fileInfo, getResult) {
function addGetResultsToFileData (fileInfo, getResult) {
fileInfo.fileName = getResult.file_name;
fileInfo.filePath = getResult.download_path;
return fileInfo;
}
function createFileRecord ({ name, claimId, outpoint, height, address, nsfw, contentType }) {
function createFileData ({ name, claimId, outpoint, height, address, nsfw, contentType }) {
return {
name,
claimId,
@ -60,24 +60,23 @@ module.exports = (app) => {
});
// route to get an asset
app.get('/api/get_claim/:name/:claimId', ({ ip, originalUrl, params }, res) => {
let fileRecord;
// resolve and get the claim
// resolve the claim
db.Claim.resolveClaim(params.name, params.claimId)
.then(resolveResult => {
// make sure a claim actually exists at that uri
if (!resolveResult) {
throw new Error('No matching uri found in Claim table');
}
fileRecord = createFileRecord(resolveResult);
let fileData = createFileData(resolveResult);
// get the claim
return getClaim(`${params.name}#${params.claimId}`);
return Promise.all([fileData, getClaim(`${params.name}#${params.claimId}`)]);
})
.then(getResult => {
fileRecord = addGetResultsToFileRecord(fileRecord, getResult);
// insert a record for the claim into the File table
return db.File.create(fileRecord);
.then(([ fileData, getResult ]) => {
fileData = addGetResultsToFileData(fileData, getResult);
return Promise.all([db.File.create(fileData), getResult]); // insert a record for the claim into the File table
})
.then(() => {
res.status(200).json({status: 'success', message: 'content was successfully retrieved via lbrynet'});
.then(([ fileRecord, {message, completed} ]) => {
res.status(200).json({ status: 'success', message, completed });
logger.debug('File record successfully created');
})
.catch(error => {

View file

@ -3,23 +3,9 @@
<p id="failure-message" hidden="true"></p>
<div id="asset-holder" hidden="true">
{{#ifConditional claimInfo.contentType '===' 'video/mp4'}}
<video id="video-player" controls poster="{{claimInfo.thumbnail}}">
<source id="asset">
<!--fallback-->
Your browser does not support the <code>video</code> element.
</video>
<!--dynamically resize video player-->
<script type="text/javascript">
document.addEventListener('DOMContentLoaded', resizeVideoPlayer)
window.addEventListener("resize", resizeVideoPlayer);
function resizeVideoPlayer() {
const div = document.getElementById('video-player');
const width = div.offsetWidth;
div.height = (width * 9 / 16);
}
</script>
{{> video}}
{{else}}
<img id="asset" class="asset" />
{{> image}}
{{/ifConditional}}
</div>
</div>
@ -34,6 +20,7 @@
searchMessage.hidden = true;
assetHolder.hidden = false;
asset.setAttribute('src', `/{{claimInfo.claimId}}/{{claimInfo.name}}.{{claimInfo.fileExt}}`);
asset.load();
},
showSearchMessage: function (msg) {
console.log(msg);
@ -58,12 +45,12 @@
if (xhr.readyState == 4) {
const response = JSON.parse(xhr.response);
if (xhr.status == 200) {
if (response.message === false) {
that.showSearchMessage();
that.getAsset(claimName, claimId)
} else {
if (response.message === true) {
console.log('local asset is available on spee.ch')
that.showAsset();
} else {
that.showSearchMessage();
that.getAsset(claimName, claimId)
}
} else {
console.log('get failed:', response);
@ -87,7 +74,13 @@
const response = JSON.parse(xhr.response);
if (xhr.status == 200) {
console.log('get returned successfully', response);
if (response.completed === true) {
console.log('lbrynet has finished downloading the asset');
that.showAsset();
} else {
console.log('lbrynet is still downloading the asset');
setTimeout(getAsset, 5000);
}
} else {
console.log('get failed:', response);
that.showFailureMessage(`${response.message}`);

View file

@ -0,0 +1,15 @@
<video id="video-player" controls poster="{{claimInfo.thumbnail}}">
<source id="asset">
<!--fallback-->
Your browser does not support the <code>video</code> element.
</video>
<!--dynamically resize video player-->
<script type="text/javascript">
document.addEventListener('DOMContentLoaded', resizeVideoPlayer)
window.addEventListener("resize", resizeVideoPlayer);
function resizeVideoPlayer() {
const div = document.getElementById('video-player');
const width = div.offsetWidth;
div.height = (width * 9 / 16);
}
</script>

View file

@ -0,0 +1,15 @@
<video id="video-player" controls poster="{{claimInfo.thumbnail}}">
<source id="asset">
<!--fallback-->
Your browser does not support the <code>video</code> element.
</video>
<!--dynamically resize video player-->
<script type="text/javascript">
document.addEventListener('DOMContentLoaded', resizeVideoPlayer)
window.addEventListener("resize", resizeVideoPlayer);
function resizeVideoPlayer() {
const div = document.getElementById('video-player');
const width = div.offsetWidth;
div.height = (width * 9 / 16);
}
</script>