spee.ch/controllers/serveController.js

245 lines
8.3 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 { serveFile, showFile, showFileLite } = require('../helpers/serveHelpers.js');
const { postToStats, sendGoogleAnalytics } = require('../controllers/statsController.js');
const SERVE = 'SERVE';
const SHOW = 'SHOW';
const SHOWLITE = 'SHOWLITE';
2017-10-06 00:14:50 +02:00
const DEFAULT_THUMBNAIL = 'https://spee.ch/assets/img/video_thumb_default.png';
const NO_CHANNEL = 'NO_CHANNEL';
const NO_CLAIM = 'NO_CLAIM';
function checkForLocalAssetByClaimId (claimId, name) {
logger.debug(`checkForLocalAssetsByClaimId(${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 addGetResultsToFileRecord (fileInfo, getResult) {
fileInfo.fileName = getResult.file_name;
fileInfo.filePath = getResult.download_path;
fileInfo.fileType = getResult.mime_type;
return fileInfo;
}
function createFileRecord ({ name, claimId, outpoint, height, address, nsfw }) {
return {
name,
claimId,
outpoint,
height,
address,
fileName: '',
filePath: '',
fileType: '',
nsfw,
};
}
2017-08-24 02:00:16 +02:00
function getAssetByLongClaimId (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');
resolve(dataValues);
return;
}
logger.debug('no local file found for this name and claimId');
// 2. if no local claim, resolve and get the claim
2017-11-01 01:19:26 +01:00
db.Claim
.resolveClaim(name, fullClaimId)
.then(resolveResult => {
// if no result, return early (claim doesn't exist or isn't free)
if (!resolveResult) {
resolve(NO_CLAIM);
return;
}
2017-11-10 01:49:38 +01:00
logger.debug('resolve result >> ', resolveResult.dataValues);
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(() => {
logger.debug('File record successfully updated');
resolve(fileRecord);
})
.catch(error => {
reject(error);
});
})
.catch(error => {
reject(error);
});
})
.catch(error => {
reject(error);
});
});
}
2017-09-12 02:57:51 +02:00
function chooseThumbnail (claimInfo, defaultThumbnail) {
2017-09-28 19:51:02 +02:00
if (!claimInfo.thumbnail || claimInfo.thumbnail.trim() === '') {
2017-09-12 02:57:51 +02:00
return defaultThumbnail;
}
return claimInfo.thumbnail;
}
module.exports = {
2017-08-24 02:00:16 +02:00
getAssetByClaim (claimName, claimId) {
logger.debug(`getAssetByClaim(${claimName}, ${claimId})`);
return new Promise((resolve, reject) => {
2017-11-01 00:00:12 +01:00
db.Claim.getLongClaimId(claimName, claimId) // 1. get the long claim id
.then(result => { // 2. get the asset using the long claim id
logger.debug('long claim id ===', result);
2017-10-18 19:00:24 +02:00
if (result === NO_CLAIM) {
logger.debug('resolving NO_CLAIM');
resolve(NO_CLAIM);
return;
}
resolve(getAssetByLongClaimId(result, claimName));
})
.catch(error => {
reject(error);
});
});
},
getAssetByChannel (channelName, channelId, claimName) {
2017-08-24 02:00:16 +02:00
logger.debug('getting asset by channel');
return new Promise((resolve, reject) => {
2017-11-01 01:19:26 +01:00
db.Certificate.getLongChannelId(channelName, channelId) // 1. get the long channel id
.then(result => { // 2. get the long claim Id
2017-10-18 19:00:24 +02:00
if (result === NO_CHANNEL) {
resolve(NO_CHANNEL);
return;
}
2017-10-31 23:28:11 +01:00
return db.Claim.getClaimIdByLongChannelId(result, claimName);
})
.then(result => { // 3. get the asset using the long claim id
logger.debug('asset claim id =', result);
if (result === NO_CHANNEL || result === NO_CLAIM) {
resolve(result);
return;
}
resolve(getAssetByLongClaimId(result, claimName));
})
.catch(error => {
reject(error);
});
});
},
getChannelContents (channelName, channelId) {
return new Promise((resolve, reject) => {
let longChannelId;
let shortChannelId;
2017-11-01 01:19:26 +01:00
db.Certificate.getLongChannelId(channelName, channelId) // 1. get the long channel Id
2017-10-13 02:56:23 +02:00
.then(result => { // 2. get all claims for that channel
if (result === NO_CHANNEL) {
return NO_CHANNEL;
}
longChannelId = result;
return db.Certificate.getShortChannelIdFromLongChannelId(longChannelId, channelName);
})
2017-10-13 02:56:23 +02:00
.then(result => { // 3. get all Claim records for this channel
if (result === NO_CHANNEL) {
return NO_CHANNEL;
}
shortChannelId = result;
2017-10-31 19:54:33 +01:00
return db.Claim.getAllChannelClaims(longChannelId);
})
.then(result => { // 4. add extra data not available from Claim table
if (result === NO_CHANNEL) {
resolve(NO_CHANNEL);
return;
}
if (result) {
result.forEach(element => {
const fileExtenstion = element.contentType.substring(element.contentType.lastIndexOf('/') + 1);
element['showUrlLong'] = `/${channelName}:${longChannelId}/${element.name}`;
element['directUrlLong'] = `/${channelName}:${longChannelId}/${element.name}.${fileExtenstion}`;
element['showUrlShort'] = `/${channelName}:${shortChannelId}/${element.name}`;
element['directUrlShort'] = `/${channelName}:${shortChannelId}/${element.name}.${fileExtenstion}`;
2017-09-12 02:57:51 +02:00
element['thumbnail'] = chooseThumbnail(element, DEFAULT_THUMBNAIL);
});
}
resolve({
2017-09-28 19:51:02 +02:00
channelName,
longChannelId,
shortChannelId,
claims: result,
2017-09-28 19:51:02 +02:00
});
})
.catch(error => {
reject(error);
});
});
},
serveOrShowAsset (fileInfo, extension, method, headers, originalUrl, ip, res) {
// add file extension to the file info
if (extension === 'gifv') {
fileInfo['fileExt'] = 'gifv';
} else {
fileInfo['fileExt'] = fileInfo.fileName.substring(fileInfo.fileName.lastIndexOf('.') + 1);
}
// add stats table
postToStats(method, originalUrl, ip, fileInfo.name, fileInfo.claimId, 'success');
// serve or show
switch (method) {
case SERVE:
serveFile(fileInfo, res);
sendGoogleAnalytics(method, headers, ip, originalUrl);
return fileInfo;
case SHOWLITE:
showFileLite(fileInfo, res);
return fileInfo;
case SHOW:
return db.Claim
.getShortClaimIdFromLongClaimId(fileInfo.claimId, fileInfo.name)
.then(shortId => {
fileInfo['shortId'] = shortId;
2017-11-01 01:19:26 +01:00
return db.Claim.resolveClaim(fileInfo.name, fileInfo.claimId);
})
.then(resolveResult => {
2017-11-01 01:42:51 +01:00
logger.debug('resolve result >>', resolveResult.dataValues);
2017-09-12 02:57:51 +02:00
fileInfo['thumbnail'] = chooseThumbnail(resolveResult, DEFAULT_THUMBNAIL);
fileInfo['title'] = resolveResult.title;
fileInfo['description'] = resolveResult.description;
if (resolveResult.certificateId) { fileInfo['certificateId'] = resolveResult.certificateId };
2017-10-26 02:07:59 +02:00
if (resolveResult.channelName) { fileInfo['channelName'] = resolveResult.channelName };
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;
}
},
};