2017-08-03 02:13:02 +02:00
|
|
|
const lbryApi = require('../helpers/lbryApi.js');
|
2017-06-19 18:37:35 +02:00
|
|
|
const db = require('../models');
|
2017-06-20 01:15:28 +02:00
|
|
|
const logger = require('winston');
|
2017-08-09 02:32:03 +02:00
|
|
|
const isFreeClaim = require('../helpers/functions/isFreeClaim.js');
|
2017-08-16 00:35:03 +02:00
|
|
|
const { getTopFreeClaim, getFullClaimIdFromShortId } = require('../helpers/serveHelpers.js');
|
2017-07-17 22:16:11 +02:00
|
|
|
|
2017-08-02 03:58:13 +02:00
|
|
|
function checkForLocalAssetByClaimId (claimId, name) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
db.File
|
|
|
|
.findOne({where: { name, claimId }})
|
|
|
|
.then(result => {
|
|
|
|
if (result) {
|
|
|
|
resolve(result.dataValues);
|
|
|
|
} else {
|
|
|
|
resolve(null);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function formatGetResultsToFileInfo ({ name, claim_id, outpoint, file_name, download_path, mime_type, metadata }) {
|
|
|
|
return {
|
|
|
|
name,
|
|
|
|
claimId : claim_id,
|
|
|
|
outpoint,
|
|
|
|
fileName: file_name,
|
|
|
|
filePath: download_path,
|
|
|
|
fileType: mime_type,
|
|
|
|
nsfw : metadata.stream.metadata.nsfw,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-08-02 22:16:39 +02:00
|
|
|
function getAssetByClaimId (fullClaimId, name) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
// 1. check locally for claim
|
|
|
|
checkForLocalAssetByClaimId(fullClaimId, name)
|
|
|
|
.then(dataValues => {
|
2017-08-10 18:47:59 +02:00
|
|
|
// 2. if a result was found, return the result
|
2017-08-02 22:16:39 +02:00
|
|
|
if (dataValues) {
|
|
|
|
logger.debug('found a local file for this claimId');
|
|
|
|
resolve(dataValues);
|
|
|
|
// 2. if not found locally, make a get request
|
|
|
|
} else {
|
|
|
|
logger.debug('no local file for this claimId');
|
|
|
|
// 3. resolve the claim
|
|
|
|
lbryApi.resolveUri(`${name}#${fullClaimId}`)
|
|
|
|
.then(resolveResult => {
|
2017-08-10 18:47:59 +02:00
|
|
|
// if the claim is free...
|
2017-08-09 02:32:03 +02:00
|
|
|
if (resolveResult.claim && isFreeClaim(resolveResult.claim)) {
|
2017-08-10 18:47:59 +02:00
|
|
|
// get the claim
|
2017-08-02 22:16:39 +02:00
|
|
|
lbryApi.getClaim(`${name}#${fullClaimId}`)
|
|
|
|
.then(getResult => {
|
2017-08-10 18:47:59 +02:00
|
|
|
// logger.debug('getResult >>', getResult);
|
2017-08-02 22:16:39 +02:00
|
|
|
let fileInfo = formatGetResultsToFileInfo(getResult);
|
|
|
|
fileInfo['address'] = resolveResult.claim.address;
|
|
|
|
fileInfo['height'] = resolveResult.claim.height;
|
2017-08-04 20:32:21 +02:00
|
|
|
// insert a record in the File table
|
|
|
|
db.File.create(fileInfo);
|
|
|
|
// resolve the promise
|
2017-08-02 22:16:39 +02:00
|
|
|
resolve(fileInfo);
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
// if not, resolve with no claims
|
2017-08-02 03:58:13 +02:00
|
|
|
} else {
|
2017-08-02 22:16:39 +02:00
|
|
|
resolve(null);
|
2017-08-02 03:58:13 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
reject(error);
|
|
|
|
});
|
2017-08-02 22:16:39 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
reject(error);
|
2017-08-02 03:58:13 +02:00
|
|
|
});
|
2017-08-02 22:16:39 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
getAssetByChannel (channelName, name) {
|
2017-08-02 03:58:13 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
2017-08-03 02:13:02 +02:00
|
|
|
// temporarily throw error
|
|
|
|
reject(new Error('channel names are not currently supported'));
|
2017-08-02 22:16:39 +02:00
|
|
|
// get the claim id
|
|
|
|
// get teh asset by claim Id
|
2017-08-02 03:58:13 +02:00
|
|
|
});
|
2017-08-01 02:02:39 +02:00
|
|
|
},
|
2017-08-10 19:49:19 +02:00
|
|
|
getAssetByShortId: function (shortId, name) {
|
2017-08-02 03:58:13 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
2017-08-02 22:16:39 +02:00
|
|
|
// get the full claimId
|
2017-08-16 00:35:03 +02:00
|
|
|
getFullClaimIdFromShortId(shortId, name)
|
2017-08-02 22:16:39 +02:00
|
|
|
// get the asset by the claimId
|
|
|
|
.then(claimId => {
|
|
|
|
resolve(getAssetByClaimId(claimId, name));
|
2017-08-02 03:58:13 +02:00
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
});
|
2017-08-01 02:02:39 +02:00
|
|
|
},
|
2017-08-02 22:16:39 +02:00
|
|
|
getAssetByClaimId (fullClaimId, name) {
|
|
|
|
return getAssetByClaimId(fullClaimId, name);
|
|
|
|
},
|
2017-08-03 20:14:50 +02:00
|
|
|
getAssetByName (name) {
|
|
|
|
return new Promise((resolve, reject) => {
|
2017-08-03 20:46:56 +02:00
|
|
|
// 1. get a list of the free public claims
|
2017-08-16 00:35:03 +02:00
|
|
|
getTopFreeClaim(name)
|
2017-08-03 20:46:56 +02:00
|
|
|
// 2. check locally for the top claim
|
|
|
|
.then(freePublicClaimList => {
|
|
|
|
// if no claims were found, return null
|
|
|
|
if (!freePublicClaimList) {
|
2017-08-16 00:35:03 +02:00
|
|
|
return resolve(null);
|
2017-08-03 20:46:56 +02:00
|
|
|
}
|
|
|
|
// parse the result
|
2017-08-16 00:35:03 +02:00
|
|
|
const claimId = freePublicClaimList[0].claimId;
|
2017-08-03 20:46:56 +02:00
|
|
|
// get the asset
|
|
|
|
resolve(getAssetByClaimId(claimId, name));
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
reject(error);
|
|
|
|
});
|
2017-06-19 18:37:35 +02:00
|
|
|
});
|
2017-06-17 22:51:30 +02:00
|
|
|
},
|
2017-06-19 18:37:35 +02:00
|
|
|
};
|