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-16 21:23:02 +02:00
|
|
|
const { getTopFreeClaim, getFullClaimIdFromShortId, resolveAgainstClaimTable } = 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) {
|
2017-08-16 09:40:49 +02:00
|
|
|
logger.debug('...getting asset by claim Id...');
|
2017-08-02 22:16:39 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
// 1. check locally for claim
|
|
|
|
checkForLocalAssetByClaimId(fullClaimId, name)
|
|
|
|
.then(dataValues => {
|
2017-08-16 21:23:02 +02:00
|
|
|
// if a result was found, return early with the result
|
2017-08-02 22:16:39 +02:00
|
|
|
if (dataValues) {
|
2017-08-16 09:40:49 +02:00
|
|
|
logger.debug('found a local file for this name and claimId');
|
2017-08-16 21:23:02 +02:00
|
|
|
return resolve(dataValues);
|
|
|
|
}
|
|
|
|
logger.debug('no local file found for this name and claimId');
|
|
|
|
// 2. if no local claim, resolve and get the claim
|
|
|
|
resolveAgainstClaimTable(name, fullClaimId)
|
|
|
|
.then(resolveResult => {
|
|
|
|
logger.debug('resolve result >> ', resolveResult);
|
|
|
|
// if no result, return early (claim doesn't exist or isn't free)
|
|
|
|
if (!resolveResult) {
|
|
|
|
return resolve(null);
|
|
|
|
}
|
|
|
|
let fileRecord = {};
|
|
|
|
// get the claim
|
|
|
|
lbryApi.getClaim(`${name}#${fullClaimId}`)
|
|
|
|
.then(getResult => {
|
|
|
|
logger.debug('getResult >>', getResult);
|
|
|
|
fileRecord = formatGetResultsToFileInfo(getResult);
|
|
|
|
fileRecord['address'] = (resolveResult.address || 0);
|
|
|
|
fileRecord['height'] = resolveResult.height;
|
|
|
|
// insert a record in the File table & Update Claim table
|
|
|
|
return db.File.create(fileRecord);
|
|
|
|
})
|
|
|
|
.then(fileRecordResults => {
|
|
|
|
logger.debug('File record successfully updated');
|
|
|
|
resolve(fileRecord);
|
2017-08-02 03:58:13 +02:00
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
reject(error);
|
|
|
|
});
|
2017-08-16 21:23:02 +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-16 09:40:49 +02:00
|
|
|
logger.debug('...getting asset by channel...');
|
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
|
2017-08-16 09:40:49 +02:00
|
|
|
// get the 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-16 09:40:49 +02:00
|
|
|
logger.debug('...getting asset by short id...');
|
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 => {
|
2017-08-16 09:40:49 +02:00
|
|
|
logger.debug('claim id =', claimId);
|
2017-08-02 22:16:39 +02:00
|
|
|
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) {
|
2017-08-16 09:40:49 +02:00
|
|
|
logger.debug('...getting asset by claim name...');
|
2017-08-03 20:14:50 +02:00
|
|
|
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
|
2017-08-16 09:40:49 +02:00
|
|
|
.then(topFreeClaim => {
|
2017-08-03 20:46:56 +02:00
|
|
|
// if no claims were found, return null
|
2017-08-16 09:40:49 +02:00
|
|
|
if (!topFreeClaim) {
|
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 09:40:49 +02:00
|
|
|
const claimId = topFreeClaim.claimId;
|
|
|
|
logger.debug('top free claim id =', 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
|
|
|
};
|