diff --git a/Temp/name b/Temp/name new file mode 100644 index 00000000..50b74eec Binary files /dev/null and b/Temp/name differ diff --git a/Temp/name22 b/Temp/name22 new file mode 100644 index 00000000..50b74eec Binary files /dev/null and b/Temp/name22 differ diff --git a/Temp/namef b/Temp/namef new file mode 100644 index 00000000..50b74eec Binary files /dev/null and b/Temp/namef differ diff --git a/Temp/names b/Temp/names new file mode 100644 index 00000000..04ef15af Binary files /dev/null and b/Temp/names differ diff --git a/helpers/lbryApi.js b/helpers/lbryApi.js index 8a09f13e..199c2ec7 100644 --- a/helpers/lbryApi.js +++ b/helpers/lbryApi.js @@ -38,7 +38,7 @@ function orderTopClaims(claimsListArray){ } function getClaimWithUri(uri, resolve, reject){ - console.log(">> making get request to lbry daemon") + console.log(">> making get request to lbry daemon"); axios.post('http://localhost:5279/lbryapi', { method: "get", params: { uri: uri } @@ -64,6 +64,10 @@ function getClaimWithUri(uri, resolve, reject){ }); } +function findAllClaims(name, resolve, reject){ + // abstract claim_list function to here +} + module.exports = { publishClaim: function(publishObject){ @@ -76,12 +80,14 @@ module.exports = { console.log(" [x] Done"); // return the claim we got //res.status(200).send(JSON.stringify({msg: "you succsessfully published!", txData: response.data})); + return; }).catch(function(error){ // receive response from LBRY // if not successfull, (1) delete file and (2) send response to the client console.log(">> 'publish' error.response.data:", error.response.data); console.log(" [x] Done"); //res.status(500).send(JSON.stringify({msg: "your file was not published", err: error.response.data.error.message})); + return; }) }, @@ -95,14 +101,14 @@ module.exports = { params: { name: claimName } }) .then(function (response) { - console.log(">> Claim_list success"); + console.log(">> 'claim_list' success"); var claimsList = response.data.result.claims; console.log(">> Number of claims:", claimsList.length) // return early if no claims were found if (claimsList.length === 0){ - reject("no claims were found"); + reject("NO_CLAIMS"); console.log("exiting due to lack of claims"); return; } @@ -112,7 +118,7 @@ module.exports = { // return early if no free, public claims were found if (!freePublicClaims || (freePublicClaims.length === 0)){ - reject("no free, public claims were found"); + reject("NO_FREE_PUBLIC_CLAIMS"); console.log("exiting due to lack of free or public claims"); return; } @@ -121,18 +127,22 @@ module.exports = { var orderedPublcClaims = orderTopClaims(freePublicClaims); // create the uri for the first (selected) claim - console.log(">> ordered free public claims", orderedPublcClaims); + console.log(">> ordered free public claims"); var freePublicClaimUri = "lbry://" + orderedPublcClaims[0].name + "#" + orderedPublcClaims[0].claim_id; - console.log(">> your free public claim uri:", freePublicClaimUri); + console.log(">> your free public claim URI:", freePublicClaimUri); // fetch the image to display getClaimWithUri(freePublicClaimUri, resolve, reject); }) .catch(function(error){ - console.log(">> error:", error); + console.log(">> 'claim_list' error:", error); // reject the promise with an approriate message - reject(error.response.data.error); + if (error.code === "ECONNREFUSED"){ + reject("Connection refused. The daemon may not be running.") + } else { + reject(error.response.data.error); + }; return; }); }); @@ -146,7 +156,7 @@ module.exports = { to do: need to pass the URI through a test (use 'resolve') to see if it is free and public. Right now it is jumping straight to 'get'ing and serving the asset. */ var deferred = new Promise(function (resolve, reject){ - console.log(">> your uri:", uri); + console.log(">> get claim based on URI:", uri); // fetch the image to display getClaimWithUri(uri, resolve, reject); }); @@ -154,39 +164,45 @@ module.exports = { }, - serveAllClaims: function(claimName, res){ // note: work in progress - // make a call to the daemon to get the claims list - axios.post('http://localhost:5279/lbryapi', { - method: "claim_list", - params: { name: claimName } - } - ).then(function (response) { - console.log(">> Claim_list success"); - console.log(">> Number of claims:", response.data.result.claims.length) - // return early if no claims were found - if (response.data.result.claims.length === 0){ - res.status(200).sendFile(path.join(__dirname, '../public', 'noClaims.html')); - return; - } - // filter the claims to return free, public claims - var freePublicClaims = filterForFreePublicClaims(response.data.result.claims); - // return early if no free, public claims were found - if (!freePublicClaims || (freePublicClaims.length === 0)){ - res.status(200).sendFile(path.join(__dirname, '../public', 'noClaims.html')); - return; - } - console.log(">> Number of free public claims:", freePublicClaims.length); - // order the claims - var orderedPublicClaims = orderTopClaims(freePublicClaims); - // serve the response - /* - to do: rather than returning json, serve a page of all these claims - */ - res.status(200).send(orderedPublicClaims); - }).catch(function(error){ - console.log(">> /c/ error:", error.response.data); - // serve the response - res.status(500).send(JSON.stringify({msg: "An error occurred while finding the claim list.", err: error.response.data.error.message})); - }) + getAllClaims: function(claimName, res){ // note: work in progress + var deferred = new Promise(function(resolve, reject){ + console.log(">> get all claims data for", claimName) + // make a call to the daemon to get the claims list + axios.post('http://localhost:5279/lbryapi', { + method: "claim_list", + params: { name: claimName } + } + ).then(function (response) { + console.log(">> 'claim_list' success"); + console.log(">> Number of claims:", response.data.result.claims.length) + // return early if no claims were found + if (response.data.result.claims.length === 0){ + res.status(307).sendFile(path.join(__dirname, '../public', 'noClaims.html')); + return; + } + // filter the claims to return free, public claims + var freePublicClaims = filterForFreePublicClaims(response.data.result.claims); + // return early if no free, public claims were found + if (!freePublicClaims || (freePublicClaims.length === 0)){ + res.status(307).sendFile(path.join(__dirname, '../public', 'noClaims.html')); + return; + } + console.log(">> Number of free public claims:", freePublicClaims.length); + // order the claims + var orderedPublicClaims = orderTopClaims(freePublicClaims); + // serve the response + /* + to do: rather than returning json, serve a page of all these claims + */ + res.status(200).send(orderedPublicClaims); + }).catch(function(error){ + console.log(">> 'claim_list' error:", error.response.data); + // serve the response + res.status(500).send(JSON.stringify({msg: "An error occurred while finding the claim list.", err: error.response.data.error.message})); + }) + + }); + return deffered; + } } diff --git a/public/claim.html b/public/claimPlus.html similarity index 100% rename from public/claim.html rename to public/claimPlus.html diff --git a/public/index.html b/public/index.html index d5af3d08..42000b2f 100644 --- a/public/index.html +++ b/public/index.html @@ -18,33 +18,35 @@
  • spee.ch/doitlive/ca3023187e901df9e9aabd95d6ae09b6cc69b3f0
  • Publish Your Own

    -
    - -
    - Image preview... -
    - Name: -
    - Title: -
    - Description: -
    - Author: -
    - Language: -
    - License: -
    - NSFW: -
    - -
    +
    +
    + +
    + Image preview... +
    + Name: +
    + Title: +
    + Description: +
    + Author: +
    + Language: +
    + License: +
    + NSFW: +
    + +
    +

    Help

    Site Navigation

    @@ -84,28 +86,95 @@ - \ No newline at end of file diff --git a/public/publishingClaim.html b/public/invalidUri.html similarity index 51% rename from public/publishingClaim.html rename to public/invalidUri.html index f599e292..a4b0673e 100644 --- a/public/publishingClaim.html +++ b/public/invalidUri.html @@ -4,11 +4,11 @@ - Publishing Asset + No Claims

    spee.ch

    -

    Publishing Asset

    -

    Your asset is being published by a handy background worker. You can return to spee.ch and your asset should be published to your claim shortly.

    +

    Invalid URI

    +

    There is no claim at that URI.

    \ No newline at end of file diff --git a/routes/html-routes.js b/routes/html-routes.js index 59df73cd..4e79eb8e 100644 --- a/routes/html-routes.js +++ b/routes/html-routes.js @@ -6,26 +6,26 @@ var multipartMiddleware = multipart(); var lbryApi = require('../helpers/lbryApi.js'); var queueApi = require('../helpers/queueApi.js'); -// helper functions -function createPublishObject(req){ - var publishObject = { - "method":"publish", - "params": { - "name": req.body.name, - "file_path": req.files.file.path, - "bid": 0.1, - "metadata": { - "description": req.body.description, - "title": req.body.title, - "author": req.body.author, - "language": req.body.language, - "license": req.body.license, - "nsfw": (req.body.nsfw.toLowerCase() === "true") - } - } - }; - return publishObject; -} +// // helper functions +// function createPublishObject(req){ +// var publishObject = { +// "method":"publish", +// "params": { +// "name": req.body.name, +// "file_path": req.files.file.path, +// "bid": 0.1, +// "metadata": { +// "description": req.body.description, +// "title": req.body.title, +// "author": req.body.author, +// "language": req.body.language, +// "license": req.body.license, +// "nsfw": (req.body.nsfw.toLowerCase() === "true") +// } +// } +// }; +// return publishObject; +// } // routes to export module.exports = function(app){ @@ -34,45 +34,102 @@ module.exports = function(app){ console.log(" >> GET request on favicon.ico"); res.sendFile(path.join(__dirname, '../public', 'favicon.ico')); }); - // route to publish a new claim - app.post("/publish", multipartMiddleware, function(req, res){ - console.log(" >> POST request on /publish"); - // build the data needed to publish the file - var publishObject = createPublishObject(req); - console.log("publish", publishObject); - // post the task to the que - queueApi.addNewTaskToQueue(JSON.stringify({ - type: 'publish', - data: publishObject - })); - // respond to the client that the task has been queued - res.status(200).sendFile(path.join(__dirname, '../public', 'publishingClaim.html')); - }); + // // route to publish a new claim + // app.post("/publish", multipartMiddleware, function(req, res){ + // console.log(" >> POST request on /publish"); + // // build the data needed to publish the file + // var publishObject = createPublishObject(req); + // console.log("publish", publishObject); + // // post the task to the que + // queueApi.addNewTaskToQueue(JSON.stringify({ + // type: 'publish', + // data: publishObject + // })); + // // respond to the client that the task has been queued + // res.status(200).sendFile(path.join(__dirname, '../public', 'publishingClaim.html')); + // }); // route to fetch one free public claim app.get("/:name/all", function(req, res){ var name = req.params.name; console.log(">> GET request on /" + name + " (all)"); lbryApi.serveAllClaims(name, res); + + var promise = lbryApi.getAllClaims(name); + // handle the promise resolve + promise.then(function(orderedFreePublicClaims){ + res.status(200).send(orderedFreePublicClaims); + return; + }) + // handle the promise rejection + .catch(function(error){ + console.log("/name promise error:", error); + // handle the error + if ((error === "NO_CLAIMS") || (error === "NO_FREE_PUBLIC_CLAIMS")){ + res.status(307).sendFile(path.join(__dirname, '../public', 'noClaims.html')); + return; + } else { + res.status(400).send(error); + return; + }; + }) + + }); // route to fetch one free public claim app.get("/:name/:claim_id", function(req, res){ - console.log(">> GET request on /" + req.params.name + "#" + req.params.claim_id); - res.status(200).sendFile(path.join(__dirname, '../public', 'claim.html')); + var uri = req.params.name + "#" + req.params.claim_id; + console.log(">> GET request on /" + uri); + var promise = lbryApi.getClaimBasedOnUri(uri); + // handle the promise resolve + promise.then(function(filePath){ + console.log("/name/claim_id promise success - filepath:", filePath) + res.status(200).sendFile(filePath); + return; + }) + // handle the promise rejection + .catch(function(error){ + console.log("/name/claim_id/ promise error:", error) + // handle the error + if (error === "Invalid URI") { + res.status(400).sendFile(path.join(__dirname, '../public', 'invalidURI.html')); + return; + } else { + res.status(400).send(error); + return; + }; + }); }); // route to fetch one free public claim app.get("/:name", function(req, res){ - var name = req.params.name; - console.log(">> GET request on /" + name) - // send page (includes a socket to get the file) - res.status(200).sendFile(path.join(__dirname, '../public', 'claim.html')); + var name = req.params.name; + console.log(">> GET request on /" + name); + var promise = lbryApi.getClaimBasedOnNameOnly(name); + // handle the promise resolve + promise.then(function(filePath){ + console.log("/name promise success - filepath:", filePath) + res.status(200).sendFile(filePath); + return; + }) + // handle the promise rejection + .catch(function(error){ + console.log("/name/ promise error:", error); + // handle the error + if ((error === "NO_CLAIMS") || (error === "NO_FREE_PUBLIC_CLAIMS")){ + res.status(307).sendFile(path.join(__dirname, '../public', 'noClaims.html')); + return; + } else { + res.status(400).send(error); + return; + }; + }); }); // route for the home page app.get("/", function(req, res){ - res.sendFile(path.join(__dirname, '../public', 'index.html')); + res.status(200).sendFile(path.join(__dirname, '../public', 'index.html')); }); // a catch-all route if someone visits a page that does not exist app.use("*", function(req, res){ - res.sendFile(path.join(__dirname, '../public', 'fourOhfour.html')); + res.status(404).sendFile(path.join(__dirname, '../public', 'fourOhfour.html')); }); } diff --git a/routes/sockets-routes.js b/routes/sockets-routes.js index 51bbbb0a..2b82417f 100644 --- a/routes/sockets-routes.js +++ b/routes/sockets-routes.js @@ -4,48 +4,97 @@ module.exports = function(app) { var fs = require('fs'); var path = require('path'); var lbryApi = require('../helpers/lbryApi.js'); + var files = {}; // for the socket uploader - function sendTheImage(socket, filePath){ - fs.readFile(filePath, function(err, buff){ - if (err) { - console.log("socket: fs err:", err); - return; - }; - //console.log("buff", buff); - socket.emit('claim-send', { image: true, buffer: buff.toString('base64') }); - console.log('socket: the image file has been sent via sockets'); - }); + // functions to create a publishing object + function createPublishObject(fileInfo, filePath){ + var publishObject = { + "method":"publish", + "params": { + "name": fileInfo.name, + "file_path": filePath, + "bid": 0.1, + "metadata": { + "description": fileInfo.description, + "title": fileInfo.title, + "author": fileInfo.author, + "language": fileInfo.language, + "license": fileInfo.license, + "nsfw": (fileInfo.nsfw.toLowerCase() === "true") + } + } + }; + return publishObject; } + // publish an image to lbry + function publish(data, filePath){ + // 1. receive the file + + // 2. create the publish object + var publishObject = createPublishObject(data, filePath); + // 3. post the task to the que + queueApi.addNewTaskToQueue(JSON.stringify({ + type: 'publish', + data: publishObject + })); + + + }; io.on('connection', function(socket){ console.log('a user connected'); - - // serve an image file from the server - socket.on('claim-request', function(query){ - // 1. retrieve the image from lbry via daemon - console.log("socket: received claim request for:", query) - if (query.indexOf("/") === -1){ - var promise = lbryApi.getClaimBasedOnNameOnly(query) - } else { - var uri = query.replace("/", "#"); - var promise = lbryApi.getClaimBasedOnUri(uri) + console.log("files", files) + // socket listener for starting an upload + socket.on('upload-start', function(data){ // data contains the variables that we passed from the client + console.log("upload-start"); + console.log("files", files) + var name = data['name']; + files[name] = { // create a new entry in the files object + fileSize: data['size'], + data: '', + downloaded: 0 } - promise.then(function(data){ - console.log("socket: claim-request - success:", data) - // 3. serve the image back once it is retrieved - sendTheImage(socket, data); - return; - }) - .catch(function(error){ - console.log("socket: claim-request - error:", error) - // handle the error - socket.emit("claim-update", error); - return; + var place = 0; + try { // if its a file we already tried + var stat = fs.statSync('Temp/' + name); + if (stat.isFile()) { + files[name]['downloaded'] = stat.size; + place = stat.size / 524288; + }; + } catch(er) {}; // if it's a new file + fs.open("Temp/" + name, "a", 0755, function(err, fd){ + if (err) { + console.log("err:", err); + } else { + files[name]['handler'] = fd; // store the handler so we can write to it later + socket.emit('moreData', {'place': place, percent: 0 }); + }; }); - - // 2. emit updates as the image is being retrieved - socket.emit("claim-update", "We are getting your claim for " + query); }) + + socket.on('upload', function(data){ + console.log("upload"); + console.log("files", files) + var name = data['name']; + files[name]['downloaded'] += data['data'].length; + files[name]['data'] += data['data']; + if (files[name]['downloaded'] == files[name]['fileSize']) { + fs.write(files[name]['handler'], files[name]['data'], null, 'binary', function(err, writen){ + // get thumnail here + }); + } else if (files[name]['data'].length > 10485760) { // if data buffer reaches 10mb + fs.write(files[name]['handler'], files[name]['data'], null, 'Binary', function(err, writen){ + files[name]['data'] = "" // reset the buffer + var place = files[name]['donwladed'] / 524288; + var percent = (files[name]['downloaded'] / files[Name]['fileSize']) * 100; + socket.emit('moreData', {'place': place, 'percent': percent}); + }) + } else { + var place = file[name]['downloaded'] / 524288; + var percent = (files[name]['downloaded'] / files[name]['fileSize']) * 100; + socket.emit('moreData', {'place': place, 'percent': percent}); + } + }); // handle disconnect socket.on('disconnect', function(){