added no-value escape hatch to filterForFreePublicCLaims()

This commit is contained in:
bill bittner 2017-06-14 15:02:38 -07:00
parent 86a0417947
commit 2c732fa2f4
2 changed files with 30 additions and 18 deletions

View file

@ -3,10 +3,14 @@ var axios = require('axios');
var lbryApi = require('./lbryApi'); var lbryApi = require('./lbryApi');
function filterForFreePublicClaims(claimsListArray){ function filterForFreePublicClaims(claimsListArray){
//console.log("claims list:", claimsListArray)
if (!claimsListArray) { if (!claimsListArray) {
return null; return null;
}; };
var freePublicClaims = claimsListArray.filter(function(claim){ var freePublicClaims = claimsListArray.filter(function(claim){
if (!claim.value){
return false;
}
return (((claim.value.stream.metadata.license.indexOf('Public Domain') != -1) || (claim.value.stream.metadata.license.indexOf('Creative Commons') != -1)) && return (((claim.value.stream.metadata.license.indexOf('Public Domain') != -1) || (claim.value.stream.metadata.license.indexOf('Creative Commons') != -1)) &&
(!claim.value.stream.metadata.fee || claim.value.stream.metadata.fee === 0)); (!claim.value.stream.metadata.fee || claim.value.stream.metadata.fee === 0));
}); });
@ -35,7 +39,7 @@ function orderTopClaims(claimsListArray){
return claimsListArray; return claimsListArray;
} }
function getAllFreePublicClaims(claimName){ // note: work in progress function getAllFreePublicClaims(claimName){
var deferred = new Promise(function(resolve, reject){ var deferred = new Promise(function(resolve, reject){
console.log(">> get all claims data for", claimName) 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
@ -97,11 +101,14 @@ module.exports = {
return deferred; return deferred;
}, },
getClaimBasedOnUri: function(uri){ getClaimBasedOnUri: function(uri){
/*
to do: need to pass the URI through a test 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(">> lbryHelpers >> getClaimBasedOnUri:", uri); 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.claim)){
// promise to get the chosen uri // promise to get the chosen uri
lbryApi.getClaim(uri) lbryApi.getClaim(uri)
.then(function(data){ .then(function(data){
@ -113,6 +120,13 @@ module.exports = {
}).catch(function(error){ }).catch(function(error){
reject(error) reject(error)
}); });
} else {
reject("NO_FREE_PUBLIC_CLAIMS");
}
}).catch(function(error){
reject(error)
});
}); });
return deferred; return deferred;
}, },

View file

@ -1,4 +1,5 @@
function serveFile(fileInfo, res){ function serveFile(fileInfo, res){
// set default options
var options = { var options = {
root: fileInfo.directory, root: fileInfo.directory,
headers: { headers: {
@ -6,25 +7,22 @@ function serveFile(fileInfo, res){
"Content-Type": fileInfo.contentType "Content-Type": fileInfo.contentType
} }
}; };
// adjust default options as needed
switch (fileInfo.contentType){ switch (fileInfo.contentType){
case "image/jpeg": case "image/jpeg":
console.log("sending jpeg");
break; break;
case "image/gif": case "image/gif":
console.log("sending gif");
break; break;
case "image/png": case "image/png":
console.log("sending png");
break; break;
case "video/mp4": case "video/mp4":
console.log("sending mp4");
break; break;
default: default:
console.log("sending unknown file type as .jpeg"); console.log("sending unknown file type as .jpeg");
options["headers"]["Content-Type"] = "image/jpeg"; options["headers"]["Content-Type"] = "image/jpeg";
break; break;
} }
console.log('options', options); // send file
res.status(200).sendFile(fileInfo.fileName, options); res.status(200).sendFile(fileInfo.fileName, options);
} }