Merge pull request #2 from billbitt/sockets

Publish via Socket.io
This commit is contained in:
Bill Bittner 2017-05-31 21:15:13 -07:00 committed by GitHub
commit bd8a3019f0
10 changed files with 313 additions and 209 deletions

View file

@ -5,26 +5,23 @@ spee.ch is a single-serving site that reads and publishes images to and from the
* start lbry * start lbry
* install the [`lbry`](https://github.com/lbryio/lbry) daemon * install the [`lbry`](https://github.com/lbryio/lbry) daemon
* start the `lbry` daemon * start the `lbry` daemon
* start RabbitMQ (this will handle the queue for background processing)
* install & run [RabbitMQ](https://www.rabbitmq.com/#getstarted)
* clone this repo * clone this repo
* create a folder called `Uploads` in the same root directory where you cloned the repo
* run `npm install` * run `npm install`
* from your terminal, run `npm start` * from your terminal, run `npm start`
* to run hot, run `nodemon server.js` * to run hot, run `nodemon server.js`
* start a worker by running `node worker.js` in a separate session in your terminal
* visit [localhost:3000](http://localhost:3000) * visit [localhost:3000](http://localhost:3000)
## site navigation ## site navigation
* spee.ch
* spee.ch.
* To publish a file, navigate to the homepage. * To publish a file, navigate to the homepage.
* spee.ch/<the name of the claim> * spee.ch/< the name of the claim >
* To view the file with the largest bid at a claim. * To view the file with the largest bid at a claim.
* E.g. spee.ch/doitlive. * E.g. spee.ch/doitlive.
* spee.ch/< the name of the claim >/< the claim_id > * spee.ch/< the name of the claim >/< the claim_id >
* To view a specific file at a claim * To view a specific file at a claim
* E.g. spee.ch/doitlive/c496c8c55ed79816fec39e36a78645aa4458edb5 * E.g. spee.ch/doitlive/c496c8c55ed79816fec39e36a78645aa4458edb5
* spee.ch/<the name of the claim>/all * spee.ch/< the name of the claim >/all
* To view a batch of files at a claim * To view a batch of files at a claim
* E.g. spee.ch/doitlive/all * E.g. spee.ch/doitlive/all
@ -39,6 +36,6 @@ spee.ch is a single-serving site that reads and publishes images to and from the
Note: these are being used for testing durring spee.ch development and may not be maintained Note: these are being used for testing durring spee.ch development and may not be maintained
* A GET request to spee.ch/claim_list/<the name of the claim> * A GET request to spee.ch/claim_list/< the name of the claim >
* Will return the claim_list for the claim in json format. * Will return the claim_list for the claim in json format.
* E.g. spee.ch/claim_list/doitlive * E.g. spee.ch/claim_list/doitlive

View file

@ -38,10 +38,10 @@ function orderTopClaims(claimsListArray){
} }
function getClaimWithUri(uri, resolve, reject){ 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', { axios.post('http://localhost:5279/lbryapi', {
method: "get", "method": "get",
params: { uri: uri } "params": { "uri": uri }
} }
).then(function (getUriResponse) { ).then(function (getUriResponse) {
console.log(">> 'get claim' success..."); console.log(">> 'get claim' success...");
@ -64,25 +64,37 @@ function getClaimWithUri(uri, resolve, reject){
}); });
} }
function findAllClaims(name, resolve, reject){
// to do: abstract claim_list function to here
}
module.exports = { module.exports = {
publishClaim: function(publishObject){ publishClaim: function(publishParams){
axios.post('http://localhost:5279/lbryapi', publishObject) console.log(publishParams);
.then(function (response) { var deferred = new Promise(function(resolve, reject){
// receive resonse from LBRY axios.post('http://localhost:5279/lbryapi', {
// if successfull, (1) delete file (2) send response to the client "method": "publish",
console.log(">> 'publish' success..."); "params": publishParams
console.log(">> 'publish' response.data:", response.data); })
console.log(" [x] Done"); .then(function (response) {
// return the claim we got // receive resonse from LBRY
//res.status(200).send(JSON.stringify({msg: "you succsessfully published!", txData: response.data})); console.log(">> 'publish' success");
}).catch(function(error){ // return the claim we got
// receive response from LBRY resolve(response.data);
// if not successfull, (1) delete file and (2) send response to the client return;
console.log(">> 'publish' error.response.data:", error.response.data); }).catch(function(error){
console.log(" [x] Done"); // receive response from LBRY
//res.status(500).send(JSON.stringify({msg: "your file was not published", err: error.response.data.error.message})); console.log(">> 'publish' error");
if (error.response.data.error){
reject(error.response.data.error);
} else {
reject(error);
}
return;
})
}) })
return deferred;
}, },
getClaimBasedOnNameOnly: function(claimName){ getClaimBasedOnNameOnly: function(claimName){
@ -90,49 +102,47 @@ module.exports = {
var deferred = new Promise(function (resolve, reject){ var deferred = new Promise(function (resolve, reject){
// 2. code to resolve or reject the promise // 2. code to resolve or reject the promise
// make a call to the daemon to get the claims list // make a call to the daemon to get the claims list
axios.post('http://localhost:5279/lbryapi', { // receives a promise axios.post('http://localhost:5279/lbryapi', {
method: "claim_list", "method": "claim_list",
params: { name: claimName } "params": { "name": claimName }
}) })
.then(function (response) { .then(function (response) {
console.log(">> Claim_list success"); console.log(">> 'claim_list' success");
var claimsList = response.data.result.claims; var claimsList = response.data.result.claims;
console.log(">> Number of claims:", claimsList.length) console.log(">> Number of claims:", claimsList.length)
// return early if no claims were found // return early if no claims were found
if (claimsList.length === 0){ if (claimsList.length === 0){
reject("no claims were found"); reject("NO_CLAIMS");
console.log("exiting due to lack of claims"); console.log("exiting due to lack of claims");
return; return;
} }
// filter the claims to return only free, public claims // filter the claims to return only free, public claims
var freePublicClaims = filterForFreePublicClaims(claimsList); var freePublicClaims = filterForFreePublicClaims(claimsList);
// return early if no free, public claims were found // return early if no free, public claims were found
if (!freePublicClaims || (freePublicClaims.length === 0)){ 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"); console.log("exiting due to lack of free or public claims");
return; return;
} }
// order the claims // order the claims
var orderedPublcClaims = orderTopClaims(freePublicClaims); var orderedPublicClaims = orderTopClaims(freePublicClaims);
// create the uri for the first (selected) claim // 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; var freePublicClaimUri = orderedPublicClaims[0].name + "#" + orderedPublicClaims[0].claim_id;
console.log(">> your free public claim uri:", freePublicClaimUri); console.log(">> your free public claim URI:", freePublicClaimUri);
// fetch the image to display // fetch the image to display
getClaimWithUri(freePublicClaimUri, resolve, reject); getClaimWithUri(freePublicClaimUri, resolve, reject);
}) })
.catch(function(error){ .catch(function(error){
console.log(">> error:", error); console.log(">> 'claim_list' error:", error);
// reject the promise with an approriate message // 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 if (error.response.data.error) {
reject(error.response.data.error);
} else {
reject(error);
};
return; 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. 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){ 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 // fetch the image to display
getClaimWithUri(uri, resolve, reject); getClaimWithUri(uri, resolve, reject);
}); });
@ -154,39 +164,52 @@ module.exports = {
}, },
serveAllClaims: function(claimName, res){ // note: work in progress getAllClaims: function(claimName, res){ // note: work in progress
// make a call to the daemon to get the claims list var deferred = new Promise(function(resolve, reject){
axios.post('http://localhost:5279/lbryapi', { console.log(">> get all claims data for", claimName)
method: "claim_list", // make a call to the daemon to get the claims list
params: { name: claimName } axios.post('http://localhost:5279/lbryapi', {
} method: "claim_list",
).then(function (response) { params: { name: claimName }
console.log(">> Claim_list success"); }
console.log(">> Number of claims:", response.data.result.claims.length) ).then(function (response) {
// return early if no claims were found console.log(">> 'claim_list' success");
if (response.data.result.claims.length === 0){ console.log(">> Number of claims:", response.data.result.claims.length)
res.status(200).sendFile(path.join(__dirname, '../public', 'noClaims.html')); console.log(">> 'claim_list' success");
return; var claimsList = response.data.result.claims;
} console.log(">> Number of claims:", claimsList.length)
// filter the claims to return free, public claims // return early if no claims were found
var freePublicClaims = filterForFreePublicClaims(response.data.result.claims); if (claimsList.length === 0){
// return early if no free, public claims were found reject("NO_CLAIMS");
if (!freePublicClaims || (freePublicClaims.length === 0)){ console.log("exiting due to lack of claims");
res.status(200).sendFile(path.join(__dirname, '../public', 'noClaims.html')); return;
return; }
} // filter the claims to return only free, public claims
console.log(">> Number of free public claims:", freePublicClaims.length); var freePublicClaims = filterForFreePublicClaims(claimsList);
// order the claims // return early if no free, public claims were found
var orderedPublicClaims = orderTopClaims(freePublicClaims); if (!freePublicClaims || (freePublicClaims.length === 0)){
// serve the response reject("NO_FREE_PUBLIC_CLAIMS");
/* console.log("exiting due to lack of free or public claims");
to do: rather than returning json, serve a page of all these claims return;
*/ }
res.status(200).send(orderedPublicClaims); // order the claims
}).catch(function(error){ var orderedPublicClaims = orderTopClaims(freePublicClaims);
console.log(">> /c/ error:", error.response.data); // serve the response
// serve the response /*
res.status(500).send(JSON.stringify({msg: "An error occurred while finding the claim list.", err: error.response.data.error.message})); to do: rather than returning json, serve a page of all these claims
}) */
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);
};
})
});
return deferred;
} }
} }

View file

@ -29,6 +29,7 @@
"connect-multiparty": "^2.0.0", "connect-multiparty": "^2.0.0",
"express": "^4.15.2", "express": "^4.15.2",
"nodemon": "^1.11.0", "nodemon": "^1.11.0",
"socket.io": "^2.0.1" "socket.io": "^2.0.1",
"socketio-file-upload": "^0.6.0"
} }
} }

View file

@ -12,39 +12,34 @@
<h3>Examples:</h3> <h3>Examples:</h3>
<ul> <ul>
<li><a href="/coconuts">spee.ch/coconuts</a></li> <li><a href="/coconuts">spee.ch/coconuts</a></li>
<li><a href="/test">spee.ch/test</a></li> <li><a href="/wood">spee.ch/wood</a></li>
<li><a href="/doitlive">spee.ch/doitlive</a></li> <li><a href="/doitlive">spee.ch/doitlive</a></li>
<li><a href="/doitlive/all">spee.ch/doitlive/all</a></li> <li><a href="/doitlive/all">spee.ch/doitlive/all</a></li>
<li><a href="/doitlive/ca3023187e901df9e9aabd95d6ae09b6cc69b3f0">spee.ch/doitlive/ca3023187e901df9e9aabd95d6ae09b6cc69b3f0</a></li> <li><a href="/doitlive/ca3023187e901df9e9aabd95d6ae09b6cc69b3f0">spee.ch/doitlive/ca3023187e901df9e9aabd95d6ae09b6cc69b3f0</a></li>
</ul> </ul>
<h3>Publish Your Own</h3> <h3>Publish Your Own</h3>
<form id="publish-form" action="/publish" method="post" enctype="multipart/form-data"> <div id="publish">
<input type="file" name="file" accept="video/*,image/*" onchange="previewFile()" enctype="multipart/form-data"/> <form id="publish-form" action="" method="" enctype="multipart/form-data">
<br/> <input type="file" id="siofu_input" name="file" accept="video/*,image/*" onchange="previewFile()" enctype="multipart/form-data"/>
<img src="" height="200" alt="Image preview..."/> <br/>
<br/> <img id="image-preview" src="" height="200" alt="Image preview..."/>
Name: <input type="text" name="name" value="name"/> <br/>
<br/> Name: <input type="text" id="publish-name" name="name" value="name"/>
Title: <input type="text" name="title" value="title"/> <br/>
<br/> License: <select type="text" id="publish-license" name="license" value="license">
Description: <input type="text" name="description" value="I love spee.ch!"/> <option value="Public Domain">Public Domain</option>
<br/> <option value="Creative Commons">Creative Commons</option>
Author: <input type="text" name="author" value="author"/> </select>
<br/> <br/>
Language: <input type="text" name="language" value="en"/> NSFW: <select type="text" id="publish-nsfw" name="nsfw" value="false">
<br/> <option value="false">False</option>
License: <select type="text" name="license" value="license"> <option value="true">True</option>
<option value="Creative Commons">Creative Commons</option> </select>
<option value="Public Domain">Public Domain</option> <br/>
</select> <button id="publish-submit">Submit</button>
<br/> </form>
NSFW: <select type="text" name="nsfw" value="false"> <p id="upload-status"></p>
<option value="false">False</option> </div>
<option value="true">True</option>
</select>
<br/>
<button type="submit">Submit</button>
</form>
<h3>Help</h3> <h3>Help</h3>
<h4>Site Navigation</h4> <h4>Site Navigation</h4>
@ -84,28 +79,69 @@
</li> </li>
</ul> </ul>
<script src="/socket.io/socket.io.js"></script>
<script src="/siofu/client.js"></script>
<script> <script>
// define global variables
var socket = io();
var uploader = new SocketIOFileUpload(socket);
// function to handle image preview
function previewFile(){ function previewFile(){
var preview = document.querySelector('img'); //selects the query named img var preview = document.querySelector('img'); //selects the query named img
var file = document.querySelector('input[type=file]').files[0]; //sames as here var claimName = document.querySelector('input[name=name]');
var reader = new FileReader(); var selectedFile = document.querySelector('input[name=file]').files[0];
var previewReader = new FileReader();
reader.onloadend = function () { previewReader.onloadend = function () {
preview.src = reader.result; preview.src = previewReader.result;
} }
if (file) { if (selectedFile) {
reader.readAsDataURL(file); //reads the data as a URL previewReader.readAsDataURL(selectedFile); // reads the data and sets the img src
claimName.value = selectedFile.name.substring(0, selectedFile.name.indexOf("."));
} else { } else {
preview.src = ""; preview.src = "";
} };
} }
previewFile(); //calls the function named previewFile() // helper function to update status
function updatePublishStatus(msg){
document.getElementById("publish").innerHTML = msg;
}
// call the previewFile function
previewFile();
// prevent default on the submit button
document.getElementById("publish-submit").addEventListener("click", function(event){
event.preventDefault();
})
// upload through the socket
uploader.listenOnSubmit(document.getElementById("publish-submit"), document.getElementById("siofu_input"));
// add listeners to the uploader
uploader.addEventListener("start", function(event){
var name = document.getElementById('publish-name').value;
var license = document.getElementById('publish-license').value;
var nsfw = document.getElementById('publish-nsfw').value;
// set meta data
event.file.meta.name = name;
event.file.meta.license = license;
event.file.meta.nsfw = nsfw;
});
uploader.addEventListener("progress", function(event){
var percent = event.bytesLoaded / event.file.size * 100;
updatePublishStatus("File is " + percent.toFixed(2) + "% loaded to the server");
})
// add listener for publish status updates
socket.on("publish-status", function(msg){
updatePublishStatus(msg);
})
socket.on("publish-complete", function(msg){
console.log("publish complete", msg);
var publishResults = `<p>You're publish is complete!</p>`;
publishResults += `<p>The Claim ID is: ${msg.result.claim_id}</p>`;
publishResults += `<p>The TX ID is: <a href="https://explorer.lbry.io/#!/transaction?id=${msg.result.txid}">${msg.result.txid}</a></p>`;
publishResults += `<p>Note: the transaction still needs to be published by the network. Click the tx id link to view the tx on the blockchain explorer</p>`
publishResults += `<p><a href="/">Reload the page to publish another (fancy button coming soon)</a></p>`;
document.getElementById("publish").innerHTML = publishResults;
})
</script> </script>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io();
</script>
</body> </body>
</html> </html>

