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)
|
2017-06-22 04:10:39 +02:00
|
|
|
.then(({ name, outpoint, claim_id, file_name, download_path, mime_type, metadata }) => {
|
|
|
|
// create entry in the db
|
|
|
|
logger.debug('creating new record in db');
|
|
|
|
db.File
|
|
|
|
.create({
|
|
|
|
name,
|
|
|
|
claimId : claim_id,
|
|
|
|
outpoint,
|
|
|
|
fileName: file_name,
|
|
|
|
filePath: download_path,
|
|
|
|
fileType: mime_type,
|
|
|
|
nsfw : metadata.stream.metadata.nsfw,
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
logger.error('sequelize create error', error);
|
|
|
|
});
|
|
|
|
// resolve the request
|
2017-06-17 22:51:30 +02:00
|
|
|
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-22 01:36:08 +02:00
|
|
|
function resolveAndUpdateIfNeeded (uri, claimName, claimId, outpoint) {
|
|
|
|
logger.debug('initiating resolve on local record to check outpoint');
|
|
|
|
// 1. resolve claim
|
|
|
|
lbryApi
|
|
|
|
.resolveUri(uri)
|
|
|
|
.then(result => {
|
|
|
|
// logger.debug('resolved result:', result);
|
|
|
|
const resolvedOutpoint = `${result[uri].claim.txid}:${result[uri].claim.nout}`;
|
|
|
|
logger.debug('database outpoint:', outpoint);
|
|
|
|
logger.debug('resolved outpoint:', resolvedOutpoint);
|
|
|
|
// 2. if the outpoint's match, no further work needed
|
|
|
|
if (outpoint === resolvedOutpoint) {
|
|
|
|
logger.debug(`local outpoint matched`);
|
|
|
|
// 2. if the outpoints don't match, get claim and update records
|
|
|
|
} else {
|
|
|
|
logger.debug(`local outpoint did not match for ${uri}. Initiating update.`);
|
|
|
|
getClaimAndUpdate(uri, claimName, claimId);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
logger.error(`error resolving ${uri}`, error);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getClaimAndUpdate (uri, claimName, claimId) {
|
|
|
|
// 1. get the claim
|
|
|
|
lbryApi
|
|
|
|
.getClaim(uri)
|
|
|
|
.then(({ outpoint, file_name, download_path, mime_type }) => {
|
|
|
|
logger.debug('getClaim outpoint', outpoint);
|
|
|
|
// 2. update the entry in db
|
|
|
|
db.File
|
|
|
|
.update({
|
|
|
|
outpoint: outpoint,
|
|
|
|
fileName: file_name,
|
|
|
|
filePath: download_path,
|
|
|
|
fileType: mime_type,
|
|
|
|
}, {
|
|
|
|
where: {
|
|
|
|
name : claimName,
|
|
|
|
claimId: claimId,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.then(result => {
|
|
|
|
logger.debug('successfully updated mysql record', result);
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
logger.error('sequelize error', error);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
logger.error(`error while getting claim for ${uri}`, error);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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 04:10:39 +02:00
|
|
|
// 1. get all free public claims
|
2017-06-17 22:51:30 +02:00
|
|
|
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-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 04:10:39 +02:00
|
|
|
.findOne({ where: { name: name, claimId: claimId } }) // note: consolidate for es6?
|
2017-06-17 22:51:30 +02:00
|
|
|
.then(claim => {
|
2017-06-22 04:10:39 +02:00
|
|
|
// 3. if a matching claim_id is found locally, serve it
|
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-22 04:10:39 +02:00
|
|
|
// trigger update if needed
|
2017-06-17 22:51:30 +02:00
|
|
|
if (claim.dataValues.outpoint === freePublicClaimOutpoint) {
|
2017-06-20 05:22:23 +02:00
|
|
|
logger.debug(`local outpoint matched for ${claimId}`);
|
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-22 04:10:39 +02:00
|
|
|
getClaimAndUpdate(freePublicClaimUri, name, claimId);
|
2017-06-17 22:51:30 +02:00
|
|
|
}
|
2017-06-22 04:10:39 +02:00
|
|
|
// return the claim
|
|
|
|
resolve(claim.dataValues);
|
|
|
|
// 3. otherwise use daemon to retrieve it
|
|
|
|
} else {
|
|
|
|
// 4. get the claim and serve it
|
2017-06-19 18:37:35 +02:00
|
|
|
getClaimAndHandleResponse(freePublicClaimUri, 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
|
|
|
},
|
|
|
|
getClaimByClaimId (claimName, claimId) {
|
|
|
|
const deferred = new Promise((resolve, reject) => {
|
2017-06-19 18:37:35 +02:00
|
|
|
const uri = `${claimName}#${claimId}`;
|
2017-06-22 01:36:08 +02:00
|
|
|
// 1. check locally for the claim
|
|
|
|
db.File
|
2017-06-22 04:10:39 +02:00
|
|
|
.findOne({ where: { name: claimName, claimId: claimId } }) // note: consolidate for es6?
|
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) {
|
|
|
|
logger.debug(`A mysql record was found for ${claimId}`);
|
|
|
|
// trigger an update if needed
|
|
|
|
resolveAndUpdateIfNeeded(uri, claimName, claimId, claim.dataValues.outpoint); // ok to just start asynch function, or need to add to a task cue?
|
|
|
|
// resolve and send
|
|
|
|
resolve(claim.dataValues);
|
|
|
|
// 2. otherwise use daemon to retrieve it
|
|
|
|
} else {
|
|
|
|
// 3. resolve the Uri
|
|
|
|
lbryApi
|
|
|
|
.resolveUri(uri)
|
|
|
|
.then(result => {
|
|
|
|
// 4. check to see if the claim is free & public
|
2017-06-17 22:51:30 +02:00
|
|
|
if (isFreePublicClaim(result[uri].claim)) {
|
2017-06-22 01:36:08 +02:00
|
|
|
// 5. get claim and serve
|
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
|
|
|
}
|
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-06-19 18:37:35 +02:00
|
|
|
};
|