diff --git a/helpers/socketHelpers.js b/controllers/publishController.js similarity index 78% rename from helpers/socketHelpers.js rename to controllers/publishController.js index 430aa683..03564076 100644 --- a/helpers/socketHelpers.js +++ b/controllers/publishController.js @@ -1,20 +1,10 @@ var fs = require('fs'); -var lbryApi = require('./lbryApi.js'); +var lbryApi = require('../helpers/libraries/lbryApi.js'); var config = require('config'); -var db = require("../models"); +var errorHandlers = require("../helpers/libraries/errorHandlers.js"); var walledAddress = config.get('WalletConfig.lbryAddress'); -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, @@ -26,7 +16,7 @@ function createPublishParams(name, filePath, license, nsfw) { "author": "spee.ch", "language": "en", "license": license, - "nsfw": nsfw + "nsfw": (nsfw.toLowerCase() === "true") }, "claim_address": walledAddress, "change_address": walledAddress //requires daemon 0.12.2rc1 or above @@ -43,7 +33,6 @@ function deleteTemporaryFile(filePath) { module.exports = { publish: function(name, filePath, fileType, license, nsfw, socket, visitor) { - nsfw = (nsfw.toLowerCase() === "true"); // update the client socket.emit("publish-status", "Your image is being published (this might take a second)..."); // send to analytics @@ -60,7 +49,7 @@ module.exports = { .catch(function(error){ visitor.event("Publish Route", "Publish Failure", filePath).send(); console.log("error:", error); - socket.emit("publish-failure", handlePublishError(error)); + socket.emit("publish-failure", errorHandlers.handlePublishError(error)); deleteTemporaryFile(filePath); }); } diff --git a/helpers/lbryHelpers.js b/controllers/serveController.js similarity index 56% rename from helpers/lbryHelpers.js rename to controllers/serveController.js index 1cf4d540..324cfd6c 100644 --- a/helpers/lbryHelpers.js +++ b/controllers/serveController.js @@ -1,77 +1,8 @@ -var path = require('path'); -var axios = require('axios'); -var lbryApi = require('./lbryApi'); +var lbryApi = require('../helpers/libraries/lbryApi.js'); var db = require("../models"); -function filterForFreePublicClaims(claimsListArray){ - //console.log("claims list:", claimsListArray) - if (!claimsListArray) { - return null; - }; - var freePublicClaims = claimsListArray.filter(function(claim){ - if (!claim.value){ - return false; - } - return (((claim.value.stream.metadata.license.indexOf('Public Domain') != -1) || (claim.value.stream.metadata.license.indexOf('Creative Commons') != -1)) && - (!claim.value.stream.metadata.fee || claim.value.stream.metadata.fee === 0)); - }); - return freePublicClaims; -} - -function isFreePublicClaim(claim){ - console.log(">> isFreePublicClaim? claim:", claim); - if ((claim.value.stream.metadata.license === 'Public Domain' || claim.value.stream.metadata.license === 'Creative Commons') && - (!claim.value.stream.metadata.fee || claim.value.stream.metadata.fee.amount === 0)) { - return true; - } else { - return false; - } -} - -function orderTopClaims(claimsListArray){ - console.log(">> orderTopClaims"); - claimsListArray.sort(function(claimA, claimB){ - if (claimA.amount === claimB.amount){ - return (claimA.height > claimB.height); - } else { - return (claimA.amount < claimB.amount); - } - }) - return claimsListArray; -} - -function getAllFreePublicClaims(claimName){ - var deferred = new Promise(function(resolve, reject){ - // make a call to the daemon to get the claims list - lbryApi.getClaimsList(claimName) - .then(function(data){ - var claimsList = data.result.claims; - console.log(">> Number of claims:", claimsList.length) - // return early if no claims were found - if (claimsList.length === 0){ - reject("NO_CLAIMS"); - console.log("exiting due to lack of claims"); - return; - } - // filter the claims to return only free, public claims - var freePublicClaims = filterForFreePublicClaims(claimsList); - // return early if no free, public claims were found - if (!freePublicClaims || (freePublicClaims.length === 0)){ - reject("NO_FREE_PUBLIC_CLAIMS"); - console.log("exiting due to lack of free or public claims"); - return; - } - // order the claims - var orderedPublicClaims = orderTopClaims(freePublicClaims); - // resolve the promise - resolve(orderedPublicClaims); - }).catch(function(error){ - console.log(">> 'claim_list' error"); - reject(error); - }); - }); - return deferred; -} +var getAllFreePublicClaims = require("../helpers/functions/getAllFreePublicClaims.js"); +var isFreePublicClaim = require("../helpers/functions/isFreePublicClaim.js"); function getClaimAndHandleResponse(claimUri, resolve, reject){ lbryApi.getClaim(claimUri) @@ -87,7 +18,7 @@ function getClaimAndHandleResponse(claimUri, resolve, reject){ } module.exports = { - getClaimBasedOnNameOnly: function(claimName){ + getClaimByName: function(claimName){ var deferred = new Promise(function (resolve, reject){ console.log(">> lbryHelpers >> getClaim BasedOnNameOnly:", claimName); // get all free public claims @@ -125,7 +56,7 @@ module.exports = { }); return deferred; }, - getClaimBasedOnUri: function(claimName, claimId){ + getClaimByClaimId: function(claimName, claimId){ var deferred = new Promise(function (resolve, reject){ var uri = claimName + "#" + claimId; console.log(">> lbryHelpers >> getClaimBasedOnUri:", uri); @@ -167,8 +98,5 @@ module.exports = { }); }); return deferred; - }, - getAllClaims: function(claimName){ - return getAllFreePublicClaims(claimName); } } diff --git a/controllers/showController.js b/controllers/showController.js new file mode 100644 index 00000000..04d81f0e --- /dev/null +++ b/controllers/showController.js @@ -0,0 +1,7 @@ +var getAllFreePublicClaims = require("../helpers/functions/getAllFreePublicClaims.js"); + +module.exports = { + getAllClaims: function(claimName){ + return getAllFreePublicClaims(claimName); // to-do: does this need to be returned? + } +} \ No newline at end of file diff --git a/helpers/functions/getAllFreePublicClaims.js b/helpers/functions/getAllFreePublicClaims.js new file mode 100644 index 00000000..f9e1f0d6 --- /dev/null +++ b/helpers/functions/getAllFreePublicClaims.js @@ -0,0 +1,61 @@ +var isFreePublicClaim = require("./isFreePublicClaim.js"); +var lbryApi = require('../libraries/lbryApi.js'); + +function filterForFreePublicClaims(claimsListArray){ + //console.log("claims list:", claimsListArray) + if (!claimsListArray) { + return null; + }; + var freePublicClaims = claimsListArray.filter(function(claim){ + if (!claim.value){ + return false; + } + return (isFreePublicClaim(claim)); + }); + return freePublicClaims; +} + +function orderTopClaims(claimsListArray){ + console.log(">> orderTopClaims"); + claimsListArray.sort(function(claimA, claimB){ + if (claimA.amount === claimB.amount){ + return (claimA.height > claimB.height); + } else { + return (claimA.amount < claimB.amount); + } + }) + return claimsListArray; +} + +module.exports = function(claimName){ + var deferred = new Promise(function(resolve, reject){ + // make a call to the daemon to get the claims list + lbryApi.getClaimsList(claimName) + .then(function(result){ + var claimsList = result.claims; + console.log(">> Number of claims:", claimsList.length) + // return early if no claims were found + if (claimsList.length === 0){ + reject("NO_CLAIMS"); + console.log("exiting due to lack of claims"); + return; + } + // filter the claims to return only free, public claims + var freePublicClaims = filterForFreePublicClaims(claimsList); + // return early if no free, public claims were found + if (!freePublicClaims || (freePublicClaims.length === 0)){ + reject("NO_FREE_PUBLIC_CLAIMS"); + console.log("exiting due to lack of free or public claims"); + return; + } + // order the claims + var orderedPublicClaims = orderTopClaims(freePublicClaims); + // resolve the promise + resolve(orderedPublicClaims); + }).catch(function(error){ + console.log(">> 'claim_list' error"); + reject(error); + }); + }); + return deferred; +} \ No newline at end of file diff --git a/helpers/functions/isFreePublicClaim.js b/helpers/functions/isFreePublicClaim.js new file mode 100644 index 00000000..3915b564 --- /dev/null +++ b/helpers/functions/isFreePublicClaim.js @@ -0,0 +1,11 @@ + +module.exports = function(claim){ + console.log(">> isFreePublicClaim? claim:", claim); + if (((claim.value.stream.metadata.license.indexOf("Public Domain") != -1 ) || (claim.value.stream.metadata.license.indexOf("Creative Commons") != -1 )) + && + (!claim.value.stream.metadata.fee || claim.value.stream.metadata.fee.amount === 0)) { + return true; + } else { + return false; + } +}; \ No newline at end of file diff --git a/helpers/routeHelpers.js b/helpers/libraries/errorHandlers.js similarity index 64% rename from helpers/routeHelpers.js rename to helpers/libraries/errorHandlers.js index f3f43814..031e1ebc 100644 --- a/helpers/routeHelpers.js +++ b/helpers/libraries/errorHandlers.js @@ -1,5 +1,3 @@ -var path = require('path'); - module.exports = { handleRequestError: function(error, res) { if ((error === "NO_CLAIMS") || (error === "NO_FREE_PUBLIC_CLAIMS")){ @@ -11,5 +9,14 @@ module.exports = { } else { res.status(400).send(error.toString()); }; - } + }, + handlePublishError: function(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; + }; +} } diff --git a/helpers/lbryApi.js b/helpers/libraries/lbryApi.js similarity index 75% rename from helpers/lbryApi.js rename to helpers/libraries/lbryApi.js index d4c27f81..56962725 100644 --- a/helpers/lbryApi.js +++ b/helpers/libraries/lbryApi.js @@ -1,5 +1,5 @@ var axios = require('axios'); -var db = require("../models"); +var db = require("../../models"); module.exports = { publishClaim: function(publishParams, fileType){ @@ -9,19 +9,20 @@ module.exports = { "method": "publish", "params": publishParams }).then(function (response) { - console.log(">> 'publish' success", response); + console.log(">> 'publish' success"); + var result = response.data.result; db.File.create({ name: publishParams.name, - claim_id: response.data.result.claim_id, - outpoint: response.data.result.outpoint, + claim_id: result.claim_id, + outpoint: result.txid + ":" + result.nout, file_name: "test", - file_path: publishParams.filePath, + file_path: publishParams.file_path, file_type: fileType, - nsfw: nsfw, + nsfw: publishParams.metadata.nsfw, }).catch(function(error){ console.log('An error occurred when writing to the MySQL database:', error); }); - resolve(response.data.result); + resolve(result); }).catch(function(error){ console.log(">> 'publish' error"); reject(error); @@ -36,7 +37,7 @@ module.exports = { "method": "get", "params": { "uri": uri, "timeout": 20} }).then(function (response) { - console.log(">> 'get' success", response.data); + console.log(">> 'get' success"); //check to make sure the daemon didn't just time out if (response.data.result.error){ reject(response.data.result.error); @@ -45,24 +46,22 @@ module.exports = { note: put in a check to make sure we do not resolve until the download is actually complete (response.data.completed === true) */ // save a record of the file to the Files table + var result = response.data.result db.File.create({ - name: response.data.result.name, - claim_id: response.data.result.claim_id, - outpoint: response.data.result.outpoint, - file_name: response.data.result.file_name, - file_path: response.data.result.download_path, - file_type: response.data.result.mime_type, - nsfw: response.data.result.metadata.stream.metadata.nsfw, + name: result.name, + claim_id: result.claim_id, + outpoint: result.outpoint, + file_name: result.file_name, + file_path: result.download_path, + file_type: result.mime_type, + nsfw: result.metadata.stream.metadata.nsfw, }).catch(function(error){ console.log('An error occurred when writing to the MySQL database:', error); }); - // resolve the promise - resolve(response.data.result); + resolve(result); }).catch(function(error){ console.log(">> 'get' error"); - // reject the promise with an error message reject(error); - return; }); }); return deferred; @@ -75,7 +74,7 @@ module.exports = { params: { name: claimName } }).then(function (response) { console.log(">> 'claim_list' success"); - resolve(response.data); + resolve(response.data.result); }).catch(function(error){ console.log(">> 'claim_list' error"); reject(error); diff --git a/routes/api-routes.js b/routes/api-routes.js index 5eb306a0..449f5b50 100644 --- a/routes/api-routes.js +++ b/routes/api-routes.js @@ -1,15 +1,17 @@ +var errorHandlers = require("../helpers/libraries/errorHandlers.js"); +var lbryApi = require("../helpers/libraries/lbryApi.js"); -module.exports = function(app, routeHelpers, lbryApi){ +module.exports = function(app){ // route to run a claim_list request on the daemon app.get("/api/claim_list/:claim", function(req, res){ lbryApi.getClaimsList(req.params.claim) - .then(function(orderedFreePublicImages){ + .then(function(claimsList){ console.log("/api/claim_list/:claim success."); - res.status(200).json(orderedFreePublicImages); + res.status(200).json(claimsList); }) .catch(function(error){ console.log("/api/claim_list/:name error:", error); - routeHelpers.handleRequestError(error, res); + errorHandlers.handleRequestError(error, res); }); }); // route to run a resolve request on the daemon @@ -19,7 +21,7 @@ module.exports = function(app, routeHelpers, lbryApi){ console.log("/api/resolve/:claim success."); res.status(200).json(resolvedUri); }).catch(function(error){ - routeHelpers.handleRequestError(error, res); + errorHandlers.handleRequestError(error, res); }); }); diff --git a/routes/serve-routes.js b/routes/serve-routes.js index fbb27eb2..933121d9 100644 --- a/routes/serve-routes.js +++ b/routes/serve-routes.js @@ -1,3 +1,6 @@ +var errorHandlers = require("../helpers/libraries/errorHandlers.js"); +var serveController = require("../controllers/serveController.js"); + function serveFile(fileInfo, res){ // set default options var options = { @@ -25,7 +28,7 @@ function serveFile(fileInfo, res){ res.status(200).sendFile(fileInfo.file_path, options); } -module.exports = function(app, routeHelpers, lbryHelpers, ua, googleAnalyticsId){ +module.exports = function(app, ua, googleAnalyticsId){ // route to fetch one free public claim app.get("/:name/:claim_id", function(req, res){ var routeString = req.params.name + "/" + req.params.claim_id; @@ -33,13 +36,13 @@ module.exports = function(app, routeHelpers, lbryHelpers, ua, googleAnalyticsId) ua(googleAnalyticsId, {https: true}).event("Serve Route", "/name/claimId", routeString).send(); // begin image-serve processes console.log(">> GET request on /" + routeString); - lbryHelpers.getClaimBasedOnUri(req.params.name, req.params.claim_id) + serveController.getClaimByClaimId(req.params.name, req.params.claim_id) .then(function(fileInfo){ console.log("/:name/:claim_id success.", fileInfo.file_name); serveFile(fileInfo, res); }).catch(function(error){ console.log("/:name/:claim_id error:", error) - routeHelpers.handleRequestError(error, res); + errorHandlers.handleRequestError(error, res); }); }); // route to fetch one free public claim @@ -48,14 +51,13 @@ module.exports = function(app, routeHelpers, lbryHelpers, ua, googleAnalyticsId) ua(googleAnalyticsId, {https: true}).event("Serve Route", "/name", req.params.name).send(); // begin image-serve processes console.log(">> GET request on /" + req.params.name); - lbryHelpers.getClaimBasedOnNameOnly(req.params.name) + serveController.getClaimByName(req.params.name) .then(function(fileInfo){ - console.log("/:name success.", fileInfo.file_name); serveFile(fileInfo, res); }).catch(function(error){ console.log("/:name error:", error); - routeHelpers.handleRequestError(error, res); + errorHandlers.handleRequestError(error, res); }); }); } \ No newline at end of file diff --git a/routes/show-routes.js b/routes/show-routes.js index 2a809d62..623f7c60 100644 --- a/routes/show-routes.js +++ b/routes/show-routes.js @@ -1,11 +1,14 @@ +var errorHandlers = require("../helpers/libraries/errorHandlers.js"); +var showController = require("../controllers/serveController.js"); -module.exports = function(app, routeHelpers, lbryHelpers, ua, googleAnalyticsId){ - // route to fetch all free public claims +module.exports = function(app, ua, googleAnalyticsId){ + // route to fetch all free public claims app.get("/:name/all", function(req, res){ console.log(">> GET request on /" + req.params.name + "/all"); + // google analytics ua(googleAnalyticsId, {https: true}).event("Show Routes", "/name/all", req.params.name + "/all").send(); - // create promise - lbryHelpers.getAllClaims(req.params.name) + // fetch all free public claims + showController.getAllClaims(req.params.name) .then(function(orderedFreePublicClaims){ console.log("/:name/all success."); res.status(200).render('allClaims', { claims: orderedFreePublicClaims }); @@ -13,7 +16,7 @@ module.exports = function(app, routeHelpers, lbryHelpers, ua, googleAnalyticsId) }) .catch(function(error){ console.log("/:name/all error:", error); - routeHelpers.handleRequestError(error, res); + errorHandlers.handleRequestError(error, res); }) }); } diff --git a/routes/sockets-routes.js b/routes/sockets-routes.js index f09f7d65..98997ee8 100644 --- a/routes/sockets-routes.js +++ b/routes/sockets-routes.js @@ -1,6 +1,6 @@ -var socketHelpers = require('../helpers/socketHelpers.js'); +var publishController = require('../controllers/publishController.js'); -module.exports = function(app, path, siofu, hostedContentPath, ua, googleAnalyticsId) { +module.exports = function(app, siofu, hostedContentPath, ua, googleAnalyticsId) { var http = require('http').Server(app); var io = require('socket.io')(http); @@ -10,7 +10,7 @@ module.exports = function(app, path, siofu, hostedContentPath, ua, googleAnalyti var visitor = ua(googleAnalyticsId, {https: true}); // attach upload listeners var uploader = new siofu(); - uploader.dir = hostedContentPath; //path.join(__dirname, '../../Uploads'); + uploader.dir = hostedContentPath; uploader.listen(socket); uploader.on("start", function(event){ // server side test to make sure file is not a bad file type @@ -26,7 +26,7 @@ module.exports = function(app, path, siofu, hostedContentPath, ua, googleAnalyti console.log("saved ", event.file.name, "deets:", event.file); if (event.file.success){ socket.emit("publish-status", "file upload successfully completed"); - socketHelpers.publish(event.file.meta.name, event.file.pathName, event.file.meta.type, event.file.meta.license, event.file.meta.nsfw, socket, visitor) + publishController.publish(event.file.meta.name, event.file.pathName, event.file.meta.type, event.file.meta.license, event.file.meta.nsfw, socket, visitor) } else { socket.emit("publish-failure", "file uploaded, but with errors") }; diff --git a/server.js b/server.js index f23ca659..158b9fb7 100644 --- a/server.js +++ b/server.js @@ -9,10 +9,6 @@ var axios = require('axios'); var config = require('config'); var ua = require('universal-analytics'); -var routeHelpers = require('./helpers/routeHelpers.js'); -var lbryApi = require('./helpers/lbryApi.js'); -var lbryHelpers = require('./helpers/lbryHelpers.js'); - var googleAnalyticsId = config.get('AnalyticsConfig.googleId'); var hostedContentPath = config.get('Database.DownloadAddress'); @@ -53,13 +49,13 @@ app.engine('handlebars', hbs.engine); app.set('view engine', 'handlebars'); // require express routes -require("./routes/api-routes.js")(app, routeHelpers, lbryApi); -require("./routes/show-routes.js")(app, routeHelpers, lbryHelpers, ua, googleAnalyticsId); -require("./routes/serve-routes.js")(app, routeHelpers, lbryHelpers, ua, googleAnalyticsId); +require("./routes/api-routes.js")(app); +require("./routes/show-routes.js")(app, ua, googleAnalyticsId); +require("./routes/serve-routes.js")(app, ua, googleAnalyticsId); require("./routes/home-routes.js")(app); // require socket.io routes -var http = require("./routes/sockets-routes.js")(app, path, siofu, hostedContentPath, ua, googleAnalyticsId); +var http = require("./routes/sockets-routes.js")(app, siofu, hostedContentPath, ua, googleAnalyticsId); // sync sequelize // wrap the server in socket.io to intercept incoming sockets requests