2017-06-19 18:37:35 +02:00
|
|
|
const lbryApi = require('../helpers/libraries/lbryApi.js');
|
|
|
|
const db = require('../models');
|
2017-06-20 01:15:28 +02:00
|
|
|
const logger = require('winston');
|
2017-06-19 18:37:35 +02:00
|
|
|
const getAllFreePublicClaims = require('../helpers/functions/getAllFreePublicClaims.js');
|
|
|
|
const isFreePublicClaim = require('../helpers/functions/isFreePublicClaim.js');
|
2017-06-14 00:39:38 +02:00
|
|
|
|
2017-06-17 22:51:30 +02:00
|
|
|
function getClaimAndHandleResponse (claimUri, resolve, reject) {
|
|
|
|
lbryApi
|
|
|
|
.getClaim(claimUri)
|
|
|
|
.then(({ file_name, download_path, mime_type }) => {
|
|
|
|
resolve({
|
2017-06-20 00:25:14 +02:00
|
|
|
fileName: file_name,
|
|
|
|
filePath: download_path,
|
|
|
|
fileType: mime_type,
|
2017-06-19 18:37:35 +02:00
|
|
|
});
|
2017-06-17 22:51:30 +02:00
|
|
|
})
|
|
|
|
.catch(error => {
|
2017-06-19 18:37:35 +02:00
|
|
|
reject(error);
|
|
|
|
});
|
2017-06-16 21:29:02 +02:00
|
|
|
}
|
|
|
|
|
2017-06-14 00:39:38 +02:00
|
|
|
module.exports = {
|
2017-06-17 22:51:30 +02:00
|
|
|
getClaimByName (claimName) {
|
|
|
|
const deferred = new Promise((resolve, reject) => {
|
|
|
|
// get all free public claims
|
|
|
|
getAllFreePublicClaims(claimName)
|
|
|
|
.then(freePublicClaimList => {
|
2017-06-19 18:37:35 +02:00
|
|
|
const claimId = freePublicClaimList[0].claim_id;
|
|
|
|
const name = freePublicClaimList[0].name;
|
|
|
|
const freePublicClaimOutpoint = `${freePublicClaimList[0].txid}:${freePublicClaimList[0].nout}`;
|
2017-06-20 01:15:28 +02:00
|
|
|
const freePublicClaimUri = `${name}#${claimId}`;
|
2017-06-17 22:51:30 +02:00
|
|
|
// check to see if the file is available locally
|
|
|
|
db.File
|
2017-06-20 00:25:14 +02:00
|
|
|
.findOne({ where: { name: name, claimId: claimId } })
|
2017-06-17 22:51:30 +02:00
|
|
|
.then(claim => {
|
2017-06-17 23:00:24 +02:00
|
|
|
// if a matching claim is found locally...
|
2017-06-17 22:51:30 +02:00
|
|
|
if (claim) {
|
2017-06-20 05:22:23 +02:00
|
|
|
logger.debug(`A mysql record was found for ${claimId}`);
|
2017-06-17 22:51:30 +02:00
|
|
|
// if the outpoint's match return it
|
|
|
|
if (claim.dataValues.outpoint === freePublicClaimOutpoint) {
|
2017-06-20 05:22:23 +02:00
|
|
|
logger.debug(`local outpoint matched for ${claimId}`);
|
2017-06-19 18:37:35 +02:00
|
|
|
resolve(claim.dataValues);
|
2017-06-20 01:15:28 +02:00
|
|
|
// if the outpoint's don't match, fetch updated claim
|
2017-06-17 22:51:30 +02:00
|
|
|
} else {
|
2017-06-20 05:22:23 +02:00
|
|
|
logger.debug(`local outpoint did not match for ${claimId}`);
|
2017-06-19 18:37:35 +02:00
|
|
|
getClaimAndHandleResponse(freePublicClaimUri, resolve, reject);
|
2017-06-17 22:51:30 +02:00
|
|
|
}
|
2017-06-20 01:15:28 +02:00
|
|
|
// ... otherwise use daemon to retrieve it
|
2017-06-17 22:51:30 +02:00
|
|
|
} else {
|
2017-06-19 18:37:35 +02:00
|
|
|
getClaimAndHandleResponse(freePublicClaimUri, resolve, reject);
|
2017-06-17 22:51:30 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => {
|
2017-06-19 18:37:35 +02:00
|
|
|
reject(error);
|
|
|
|
});
|
2017-06-17 22:51:30 +02:00
|
|
|
})
|
|
|
|
.catch(error => {
|
2017-06-19 18:37:35 +02:00
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return deferred;
|
2017-06-17 22:51:30 +02:00
|
|
|
},
|
|
|
|
getClaimByClaimId (claimName, claimId) {
|
|
|
|
const deferred = new Promise((resolve, reject) => {
|
2017-06-19 18:37:35 +02:00
|
|
|
const uri = `${claimName}#${claimId}`;
|
2017-06-17 22:51:30 +02:00
|
|
|
// resolve the Uri
|
|
|
|
lbryApi
|
|
|
|
.resolveUri(uri) // note: use 'spread' and make parallel with db.File.findOne()
|
|
|
|
.then(result => {
|
2017-06-19 18:37:35 +02:00
|
|
|
const resolvedOutpoint = `${result[uri].claim.txid}:${result[uri].claim.nout}`;
|
2017-06-17 22:51:30 +02:00
|
|
|
// check locally for the claim
|
|
|
|
db.File
|
2017-06-20 00:25:14 +02:00
|
|
|
.findOne({ where: { name: claimName, claimId: claimId } })
|
2017-06-17 22:51:30 +02:00
|
|
|
.then(claim => {
|
|
|
|
// if a found locally...
|
|
|
|
if (claim) {
|
2017-06-20 05:22:23 +02:00
|
|
|
logger.debug(`A mysql record was found for ${claimId}`);
|
2017-06-17 22:51:30 +02:00
|
|
|
// if the outpoint's match return it
|
|
|
|
if (claim.dataValues.outpoint === resolvedOutpoint) {
|
2017-06-20 05:22:23 +02:00
|
|
|
logger.debug(`local outpoint matched for ${claimId}`);
|
2017-06-19 18:37:35 +02:00
|
|
|
resolve(claim.dataValues);
|
2017-06-17 22:51:30 +02:00
|
|
|
// if the outpoint's don't match, fetch updated claim
|
|
|
|
} else {
|
2017-06-20 05:22:23 +02:00
|
|
|
logger.debug(`local outpoint did not match for ${claimId}`);
|
2017-06-19 18:37:35 +02:00
|
|
|
getClaimAndHandleResponse(uri, resolve, reject);
|
2017-06-17 22:51:30 +02:00
|
|
|
}
|
|
|
|
// ... otherwise use daemon to retrieve it
|
|
|
|
} else {
|
|
|
|
if (isFreePublicClaim(result[uri].claim)) {
|
2017-06-19 18:37:35 +02:00
|
|
|
getClaimAndHandleResponse(uri, resolve, reject);
|
2017-06-17 22:51:30 +02:00
|
|
|
} else {
|
2017-06-19 18:37:35 +02:00
|
|
|
reject('NO_FREE_PUBLIC_CLAIMS');
|
2017-06-17 22:51:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => {
|
2017-06-19 18:37:35 +02:00
|
|
|
reject(error);
|
|
|
|
});
|
2017-06-17 22:51:30 +02:00
|
|
|
})
|
|
|
|
.catch(error => {
|
2017-06-19 18:37:35 +02:00
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return deferred;
|
2017-06-17 22:51:30 +02:00
|
|
|
},
|
2017-06-19 18:37:35 +02:00
|
|
|
};
|