diff --git a/routes/api-routes.js b/routes/api-routes.js index 0127e0e7..95488c57 100644 --- a/routes/api-routes.js +++ b/routes/api-routes.js @@ -72,12 +72,19 @@ module.exports = (app) => { return Promise.all([fileData, getClaim(`${params.name}#${params.claimId}`)]); }) .then(([ fileData, getResult ]) => { + if (getResult.completed !== true) { + return Promise.all([null, getResult]); // pass get results to next function + } fileData = addGetResultsToFileData(fileData, getResult); - return Promise.all([db.File.create(fileData), getResult]); // insert a record for the claim into the File table + return Promise.all([db.File.create(fileData), getResult]); // note: make this 'upsert' ? }) .then(([ fileRecord, {message, completed} ]) => { res.status(200).json({ status: 'success', message, completed }); - logger.debug('File record successfully created'); + if (fileRecord) { + logger.debug('File record successfully created'); + } else { + logger.debug('No file record created'); + } }) .catch(error => { errorHandlers.handleApiError('get', originalUrl, ip, error, res); diff --git a/views/partials/asset.handlebars b/views/partials/asset.handlebars index 76836245..66e57c3e 100644 --- a/views/partials/asset.handlebars +++ b/views/partials/asset.handlebars @@ -1,7 +1,6 @@
Sit tight, we're combing the LBRY blockchain for your asset!
-Hooray! We found peers with copies of your asset and we are downloading it from the blockchain now.
Unfortunately, we couldn't download your asset from LBRY. You can help us out by sharing the below error message in the LBRY discord.
@@ -57,12 +56,6 @@ const searchMessage = document.getElementById('searching-message'); searchMessage.hidden = false; }, - showInProgressMessage: function () { - const searchMessage = document.getElementById('searching-message'); - const inProgressMessage = document.getElementById('in-progress-message'); - searchMessage.hidden = true; - inProgressMessage.hidden = false; - }, showFailureMessage: function (msg) { console.log(msg); const searchMessage = document.getElementById('searching-message'); @@ -111,14 +104,7 @@ 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 has not finished downloading the asset'); - that.showInProgressMessage(); - setTimeout(that.getAsset.bind(that, claimName, claimId), 5000); - } + that.showAsset(); } else { console.log('get failed:', response); that.showFailureMessage(`${response.message}`); @@ -130,6 +116,33 @@ // Initiate a multipart/form-data upload xhr.send(); }, + checkIfAssetIsFullyDownloaded: function(claimName, claimId) { + // make a get request, and see fi the asset is fully downloaded. + console.log(`getting ${claimName}#${claimId}`) + var uri = `/api/get_claim/${claimName}/${claimId}`; + var xhr = new XMLHttpRequest(); + var that = this; + xhr.open("GET", uri, true); + xhr.onreadystatechange = function() { + if (xhr.readyState == 4) { + 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'); + } else { + console.log('lbrynet has not finished downloading the asset'); + setTimeout(that.checkIfAssetIsFullyDownloaded.bind(that, claimName, claimId), 5000); + } + } else { + console.log('get failed:', response); + } + } else { + console.log('xhr.readyState', xhr.readyState); + } + }; + xhr.send(); + } } getAssetFunctions.checkClaimAvailability('{{claimInfo.name}}', '{{claimInfo.claimId}}');