uploader works locally

This commit is contained in:
bill bittner 2017-05-31 19:48:09 -07:00
parent 6098537c12
commit fab8216abb
6 changed files with 122 additions and 159 deletions

1
.gitignore vendored
View file

@ -1 +1,2 @@
node_modules node_modules
temp

View file

@ -70,25 +70,31 @@ function findAllClaims(name, resolve, reject){
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");
return; // return the claim we got
}).catch(function(error){ resolve(response.data);
// receive response from LBRY return;
// if not successfull, (1) delete file and (2) send response to the client }).catch(function(error){
console.log(">> 'publish' error.response.data:", error.response.data); // receive response from LBRY
console.log(" [x] Done"); console.log(">> 'publish' error");
//res.status(500).send(JSON.stringify({msg: "your file was not published", err: error.response.data.error.message})); if (error.response.data.error){
return; reject(error.response.data.error);
} else {
reject(error);
}
return;
})
}) })
return deferred;
}, },
getClaimBasedOnNameOnly: function(claimName){ getClaimBasedOnNameOnly: function(claimName){
@ -119,10 +125,10 @@ module.exports = {
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"); 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); 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);
@ -187,7 +193,7 @@ module.exports = {
return; return;
} }
// order the claims // order the claims
var orderedPublcClaims = orderTopClaims(freePublicClaims); var orderedPublicClaims = orderTopClaims(freePublicClaims);
// serve the response // serve the response
/* /*
to do: rather than returning json, serve a page of all these claims to do: rather than returning json, serve a page of all these claims

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,7 +12,7 @@
<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="/sunflower">spee.ch/sunflower</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>
@ -20,7 +20,7 @@
<h3>Publish Your Own</h3> <h3>Publish Your Own</h3>
<div id="publish"> <div id="publish">
<form id="publish-form" action="" method="" enctype="multipart/form-data"> <form id="publish-form" action="" method="" enctype="multipart/form-data">
<input type="file" id="file-upload-input" name="file" accept="video/*,image/*" onchange="previewFile()" enctype="multipart/form-data"/> <input type="file" id="siofu_input" name="file" accept="video/*,image/*" onchange="previewFile()" enctype="multipart/form-data"/>
<br/> <br/>
<img id="image-preview" src="" height="200" alt="Image preview..."/> <img id="image-preview" src="" height="200" alt="Image preview..."/>
<br/> <br/>
@ -36,8 +36,9 @@
<option value="true">True</option> <option value="true">True</option>
</select> </select>
<br/> <br/>
<button type="submit" id="publish-submit">Submit</button> <button id="publish-submit">Submit</button>
</form> </form>
<p id="upload-status"></p>
</div> </div>
<h3>Help</h3> <h3>Help</h3>
@ -79,17 +80,19 @@
</ul> </ul>
<script src="/socket.io/socket.io.js"></script> <script src="/socket.io/socket.io.js"></script>
<script src="/siofu/client.js"></script>
<script> <script>
// define global variables // define global variables
var socket = io(); var socket = io();
var selectedFile; var uploader = new SocketIOFileUpload(socket);
var uploadReader = new FileReader(); var name = document.getElementById('publish-name').value;
var license = document.getElementById('publish-license').value;
var nsfw = document.getElementById('publish-nsfw').value;
// function to handle image preview // 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
selectedFile = document.querySelector('input[name=file]').files[0]; var selectedFile = document.querySelector('input[name=file]').files[0];
var previewReader = new FileReader(); var previewReader = new FileReader();
previewReader.onloadend = function () { previewReader.onloadend = function () {
preview.src = previewReader.result; preview.src = previewReader.result;
} }
@ -100,63 +103,38 @@
preview.src = ""; preview.src = "";
}; };
} }
// function for updating progress bar // helper function to update status
function updateProgressBar(percent){ function updatePublishStatus(msg){
document.getElementById('progress-bar').style.width = percent + '%'; document.getElementById("upload-status").innerHTML = msg;
document.getElementById('percent').innerHTML = (Math.round(percent*100)/100) + '%';
document.getElementById('MB').innerHTML = Math.round(((percent / 100.0 ) * selectedFile.size) / 1048576);
} }
// call the previewFile function // call the previewFile function
previewFile(); previewFile();
// publish through the socket // prevent default on the submit button
document.getElementById('publish-submit').addEventListener('click', function(event) { document.getElementById("publish-submit").addEventListener("click", function(event){
console.log("upload event:", event);
event.preventDefault(); event.preventDefault();
})
var fileName = document.getElementById('publish-name').value; // upload through the socket
var license = document.getElementById('publish-license').value; uploader.listenOnSubmit(document.getElementById("publish-submit"), document.getElementById("siofu_input"));
var nsfw = (document.getElementById('publish-nsfw').value.toLowerCase() === 'true'); // add listeners to the uploader
uploader.addEventListener("start", function(event){
if (document.getElementById('file-upload-input').value != ""){ // set meta data
// build the upload visualizer event.file.meta.name = name;
var uploadVisualizer = '<div id="upload-visualizer">Uploading ' + fileName + '</div>'; event.file.meta.license = license;
uploadVisualizer += '<div id="progress-container"><div id="progress-bar"></div></div><span id="percent">0%</span>'; event.file.meta.nsfw = nsfw;
uploadVisualizer += "<span id='uploaded'> - <span id='MB'>0</span>/" + Math.round(selectedFile.size / 1048576) + "MB</span>";
// place upload visualizer in the HTML
document.getElementById("publish").innerHTML = uploadVisualizer;
// reade the file and emit socket msg
uploadReader.onload = function(readerEvent){
socket.emit('upload', {
'name': fileName,
'license': license,
'nsfw': nsfw,
'data': readerEvent.target.result
});
}
socket.emit('upload-start', {'name': fileName, 'size' : selectedFile.size })
} else {
alert('Please select a file');
};
}); });
// provide more data uploader.addEventListener("progress", function(event){
socket.on('moreData', function(data){ var percent = event.bytesLoaded / event.file.size * 100;
console.log("moreData"); console.log();
console.log("data:", data); updatePublishStatus("File is " + percent.toFixed(2) + "% loaded");
updateProgressBar(data['percent']); })
var place = data['place'] * 524288; // the next block's starting position // add listener for publish status updates
var newFile; // variable that will hold the new block of data socket.on("publish-status", function(msg){
if (selectedFile.slice) { updatePublishStatus(msg);
newFile = selectedFile.slice(place, place + Math.min(524288, (selectedFile.size - place))); })
} else { socket.on("publish-complete", function(msg){
newFile = selectedFile.mozSlice(place, place + Math.min(524288, (selectedFile.size - place))); console.log("publish complete", msg);
}; var publishResults = `<p>You're publish is complete!</p><p>The Claim Id is ${msg.result.claim_id}</p><p>The TX Id is ${msg.result.txid}</p><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><button id="reset-publish">Publish another</button>`;
uploadReader.readAsBinaryString(newFile); document.getElementById("publish").innerHTML = publishResults;
});
// listen for updates
socket.on('publish-update', function(data){
console.log('data:', data);
document.getElementById('status').innerHTML = data;
}) })
</script> </script>

View file

@ -4,96 +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 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 // functions to create a publishing object
function createPublishObject(fileInfo, filePath){ function createPublishParams(name, filepath, license, nsfw){
var publishObject = { var publishParams = {
"method":"publish", "name": name,
"params": { "file_path": rootDirectory + filepath,
"name": fileInfo.name, "bid": 0.1,
"file_path": filePath, "metadata": {
"bid": 0.1, "description": name + "published via spee.ch",
"metadata": { "title": name,
"description": fileInfo.description, "author": "spee.ch",
"title": fileInfo.title, "language": "en",
"author": fileInfo.author, "license": license,
"language": fileInfo.language, "nsfw": (nsfw.toLowerCase() === "true")
"license": fileInfo.license,
"nsfw": (fileInfo.nsfw.toLowerCase() === "true")
}
} }
}; };
return publishObject; return publishParams;
} }
// publish an image to lbry // publish an image to lbry
function publish(data, filePath){ function publish(name, filepath, license, nsfw, socket){
// 1. receive the file // update the client
socket.emit("publish-status", "starting publishing...");
// 2. create the publish object // create the publish object
var publishObject = createPublishObject(data, filePath); var publishParams = createPublishParams(name, filepath, license, nsfw);
// 3. post the task to the que // get a promise to publish
queueApi.addNewTaskToQueue(JSON.stringify({ var promise = lbryApi.publishClaim(publishParams);
type: 'publish', // handle promise
data: publishObject 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');
console.log("files", files) // listener for uploader
// socket listener for starting an upload var uploader = new siofu();
socket.on('upload-start', function(data){ // data contains the variables that we passed from the client uploader.dir = "./temp";
console.log("upload-start"); uploader.listen(socket);
console.log("files", files) // attach upload listeners
var name = data['name']; uploader.on("error", function(event){
files[name] = { // create a new entry in the files object console.log("an error occured while uploading", event.error);
fileSize: data['size'], socket.emit("publish-status", event.error)
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 });
};
});
}) })
uploader.on("saved", function(event){
socket.on('upload', function(data){ console.log("saved " + event.file.name);
console.log("upload"); if (event.file.success){
console.log("files", files) socket.emit("publish-status", "file upload successfully completed");
var name = data['name']; publish(event.file.meta.name, event.file.pathName, event.file.meta.license,event.file.meta.nsfw, socket)
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 { } else {
var place = file[name]['downloaded'] / 524288; socket.emit("publish-status", "file saved, but with errors")
var percent = (files[name]['downloaded'] / files[name]['fileSize']) * 100; };
socket.emit('moreData', {'place': place, 'percent': percent});
}
}); });
// handle disconnect // handle disconnect

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