sockets work for /claim and /claim/claim_id
This commit is contained in:
parent
fec251902e
commit
654c95e7ed
4 changed files with 84 additions and 106 deletions
|
@ -37,7 +37,30 @@ function orderTopClaims(claimsListArray){
|
||||||
return claimsListArray;
|
return claimsListArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getClaimWithUri(uri, resolve, reject){
|
||||||
|
axios.post('http://localhost:5279/lbryapi', {
|
||||||
|
method: "get",
|
||||||
|
params: { uri: uri }
|
||||||
|
}
|
||||||
|
).then(function (getUriResponse) {
|
||||||
|
console.log(">> 'get claim' success...");
|
||||||
|
console.log(">> response data:", getUriResponse.data);
|
||||||
|
console.log(">> dl path =", getUriResponse.data.result.download_path)
|
||||||
|
// resolve the promise with the download path for the claim we got
|
||||||
|
/* note: do not resolve until the download is actually complete */
|
||||||
|
resolve(getUriResponse.data.result.download_path);
|
||||||
|
}).catch(function(getUriError){
|
||||||
|
console.log(">> 'get' error:", getUriError.response.data);
|
||||||
|
// reject the promise with an error message
|
||||||
|
reject({
|
||||||
|
msg: "An error occurred while fetching the free, public claim by URI.",
|
||||||
|
err: getUriError.response.data.error.message
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|
||||||
publishClaim: function(publishObject){
|
publishClaim: function(publishObject){
|
||||||
axios.post('http://localhost:5279/lbryapi', publishObject)
|
axios.post('http://localhost:5279/lbryapi', publishObject)
|
||||||
.then(function (response) {
|
.then(function (response) {
|
||||||
|
@ -56,120 +79,82 @@ module.exports = {
|
||||||
//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}));
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
|
||||||
getClaimBasedOnNameOnly: function(claimName){
|
getClaimBasedOnNameOnly: function(claimName){
|
||||||
// 1. create a promise
|
// 1. create a promise
|
||||||
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', {
|
axios.post('http://localhost:5279/lbryapi', { // receives a promise
|
||||||
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)
|
|
||||||
|
var claimsList = response.data.result.claims;
|
||||||
|
console.log(">> Number of claims:", claimsList.length)
|
||||||
|
|
||||||
// return early if no claims were found
|
// return early if no claims were found
|
||||||
if (response.data.result.claims.length === 0){
|
if (claimsList.length === 0){
|
||||||
reject({
|
reject("no claims were found");
|
||||||
msg: "no claims",
|
console.log("exiting due to lack of claims");
|
||||||
err: "no claims"
|
|
||||||
});
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// filter the claims to return free, public claims
|
|
||||||
var freePublicClaims = filterForFreePublicClaims(response.data.result.claims);
|
// filter the claims to return only free, public claims
|
||||||
|
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({
|
reject("no free, public claims were found");
|
||||||
msg: "no free, public claims",
|
console.log("exiting due to lack of free or public claims");
|
||||||
err: "no free, public claims"
|
|
||||||
});
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// order the claims
|
// order the claims
|
||||||
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", orderedPublcClaims);
|
||||||
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
|
||||||
axios.post('http://localhost:5279/lbryapi', {
|
getClaimWithUri(freePublicClaimUri, resolve, reject);
|
||||||
method: "get",
|
|
||||||
params: { uri: freePublicClaimUri }
|
|
||||||
}
|
|
||||||
).then(function (getResponse) {
|
|
||||||
console.log(">> 'get claim' success...");
|
|
||||||
console.log(">> response data:", getResponse.data);
|
|
||||||
console.log(">> dl path =", getResponse.data.result.download_path)
|
|
||||||
// resolve the promise with the download path for the claim we got
|
|
||||||
resolve(getResponse.data.result.download_path);
|
|
||||||
}).catch(function(getError){
|
|
||||||
console.log(">> 'get' error:", getError.response.data);
|
|
||||||
// reject the promise with an error message
|
|
||||||
reject({
|
|
||||||
msg: "An error occurred while fetching the free, public claim by URI.",
|
|
||||||
err: getError.response.data.error.message
|
|
||||||
});
|
|
||||||
});
|
|
||||||
})
|
})
|
||||||
.catch(function(error){
|
.catch(function(error){
|
||||||
console.log(">> error:", error.response.data);
|
console.log(">> error:", error);
|
||||||
// check to see what kind of error came back from lbry
|
|
||||||
// reject the promise with an approriate message
|
// reject the promise with an approriate message
|
||||||
if (error.response.data){
|
reject(error.response.data.error);
|
||||||
reject({
|
return;
|
||||||
msg: "An error occurred while getting the claim list.",
|
|
||||||
err: error.response.data.error.message
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
reject({
|
|
||||||
msg: "An error occurred while getting the claim list.",
|
|
||||||
err: error.response
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
// 3. return the promise
|
// 3. return the promise
|
||||||
return deferred;
|
return deferred;
|
||||||
|
|
||||||
},
|
},
|
||||||
serveClaimBasedOnUri: function(uri, res){
|
|
||||||
|
getClaimBasedOnUri: function(uri){
|
||||||
/*
|
/*
|
||||||
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.
|
||||||
*/
|
*/
|
||||||
console.log(">> your uri:", uri);
|
var deferred = new Promise(function (resolve, reject){
|
||||||
// fetch the image to display
|
console.log(">> your uri:", uri);
|
||||||
axios.post('http://localhost:5279/lbryapi', { // to do: abstract this code to a function that can be shared
|
|
||||||
method: "get",
|
|
||||||
params: {
|
|
||||||
uri: uri
|
|
||||||
}
|
|
||||||
}
|
|
||||||
).then(function (getResponse) {
|
|
||||||
console.log(">> 'get claim' success...");
|
|
||||||
console.log(">> response data:", getResponse.data);
|
|
||||||
console.log(">> dl path =", getResponse.data.result.download_path)
|
|
||||||
/*
|
|
||||||
to do: make sure the file has completed downloading before serving back the file
|
|
||||||
*/
|
|
||||||
// return the claim we got
|
|
||||||
res.status(200).sendFile(getResponse.data.result.download_path);
|
|
||||||
|
|
||||||
/* delete the file after a certain amount of time? */
|
// fetch the image to display
|
||||||
|
getClaimWithUri(uri, resolve, reject);
|
||||||
|
});
|
||||||
|
return deferred;
|
||||||
|
|
||||||
}).catch(function(error){
|
|
||||||
console.log(">> /c/ 'get' error:", error.response.data);
|
|
||||||
res.status(500).send(JSON.stringify({msg: "an error occurred", err: error.response.data.error.message}));
|
|
||||||
})
|
|
||||||
},
|
},
|
||||||
|
|
||||||
serveAllClaims: function(claimName, res){
|
serveAllClaims: function(claimName, res){
|
||||||
// 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: {
|
params: { name: claimName }
|
||||||
name: claimName
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
).then(function (response) {
|
).then(function (response) {
|
||||||
console.log(">> Claim_list success");
|
console.log(">> Claim_list success");
|
||||||
|
@ -190,7 +175,10 @@ module.exports = {
|
||||||
// order the claims
|
// order the claims
|
||||||
var orderedPublicClaims = orderTopClaims(freePublicClaims);
|
var orderedPublicClaims = orderTopClaims(freePublicClaims);
|
||||||
// serve the response
|
// serve the response
|
||||||
res.status(200).send(orderedPublicClaims); //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
|
||||||
|
*/
|
||||||
|
res.status(200).send(orderedPublicClaims);
|
||||||
}).catch(function(error){
|
}).catch(function(error){
|
||||||
console.log(">> /c/ error:", error.response.data);
|
console.log(">> /c/ error:", error.response.data);
|
||||||
// serve the response
|
// serve the response
|
||||||
|
|
|
@ -18,15 +18,15 @@
|
||||||
console.log(document.URL)
|
console.log(document.URL)
|
||||||
var url = document.URL.substring(document.URL.indexOf('3000/') + 5); // get from the window?
|
var url = document.URL.substring(document.URL.indexOf('3000/') + 5); // get from the window?
|
||||||
// request the image through the socket
|
// request the image through the socket
|
||||||
socket.emit("image-request", url);
|
socket.emit("claim-request", url);
|
||||||
|
|
||||||
socket.on("image-update", function(data){
|
socket.on("claim-update", function(data){
|
||||||
console.log("data:", data);
|
console.log("data:", data);
|
||||||
document.getElementById("status").innerHTML = data;
|
document.getElementById("status").innerHTML = data;
|
||||||
})
|
})
|
||||||
|
|
||||||
// receive the image through the socket
|
// receive the image through the socket
|
||||||
socket.on("image-send", function(data){
|
socket.on("claim-send", function(data){
|
||||||
if (data.image) {
|
if (data.image) {
|
||||||
var base64Image = 'data:image/jpeg;base64,' + data.buffer;
|
var base64Image = 'data:image/jpeg;base64,' + data.buffer;
|
||||||
document.getElementById("image").innerHTML = '<img src="' + base64Image + '"/>';
|
document.getElementById("image").innerHTML = '<img src="' + base64Image + '"/>';
|
||||||
|
|
|
@ -17,9 +17,6 @@ module.exports = function(app){
|
||||||
app.post("/publish", multipartMiddleware, function(req, res){
|
app.post("/publish", multipartMiddleware, function(req, res){
|
||||||
// receive the request
|
// receive the request
|
||||||
console.log(" >> POST request on /publish");
|
console.log(" >> POST request on /publish");
|
||||||
//console.log(">> req.files:", req.files)
|
|
||||||
//console.log(" >> req.body:", req.body)
|
|
||||||
|
|
||||||
// build the data needed to publish the file
|
// build the data needed to publish the file
|
||||||
var publishObject = {
|
var publishObject = {
|
||||||
"method":"publish",
|
"method":"publish",
|
||||||
|
@ -37,8 +34,6 @@ module.exports = function(app){
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
//console.log(">> publishObject:", 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',
|
||||||
|
@ -46,7 +41,6 @@ module.exports = function(app){
|
||||||
}));
|
}));
|
||||||
// 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){
|
||||||
|
@ -56,16 +50,14 @@ module.exports = function(app){
|
||||||
});
|
});
|
||||||
// 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){
|
||||||
var uri = "lbry://" + req.params.name + "#" + req.params.claim_id;
|
console.log(">> GET request on /" + req.params.name + "#" + req.params.claim_id);
|
||||||
console.log(">> GET request on /" + uri);
|
res.status(200).sendFile(path.join(__dirname, '../public', 'claim.html'));
|
||||||
lbryApi.serveClaimBasedOnUri(uri, res);
|
|
||||||
});
|
});
|
||||||
// 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)
|
||||||
// retrieve the claim
|
// send page (includes a socket to get the file)
|
||||||
//lbryApi.serveClaimBasedOnNameOnly(name, res);
|
|
||||||
res.status(200).sendFile(path.join(__dirname, '../public', 'claim.html'));
|
res.status(200).sendFile(path.join(__dirname, '../public', 'claim.html'));
|
||||||
});
|
});
|
||||||
// route for the home page
|
// route for the home page
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
|
|
||||||
// routes to export
|
|
||||||
module.exports = function(app) {
|
module.exports = function(app) {
|
||||||
var http = require('http').Server(app);
|
var http = require('http').Server(app);
|
||||||
var io = require('socket.io')(http);
|
var io = require('socket.io')(http);
|
||||||
|
@ -10,12 +8,12 @@ module.exports = function(app) {
|
||||||
function sendTheImage(socket, filePath){
|
function sendTheImage(socket, filePath){
|
||||||
fs.readFile(filePath, function(err, buff){
|
fs.readFile(filePath, function(err, buff){
|
||||||
if (err) {
|
if (err) {
|
||||||
console.log("fs err", err);
|
console.log("socket: fs err:", err);
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
//console.log("buff", buff);
|
//console.log("buff", buff);
|
||||||
socket.emit('image-send', { image: true, buffer: buff.toString('base64') });
|
socket.emit('claim-send', { image: true, buffer: buff.toString('base64') });
|
||||||
console.log('image file has been sent via sockets');
|
console.log('socket: the image file has been sent via sockets');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,30 +21,30 @@ module.exports = function(app) {
|
||||||
console.log('a user connected');
|
console.log('a user connected');
|
||||||
|
|
||||||
// serve an image file from the server
|
// serve an image file from the server
|
||||||
socket.on('image-request', function(name){
|
socket.on('claim-request', function(query){
|
||||||
// 1. retrieve the image from lbry via daemon
|
// 1. retrieve the image from lbry via daemon
|
||||||
console.log("received image request for:", name)
|
console.log("socket: received claim request for:", query)
|
||||||
var promise = lbryApi.getClaimBasedOnNameOnly(name);
|
if (query.indexOf("/") === -1){
|
||||||
|
var promise = lbryApi.getClaimBasedOnNameOnly(query)
|
||||||
|
} else {
|
||||||
|
var uri = query.replace("/", "#");
|
||||||
|
var promise = lbryApi.getClaimBasedOnUri(uri)
|
||||||
|
}
|
||||||
promise.then(function(data){
|
promise.then(function(data){
|
||||||
console.log("socket-routes / image-request - success:", data)
|
console.log("socket: claim-request - success:", data)
|
||||||
// 3. serve the image back once it is retrieved
|
// 3. serve the image back once it is retrieved
|
||||||
sendTheImage(socket, data);
|
sendTheImage(socket, data);
|
||||||
|
return;
|
||||||
})
|
})
|
||||||
.catch(function(error){
|
.catch(function(error){
|
||||||
console.log("socket-routes / image-request - error:", error)
|
console.log("socket: claim-request - error:", error)
|
||||||
// handle the errors
|
// handle the error
|
||||||
if (error.msg === "no claims"){
|
socket.emit("claim-update", error);
|
||||||
socket.emit("image-update", "no claims were found for " + name);
|
|
||||||
} else if (error.msg === "no free, public claims"){
|
|
||||||
socket.emit("image-update", "no free, public claims were found for " + name);
|
|
||||||
} else {
|
|
||||||
socket.emit("image-update", "an unknown error occured with fetching claim");
|
|
||||||
};
|
|
||||||
return;
|
return;
|
||||||
});
|
});
|
||||||
|
|
||||||
// 2. emit updates as the image is being retrieved
|
// 2. emit updates as the image is being retrieved
|
||||||
socket.emit("image-update", "we are getting your image for " + name);
|
socket.emit("claim-update", "We are getting your claim for " + query);
|
||||||
})
|
})
|
||||||
|
|
||||||
// handle disconnect
|
// handle disconnect
|
||||||
|
|
Loading…
Reference in a new issue