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-22 19:08:41 +02:00
|
|
|
function updateFileIfNeeded (uri, claimName, claimId, localOutpoint, localHeight) {
|
2017-06-30 23:37:47 +02:00
|
|
|
logger.debug(`A mysql record was found for ${claimName}:${claimId}. Initiating resolve to check outpoint.`);
|
2017-06-22 01:36:08 +02:00
|
|
|
// 1. resolve claim
|
|
|
|
lbryApi
|
|
|
|
.resolveUri(uri)
|
|
|
|
.then(result => {
|
2017-06-30 23:37:47 +02:00
|
|
|
// check to make sure the result is a claim
|
|
|
|
if (!result.claim) {
|
|
|
|
logger.debug('resolve did not return a claim');
|
|
|
|
return;
|
|
|
|
}
|
2017-06-22 01:36:08 +02:00
|
|
|
// logger.debug('resolved result:', result);
|
2017-06-30 23:37:47 +02:00
|
|
|
const resolvedOutpoint = `${result.claim.txid}:${result.claim.nout}`;
|
|
|
|
const resolvedHeight = result.claim.height;
|
2017-07-04 03:27:35 +02:00
|
|
|
const resolvedAddress = result.claim.address;
|
2017-06-22 08:33:03 +02:00
|
|
|
logger.debug('database outpoint:', localOutpoint);
|
2017-06-22 01:36:08 +02:00
|
|
|
logger.debug('resolved outpoint:', resolvedOutpoint);
|
|
|
|
// 2. if the outpoint's match, no further work needed
|
2017-06-22 08:33:03 +02:00
|
|
|
if (localOutpoint === resolvedOutpoint) {
|
|
|
|
logger.debug('local outpoint matched');
|
|
|
|
// 2. if the outpoints don't match, check the height
|
|
|
|
} else if (localHeight > resolvedHeight) {
|
2017-06-22 19:08:41 +02:00
|
|
|
logger.debug('local height was greater than resolved height');
|
2017-06-22 08:33:03 +02:00
|
|
|
// 2. get the resolved claim
|
2017-06-22 01:36:08 +02:00
|
|
|
} else {
|
|
|
|
logger.debug(`local outpoint did not match for ${uri}. Initiating update.`);
|
2017-07-04 03:27:35 +02:00
|
|
|
getClaimAndUpdate(uri, resolvedAddress, resolvedHeight);
|
2017-06-22 01:36:08 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => {
|
2017-06-22 08:33:03 +02:00
|
|
|
logger.error(`error resolving "${uri}" >> `, error);
|
2017-06-22 01:36:08 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-07-04 03:27:35 +02:00
|
|
|
function getClaimAndUpdate (uri, address, height) {
|
2017-06-22 01:36:08 +02:00
|
|
|
// 1. get the claim
|
|
|
|
lbryApi
|
|
|
|
.getClaim(uri)
|
2017-06-22 19:08:41 +02:00
|
|
|
.then(({ name, claim_id, outpoint, file_name, download_path, mime_type, metadata }) => {
|
|
|
|
logger.debug(' Get returned outpoint: ', outpoint);
|
2017-06-22 01:36:08 +02:00
|
|
|
// 2. update the entry in db
|
|
|
|
db.File
|
|
|
|
.update({
|
2017-06-22 19:08:41 +02:00
|
|
|
outpoint,
|
2017-07-04 03:27:35 +02:00
|
|
|
height, // note: height is coming from the 'resolve', not 'get'.
|
|
|
|
address, // note: address is coming from the 'resolve', not 'get'.
|
2017-06-22 01:36:08 +02:00
|
|
|
fileName: file_name,
|
|
|
|
filePath: download_path,
|
|
|
|
fileType: mime_type,
|
2017-06-22 19:08:41 +02:00
|
|
|
nsfw : metadata.stream.metadata.nsfw,
|
2017-06-22 01:36:08 +02:00
|
|
|
}, {
|
|
|
|
where: {
|
2017-06-22 19:08:41 +02:00
|
|
|
name,
|
|
|
|
claimId: claim_id,
|
2017-06-22 01:36:08 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
.then(result => {
|
|
|
|
logger.debug('successfully updated mysql record', result);
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
logger.error('sequelize error', error);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(error => {
|
2017-06-22 19:08:41 +02:00
|
|
|
logger.error(`error while getting claim for ${uri} >> `, error);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-07-04 03:27:35 +02:00
|
|
|
function getClaimAndHandleResponse (uri, address, height, resolve, reject) {
|
2017-06-22 19:08:41 +02:00
|
|
|
lbryApi
|
|
|
|
.getClaim(uri)
|
|
|
|
.then(({ name, claim_id, outpoint, file_name, download_path, mime_type, metadata }) => {
|
|
|
|
// create entry in the db
|
2017-07-01 02:28:53 +02:00
|
|
|
logger.silly(`creating "${name}" record in File db`);
|
2017-06-22 19:08:41 +02:00
|
|
|
db.File
|
|
|
|
.create({
|
|
|
|
name,
|
|
|
|
claimId : claim_id,
|
2017-07-04 03:27:35 +02:00
|
|
|
address, // note: comes from parent 'resolve,' not this 'get' call
|
2017-06-22 19:08:41 +02:00
|
|
|
outpoint,
|
2017-07-04 03:27:35 +02:00
|
|
|
height, // note: comes from parent 'resolve,' not this 'get' call
|
2017-06-22 19:08:41 +02:00
|
|
|
fileName: file_name,
|
|
|
|
filePath: download_path,
|
|
|
|
fileType: mime_type,
|
|
|
|
nsfw : metadata.stream.metadata.nsfw,
|
|
|
|
})
|
|
|
|
.then(result => {
|
2017-06-22 19:28:34 +02:00
|
|
|
logger.debug('successfully created mysql record');
|
2017-06-22 19:08:41 +02:00
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
logger.error('sequelize create error', error);
|
|
|
|
});
|
|
|
|
// resolve the request
|
|
|
|
resolve({
|
|
|
|
fileName: file_name,
|
|
|
|
filePath: download_path,
|
|
|
|
fileType: mime_type,
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
reject(error);
|
2017-06-22 01:36:08 +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) => {
|
2017-06-22 19:08:41 +02:00
|
|
|
// 1. get the top free, public claims
|
2017-06-17 22:51:30 +02:00
|
|
|
getAllFreePublicClaims(claimName)
|
|
|
|
.then(freePublicClaimList => {
|
2017-07-01 00:31:23 +02: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 19:08:41 +02:00
|
|
|
const claimId = freePublicClaimList[0].claim_id;
|
2017-06-22 08:33:03 +02:00
|
|
|
const uri = `${name}#${claimId}`;
|
|
|
|
const height = freePublicClaimList[0].height;
|
2017-07-04 03:27:35 +02:00
|
|
|
const address = freePublicClaimList[0].address;
|
2017-06-22 04:10:39 +02:00
|
|
|
// 2. check to see if the file is available locally
|
2017-06-17 22:51:30 +02:00
|
|
|
db.File
|
2017-06-22 19:28:34 +02:00
|
|
|
.findOne({ where: { name, claimId } })
|
2017-06-17 22:51:30 +02:00
|
|
|
.then(claim => {
|
2017-07-01 00:31:23 +02:00
|
|
|
// 3. if a matching record is found locally, serve it
|
2017-06-17 22:51:30 +02:00
|
|
|
if (claim) {
|
2017-06-22 19:08:41 +02:00
|
|
|
// serve the file
|
2017-06-22 04:10:39 +02:00
|
|
|
resolve(claim.dataValues);
|
2017-06-22 19:28:34 +02:00
|
|
|
// trigger update if needed
|
|
|
|
updateFileIfNeeded(uri, name, claimId, claim.dataValues.outpoint, claim.dataValues.height);
|
2017-06-22 04:10:39 +02:00
|
|
|
// 3. otherwise use daemon to retrieve it
|
2017-06-22 04:45:56 +02:00
|
|
|
} else {
|
2017-06-22 19:08:41 +02:00
|
|
|
// get the claim and serve it
|
2017-07-04 03:27:35 +02:00
|
|
|
getClaimAndHandleResponse(uri, address, height, resolve, reject);
|
2017-06-17 22:51:30 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => {
|
2017-06-22 01:36:08 +02:00
|
|
|
logger.error('sequelize error', 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-22 19:28:34 +02:00
|
|
|
getClaimByClaimId (name, claimId) {
|
2017-06-17 22:51:30 +02:00
|
|
|
const deferred = new Promise((resolve, reject) => {
|
2017-06-22 19:28:34 +02:00
|
|
|
const uri = `${name}#${claimId}`;
|
2017-06-22 01:36:08 +02:00
|
|
|
// 1. check locally for the claim
|
|
|
|
db.File
|
2017-06-22 19:28:34 +02:00
|
|
|
.findOne({ where: { name, claimId } })
|
2017-06-22 01:36:08 +02:00
|
|
|
.then(claim => {
|
2017-06-22 04:10:39 +02:00
|
|
|
// 2. if a match is found locally, serve it
|
2017-06-22 01:36:08 +02:00
|
|
|
if (claim) {
|
2017-06-22 19:08:41 +02:00
|
|
|
// serve the file
|
2017-06-22 01:36:08 +02:00
|
|
|
resolve(claim.dataValues);
|
2017-06-22 19:28:34 +02:00
|
|
|
// trigger an update if needed
|
|
|
|
updateFileIfNeeded(uri, name, claimId, claim.dataValues.outpoint, claim.dataValues.outpoint);
|
2017-06-22 01:36:08 +02:00
|
|
|
// 2. otherwise use daemon to retrieve it
|
|
|
|
} else {
|
|
|
|
// 3. resolve the Uri
|
|
|
|
lbryApi
|
|
|
|
.resolveUri(uri)
|
|
|
|
.then(result => {
|
2017-06-30 23:37:47 +02:00
|
|
|
// check to make sure the result is a claim
|
|
|
|
if (!result.claim) {
|
|
|
|
logger.debug('resolve did not return a claim');
|
2017-07-01 00:31:23 +02:00
|
|
|
resolve(null);
|
2017-06-30 23:37:47 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-06-22 01:36:08 +02:00
|
|
|
// 4. check to see if the claim is free & public
|
2017-06-30 23:37:47 +02:00
|
|
|
if (isFreePublicClaim(result.claim)) {
|
2017-06-22 01:36:08 +02:00
|
|
|
// 5. get claim and serve
|
2017-07-04 03:27:35 +02:00
|
|
|
getClaimAndHandleResponse(uri, result.claim.address, result.claim.height, resolve, reject);
|
2017-06-17 22:51:30 +02:00
|
|
|
} else {
|
2017-07-01 00:31:23 +02:00
|
|
|
reject(null);
|
2017-06-17 22:51:30 +02:00
|
|
|
}
|
2017-06-22 01:36:08 +02:00
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
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-06 22:37:03 +02:00
|
|
|
getAllClaims (claimName) {
|
|
|
|
return getAllFreePublicClaims(claimName);
|
|
|
|
},
|
2017-06-19 18:37:35 +02:00
|
|
|
};
|