spee.ch/controllers/serveController.js

145 lines
4.4 KiB
JavaScript
Raw Normal View History

2017-08-03 02:13:02 +02:00
const lbryApi = require('../helpers/lbryApi.js');
const db = require('../models');
2017-06-20 01:15:28 +02:00
const logger = require('winston');
const { getTopFreeClaim, getFullClaimIdFromShortId, resolveAgainstClaimTable, getClaimIdByChannel } = require('../helpers/serveHelpers.js');
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,
};
}
function getAssetByClaimId (fullClaimId, name) {
2017-08-16 09:40:49 +02:00
logger.debug('...getting asset by claim Id...');
return new Promise((resolve, reject) => {
// 1. check locally for claim
checkForLocalAssetByClaimId(fullClaimId, name)
.then(dataValues => {
// if a result was found, return early with the result
if (dataValues) {
2017-08-16 09:40:49 +02:00
logger.debug('found a local file for this name and claimId');
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);
})
.catch(error => {
reject(error);
});
})
.catch(error => {
reject(error);
});
})
.catch(error => {
reject(error);
});
});
}
module.exports = {
getAssetByChannel (channelName, channelId, claimName) {
logger.debug('channelId =', channelId);
return new Promise((resolve, reject) => {
getClaimIdByChannel(channelName, channelId, claimName)
.then(claimId => {
logger.debug('claim id = ', claimId);
resolve(getAssetByClaimId(claimId, claimName));
})
.catch(error => {
reject(error);
2017-08-22 03:03:57 +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...');
return new Promise((resolve, reject) => {
// get the full claimId
getFullClaimIdFromShortId(shortId, name)
// get the asset by the claimId
.then(claimId => {
2017-08-16 09:40:49 +02:00
logger.debug('claim id =', claimId);
resolve(getAssetByClaimId(claimId, name));
})
.catch(error => {
reject(error);
});
});
},
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
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) {
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);
});
});
},
getChannelByName (name) {
logger.debug('...getting channel by channel name...');
return new Promise((resolve, reject) => {
reject(new Error('This feature is not yet supported'));
});
},
};