C:/Program Files/Git/name/claim_id checks local first

This commit is contained in:
bill bittner 2017-06-15 13:06:35 -07:00
parent 36d64ad7b0
commit 6f7d9af768
5 changed files with 57 additions and 35 deletions

View file

@ -24,7 +24,7 @@ module.exports = {
"method": "get", "method": "get",
"params": { "uri": uri, "timeout": 20} "params": { "uri": uri, "timeout": 20}
}).then(function (getResponse) { }).then(function (getResponse) {
console.log(">> 'get' success"); console.log(">> 'get' success", getResponse.data);
//check to make sure the daemon didn't just time out (or otherwise send an error that appears to be a success response) //check to make sure the daemon didn't just time out (or otherwise send an error that appears to be a success response)
if (getResponse.data.result.error){ if (getResponse.data.result.error){
reject(getResponse.data.result.error); reject(getResponse.data.result.error);

View file

@ -1,6 +1,7 @@
var path = require('path'); var path = require('path');
var axios = require('axios'); var axios = require('axios');
var lbryApi = require('./lbryApi'); var lbryApi = require('./lbryApi');
var db = require("../models");
function filterForFreePublicClaims(claimsListArray){ function filterForFreePublicClaims(claimsListArray){
//console.log("claims list:", claimsListArray) //console.log("claims list:", claimsListArray)
@ -98,33 +99,50 @@ module.exports = {
}); });
return deferred; return deferred;
}, },
getClaimBasedOnUri: function(uri){ getClaimBasedOnUri: function(claimName, claimId){
var deferred = new Promise(function (resolve, reject){ var deferred = new Promise(function (resolve, reject){
var uri = claimName + "#" + claimId;
console.log(">> lbryHelpers >> getClaimBasedOnUri:", uri); console.log(">> lbryHelpers >> getClaimBasedOnUri:", uri);
// resolve the claim // check locally for the claim
lbryApi.resolveUri(uri) db.File.findOne({where: { claim_id: claimId }})
.then(function(resolvedUri){ .then(function(claim){
//console.log("result >>", resolvedUri) console.log("asset found locally >>", claim)
// check to make sure it is free and public // if a record is found, return it
if (isFreePublicClaim(resolvedUri.result[uri].claim)){ if (claim){
// promise to get the chosen uri var fileInfo = {
lbryApi.getClaim(uri) file_name: claim.dataValues.name,
.then(function(data){ download_path: claim.dataValues.path,
resolve({ content_type: claim.dataValues.file_type
fileName: data.result.file_name, }
directory: data.result.download_directory, resolve(fileInfo);
contentType: data.result.metadata.stream.source.contentType // ... otherwise use daemon to retrieve it
}); } else {
// get the claim info via 'resolve'
lbryApi.resolveUri(uri)
.then(function(resolvedUri){
// check to make sure it is free and public
if (isFreePublicClaim(resolvedUri.result[uri].claim)){
// 'get' the claim
lbryApi.getClaim(uri)
.then(function(data){
resolve({
file_name: data.result.file_name,
download_path: data.result.download_path,
content_type: data.result.metadata.stream.source.contentType
});
}).catch(function(error){
reject(error)
});
} else {
reject("NO_FREE_PUBLIC_CLAIMS");
}
}).catch(function(error){ }).catch(function(error){
reject(error) reject(error)
}); });
} else {
reject("NO_FREE_PUBLIC_CLAIMS");
} }
}).catch(function(error){ }).catch(function(error){
reject(error) reject(error);
}); })
}); });
return deferred; return deferred;
}, },

View file

@ -62,6 +62,8 @@ module.exports = {
file_type: fileType, file_type: fileType,
claim_id: data.result.claim_id, claim_id: data.result.claim_id,
nsfw: nsfw, nsfw: nsfw,
}).catch(function(error){
console.log('an error occurred when writing to the MySQL database. Check the logs.');
}); });
}) })
.catch(function(error){ .catch(function(error){

View file

@ -1,14 +1,13 @@
function serveFile(fileInfo, res){ function serveFile(fileInfo, res){
// set default options // set default options
var options = { var options = {
root: fileInfo.directory,
headers: { headers: {
"X-Content-Type-Options": "nosniff", "X-Content-Type-Options": "nosniff",
"Content-Type": fileInfo.contentType "Content-Type": fileInfo.content_type
} }
}; };
// adjust default options as needed // adjust default options as needed
switch (fileInfo.contentType){ switch (fileInfo.content_type){
case "image/jpeg": case "image/jpeg":
break; break;
case "image/gif": case "image/gif":
@ -23,19 +22,20 @@ function serveFile(fileInfo, res){
break; break;
} }
// send file // send file
res.status(200).sendFile(fileInfo.fileName, options); res.status(200).sendFile(fileInfo.download_path, options);
} }
module.exports = function(app, routeHelpers, lbryHelpers, ua, googleAnalyticsId){ module.exports = function(app, routeHelpers, lbryHelpers, ua, googleAnalyticsId){
// 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){
ua(googleAnalyticsId, {https: true}).event("Serve Route", "/name/claimId", req.params.name + "/" + req.params.claim_id).send(); var routeString = req.params.name + "/" + req.params.claim_id;
var uri = req.params.name + "#" + req.params.claim_id; // google analytics
console.log(">> GET request on /" + uri); ua(googleAnalyticsId, {https: true}).event("Serve Route", "/name/claimId", routeString).send();
// create promise // begin image-serve processes
lbryHelpers.getClaimBasedOnUri(uri) console.log(">> GET request on /" + routeString);
lbryHelpers.getClaimBasedOnUri(req.params.name, req.params.claim_id)
.then(function(fileInfo){ .then(function(fileInfo){
console.log("/:name/:claim_id success.", fileInfo.fileName); console.log("/:name/:claim_id success.", fileInfo.file_name);
serveFile(fileInfo, res); serveFile(fileInfo, res);
}).catch(function(error){ }).catch(function(error){
console.log("/:name/:claim_id error:", error) console.log("/:name/:claim_id error:", error)
@ -44,12 +44,13 @@ module.exports = function(app, routeHelpers, lbryHelpers, ua, googleAnalyticsId)
}); });
// 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){
// google analytics
ua(googleAnalyticsId, {https: true}).event("Serve Route", "/name", req.params.name).send(); ua(googleAnalyticsId, {https: true}).event("Serve Route", "/name", req.params.name).send();
// begin image-serve processes
console.log(">> GET request on /" + req.params.name); console.log(">> GET request on /" + req.params.name);
// create promise
lbryHelpers.getClaimBasedOnNameOnly(req.params.name) lbryHelpers.getClaimBasedOnNameOnly(req.params.name)
.then(function(fileInfo){ .then(function(fileInfo){
console.log("/:name success.", fileInfo.fileName); console.log("/:name success.", fileInfo.file_name);
serveFile(fileInfo, res); serveFile(fileInfo, res);
}).catch(function(error){ }).catch(function(error){
console.log("/:name error:", error); console.log("/:name error:", error);

View file

@ -64,9 +64,10 @@ var http = require("./routes/sockets-routes.js")(app, path, siofu, hostedContent
// sync sequelize // sync sequelize
// wrap the server in socket.io to intercept incoming sockets requests // wrap the server in socket.io to intercept incoming sockets requests
// start server // start server
db.sequelize.sync({}).then(function(){ db.sequelize.sync({})
.then(function(){
http.listen(PORT, function() { http.listen(PORT, function() {
console.log("Listening on PORT " + PORT); console.log("Listening on PORT " + PORT);
}); });
}) });