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-09-08 02:08:06 +02:00
|
|
|
const { serveFile, showFile, showFileLite } = require('../helpers/serveHelpers.js');
|
2017-08-24 01:01:28 +02:00
|
|
|
const { postToStats, sendGoogleAnalytics } = require('../controllers/statsController.js');
|
2017-09-08 00:31:32 +02:00
|
|
|
|
|
|
|
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';
|
2017-10-18 00:09:43 +02:00
|
|
|
const NO_CHANNEL = 'NO_CHANNEL';
|
2017-10-18 00:50:35 +02:00
|
|
|
const NO_CLAIM = 'NO_CLAIM';
|
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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-08-23 19:21:31 +02:00
|
|
|
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 }) {
|
2017-08-02 03:58:13 +02:00
|
|
|
return {
|
|
|
|
name,
|
2017-08-23 19:21:31 +02:00
|
|
|
claimId,
|
2017-08-02 03:58:13 +02:00
|
|
|
outpoint,
|
2017-08-23 19:21:31 +02:00
|
|
|
height,
|
|
|
|
address,
|
|
|
|
fileName: '',
|
|
|
|
filePath: '',
|
|
|
|
fileType: '',
|
|
|
|
nsfw,
|
2017-08-02 03:58:13 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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...');
|
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-10-17 06:25:29 +02:00
|
|
|
resolve(dataValues);
|
|
|
|
return;
|
2017-08-16 21:23:02 +02:00
|
|
|
}
|
|
|
|
logger.debug('no local file found for this name and claimId');
|
|
|
|
// 2. if no local claim, resolve and get the claim
|
2017-09-08 02:08:06 +02:00
|
|
|
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);
|
|
|
|
})
|
2017-10-18 00:50:35 +02:00
|
|
|
.then(() => {
|
2017-09-08 02:08:06 +02:00
|
|
|
logger.debug('File record successfully updated');
|
|
|
|
resolve(fileRecord);
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
reject(error);
|
|
|
|
});
|
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
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-08-02 22:16:39 +02:00
|
|
|
module.exports = {
|
2017-08-24 02:00:16 +02:00
|
|
|
getAssetByClaim (claimName, claimId) {
|
|
|
|
logger.debug('getting asset by claim');
|
2017-08-02 03:58:13 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
2017-10-18 00:50:35 +02:00
|
|
|
db.getLongClaimId(claimName, claimId) // 1. get the long claim id
|
2017-10-18 18:49:19 +02:00
|
|
|
.then(result => { // 2. get the asset using the long claim id
|
2017-10-18 00:50:35 +02:00
|
|
|
if (result === NO_CLAIM || !result) {
|
|
|
|
resolve(NO_CLAIM);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
logger.debug('long claim id = ', result);
|
|
|
|
resolve(getAssetByLongClaimId(result, claimName));
|
2017-09-08 02:08:06 +02:00
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
reject(error);
|
|
|
|
});
|
2017-06-19 18:37:35 +02:00
|
|
|
});
|
2017-06-17 22:51:30 +02:00
|
|
|
},
|
2017-08-24 01:01:28 +02:00
|
|
|
getAssetByChannel (channelName, channelId, claimName) {
|
2017-08-24 02:00:16 +02:00
|
|
|
logger.debug('getting asset by channel');
|
2017-08-23 00:50:20 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
2017-10-18 00:50:35 +02:00
|
|
|
db.getLongChannelId(channelName, channelId) // 1. get the long channel id
|
2017-10-18 18:49:19 +02:00
|
|
|
.then(result => { // 2. get the long claim Id
|
|
|
|
if (result === NO_CHANNEL || !result) {
|
|
|
|
resolve(NO_CHANNEL);
|
2017-10-18 00:50:35 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
return db.getClaimIdByLongChannelId(result, claimName);
|
2017-09-08 02:08:06 +02:00
|
|
|
})
|
2017-10-18 18:49:19 +02:00
|
|
|
.then(result => { // 3. get the asset using the long claim id
|
2017-10-18 00:50:35 +02:00
|
|
|
logger.debug('asset claim id =', result);
|
|
|
|
if (result === NO_CHANNEL || result === NO_CLAIM) {
|
|
|
|
resolve(result);
|
|
|
|
return;
|
2017-10-18 18:49:19 +02:00
|
|
|
} else if (!result) {
|
|
|
|
resolve(NO_CLAIM);
|
|
|
|
return;
|
2017-10-18 00:50:35 +02:00
|
|
|
}
|
|
|
|
resolve(getAssetByLongClaimId(result, claimName));
|
2017-09-08 02:08:06 +02:00
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
reject(error);
|
|
|
|
});
|
2017-08-24 01:01:28 +02:00
|
|
|
});
|
|
|
|
},
|
|
|
|
getChannelContents (channelName, channelId) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let longChannelId;
|
|
|
|
let shortChannelId;
|
2017-10-18 18:49:19 +02:00
|
|
|
db.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
|
2017-10-18 00:09:43 +02:00
|
|
|
if (result === NO_CHANNEL) {
|
|
|
|
return NO_CHANNEL;
|
|
|
|
}
|
2017-09-08 02:08:06 +02:00
|
|
|
longChannelId = result;
|
2017-09-28 20:42:29 +02:00
|
|
|
return db.getShortChannelIdFromLongChannelId(longChannelId, channelName);
|
2017-09-08 02:08:06 +02:00
|
|
|
})
|
2017-10-13 02:56:23 +02:00
|
|
|
.then(result => { // 3. get all Claim records for this channel
|
2017-10-18 00:09:43 +02:00
|
|
|
if (result === NO_CHANNEL) {
|
|
|
|
return NO_CHANNEL;
|
|
|
|
}
|
2017-09-08 02:08:06 +02:00
|
|
|
shortChannelId = result;
|
|
|
|
return db.getAllChannelClaims(longChannelId);
|
|
|
|
})
|
2017-10-18 00:09:43 +02:00
|
|
|
.then(result => { // 4. add extra data not available from Claim table
|
|
|
|
if (result === NO_CHANNEL) {
|
|
|
|
resolve(NO_CHANNEL);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (result) {
|
|
|
|
result.forEach(element => {
|
2017-09-11 20:09:38 +02:00
|
|
|
const fileExtenstion = element.contentType.substring(element.contentType.lastIndexOf('/') + 1);
|
|
|
|
element['showUrlLong'] = `/${channelName}:${longChannelId}/${element.name}`;
|
|
|
|
element['directUrlLong'] = `/${channelName}:${longChannelId}/${element.name}.${fileExtenstion}`;
|
2017-10-18 18:49:19 +02:00
|
|
|
element['showUrlShort'] = `/${channelName}:${shortChannelId}/${element.name}`;
|
2017-09-11 20:09:38 +02:00
|
|
|
element['directUrlShort'] = `/${channelName}:${shortChannelId}/${element.name}.${fileExtenstion}`;
|
2017-09-12 02:57:51 +02:00
|
|
|
element['thumbnail'] = chooseThumbnail(element, DEFAULT_THUMBNAIL);
|
2017-09-08 02:08:06 +02:00
|
|
|
});
|
|
|
|
}
|
2017-10-18 00:09:43 +02:00
|
|
|
resolve({
|
2017-09-28 19:51:02 +02:00
|
|
|
channelName,
|
|
|
|
longChannelId,
|
|
|
|
shortChannelId,
|
2017-10-18 00:09:43 +02:00
|
|
|
claims: result,
|
2017-09-28 19:51:02 +02:00
|
|
|
});
|
2017-09-08 02:08:06 +02:00
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
reject(error);
|
|
|
|
});
|
2017-08-23 00:50:20 +02:00
|
|
|
});
|
|
|
|
},
|
2017-08-24 01:01:28 +02:00
|
|
|
serveOrShowAsset (fileInfo, extension, method, headers, originalUrl, ip, res) {
|
|
|
|
// add file extension to the file info
|
2017-09-07 18:45:00 +02:00
|
|
|
if (extension === 'gifv') {
|
|
|
|
fileInfo['fileExt'] = 'gifv';
|
2017-08-24 01:01:28 +02:00
|
|
|
} else {
|
2017-09-07 18:45:00 +02:00
|
|
|
fileInfo['fileExt'] = fileInfo.fileName.substring(fileInfo.fileName.lastIndexOf('.') + 1);
|
2017-08-24 01:01:28 +02:00
|
|
|
}
|
2017-09-07 20:46:06 +02:00
|
|
|
// add stats table
|
|
|
|
postToStats(method, originalUrl, ip, fileInfo.name, fileInfo.claimId, 'success');
|
2017-08-24 01:01:28 +02:00
|
|
|
// 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:
|
2017-09-08 02:08:06 +02:00
|
|
|
return db
|
|
|
|
.getShortClaimIdFromLongClaimId(fileInfo.claimId, fileInfo.name)
|
|
|
|
.then(shortId => {
|
|
|
|
fileInfo['shortId'] = shortId;
|
|
|
|
return db.resolveClaim(fileInfo.name, fileInfo.claimId);
|
|
|
|
})
|
|
|
|
.then(resolveResult => {
|
2017-09-12 02:57:51 +02:00
|
|
|
fileInfo['thumbnail'] = chooseThumbnail(resolveResult, DEFAULT_THUMBNAIL);
|
2017-09-08 02:08:06 +02:00
|
|
|
fileInfo['title'] = resolveResult.title;
|
|
|
|
fileInfo['description'] = resolveResult.description;
|
|
|
|
showFile(fileInfo, res);
|
|
|
|
return fileInfo;
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
logger.error('throwing serve/show error...');
|
|
|
|
throw error;
|
|
|
|
});
|
2017-08-24 01:01:28 +02:00
|
|
|
default:
|
|
|
|
logger.error('I did not recognize that method');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
2017-06-19 18:37:35 +02:00
|
|
|
};
|