added urls on publish completion

This commit is contained in:
bill bittner 2017-06-03 00:41:02 -07:00
parent bb5e67ade5
commit 57ae1612cc
13 changed files with 132 additions and 214 deletions

View file

@ -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){

View file

@ -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);
});
}
}

View file

@ -0,0 +1,13 @@
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.status === 500) {
res.status(400).send(error.response.data.error.message);
} else {
res.status(400).send(error.toString());
};
}
}

View file

@ -10,6 +10,7 @@ function handlePublishError(error) {
return error;
};
}
function createPublishParams(name, filepath, license, nsfw) {
var publishParams = {
"name": name,
@ -26,6 +27,7 @@ function createPublishParams(name, filepath, license, nsfw) {
};
return publishParams;
}
function deleteTemporaryFile(filepath) {
fs.unlink(filepath, function(err) {
if (err) throw err;
@ -43,12 +45,12 @@ module.exports = {
lbryApi.publishClaim(publishParams)
.then(function(data){
console.log("publish promise success. Tx info:", data)
socket.emit("publish-complete", data);
socket.emit("publish-complete", {name: name, result: data.result});
deleteTemporaryFile(filepath);
})
.catch(function(error){
console.log("error:", error);
socket.emit("publish-status", handlePublishError(error));
socket.emit("publish-failure", handlePublishError(error));
deleteTemporaryFile(filepath);
});
}

View file

@ -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",

View file

View file

@ -1,2 +0,0 @@

View file

@ -1,36 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Spee.ch Claim</title>
</head>
<body>
<div id="image">
<h1>spee.ch</h1>
<p>spee.ch is a single-serving site that reads and publishes images to and from the <a href="https://lbry.io">LBRY</a> blockchain.</p>
<h3>Status:</h3>
<p id="status">Your image is being retrieved</p>
</div>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io();
var url = document.URL.substring(document.URL.indexOf('spee.ch/') + 8);
// request the claim through the socket
socket.emit("claim-request", url);
// listen for updates
socket.on("claim-update", function(data){
console.log("data:", data);
document.getElementById("status").innerHTML = data;
})
// receive the claim through the socket
socket.on("claim-send", function(data){
if (data.image) {
var base64Image = 'data:image/jpeg;base64,' + data.buffer;
document.getElementById("image").innerHTML = '<img src="' + base64Image + '"/>';
}
})
</script>
</body>
</html>

View file

@ -5,85 +5,102 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Spee.ch</title>
<link rel="stylesheet" type="text/css" href="/assets/css/style.css">
</head>
<body>
<h1>spee.ch</h1>
<p>spee.ch is a single-serving site that reads and publishes images to and from the <a href="https://lbry.io">LBRY</a> blockchain.</p>
<h2>Examples:</h2>
<ul>
<li><a href="/coconuts">spee.ch/coconuts</a></li>
<li><a href="/wood">spee.ch/wood</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/ca3023187e901df9e9aabd95d6ae09b6cc69b3f0">spee.ch/doitlive/ca3023187e901df9e9aabd95d6ae09b6cc69b3f0</a></li>
</ul>
<h2>Publish Your Own</h2>
<div id="publish">
<form id="publish-form" action="" method="" enctype="multipart/form-data">
<input type="file" id="siofu_input" name="file" accept="video/*,image/*" onchange="previewFile()" enctype="multipart/form-data"/>
<br/>
<img id="image-preview" src="" height="200" alt="Image preview..."/>
<br/>
Name: <input type="text" id="publish-name" name="name" value="name"/>
<br/>
License: <select type="text" id="publish-license" name="license" value="license">
<option value="Public Domain">Public Domain</option>
<option value="Creative Commons">Creative Commons</option>
</select>
<br/>
NSFW: <select type="text" id="publish-nsfw" name="nsfw" value="false">
<option value="false">False</option>
<option value="true">True</option>
</select>
<br/>
<button id="publish-submit">Submit</button>
</form>
</div>
<h2>Help Wanted!</h2>
<p>If you would like to help make spee.ch amazing, join our slack channel.</p>
<p>We are currently in need of a designer to help with styling spee.ch's front end, but all help is welcome!</p>
<h2>Documentation</h2>
<h3>Site Navigation</h3>
<ul>
<li><strong><a href="/">spee.ch</a></strong>.
<ul>
<li>To publish a file, navigate to the homepage.</li>
</ul>
</li>
<li><strong>spee.ch/&ltthe name of the claim&gt</strong>
<ul>
<li>To view the file with the largest bid at a claim.</li>
<li>E.g. <a href="/doitlive">spee.ch/doitlive</a>.</li>
</ul>
</li>
<li><strong>spee.ch/&lt the name of the claim &gt/&lt the claim_id &gt</strong>
<ul>
<li>To view a specific file at a claim</li>
<li>E.g. <a href="/doitlive/c496c8c55ed79816fec39e36a78645aa4458edb5">spee.ch/doitlive/c496c8c55ed79816fec39e36a78645aa4458edb5</a></li>
</ul>
</li>
<li><strong>spee.ch/&ltthe name of the claim&gt/all</strong>
<ul>
<li>To view a batch of files at a claim</li>
<li>E.g. <a href="/doitlive/all">spee.ch/doitlive/all</a></li>
</ul>
</li>
</ul>
<h3>API</h3>
<p>Note: these are being used for testing durring spee.ch development and may not be maintained</p>
<ul>
<li>A GET request to <strong>spee.ch/claim_list/&ltthe name of the claim&gt</strong>
<ul>
<li>Will return the claim_list for the claim in json format. </li>
<li>E.g. <a href="/claim_list/doitlive">spee.ch/claim_list/doitlive</a></li>
</ul>
</li>
</ul>
<section>
<h2>Examples:</h2>
<ul>
<li><a href="/coconuts">spee.ch/coconuts</a></li>
<li><a href="/wood">spee.ch/wood</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/ca3023187e901df9e9aabd95d6ae09b6cc69b3f0">spee.ch/doitlive/ca3023187e901df9e9aabd95d6ae09b6cc69b3f0</a></li>
</ul>
</section>
<section>
<h2>Publish Your Own</h2>
<div id="publish">
<form id="publish-form" action="" method="" enctype="multipart/form-data">
<input type="file" id="siofu_input" name="file" accept="video/*,image/*" onchange="previewFile()" enctype="multipart/form-data"/>
<br/>
<img id="image-preview" src="" height="200" alt="Image preview..."/>
<br/>
Name: <input type="text" id="publish-name" name="name" value="name"/>
<br/>
License: <select type="text" id="publish-license" name="license" value="license">
<option value="Public Domain">Public Domain</option>
<option value="Creative Commons">Creative Commons</option>
</select>
<br/>
NSFW: <select type="text" id="publish-nsfw" name="nsfw" value="false">
<option value="false">False</option>
<option value="true">True</option>
</select>
<br/>
<button id="publish-submit">Submit</button>
</form>
</div>
</section>
<section>
<h2>Help Wanted!</h2>
<p>If you would like to help make spee.ch amazing, join our slack channel.</p>
<p>We are currently in need of a designer to help with styling spee.ch's front end, but all help is welcome!</p>
</section>
<section>
<h2>Documentation</h2>
<h3>Site Navigation</h3>
<ul>
<li><strong><a href="/">spee.ch</a></strong>.
<ul>
<li>To publish a file, navigate to the homepage.</li>
</ul>
</li>
<li><strong>spee.ch/&ltthe name of the claim&gt</strong>
<ul>
<li>To view the file with the largest bid at a claim.</li>
<li>E.g. <a href="/doitlive">spee.ch/doitlive</a>.</li>
</ul>
</li>
<li><strong>spee.ch/&lt the name of the claim &gt/&lt the claim_id &gt</strong>
<ul>
<li>To view a specific file at a claim</li>
<li>E.g. <a href="/doitlive/c496c8c55ed79816fec39e36a78645aa4458edb5">spee.ch/doitlive/c496c8c55ed79816fec39e36a78645aa4458edb5</a></li>
</ul>
</li>
<li><strong>spee.ch/&ltthe name of the claim&gt/all</strong>
<ul>
<li>To view a batch of files at a claim</li>
<li>E.g. <a href="/doitlive/all">spee.ch/doitlive/all</a></li>
</ul>
</li>
</ul>
<h3>API</h3>
<p>Note: these are being used for testing durring spee.ch development and may not be maintained</p>
<ul>
<li>A GET request to <strong>spee.ch/claim_list/&ltthe name of the claim&gt</strong>
<ul>
<li>Will return the claim_list for the claim in json format. </li>
<li>E.g. <a href="/claim_list/doitlive">spee.ch/claim_list/doitlive</a></li>
</ul>
</li>
</ul>
</section>
<section>
<h2>Links</h2>
<a href="https://github.com/lbryio/spee.ch">github</a>
<a href="https://lbry.io/">lbry</a>
<a href="https://lbry.slack.com">slack</a>
</section>
<section>
<h2>Bugs</h2>
<p>Spee.ch is young and under continuous development so it will have bugs. Please leave an issue on our <a href="https://github.com/lbryio/spee.ch">github</a> if you experience a problem or have suggestions.</p>
<br>&nbsp;.w.
<br>(o|o)
<br>&nbsp;&nbsp;`'`
</section>
<script src="/socket.io/socket.io.js"></script>
<script src="/siofu/client.js"></script>
<script>
@ -94,11 +111,11 @@
var x = 1;
var adder = 1;
function addOne(){
var bars = '<h3>|';
var bars = '<p>|';
for (var i = 0; i < x; i++){
bars += ' | ';
}
bars += '</h3>';
bars += '</p>';
element.innerHTML = bars;
if (x === size){
adder = -1;
@ -117,14 +134,13 @@
var previewReader = new FileReader();
previewReader.onloadend = function () {
preview.src = previewReader.result;
}
};
if (selectedFile) {
previewReader.readAsDataURL(selectedFile); // reads the data and sets the img src
claimName.value = selectedFile.name.substring(0, selectedFile.name.indexOf("."));
} else {
preview.src = "";
};
}
}
function updatePublishStatus(msg){
@ -159,13 +175,20 @@
socket.on("publish-status", function(msg){
updatePublishStatus(msg);
})
socket.on("publish-failure", function(msg){
document.getElementById("publish").innerHTML = `<p>${msg}</p><p> --(✖╭╮✖)→ </p>`;
})
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>`;
publishResults += `<p>Your Claim ID is: ${msg.result.claim_id}</p>`;
publishResults += `<p>Your Transaction ID is: <a href="https://explorer.lbry.io/#!/transaction?id=${msg.result.txid}">${msg.result.txid}</a></p>`;
publishResults += `<p>Here is a link to the claim where your asset was published: <a href="https://spee.ch/${msg.name}">spee.ch/${msg.name}</a></p>`;
publishResults += `<p>Here is a direct link to your asset: <a href="https://spee.ch/${msg.name}/${msg.result.claim_id}">spee.ch/${msg.name}/${msg.result.claim_id}</a></p>`;
publishResults += `<p><i>NOTE: the transaction still needs to be mined by the network before you can access it! This may take a few minutes. To to view the transaction on the blockchain explorer click the Transaction ID link above.</i></p>`
publishResults += `<p><a href="/">Reload the page to publish another</a></p>`;
document.getElementById("publish").innerHTML = publishResults;
})

View file

@ -1,11 +1,7 @@
// require dependencies
var path = require('path');
var axios = require('axios');
var multipart = require('connect-multiparty');
var multipartMiddleware = multipart();
// import helpers
var lbryApi = require('../helpers/lbryApi.js');
var queueApi = require('../helpers/queueApi.js');
module.exports = function(app){
// route to return claim list in json

View file

@ -1,23 +1,7 @@
// load dependencies
var path = require('path');
var multipart = require('connect-multiparty');
var multipartMiddleware = multipart();
// load helpers
var routeHelpers = require('../helpers/routeHelpers.js');
var lbryApi = require('../helpers/lbryApi.js');
var queueApi = require('../helpers/queueApi.js');
function handleRequestError(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.status === 500) {
res.status(400).send(error.response.data.error.message);
} else {
res.status(400).send(error.toString());
};
}
// routes to export
module.exports = function(app){
// route to fetch one free public claim
app.get("/favicon.ico", function(req, res){
@ -36,7 +20,7 @@ module.exports = function(app){
})
.catch(function(error){
console.log("/:name/all error:", error);
handleRequestError(error, res);
routeHelpers.handleRequestError(error, res);
})
});
// route to fetch one free public claim
@ -51,10 +35,9 @@ module.exports = function(app){
})
.catch(function(error){
console.log("/:name/:claim_id error.")
handleRequestError(error, res);
routeHelpers.handleRequestError(error, res);
});
});
// route to fetch one free public claim
app.get("/:name", function(req, res){
console.log(">> GET request on /" + req.params.name);
@ -65,15 +48,13 @@ module.exports = function(app){
res.status(200).sendFile(filePath);
}).catch(function(error){
console.log("/:name error.");
handleRequestError(error, res);
routeHelpers.handleRequestError(error, res);
});
});
// route for the home page
app.get("/", function(req, res){
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.status(404).sendFile(path.join(__dirname, '../public', 'fourOhfour.html'));

View file

@ -1,7 +1,6 @@
module.exports = function(app) {
var http = require('http').Server(app);
var io = require('socket.io')(http);
//var fs = require('fs');
var path = require('path');
var siofu = require("socketio-file-upload");
var socketHelpers = require('../helpers/socketHelpers.js');
@ -22,7 +21,7 @@ module.exports = function(app) {
socket.emit("publish-status", "file upload successfully completed");
socketHelpers.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")
socket.emit("publish-failure", "file uploaded, but with errors")
};
});
// handle disconnect

View file

@ -1,31 +0,0 @@
// load dependencies
var amqp = require('amqplib/callback_api');
// load helpers
var lbryApi = require('./helpers/lbryApi');
// open a connection and a channel
amqp.connect('amqp://localhost', function(err, conn) {
// open a channel
conn.createChannel(function(err, ch) {
var q = 'task_queue2';
// declare the cue (in case the publisher hasn't made it yet)
ch.assertQueue(q, {durable: true});
// tell the queue to only assign one task at a time to this worker
ch.prefetch(1);
// listen for messages & pass callback for what to do with the msgs
console.log(" [x] Waiting for messages in %s. To exit press ctrl+c", q);
ch.consume(q, function(msg) {
var task = JSON.parse(msg.content.toString());
console.log(` [o] Received a ${task.type} task`);
// initiate the task
switch(task.type) {
case 'publish':
console.log(" [-] publishing:", task.data);
lbryApi.publishClaim(task.data);
break;
default:
console.log(" [-] that task type is not recognized");
console.log(" [x] Done");
}
}, {noAck: true});
});
});