added promises to claim retrieval and sockets to claim publishing #17
10 changed files with 359 additions and 168 deletions
BIN
Temp/name
Normal file
BIN
Temp/name
Normal file
Binary file not shown.
After Width: | Height: | Size: 27 KiB |
BIN
Temp/name22
Normal file
BIN
Temp/name22
Normal file
Binary file not shown.
After Width: | Height: | Size: 27 KiB |
BIN
Temp/namef
Normal file
BIN
Temp/namef
Normal file
Binary file not shown.
After Width: | Height: | Size: 27 KiB |
BIN
Temp/names
Normal file
BIN
Temp/names
Normal file
Binary file not shown.
After Width: | Height: | Size: 14 KiB |
|
@ -38,7 +38,7 @@ 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 }
|
||||||
|
@ -64,6 +64,10 @@ function getClaimWithUri(uri, resolve, reject){
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function findAllClaims(name, resolve, reject){
|
||||||
|
// abstract claim_list function to here
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|
||||||
publishClaim: function(publishObject){
|
publishClaim: function(publishObject){
|
||||||
|
@ -76,12 +80,14 @@ module.exports = {
|
||||||
console.log(" [x] Done");
|
console.log(" [x] Done");
|
||||||
// return the claim we got
|
// return the claim we got
|
||||||
//res.status(200).send(JSON.stringify({msg: "you succsessfully published!", txData: response.data}));
|
//res.status(200).send(JSON.stringify({msg: "you succsessfully published!", txData: response.data}));
|
||||||
|
return;
|
||||||
}).catch(function(error){
|
}).catch(function(error){
|
||||||
// receive response from LBRY
|
// receive response from LBRY
|
||||||
// if not successfull, (1) delete file and (2) send response to the client
|
// if not successfull, (1) delete file and (2) send response to the client
|
||||||
console.log(">> 'publish' error.response.data:", error.response.data);
|
console.log(">> 'publish' error.response.data:", error.response.data);
|
||||||
console.log(" [x] Done");
|
console.log(" [x] Done");
|
||||||
//res.status(500).send(JSON.stringify({msg: "your file was not published", err: error.response.data.error.message}));
|
//res.status(500).send(JSON.stringify({msg: "your file was not published", err: error.response.data.error.message}));
|
||||||
|
return;
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -95,14 +101,14 @@ module.exports = {
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
@ -112,7 +118,7 @@ module.exports = {
|
||||||
|
|
||||||
// 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;
|
||||||
}
|
}
|
||||||
|
@ -121,18 +127,22 @@ module.exports = {
|
||||||
var orderedPublcClaims = orderTopClaims(freePublicClaims);
|
var orderedPublcClaims = 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 = "lbry://" + orderedPublcClaims[0].name + "#" + orderedPublcClaims[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
|
||||||
|
if (error.code === "ECONNREFUSED"){
|
||||||
|
reject("Connection refused. The daemon may not be running.")
|
||||||
|
} else {
|
||||||
reject(error.response.data.error);
|
reject(error.response.data.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,25 +164,27 @@ module.exports = {
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|
||||||
serveAllClaims: function(claimName, res){ // note: work in progress
|
getAllClaims: function(claimName, res){ // note: work in progress
|
||||||
|
var deferred = new Promise(function(resolve, reject){
|
||||||
|
console.log(">> get all claims data for", claimName)
|
||||||
// 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', {
|
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");
|
||||||
console.log(">> Number of claims:", response.data.result.claims.length)
|
console.log(">> Number of claims:", response.data.result.claims.length)
|
||||||
// return early if no claims were found
|
// return early if no claims were found
|
||||||
if (response.data.result.claims.length === 0){
|
if (response.data.result.claims.length === 0){
|
||||||
res.status(200).sendFile(path.join(__dirname, '../public', 'noClaims.html'));
|
res.status(307).sendFile(path.join(__dirname, '../public', 'noClaims.html'));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// filter the claims to return free, public claims
|
// filter the claims to return free, public claims
|
||||||
var freePublicClaims = filterForFreePublicClaims(response.data.result.claims);
|
var freePublicClaims = filterForFreePublicClaims(response.data.result.claims);
|
||||||
// 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)){
|
||||||
res.status(200).sendFile(path.join(__dirname, '../public', 'noClaims.html'));
|
res.status(307).sendFile(path.join(__dirname, '../public', 'noClaims.html'));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
console.log(">> Number of free public claims:", freePublicClaims.length);
|
console.log(">> Number of free public claims:", freePublicClaims.length);
|
||||||
|
@ -184,9 +196,13 @@ module.exports = {
|
||||||
*/
|
*/
|
||||||
res.status(200).send(orderedPublicClaims);
|
res.status(200).send(orderedPublicClaims);
|
||||||
}).catch(function(error){
|
}).catch(function(error){
|
||||||
console.log(">> /c/ error:", error.response.data);
|
console.log(">> 'claim_list' 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}));
|
res.status(500).send(JSON.stringify({msg: "An error occurred while finding the claim list.", err: error.response.data.error.message}));
|
||||||
})
|
})
|
||||||
|
|
||||||
|
});
|
||||||
|
return deffered;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,33 +18,35 @@
|
||||||
<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">
|
||||||
|
<input type="file" id="file-upload-input" name="file" accept="video/*,image/*" onchange="previewFile()" enctype="multipart/form-data"/>
|
||||||
<br/>
|
<br/>
|
||||||
<img src="" height="200" alt="Image preview..."/>
|
<img id="image-preview" src="" height="200" alt="Image preview..."/>
|
||||||
<br/>
|
<br/>
|
||||||
Name: <input type="text" name="name" value="name"/>
|
Name: <input type="text" id="publish-name" name="name" value="name"/>
|
||||||
<br/>
|
<br/>
|
||||||
Title: <input type="text" name="title" value="title"/>
|
Title: <input id="publish-title" type="text" name="title" value="title"/>
|
||||||
<br/>
|
<br/>
|
||||||
Description: <input type="text" name="description" value="I love spee.ch!"/>
|
Description: <input id="publish-description" type="text" name="description" value="I love spee.ch!"/>
|
||||||
<br/>
|
<br/>
|
||||||
Author: <input type="text" name="author" value="author"/>
|
Author: <input type="text" id="publish-author" name="author" value="author"/>
|
||||||
<br/>
|
<br/>
|
||||||
Language: <input type="text" name="language" value="en"/>
|
Language: <input type="text" id="publish-language" name="language" value="en"/>
|
||||||
<br/>
|
<br/>
|
||||||
License: <select type="text" name="license" value="license">
|
License: <select type="text" id="publish-license" name="license" value="license">
|
||||||
<option value="Creative Commons">Creative Commons</option>
|
<option value="Creative Commons">Creative Commons</option>
|
||||||
<option value="Public Domain">Public Domain</option>
|
<option value="Public Domain">Public Domain</option>
|
||||||
</select>
|
</select>
|
||||||
<br/>
|
<br/>
|
||||||
NSFW: <select type="text" name="nsfw" value="false">
|
NSFW: <select type="text" id="publish-nsfw" name="nsfw" value="false">
|
||||||
<option value="false">False</option>
|
<option value="false">False</option>
|
||||||
<option value="true">True</option>
|
<option value="true">True</option>
|
||||||
</select>
|
</select>
|
||||||
<br/>
|
<br/>
|
||||||
<button type="submit">Submit</button>
|
<button type="submit" id="publish-submit">Submit</button>
|
||||||
</form>
|
</form>
|
||||||
|
</div>
|
||||||
|
|
||||||
<h3>Help</h3>
|
<h3>Help</h3>
|
||||||
<h4>Site Navigation</h4>
|
<h4>Site Navigation</h4>
|
||||||
|
@ -84,28 +86,95 @@
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
<script>
|
|
||||||
function previewFile(){
|
|
||||||
var preview = document.querySelector('img'); //selects the query named img
|
|
||||||
var file = document.querySelector('input[type=file]').files[0]; //sames as here
|
|
||||||
var reader = new FileReader();
|
|
||||||
|
|
||||||
reader.onloadend = function () {
|
|
||||||
preview.src = reader.result;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (file) {
|
|
||||||
reader.readAsDataURL(file); //reads the data as a URL
|
|
||||||
} else {
|
|
||||||
preview.src = "";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
previewFile(); //calls the function named previewFile()
|
|
||||||
|
|
||||||
</script>
|
|
||||||
<script src="/socket.io/socket.io.js"></script>
|
<script src="/socket.io/socket.io.js"></script>
|
||||||
<script>
|
<script>
|
||||||
|
// define global variables
|
||||||
var socket = io();
|
var socket = io();
|
||||||
|
var selectedFile;
|
||||||
|
var uploadReader = new FileReader();
|
||||||
|
// 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 previewReader = new FileReader();
|
||||||
|
|
||||||
|
previewReader.onloadend = function () {
|
||||||
|
preview.src = previewReader.result;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (selectedFile) {
|
||||||
|
previewReader.readAsDataURL(selectedFile); // reads the data as a URL
|
||||||
|
} else {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
// call the previewFile function
|
||||||
|
previewFile();
|
||||||
|
// publish through the socket
|
||||||
|
document.getElementById('publish-submit').addEventListener('click', function(event) {
|
||||||
|
console.log("upload event:", event);
|
||||||
|
event.preventDefault();
|
||||||
|
|
||||||
|
var fileName = document.getElementById('publish-name').value;
|
||||||
|
var description = document.getElementById('publish-description').value;
|
||||||
|
var title = document.getElementById('publish-title').value;
|
||||||
|
var author = document.getElementById('publish-author').value;
|
||||||
|
var language = document.getElementById('publish-language').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,
|
||||||
|
'description': description,
|
||||||
|
'title': title,
|
||||||
|
'author': author,
|
||||||
|
'language': language,
|
||||||
|
'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
|
||||||
|
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;
|
||||||
|
})
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
|
@ -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>
|
|
@ -6,26 +6,26 @@ 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
|
// // helper functions
|
||||||
function createPublishObject(req){
|
// function createPublishObject(req){
|
||||||
var publishObject = {
|
// var publishObject = {
|
||||||
"method":"publish",
|
// "method":"publish",
|
||||||
"params": {
|
// "params": {
|
||||||
"name": req.body.name,
|
// "name": req.body.name,
|
||||||
"file_path": req.files.file.path,
|
// "file_path": req.files.file.path,
|
||||||
"bid": 0.1,
|
// "bid": 0.1,
|
||||||
"metadata": {
|
// "metadata": {
|
||||||
"description": req.body.description,
|
// "description": req.body.description,
|
||||||
"title": req.body.title,
|
// "title": req.body.title,
|
||||||
"author": req.body.author,
|
// "author": req.body.author,
|
||||||
"language": req.body.language,
|
// "language": req.body.language,
|
||||||
"license": req.body.license,
|
// "license": req.body.license,
|
||||||
"nsfw": (req.body.nsfw.toLowerCase() === "true")
|
// "nsfw": (req.body.nsfw.toLowerCase() === "true")
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
};
|
// };
|
||||||
return publishObject;
|
// return publishObject;
|
||||||
}
|
// }
|
||||||
|
|
||||||
// routes to export
|
// routes to export
|
||||||
module.exports = function(app){
|
module.exports = function(app){
|
||||||
|
@ -34,45 +34,102 @@ 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
|
// // route to publish a new claim
|
||||||
app.post("/publish", multipartMiddleware, function(req, res){
|
// app.post("/publish", multipartMiddleware, function(req, res){
|
||||||
console.log(" >> POST request on /publish");
|
// console.log(" >> POST request on /publish");
|
||||||
// build the data needed to publish the file
|
// // build the data needed to publish the file
|
||||||
var publishObject = createPublishObject(req);
|
// var publishObject = createPublishObject(req);
|
||||||
console.log("publish", publishObject);
|
// console.log("publish", publishObject);
|
||||||
// post the task to the que
|
// // post the task to the que
|
||||||
queueApi.addNewTaskToQueue(JSON.stringify({
|
// queueApi.addNewTaskToQueue(JSON.stringify({
|
||||||
type: 'publish',
|
// type: 'publish',
|
||||||
data: publishObject
|
// data: publishObject
|
||||||
}));
|
// }));
|
||||||
// respond to the client that the task has been queued
|
// // respond to the client that the task has been queued
|
||||||
res.status(200).sendFile(path.join(__dirname, '../public', 'publishingClaim.html'));
|
// 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);
|
lbryApi.serveAllClaims(name, res);
|
||||||
|
|
||||||
|
var promise = lbryApi.getAllClaims(name);
|
||||||
|
// handle the promise resolve
|
||||||
|
promise.then(function(orderedFreePublicClaims){
|
||||||
|
res.status(200).send(orderedFreePublicClaims);
|
||||||
|
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;
|
||||||
|
} 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);
|
||||||
|
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)
|
var promise = lbryApi.getClaimBasedOnNameOnly(name);
|
||||||
res.status(200).sendFile(path.join(__dirname, '../public', 'claim.html'));
|
// 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;
|
||||||
|
} else {
|
||||||
|
res.status(400).send(error);
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
});
|
||||||
});
|
});
|
||||||
// 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'));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,49 +4,98 @@ 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
|
||||||
|
|
||||||
function sendTheImage(socket, filePath){
|
// functions to create a publishing object
|
||||||
fs.readFile(filePath, function(err, buff){
|
function createPublishObject(fileInfo, filePath){
|
||||||
if (err) {
|
var publishObject = {
|
||||||
console.log("socket: fs err:", err);
|
"method":"publish",
|
||||||
return;
|
"params": {
|
||||||
};
|
"name": fileInfo.name,
|
||||||
//console.log("buff", buff);
|
"file_path": filePath,
|
||||||
socket.emit('claim-send', { image: true, buffer: buff.toString('base64') });
|
"bid": 0.1,
|
||||||
console.log('socket: the image file has been sent via sockets');
|
"metadata": {
|
||||||
});
|
"description": fileInfo.description,
|
||||||
|
"title": fileInfo.title,
|
||||||
|
"author": fileInfo.author,
|
||||||
|
"language": fileInfo.language,
|
||||||
|
"license": fileInfo.license,
|
||||||
|
"nsfw": (fileInfo.nsfw.toLowerCase() === "true")
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
return publishObject;
|
||||||
|
}
|
||||||
|
// 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
|
||||||
|
}));
|
||||||
|
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
io.on('connection', function(socket){
|
io.on('connection', function(socket){
|
||||||
console.log('a user connected');
|
console.log('a user connected');
|
||||||
|
console.log("files", files)
|
||||||
// serve an image file from the server
|
// socket listener for starting an upload
|
||||||
socket.on('claim-request', function(query){
|
socket.on('upload-start', function(data){ // data contains the variables that we passed from the client
|
||||||
// 1. retrieve the image from lbry via daemon
|
console.log("upload-start");
|
||||||
console.log("socket: received claim request for:", query)
|
console.log("files", files)
|
||||||
if (query.indexOf("/") === -1){
|
var name = data['name'];
|
||||||
var promise = lbryApi.getClaimBasedOnNameOnly(query)
|
files[name] = { // create a new entry in the files object
|
||||||
} else {
|
fileSize: data['size'],
|
||||||
var uri = query.replace("/", "#");
|
data: '',
|
||||||
var promise = lbryApi.getClaimBasedOnUri(uri)
|
downloaded: 0
|
||||||
}
|
}
|
||||||
promise.then(function(data){
|
var place = 0;
|
||||||
console.log("socket: claim-request - success:", data)
|
try { // if its a file we already tried
|
||||||
// 3. serve the image back once it is retrieved
|
var stat = fs.statSync('Temp/' + name);
|
||||||
sendTheImage(socket, data);
|
if (stat.isFile()) {
|
||||||
return;
|
files[name]['downloaded'] = stat.size;
|
||||||
})
|
place = stat.size / 524288;
|
||||||
.catch(function(error){
|
};
|
||||||
console.log("socket: claim-request - error:", error)
|
} catch(er) {}; // if it's a new file
|
||||||
// handle the error
|
fs.open("Temp/" + name, "a", 0755, function(err, fd){
|
||||||
socket.emit("claim-update", error);
|
if (err) {
|
||||||
return;
|
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 });
|
||||||
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
// 2. emit updates as the image is being retrieved
|
|
||||||
socket.emit("claim-update", "We are getting your claim for " + query);
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
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});
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
var place = file[name]['downloaded'] / 524288;
|
||||||
|
var percent = (files[name]['downloaded'] / files[name]['fileSize']) * 100;
|
||||||
|
socket.emit('moreData', {'place': place, 'percent': percent});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// handle disconnect
|
// handle disconnect
|
||||||
socket.on('disconnect', function(){
|
socket.on('disconnect', function(){
|
||||||
console.log('user disconnected');
|
console.log('user disconnected');
|
||||||
|
|
Loading…
Reference in a new issue