added promises to claim retrieval and sockets to claim publishing #17
6 changed files with 122 additions and 159 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1 +1,2 @@
|
|||
node_modules
|
||||
temp
|
|
@ -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
|
||||
|
|
|
@ -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"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,7 +12,7 @@
|
|||
<h3>Examples:</h3>
|
||||
<ul>
|
||||
<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/all">spee.ch/doitlive/all</a></li>
|
||||
<li><a href="/doitlive/ca3023187e901df9e9aabd95d6ae09b6cc69b3f0">spee.ch/doitlive/ca3023187e901df9e9aabd95d6ae09b6cc69b3f0</a></li>
|
||||
|
@ -20,7 +20,7 @@
|
|||
<h3>Publish Your Own</h3>
|
||||
<div id="publish">
|
||||
<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/>
|
||||
<img id="image-preview" src="" height="200" alt="Image preview..."/>
|
||||
<br/>
|
||||
|
@ -36,8 +36,9 @@
|
|||
<option value="true">True</option>
|
||||
</select>
|
||||
<br/>
|
||||
<button type="submit" id="publish-submit">Submit</button>
|
||||
<button id="publish-submit">Submit</button>
|
||||
</form>
|
||||
<p id="upload-status"></p>
|
||||
</div>
|
||||
|
||||
<h3>Help</h3>
|
||||
|
@ -79,17 +80,19 @@
|
|||
</ul>
|
||||
|
||||
<script src="/socket.io/socket.io.js"></script>
|
||||
<script src="/siofu/client.js"></script>
|
||||
<script>
|
||||
// define global variables
|
||||
var socket = io();
|
||||
var selectedFile;
|
||||
var uploadReader = new FileReader();
|
||||
var uploader = new SocketIOFileUpload(socket);
|
||||
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 previewFile(){
|
||||
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();
|
||||
|
||||
previewReader.onloadend = function () {
|
||||
preview.src = previewReader.result;
|
||||
}
|
||||
|
@ -100,63 +103,38 @@
|
|||
preview.src = "";
|
||||
};
|
||||
}
|
||||
// function for updating progress bar
|
||||
function updateProgressBar(percent){
|
||||
document.getElementById('progress-bar').style.width = percent + '%';
|
||||
document.getElementById('percent').innerHTML = (Math.round(percent*100)/100) + '%';
|
||||
document.getElementById('MB').innerHTML = Math.round(((percent / 100.0 ) * selectedFile.size) / 1048576);
|
||||
// helper function to update status
|
||||
function updatePublishStatus(msg){
|
||||
document.getElementById("upload-status").innerHTML = msg;
|
||||
}
|
||||
// call the previewFile function
|
||||
previewFile();
|
||||
// publish through the socket
|
||||
document.getElementById('publish-submit').addEventListener('click', function(event) {
|
||||
console.log("upload event:", event);
|
||||
// prevent default on the submit button
|
||||
document.getElementById("publish-submit").addEventListener("click", function(event){
|
||||
event.preventDefault();
|
||||
|
||||
var fileName = document.getElementById('publish-name').value;
|
||||
var license = document.getElementById('publish-license').value;
|
||||
var nsfw = (document.getElementById('publish-nsfw').value.toLowerCase() === 'true');
|
||||
|
||||
if (document.getElementById('file-upload-input').value != ""){
|
||||
// build the upload visualizer
|
||||
var uploadVisualizer = '<div id="upload-visualizer">Uploading ' + fileName + '</div>';
|
||||
uploadVisualizer += '<div id="progress-container"><div id="progress-bar"></div></div><span id="percent">0%</span>';
|
||||
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');
|
||||
};
|
||||
|
||||
})
|
||||
// upload through the socket
|
||||
uploader.listenOnSubmit(document.getElementById("publish-submit"), document.getElementById("siofu_input"));
|
||||
// add listeners to the uploader
|
||||
uploader.addEventListener("start", function(event){
|
||||
// set meta data
|
||||
event.file.meta.name = name;
|
||||
event.file.meta.license = license;
|
||||
event.file.meta.nsfw = nsfw;
|
||||
});
|
||||
// provide more data
|
||||
socket.on('moreData', function(data){
|
||||
console.log("moreData");
|
||||
console.log("data:", data);
|
||||
updateProgressBar(data['percent']);
|
||||
var place = data['place'] * 524288; // the next block's starting position
|
||||
var newFile; // variable that will hold the new block of data
|
||||
if (selectedFile.slice) {
|
||||
newFile = selectedFile.slice(place, place + Math.min(524288, (selectedFile.size - place)));
|
||||
} else {
|
||||
newFile = selectedFile.mozSlice(place, place + Math.min(524288, (selectedFile.size - place)));
|
||||
};
|
||||
uploadReader.readAsBinaryString(newFile);
|
||||
});
|
||||
// listen for updates
|
||||
socket.on('publish-update', function(data){
|
||||
console.log('data:', data);
|
||||
document.getElementById('status').innerHTML = data;
|
||||
uploader.addEventListener("progress", function(event){
|
||||
var percent = event.bytesLoaded / event.file.size * 100;
|
||||
console.log();
|
||||
updatePublishStatus("File is " + percent.toFixed(2) + "% loaded");
|
||||
})
|
||||
// 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><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>`;
|
||||
document.getElementById("publish").innerHTML = publishResults;
|
||||
})
|
||||
|
||||
</script>
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue