diff --git a/routes/api-routes.js b/routes/api-routes.js index 9be9df97..0127e0e7 100644 --- a/routes/api-routes.js +++ b/routes/api-routes.js @@ -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 => { diff --git a/views/partials/asset.handlebars b/views/partials/asset.handlebars index d09cb1de..ddee94b1 100644 --- a/views/partials/asset.handlebars +++ b/views/partials/asset.handlebars @@ -3,23 +3,9 @@ @@ -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); - that.showAsset(); + 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}`); diff --git a/views/partials/image.handlebars b/views/partials/image.handlebars new file mode 100644 index 00000000..0ad3154b --- /dev/null +++ b/views/partials/image.handlebars @@ -0,0 +1,15 @@ + + + \ No newline at end of file diff --git a/views/partials/video.handlebars b/views/partials/video.handlebars new file mode 100644 index 00000000..0ad3154b --- /dev/null +++ b/views/partials/video.handlebars @@ -0,0 +1,15 @@ + + + \ No newline at end of file