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",
"params": { "uri": uri, "timeout": 20}
}).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)
if (getResponse.data.result.error){
reject(getResponse.data.result.error);

View file

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

View file

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

View file

@ -1,14 +1,13 @@
function serveFile(fileInfo, res){
// set default options
var options = {
root: fileInfo.directory,
headers: {
"X-Content-Type-Options": "nosniff",
"Content-Type": fileInfo.contentType
"Content-Type": fileInfo.content_type
}
};
// adjust default options as needed
switch (fileInfo.contentType){
switch (fileInfo.content_type){
case "image/jpeg":
break;
case "image/gif":
@ -23,19 +22,20 @@ function serveFile(fileInfo, res){
break;
}
// 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){
// route to fetch one free public claim
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 uri = req.params.name + "#" + req.params.claim_id;
console.log(">> GET request on /" + uri);
// create promise
lbryHelpers.getClaimBasedOnUri(uri)
var routeString = req.params.name + "/" + req.params.claim_id;
// google analytics
ua(googleAnalyticsId, {https: true}).event("Serve Route", "/name/claimId", routeString).send();
// begin image-serve processes
console.log(">> GET request on /" + routeString);
lbryHelpers.getClaimBasedOnUri(req.params.name, req.params.claim_id)
.then(function(fileInfo){
console.log("/:name/:claim_id success.", fileInfo.fileName);
console.log("/:name/:claim_id success.", fileInfo.file_name);
serveFile(fileInfo, res);
}).catch(function(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
app.get("/:name", function(req, res){
// google analytics
ua(googleAnalyticsId, {https: true}).event("Serve Route", "/name", req.params.name).send();
// begin image-serve processes
console.log(">> GET request on /" + req.params.name);
// create promise
lbryHelpers.getClaimBasedOnNameOnly(req.params.name)
.then(function(fileInfo){
console.log("/:name success.", fileInfo.fileName);
console.log("/:name success.", fileInfo.file_name);
serveFile(fileInfo, res);
}).catch(function(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
// wrap the server in socket.io to intercept incoming sockets requests
// start server
db.sequelize.sync({}).then(function(){
db.sequelize.sync({})
.then(function(){
http.listen(PORT, function() {
console.log("Listening on PORT " + PORT);
});
})
});