added outpoint check for local files
This commit is contained in:
parent
2f3f2784b5
commit
5c40a5ccd0
2 changed files with 61 additions and 50 deletions
|
@ -91,7 +91,7 @@ module.exports = {
|
||||||
"params": { "uri": uri}
|
"params": { "uri": uri}
|
||||||
}).then(function(response){
|
}).then(function(response){
|
||||||
console.log(">> 'resolve' success");
|
console.log(">> 'resolve' success");
|
||||||
resolve(response.data);
|
resolve(response.data.result);
|
||||||
}).catch(function(error){
|
}).catch(function(error){
|
||||||
console.log(">> 'resolve' error");
|
console.log(">> 'resolve' error");
|
||||||
reject(error);
|
reject(error);
|
||||||
|
|
|
@ -73,29 +73,8 @@ function getAllFreePublicClaims(claimName){
|
||||||
return deferred;
|
return deferred;
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
function getClaimAndHandleResponse(claimUri, resolve, reject){
|
||||||
getClaimBasedOnNameOnly: function(claimName){
|
lbryApi.getClaim(claimUri)
|
||||||
var deferred = new Promise(function (resolve, reject){
|
|
||||||
console.log(">> lbryHelpers >> getClaim BasedOnNameOnly:", claimName);
|
|
||||||
// get all free public claims
|
|
||||||
getAllFreePublicClaims(claimName)
|
|
||||||
.then(function(freePublicClaimList){
|
|
||||||
var claimName = freePublicClaimList[0].name;
|
|
||||||
var claimId = freePublicClaimList[0].claim_id;
|
|
||||||
var freePublicClaimUri = claimName + "#" + claimId;
|
|
||||||
console.log(">> Decided on public claim URI:", freePublicClaimUri);
|
|
||||||
// check to see if the file is available locally
|
|
||||||
db.File.findOne({where: { claim_id: claimId }})
|
|
||||||
.then(function(claim){
|
|
||||||
console.log(">> Asset was found locally");
|
|
||||||
// if a record is found, return it
|
|
||||||
if (claim){
|
|
||||||
console.log("claim.dataValues", claim.dataValues);
|
|
||||||
resolve(claim.dataValues);
|
|
||||||
// ... otherwise use daemon to retrieve it
|
|
||||||
} else {
|
|
||||||
// promise to get the chosen uri
|
|
||||||
lbryApi.getClaim(freePublicClaimUri)
|
|
||||||
.then(function(result){
|
.then(function(result){
|
||||||
resolve({
|
resolve({
|
||||||
file_name: result.file_name,
|
file_name: result.file_name,
|
||||||
|
@ -105,6 +84,37 @@ module.exports = {
|
||||||
}).catch(function(error){
|
}).catch(function(error){
|
||||||
reject(error)
|
reject(error)
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
getClaimBasedOnNameOnly: function(claimName){
|
||||||
|
var deferred = new Promise(function (resolve, reject){
|
||||||
|
console.log(">> lbryHelpers >> getClaim BasedOnNameOnly:", claimName);
|
||||||
|
// get all free public claims
|
||||||
|
getAllFreePublicClaims(claimName)
|
||||||
|
.then(function(freePublicClaimList){
|
||||||
|
console.log(">> Decided on public claim id:", freePublicClaimList[0].claim_id);
|
||||||
|
var freePublicClaimOutpoint = freePublicClaimList[0].txid + ":" + freePublicClaimList[0].nout;
|
||||||
|
var freePublicClaimUri = freePublicClaimList[0].name + "#" + freePublicClaimList[0].claim_id;
|
||||||
|
// check to see if the file is available locally
|
||||||
|
db.File.findOne({where: { claim_id: freePublicClaimList[0].claim_id }})
|
||||||
|
.then(function(claim){
|
||||||
|
// if a found locally...
|
||||||
|
if (claim){
|
||||||
|
console.log(">> A matching claim_id was found locally");
|
||||||
|
// if the outpoint's match return it
|
||||||
|
if (claim.dataValues.outpoint === freePublicClaimOutpoint){
|
||||||
|
console.log(">> Local outpoint matched");
|
||||||
|
resolve(claim.dataValues);
|
||||||
|
// if the outpoint's don't match, fetch updated claim
|
||||||
|
} else {
|
||||||
|
console.log(">> local outpoint did not match");
|
||||||
|
getClaimAndHandleResponse(freePublicClaimUri, resolve, reject);
|
||||||
|
}
|
||||||
|
// ... otherwise use daemon to retrieve it
|
||||||
|
} else {
|
||||||
|
// 'get' the claim
|
||||||
|
getClaimAndHandleResponse(freePublicClaimUri, resolve, reject)
|
||||||
}
|
}
|
||||||
}).catch(function(error){
|
}).catch(function(error){
|
||||||
reject(error);
|
reject(error);
|
||||||
|
@ -119,45 +129,46 @@ module.exports = {
|
||||||
var deferred = new Promise(function (resolve, reject){
|
var deferred = new Promise(function (resolve, reject){
|
||||||
var uri = claimName + "#" + claimId;
|
var uri = claimName + "#" + claimId;
|
||||||
console.log(">> lbryHelpers >> getClaimBasedOnUri:", uri);
|
console.log(">> lbryHelpers >> getClaimBasedOnUri:", uri);
|
||||||
|
// resolve the Uri
|
||||||
|
lbryApi.resolveUri(uri) // note: use 'spread' and make parallel with db.File.findOne()
|
||||||
|
.then(function(result){ // note should just be 'result' returned.
|
||||||
|
// get the outpoint
|
||||||
|
var resolvedOutpoint = result[uri].claim.txid + ":" + result[uri].claim.nout;
|
||||||
// check locally for the claim
|
// check locally for the claim
|
||||||
db.File.findOne({where: { claim_id: claimId }})
|
db.File.findOne({where: { claim_id: claimId }})
|
||||||
.then(function(claim){
|
.then(function(claim){
|
||||||
console.log(">> Asset was found locally");
|
// if a found locally...
|
||||||
// if a record is found, return it
|
|
||||||
if (claim){
|
if (claim){
|
||||||
|
console.log(">> A matching claim_id was found locally");
|
||||||
|
// if the outpoint's match return it
|
||||||
|
if (claim.dataValues.outpoint === resolvedOutpoint){
|
||||||
|
console.log(">> Local outpoint matched");
|
||||||
resolve(claim.dataValues);
|
resolve(claim.dataValues);
|
||||||
|
// if the outpoint's don't match, fetch updated claim
|
||||||
|
} else {
|
||||||
|
console.log(">> Local outpoint did not match");
|
||||||
|
getClaimAndHandleResponse(uri, resolve, reject);
|
||||||
|
}
|
||||||
// ... otherwise use daemon to retrieve it
|
// ... otherwise use daemon to retrieve it
|
||||||
} else {
|
} else {
|
||||||
// get the claim info via 'resolve'
|
// check to make sure it is free and public (note: no need for another resolve?)
|
||||||
lbryApi.resolveUri(uri)
|
if (isFreePublicClaim(result[uri].claim)){
|
||||||
.then(function(resolvedUri){
|
|
||||||
// check to make sure it is free and public
|
|
||||||
if (isFreePublicClaim(resolvedUri.result[uri].claim)){
|
|
||||||
// 'get' the claim
|
// 'get' the claim
|
||||||
lbryApi.getClaim(uri)
|
getClaimAndHandleResponse(uri, resolve, reject);
|
||||||
.then(function(result){
|
|
||||||
resolve({
|
|
||||||
file_name: result.file_name,
|
|
||||||
file_path: result.download_path,
|
|
||||||
file_type: result.mime_type
|
|
||||||
});
|
|
||||||
}).catch(function(error){
|
|
||||||
reject(error)
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
reject("NO_FREE_PUBLIC_CLAIMS");
|
reject("NO_FREE_PUBLIC_CLAIMS");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}).catch(function(error){
|
}).catch(function(error){
|
||||||
reject(error)
|
reject(error)
|
||||||
});
|
});
|
||||||
}
|
|
||||||
}).catch(function(error){
|
}).catch(function(error){
|
||||||
reject(error);
|
reject(error);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
return deferred;
|
return deferred;
|
||||||
},
|
},
|
||||||
getAllClaims: function(claimName){ // note: work in progress
|
getAllClaims: function(claimName){
|
||||||
return getAllFreePublicClaims(claimName);
|
return getAllFreePublicClaims(claimName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue