From fab8216abb1865a4c177da761d1fcb9472571055 Mon Sep 17 00:00:00 2001 From: bill bittner Date: Wed, 31 May 2017 19:48:09 -0700 Subject: [PATCH] uploader works locally --- .gitignore | 1 + helpers/lbryApi.js | 48 +++++++------- package.json | 3 +- public/index.html | 96 +++++++++++----------------- routes/sockets-routes.js | 131 ++++++++++++++++----------------------- server.js | 2 + 6 files changed, 122 insertions(+), 159 deletions(-) diff --git a/.gitignore b/.gitignore index 3c3629e6..241a7964 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules +temp \ No newline at end of file diff --git a/helpers/lbryApi.js b/helpers/lbryApi.js index 0e732bfb..25351e5f 100644 --- a/helpers/lbryApi.js +++ b/helpers/lbryApi.js @@ -70,25 +70,31 @@ function findAllClaims(name, resolve, reject){ module.exports = { - publishClaim: function(publishObject){ - axios.post('http://localhost:5279/lbryapi', publishObject) - .then(function (response) { - // receive resonse from LBRY - // if successfull, (1) delete file (2) send response to the client - console.log(">> 'publish' success..."); - console.log(">> 'publish' response.data:", response.data); - 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; + publishClaim: function(publishParams){ + console.log(publishParams); + var deferred = new Promise(function(resolve, reject){ + axios.post('http://localhost:5279/lbryapi', { + "method": "publish", + "params": publishParams + }) + .then(function (response) { + // receive resonse from LBRY + 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; + }) }) + return deferred; }, getClaimBasedOnNameOnly: function(claimName){ @@ -119,10 +125,10 @@ module.exports = { return; } // order the claims - var orderedPublcClaims = orderTopClaims(freePublicClaims); + var orderedPublicClaims = orderTopClaims(freePublicClaims); // create the uri for the first (selected) claim console.log(">> ordered free public claims"); - var freePublicClaimUri = "lbry://" + orderedPublcClaims[0].name + "#" + orderedPublcClaims[0].claim_id; + var freePublicClaimUri = "lbry://" + orderedPublicClaims[0].name + "#" + orderedPublicClaims[0].claim_id; console.log(">> your free public claim URI:", freePublicClaimUri); // fetch the image to display getClaimWithUri(freePublicClaimUri, resolve, reject); @@ -187,7 +193,7 @@ module.exports = { return; } // order the claims - var orderedPublcClaims = orderTopClaims(freePublicClaims); + var orderedPublicClaims = orderTopClaims(freePublicClaims); // serve the response /* to do: rather than returning json, serve a page of all these claims diff --git a/package.json b/package.json index 38ad5ab0..a3ef74fd 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,7 @@ "connect-multiparty": "^2.0.0", "express": "^4.15.2", "nodemon": "^1.11.0", - "socket.io": "^2.0.1" + "socket.io": "^2.0.1", + "socketio-file-upload": "^0.6.0" } } diff --git a/public/index.html b/public/index.html index 1035030b..30ce073a 100644 --- a/public/index.html +++ b/public/index.html @@ -12,7 +12,7 @@

Examples:

+ diff --git a/routes/sockets-routes.js b/routes/sockets-routes.js index 2b82417f..cc7b6b97 100644 --- a/routes/sockets-routes.js +++ b/routes/sockets-routes.js @@ -4,96 +4,71 @@ module.exports = function(app) { var fs = require('fs'); var path = require('path'); var lbryApi = require('../helpers/lbryApi.js'); - var files = {}; // for the socket uploader + var queueApi = require('../helpers/queueApi.js'); + var siofu = require("socketio-file-upload"); + var rootDirectory = "C:\\Users\\Bones\\development\\Lbry\\spee.ch\\"; // 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") - } + function createPublishParams(name, filepath, license, nsfw){ + var publishParams = { + "name": name, + "file_path": rootDirectory + 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 publishObject; + return publishParams; } // 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 - })); - - + function publish(name, filepath, license, nsfw, socket){ + // update the client + socket.emit("publish-status", "starting publishing..."); + // create the publish object + var publishParams = createPublishParams(name, filepath, license, nsfw); + // get a promise to publish + var promise = lbryApi.publishClaim(publishParams); + // handle promise + promise.then(function(data){ + console.log("publish promise success. Tx info:", data) + socket.emit("publish-complete", data); + /* + note: remember to delete the local file + */ + }) + .catch(function(error){ + console.log("error:", error); + socket.emit("publish-status", "publish failed"); + /* + note: remember to delete the local file + */ + }); }; io.on('connection', function(socket){ console.log('a user connected'); - 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 - } - 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 }); - }; - }); + // listener for uploader + var uploader = new siofu(); + uploader.dir = "./temp"; + uploader.listen(socket); + // attach upload listeners + uploader.on("error", function(event){ + console.log("an error occured while uploading", event.error); + socket.emit("publish-status", event.error) }) - - 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}); - }) + uploader.on("saved", function(event){ + console.log("saved " + event.file.name); + if (event.file.success){ + socket.emit("publish-status", "file upload successfully completed"); + publish(event.file.meta.name, event.file.pathName, event.file.meta.license,event.file.meta.nsfw, socket) } else { - var place = file[name]['downloaded'] / 524288; - var percent = (files[name]['downloaded'] / files[name]['fileSize']) * 100; - socket.emit('moreData', {'place': place, 'percent': percent}); - } + socket.emit("publish-status", "file saved, but with errors") + }; }); // handle disconnect diff --git a/server.js b/server.js index 5872b342..abf98fba 100644 --- a/server.js +++ b/server.js @@ -2,6 +2,7 @@ var express = require('express'); var bodyParser = require('body-parser'); var path = require('path'); +var siofu = require("socketio-file-upload"); // set port var PORT = 3000; @@ -15,6 +16,7 @@ app.use(express.static(__dirname + '/public')); // configure express app app.use(bodyParser.json()); // for parsing application/json app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded +app.use(siofu.router); // require express routes require("./routes/api-routes.js")(app);