diff --git a/helpers/lbryApi.js b/helpers/lbryApi.js index 6170d83e..a9314401 100644 --- a/helpers/lbryApi.js +++ b/helpers/lbryApi.js @@ -1,10 +1,7 @@ -// load dependencies var path = require('path'); var axios = require('axios'); -// helper function to filter an array of claims for only free, public claims function filterForFreePublicClaims(claimsListArray){ - //console.log(">> filterForFreePublicClaims, claimsListArray:", claimsListArray); if (!claimsListArray) { return null; }; @@ -14,7 +11,7 @@ function filterForFreePublicClaims(claimsListArray){ }); return freePublicClaims; } -// helper function to decide if a claim is free and public + function isFreePublicClaim(claim){ console.log(">> isFreePublicClaim, claim:", claim); if ((claim.value.stream.metadata.license === 'Public Domain' || claim.value.stream.metadata.license === 'Creative Commons') && @@ -24,7 +21,7 @@ function isFreePublicClaim(claim){ return false; } } -// helper function to order a set of claims + function orderTopClaims(claimsListArray){ console.log(">> orderTopClaims, claimsListArray:"); claimsListArray.sort(function(claimA, claimB){ @@ -46,8 +43,8 @@ function getClaimWithUri(uri, resolve, reject){ ).then(function (getUriResponse) { console.log(">> 'get claim' success..."); //check to make sure the daemon didn't just time out - if (getUriResponse.data.result.error === "Timeout"){ - reject("get request to lbry daemon timed out"); + if (getUriResponse.data.result.error){ + reject(getUriResponse.data.result.error); } console.log(">> response data:", getUriResponse.data); console.log(">> dl path =", getUriResponse.data.result.download_path) @@ -57,9 +54,9 @@ function getClaimWithUri(uri, resolve, reject){ */ resolve(getUriResponse.data.result.download_path); }).catch(function(getUriError){ - console.log(">> 'get' error:", getUriError.response.data); + console.log(">> 'get' error."); // reject the promise with an error message - reject(getUriError.response.data.error.message); + reject(getUriError); return; }); } @@ -82,16 +79,10 @@ module.exports = { console.log(">> 'publish' success"); // return the claim we got resolve(response.data); - return; }).catch(function(error){ // receive response from LBRY console.log(">> 'publish' error"); - if (error.response.data.error){ - reject(error.response.data.error); - } else { - reject(error); - } - return; + reject(error); }) }) return deferred; @@ -134,16 +125,8 @@ module.exports = { getClaimWithUri(freePublicClaimUri, resolve, reject); }) .catch(function(error){ - console.log(">> 'claim_list' error:", error); - // reject the promise with an approriate message - if (error.code === "ECONNREFUSED"){ - reject("Connection refused. The daemon may not be running.") - } else if (error.response.data.error) { - reject(error.response.data.error); - } else { - reject(error); - }; - return; + console.log(">> 'claim_list' error."); + reject(error); }); }); // 3. return the promise @@ -200,14 +183,8 @@ module.exports = { */ resolve(orderedPublicClaims); }).catch(function(error){ - console.log(">> 'claim_list' error:", error); - if (error.code === "ECONNREFUSED"){ - reject("Connection refused. The daemon may not be running.") - } else if (error.response.data.error) { - reject(error.response.data.error); - } else { - reject(error); - }; + console.log(">> 'claim_list' error"); + reject(error); }) }); return deferred; diff --git a/helpers/queueApi.js b/helpers/queueApi.js deleted file mode 100644 index 6f8c5fef..00000000 --- a/helpers/queueApi.js +++ /dev/null @@ -1,22 +0,0 @@ -// require amqp library -var amqp = require('amqplib/callback_api'); - -module.exports = { - addNewTaskToQueue: function(task){ - // connect to RabbitMQ server - amqp.connect('amqp://localhost', function(err, conn) { - // create a channel - conn.createChannel(function(err, ch) { - var q = 'task_queue2'; // declaring a que is idempotent (it will only be created if it doesnt already exist) - var msg = task || "request received with no task!"; - // declare a queue - ch.assertQueue(q, {durable: true}); - // publish a message to the queue - ch.sendToQueue(q, new Buffer.from(msg), {persistent: true}); - console.log(` [x] Sent '${msg}' to ${q}`); - }); - // close the connection and exit - setTimeout(function() {conn.close() }, 500); - }); - } -} \ No newline at end of file diff --git a/helpers/routeHelpers.js b/helpers/routeHelpers.js new file mode 100644 index 00000000..6aa294c4 --- /dev/null +++ b/helpers/routeHelpers.js @@ -0,0 +1,15 @@ +var path = require('path'); + +module.exports = { + handleRequestError: function(error, res) { + if ((error === "NO_CLAIMS") || (error === "NO_FREE_PUBLIC_CLAIMS")){ + res.status(307).sendFile(path.join(__dirname, '../public', 'noClaims.html')); + } else if (error.response){ + res.status(error.response.status).send(error.response.data.error.message); + } else if (error.code === "ECONNREFUSED") { + res.status(400).send("Connection refused. The daemon may not be running."); + } else { + res.status(400).send(error.toString()); + }; + } +} diff --git a/helpers/socketHelpers.js b/helpers/socketHelpers.js new file mode 100644 index 00000000..0a4f41fe --- /dev/null +++ b/helpers/socketHelpers.js @@ -0,0 +1,57 @@ +var fs = require('fs'); +var lbryApi = require('../helpers/lbryApi.js'); + +function handlePublishError(error) { + if (error.code === "ECONNREFUSED"){ + return "Connection refused. The daemon may not be running."; + } else if (error.response.data.error) { + return error.response.data.error; + } else { + return error; + }; +} + +function createPublishParams(name, filepath, license, nsfw) { + var publishParams = { + "name": name, + "file_path": filepath, + "bid": 0.1, + "metadata": { + "description": name + " published via spee.ch", + "title": name, + "author": "spee.ch", + "language": "en", + "license": license, + "nsfw": (nsfw.toLowerCase() === "true") + } + }; + return publishParams; +} + +function deleteTemporaryFile(filepath) { + fs.unlink(filepath, function(err) { + if (err) throw err; + console.log('successfully deleted ' + filepath); + }); +} + +module.exports = { + publish: function(name, filepath, license, nsfw, socket) { + // update the client + socket.emit("publish-status", "Your image is being published (this might take a second)..."); + // create the publish object + var publishParams = createPublishParams(name, filepath, license, nsfw); + // get a promise to publish + lbryApi.publishClaim(publishParams) + .then(function(data){ + console.log("publish promise success. Tx info:", data) + socket.emit("publish-complete", {name: name, result: data.result}); + deleteTemporaryFile(filepath); + }) + .catch(function(error){ + console.log("error:", error); + socket.emit("publish-failure", handlePublishError(error)); + deleteTemporaryFile(filepath); + }); + } +} \ No newline at end of file diff --git a/package.json b/package.json index a3ef74fd..2300bd02 100644 --- a/package.json +++ b/package.json @@ -23,10 +23,8 @@ }, "homepage": "https://github.com/billbitt/spee.ch-backend#readme", "dependencies": { - "amqplib": "^0.5.1", "axios": "^0.16.1", "body-parser": "^1.17.1", - "connect-multiparty": "^2.0.0", "express": "^4.15.2", "nodemon": "^1.11.0", "socket.io": "^2.0.1", diff --git a/public/assets/css/style.css b/public/assets/css/style.css new file mode 100644 index 00000000..e69de29b diff --git a/public/favicon.ico b/public/assets/img/favicon.ico similarity index 100% rename from public/favicon.ico rename to public/assets/img/favicon.ico diff --git a/public/claimPlus.html b/public/claimPlus.html deleted file mode 100644 index 678eedd9..00000000 --- a/public/claimPlus.html +++ /dev/null @@ -1,36 +0,0 @@ - - -
- - - -spee.ch is a single-serving site that reads and publishes images to and from the LBRY blockchain.
-Your image is being retrieved
-spee.ch is a single-serving site that reads and publishes images to and from the LBRY blockchain.
-If you would like to help make spee.ch amazing, join our slack channel.
-We are currently in need of a designer to help with styling spee.ch's front end, but all help is welcome!
- -Note: these are being used for testing durring spee.ch development and may not be maintained
-If you would like to help make spee.ch amazing, join our slack channel.
+We are currently in need of a designer to help with styling spee.ch's front end, but all help is welcome!
+Note: these are being used for testing durring spee.ch development and may not be maintained
+Spee.ch is young and under continuous development so it will have bugs. Please leave an issue on our github if you experience a problem or have suggestions.
+