View file

@ -4,11 +4,11 @@
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge"> <meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Publishing Asset</title> <title>No Claims</title>
</head> </head>
<body> <body>
<h1>spee.ch</h1> <h1>spee.ch</h1>
<h3>Publishing Asset</h3> <h3>Invalid URI</h3>
<p>Your asset is being published by a handy background worker. You can return to <a href="/">spee.ch</a> and your asset should be published to your claim shortly.</p> <p>There is no claim at that URI.</p>
</body> </body>
</html> </html>

View file

@ -6,27 +6,6 @@ var multipartMiddleware = multipart();
var lbryApi = require('../helpers/lbryApi.js'); var lbryApi = require('../helpers/lbryApi.js');
var queueApi = require('../helpers/queueApi.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;
}
// routes to export // routes to export
module.exports = function(app){ module.exports = function(app){
// route to fetch one free public claim // route to fetch one free public claim
@ -34,45 +13,88 @@ module.exports = function(app){
console.log(" >> GET request on favicon.ico"); console.log(" >> GET request on favicon.ico");
res.sendFile(path.join(__dirname, '../public', '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 fetch one free public claim // route to fetch one free public claim
app.get("/:name/all", function(req, res){ app.get("/:name/all", function(req, res){
var name = req.params.name; var name = req.params.name;
console.log(">> GET request on /" + name + " (all)"); console.log(">> GET request on /" + name + " (all)");
lbryApi.serveAllClaims(name, res); // create promise
var promise = lbryApi.getAllClaims(name);
// handle the promise resolve
promise.then(function(orderedFreePublicClaims){
console.log("/name/all promise success.")
res.status(200).send(orderedFreePublicClaims);
return;
})
// handle the promise rejection
.catch(function(error){
console.log("/name/all/ 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 // route to fetch one free public claim
app.get("/:name/:claim_id", function(req, res){ app.get("/:name/:claim_id", function(req, res){
console.log(">> GET request on /" + req.params.name + "#" + req.params.claim_id); var uri = req.params.name + "#" + req.params.claim_id;
res.status(200).sendFile(path.join(__dirname, '../public', 'claim.html')); console.log(">> GET request on /" + uri);
// create promise
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 // route to fetch one free public claim
app.get("/:name", function(req, res){ app.get("/:name", function(req, res){
var name = req.params.name; var name = req.params.name;
console.log(">> GET request on /" + name) console.log(">> GET request on /" + name);
// send page (includes a socket to get the file) // create promise
res.status(200).sendFile(path.join(__dirname, '../public', 'claim.html')); 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;
};
res.status(400).send(error);
});
}); });
// route for the home page // route for the home page
app.get("/", function(req, res){ 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 // a catch-all route if someone visits a page that does not exist
app.use("*", function(req, res){ app.use("*", function(req, res){
res.sendFile(path.join(__dirname, '../public', 'fourOhfour.html')); res.status(404).sendFile(path.join(__dirname, '../public', 'fourOhfour.html'));
}); });
} }

View file

@ -4,48 +4,71 @@ module.exports = function(app) {
var fs = require('fs'); var fs = require('fs');
var path = require('path'); var path = require('path');
var lbryApi = require('../helpers/lbryApi.js'); var lbryApi = require('../helpers/lbryApi.js');
var queueApi = require('../helpers/queueApi.js');
var siofu = require("socketio-file-upload");
function sendTheImage(socket, filePath){ // functions to create a publishing object
fs.readFile(filePath, function(err, buff){ function createPublishParams(name, filepath, license, nsfw){
if (err) { var publishParams = {
console.log("socket: fs err:", err); "name": name,
return; "file_path": filepath,
}; "bid": 0.1,
//console.log("buff", buff); "metadata": {
socket.emit('claim-send', { image: true, buffer: buff.toString('base64') }); "description": name + " published via spee.ch",
console.log('socket: the image file has been sent via sockets'); "title": name,
}); "author": "spee.ch",
"language": "en",
"license": license,
"nsfw": (nsfw.toLowerCase() === "true")
}
};
return publishParams;
} }
// publish an image to lbry
function publish(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
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){ io.on('connection', function(socket){
console.log('a user connected'); console.log('a user connected');
// listener for uploader
// serve an image file from the server var uploader = new siofu();
socket.on('claim-request', function(query){ uploader.dir = path.join(__dirname, '../../Uploads');
// 1. retrieve the image from lbry via daemon uploader.listen(socket);
console.log("socket: received claim request for:", query) // attach upload listeners
if (query.indexOf("/") === -1){ uploader.on("error", function(event){
var promise = lbryApi.getClaimBasedOnNameOnly(query) console.log("an error occured while uploading", event.error);
} else { socket.emit("publish-status", event.error)
var uri = query.replace("/", "#");
var promise = lbryApi.getClaimBasedOnUri(uri)
}
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;
});
// 2. emit updates as the image is being retrieved
socket.emit("claim-update", "We are getting your claim for " + query);
}) })
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 {
socket.emit("publish-status", "file saved, but with errors")
};
});
// handle disconnect // handle disconnect
socket.on('disconnect', function(){ socket.on('disconnect', function(){

View file

@ -2,6 +2,7 @@
var express = require('express'); var express = require('express');
var bodyParser = require('body-parser'); var bodyParser = require('body-parser');
var path = require('path'); var path = require('path');
var siofu = require("socketio-file-upload");
// set port // set port
var PORT = 3000; var PORT = 3000;
@ -15,6 +16,7 @@ app.use(express.static(__dirname + '/public'));
// configure express app // configure express app
app.use(bodyParser.json()); // for parsing application/json app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
app.use(siofu.router);
// require express routes // require express routes
require("./routes/api-routes.js")(app); require("./routes/api-routes.js")(app);