2017-06-19 18:37:35 +02:00
|
|
|
const lbryApi = require('../helpers/libraries/lbryApi.js');
|
|
|
|
const db = require('../models');
|
2017-06-19 16:15:28 -07: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-07-19 14:11:47 -07:00
|
|
|
const serveHelpers = require('../helpers/libraries/serveHelpers.js');
|
2017-07-17 13:16:11 -07:00
|
|
|
|
2017-06-13 15:39:38 -07:00
|
|
|
module.exports = {
|
2017-07-19 14:11:47 -07:00
|
|
|
serveClaimByName (claimName) {
|
2017-06-17 22:51:30 +02:00
|
|
|
const deferred = new Promise((resolve, reject) => {
|
2017-06-22 10:08:41 -07:00
|
|
|
// 1. get the top free, public claims
|
2017-06-17 22:51:30 +02:00
|
|
|
getAllFreePublicClaims(claimName)
|
|
|
|
.then(freePublicClaimList => {
|
2017-06-30 15:31:23 -07:00
|
|
|
// check to make sure some claims were found
|
|
|
|
if (!freePublicClaimList) {
|
|
|
|
resolve(null);
|
|
|
|
return;
|
|
|
|
}
|
2017-06-19 18:37:35 +02:00
|
|
|
const name = freePublicClaimList[0].name;
|
2017-06-22 10:08:41 -07:00
|
|
|
const claimId = freePublicClaimList[0].claim_id;
|
2017-06-21 23:33:03 -07:00
|
|
|
const uri = `${name}#${claimId}`;
|
|
|
|
const height = freePublicClaimList[0].height;
|
2017-07-03 18:27:35 -07:00
|
|
|
const address = freePublicClaimList[0].address;
|
2017-06-21 19:10:39 -07:00
|
|
|
// 2. check to see if the file is available locally
|
2017-06-17 22:51:30 +02:00
|
|
|
db.File
|
2017-06-22 10:28:34 -07:00
|
|
|
.findOne({ where: { name, claimId } })
|
2017-06-17 22:51:30 +02:00
|
|
|
.then(claim => {
|
2017-06-30 15:31:23 -07:00
|
|
|
// 3. if a matching record is found locally, serve it
|
2017-06-17 22:51:30 +02:00
|
|
|
if (claim) {
|
2017-06-22 10:08:41 -07:00
|
|
|
// serve the file
|
2017-06-21 19:10:39 -07:00
|
|
|
resolve(claim.dataValues);
|
2017-06-22 10:28:34 -07:00
|
|
|
// trigger update if needed
|
2017-07-19 14:11:47 -07:00
|
|
|
serveHelpers.updateFileIfNeeded(uri, claim.dataValues.outpoint, claim.dataValues.height);
|
2017-06-21 19:10:39 -07:00
|
|
|
// 3. otherwise use daemon to retrieve it
|
2017-06-21 19:45:56 -07:00
|
|
|
} else {
|
2017-06-22 10:08:41 -07:00
|
|
|
// get the claim and serve it
|
2017-07-19 14:11:47 -07:00
|
|
|
serveHelpers.getClaimAndHandleResponse(uri, address, height, 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
|
|
|
},
|
2017-07-19 14:11:47 -07:00
|
|
|
serveClaimByClaimId (name, claimId) {
|
|
|
|
logger.debug(`serving claim "${name}" with claimid "${claimId}"`);
|
2017-07-19 09:11:08 -07:00
|
|
|
const deferred = new Promise((resolve, reject) => {
|
|
|
|
// 1. check locally for the claim
|
|
|
|
const uri = `${name}#${claimId}`;
|
|
|
|
db.File
|
|
|
|
.findOne({ where: { name, claimId } })
|
|
|
|
.then(result => {
|
|
|
|
// 3. if a match is found locally, serve that claim
|
|
|
|
if (result) {
|
|
|
|
logger.debug('local result found');
|
|
|
|
// return the data for the file to be served
|
2017-07-19 14:11:47 -07:00
|
|
|
resolve(result.dataValues);
|
|
|
|
serveHelpers.updateFileIfNeeded(uri, result.dataValues.outpoint, result.dataValues.outpoint);
|
2017-07-19 09:11:08 -07:00
|
|
|
// 3. if a match was not found locally, use the daemon to retrieve the claim & return the db data once it is created
|
|
|
|
} else {
|
|
|
|
logger.debug('no local result found');
|
|
|
|
lbryApi
|
|
|
|
.resolveUri(uri)
|
|
|
|
.then(result => {
|
|
|
|
if (result.claim && isFreePublicClaim(result.claim)) { // check to see if the claim is free & public
|
|
|
|
// get claim and serve
|
2017-07-19 14:11:47 -07:00
|
|
|
serveHelpers
|
|
|
|
.getClaimAndReturnResponse(uri, result.claim.address, result.claim.height)
|
2017-07-19 09:11:08 -07:00
|
|
|
.then(result => {
|
2017-07-19 14:11:47 -07:00
|
|
|
resolve(result.dataValues);
|
2017-07-19 09:11:08 -07:00
|
|
|
})
|
|
|
|
.catch(error => reject(error));
|
|
|
|
} else {
|
|
|
|
logger.debug('Resolve did not return a free, public claim');
|
2017-07-19 14:11:47 -07:00
|
|
|
resolve(null);
|
2017-07-19 09:11:08 -07:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
logger.debug('resolve returned an error');
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => reject(error));
|
|
|
|
});
|
|
|
|
return deferred;
|
|
|
|
},
|
2017-07-19 14:11:47 -07:00
|
|
|
serveClaimByShortUrl (name, shortUrl) {
|
2017-06-17 22:51:30 +02:00
|
|
|
const deferred = new Promise((resolve, reject) => {
|
2017-07-17 13:16:11 -07:00
|
|
|
let uri;
|
2017-07-19 09:11:08 -07:00
|
|
|
let claimId;
|
2017-07-18 09:52:18 -07:00
|
|
|
// 1. validate the claim id & retrieve the full claim id if needed
|
2017-07-19 14:11:47 -07:00
|
|
|
serveHelpers
|
|
|
|
.getClaimIdByShortUrl(name, shortUrl)
|
2017-07-19 08:12:59 -07:00
|
|
|
.then(result => {
|
2017-07-18 09:52:18 -07:00
|
|
|
// 2. check locally for the claim
|
2017-07-19 09:11:08 -07:00
|
|
|
uri = `${name}#${result}`;
|
|
|
|
claimId = result;
|
|
|
|
return db.File.findOne({ where: { name, claimId } });
|
2017-07-17 13:16:11 -07:00
|
|
|
})
|
2017-07-17 13:34:21 -07:00
|
|
|
.then(result => {
|
|
|
|
// 3. if a match is found locally, serve that claim
|
|
|
|
if (result) {
|
2017-07-17 13:16:11 -07:00
|
|
|
// return the data for the file to be served
|
2017-07-17 13:34:21 -07:00
|
|
|
resolve(result.dataValues);
|
2017-07-17 13:16:11 -07:00
|
|
|
// update the file, as needed
|
2017-07-19 14:11:47 -07:00
|
|
|
serveHelpers.updateFileIfNeeded(uri, result.dataValues.outpoint, result.dataValues.outpoint);
|
2017-07-18 09:52:18 -07:00
|
|
|
// 3. if a match was not found locally, use the daemon to retrieve the claim & return the db data once it is created
|
2017-07-17 13:34:21 -07:00
|
|
|
} else {
|
2017-06-21 16:36:08 -07:00
|
|
|
lbryApi
|
|
|
|
.resolveUri(uri)
|
|
|
|
.then(result => {
|
2017-07-18 22:05:34 -07:00
|
|
|
if (result.claim && isFreePublicClaim(result.claim)) { // check to see if the claim is free & public
|
2017-07-17 13:16:11 -07:00
|
|
|
// get claim and serve
|
2017-07-19 14:11:47 -07:00
|
|
|
serveHelpers
|
|
|
|
.getClaimAndReturnResponse(uri, result.claim.address, result.claim.height)
|
2017-07-18 22:05:34 -07:00
|
|
|
.then(result => {
|
|
|
|
resolve(result.dataValues);
|
|
|
|
})
|
|
|
|
.catch(error => reject(error));
|
2017-06-17 22:51:30 +02:00
|
|
|
} else {
|
2017-07-18 22:05:34 -07:00
|
|
|
logger.debug('Resolve did not return a free, public claim');
|
2017-07-19 14:11:47 -07:00
|
|
|
resolve(null);
|
2017-06-17 22:51:30 +02:00
|
|
|
}
|
2017-06-21 16:36:08 -07:00
|
|
|
})
|
2017-07-18 22:05:34 -07:00
|
|
|
.catch(error => reject(error));
|
2017-06-21 16:36:08 -07:00
|
|
|
}
|
2017-06-17 22:51:30 +02:00
|
|
|
})
|
2017-07-18 22:05:34 -07:00
|
|
|
.catch(error => reject(error));
|
2017-06-19 18:37:35 +02:00
|
|
|
});
|
|
|
|
return deferred;
|
2017-06-17 22:51:30 +02:00
|
|
|
},
|
2017-06-19 18:37:35 +02:00
|
|
|
};
|