moved database related functions to model layer
This commit is contained in:
parent
f0435be0b2
commit
af046e9d36
4 changed files with 328 additions and 313 deletions
controllers
|
@ -1,7 +1,7 @@
|
|||
const lbryApi = require('../helpers/lbryApi.js');
|
||||
const db = require('../models');
|
||||
const logger = require('winston');
|
||||
const { resolveAgainstClaimTable, serveFile, showFile, showFileLite, getShortClaimIdFromLongClaimId, getClaimIdByLongChannelId, getAllChannelClaims, getLongChannelId, getShortChannelIdFromLongChannelId, getLongClaimId } = require('../helpers/serveHelpers.js');
|
||||
const { serveFile, showFile, showFileLite } = require('../helpers/serveHelpers.js');
|
||||
const { postToStats, sendGoogleAnalytics } = require('../controllers/statsController.js');
|
||||
|
||||
const SERVE = 'SERVE';
|
||||
|
@ -59,34 +59,35 @@ function getAssetByLongClaimId (fullClaimId, name) {
|
|||
}
|
||||
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 = createFileRecord(resolveResult);
|
||||
fileRecord = addGetResultsToFileRecord(fileRecord, getResult);
|
||||
// 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);
|
||||
db
|
||||
.resolveClaim(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 = createFileRecord(resolveResult);
|
||||
fileRecord = addGetResultsToFileRecord(fileRecord, getResult);
|
||||
// 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);
|
||||
});
|
||||
})
|
||||
.catch(error => {
|
||||
reject(error);
|
||||
|
@ -99,34 +100,36 @@ module.exports = {
|
|||
logger.debug('getting asset by claim');
|
||||
return new Promise((resolve, reject) => {
|
||||
// 1. get the long claim id
|
||||
getLongClaimId(claimName, claimId) // here
|
||||
// 2. get the claim Id
|
||||
.then(longClaimId => {
|
||||
logger.debug('long claim id = ', longClaimId);
|
||||
resolve(getAssetByLongClaimId(longClaimId, claimName));
|
||||
})
|
||||
.catch(error => {
|
||||
reject(error);
|
||||
});
|
||||
db
|
||||
.getLongClaimId(claimName, claimId)
|
||||
// 2. get the claim Id
|
||||
.then(longClaimId => {
|
||||
logger.debug('long claim id = ', longClaimId);
|
||||
resolve(getAssetByLongClaimId(longClaimId, claimName));
|
||||
})
|
||||
.catch(error => {
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
},
|
||||
getAssetByChannel (channelName, channelId, claimName) {
|
||||
logger.debug('getting asset by channel');
|
||||
return new Promise((resolve, reject) => {
|
||||
// 1. get the long channel id
|
||||
getLongChannelId(channelName, channelId)
|
||||
// 2. get the claim Id
|
||||
.then(longChannelId => {
|
||||
return getClaimIdByLongChannelId(longChannelId, claimName);
|
||||
})
|
||||
// 3. get the asset by this claim id and name
|
||||
.then(claimId => {
|
||||
logger.debug('asset claim id = ', claimId);
|
||||
resolve(getAssetByLongClaimId(claimId, claimName));
|
||||
})
|
||||
.catch(error => {
|
||||
reject(error);
|
||||
});
|
||||
db
|
||||
.getLongChannelId(channelName, channelId)
|
||||
// 2. get the claim Id
|
||||
.then(longChannelId => {
|
||||
return db.getClaimIdByLongChannelId(longChannelId, claimName);
|
||||
})
|
||||
// 3. get the asset by this claim id and name
|
||||
.then(claimId => {
|
||||
logger.debug('asset claim id = ', claimId);
|
||||
resolve(getAssetByLongClaimId(claimId, claimName));
|
||||
})
|
||||
.catch(error => {
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
},
|
||||
getChannelContents (channelName, channelId) {
|
||||
|
@ -134,32 +137,33 @@ module.exports = {
|
|||
let longChannelId;
|
||||
let shortChannelId;
|
||||
// 1. get the long channel Id
|
||||
getLongChannelId(channelName, channelId)
|
||||
// 2. get all claims for that channel
|
||||
.then(result => {
|
||||
longChannelId = result;
|
||||
return getShortChannelIdFromLongChannelId(channelName, longChannelId);
|
||||
})
|
||||
// 3. get all Claim records for this channel
|
||||
.then(result => {
|
||||
shortChannelId = result;
|
||||
return getAllChannelClaims(longChannelId);
|
||||
})
|
||||
// 4. add extra data not available from Claim table
|
||||
.then(allChannelClaims => {
|
||||
if (allChannelClaims) {
|
||||
allChannelClaims.forEach(element => {
|
||||
element['channelName'] = channelName;
|
||||
element['longChannelId'] = longChannelId;
|
||||
element['shortChannelId'] = shortChannelId;
|
||||
element['fileExtension'] = element.contentType.substring(element.contentType.lastIndexOf('/') + 1);
|
||||
});
|
||||
}
|
||||
return resolve(allChannelClaims);
|
||||
})
|
||||
.catch(error => {
|
||||
reject(error);
|
||||
});
|
||||
db
|
||||
.getLongChannelId(channelName, channelId)
|
||||
// 2. get all claims for that channel
|
||||
.then(result => {
|
||||
longChannelId = result;
|
||||
return db.getShortChannelIdFromLongChannelId(channelName, longChannelId);
|
||||
})
|
||||
// 3. get all Claim records for this channel
|
||||
.then(result => {
|
||||
shortChannelId = result;
|
||||
return db.getAllChannelClaims(longChannelId);
|
||||
})
|
||||
// 4. add extra data not available from Claim table
|
||||
.then(allChannelClaims => {
|
||||
if (allChannelClaims) {
|
||||
allChannelClaims.forEach(element => {
|
||||
element['channelName'] = channelName;
|
||||
element['longChannelId'] = longChannelId;
|
||||
element['shortChannelId'] = shortChannelId;
|
||||
element['fileExtension'] = element.contentType.substring(element.contentType.lastIndexOf('/') + 1);
|
||||
});
|
||||
}
|
||||
return resolve(allChannelClaims);
|
||||
})
|
||||
.catch(error => {
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
},
|
||||
serveOrShowAsset (fileInfo, extension, method, headers, originalUrl, ip, res) {
|
||||
|
@ -181,21 +185,22 @@ module.exports = {
|
|||
showFileLite(fileInfo, res);
|
||||
return fileInfo;
|
||||
case SHOW:
|
||||
return getShortClaimIdFromLongClaimId(fileInfo.claimId, fileInfo.name)
|
||||
.then(shortId => {
|
||||
fileInfo['shortId'] = shortId;
|
||||
return resolveAgainstClaimTable(fileInfo.name, fileInfo.claimId);
|
||||
})
|
||||
.then(resolveResult => {
|
||||
fileInfo['title'] = resolveResult.title;
|
||||
fileInfo['description'] = resolveResult.description;
|
||||
showFile(fileInfo, res);
|
||||
return fileInfo;
|
||||
})
|
||||
.catch(error => {
|
||||
logger.error('throwing serve/show error...');
|
||||
throw error;
|
||||
});
|
||||
return db
|
||||
.getShortClaimIdFromLongClaimId(fileInfo.claimId, fileInfo.name)
|
||||
.then(shortId => {
|
||||
fileInfo['shortId'] = shortId;
|
||||
return db.resolveClaim(fileInfo.name, fileInfo.claimId);
|
||||
})
|
||||
.then(resolveResult => {
|
||||
fileInfo['title'] = resolveResult.title;
|
||||
fileInfo['description'] = resolveResult.description;
|
||||
showFile(fileInfo, res);
|
||||
return fileInfo;
|
||||
})
|
||||
.catch(error => {
|
||||
logger.error('throwing serve/show error...');
|
||||
throw error;
|
||||
});
|
||||
default:
|
||||
logger.error('I did not recognize that method');
|
||||
break;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue