2017-07-17 22:16:11 +02:00
|
|
|
const logger = require('winston');
|
2017-08-03 02:13:02 +02:00
|
|
|
const db = require('../models');
|
2017-07-19 23:11:47 +02:00
|
|
|
const lbryApi = require('./lbryApi');
|
2017-07-17 22:16:11 +02:00
|
|
|
|
2017-08-04 21:58:42 +02:00
|
|
|
function determineShortUrl (claimId, height, claimList) {
|
2017-07-19 23:11:47 +02:00
|
|
|
logger.debug('determining short url based on claim id and claim list');
|
2017-08-04 21:58:42 +02:00
|
|
|
// remove this claim from the claim list, if it exists
|
|
|
|
claimList = claimList.filter(claim => {
|
2017-07-19 16:58:44 +02:00
|
|
|
return claim.claim_id !== claimId;
|
|
|
|
});
|
2017-08-04 20:32:21 +02:00
|
|
|
logger.debug('claim list length:', claimList.length);
|
2017-08-04 21:58:42 +02:00
|
|
|
// if there are no other claims, return the first letter of the claim id
|
|
|
|
if (claimList.length === 0) {
|
2017-07-19 23:11:47 +02:00
|
|
|
return claimId.substring(0, 1);
|
2017-08-04 21:58:42 +02:00
|
|
|
// otherwise determine the proper url
|
2017-07-19 23:11:47 +02:00
|
|
|
} else {
|
|
|
|
let i = 0;
|
|
|
|
const claimListCopy = claimList;
|
|
|
|
while (claimList.length !== 0) { // filter out matching claims
|
|
|
|
i++;
|
|
|
|
claimList = claimList.filter(claim => {
|
|
|
|
return (claim.claim_id.substring(0, i) === claimId.substring(0, i));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
i -= 1;
|
|
|
|
const lastMatch = claimId.substring(0, i);
|
|
|
|
|
|
|
|
const matchingClaims = claimListCopy.filter(claim => {
|
|
|
|
return (claim.claim_id.substring(0, i) === lastMatch);
|
2017-07-19 16:58:44 +02:00
|
|
|
});
|
2017-07-19 23:11:47 +02:00
|
|
|
for (let j = 0; j < matchingClaims.length; j++) {
|
2017-08-04 21:58:42 +02:00
|
|
|
if (matchingClaims[j].height < height) {
|
2017-07-19 23:11:47 +02:00
|
|
|
return claimId.substring(0, i + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return claimId.substring(0, i);
|
2017-07-19 16:58:44 +02:00
|
|
|
}
|
2017-07-19 23:11:47 +02:00
|
|
|
}
|
|
|
|
|
2017-07-20 19:15:20 +02:00
|
|
|
function checkLocalDbForClaims (name, shortUrl) {
|
|
|
|
return db.File
|
|
|
|
.findAll({
|
|
|
|
where: {
|
|
|
|
name,
|
|
|
|
claimId: { $like: `${shortUrl}%` },
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.then(records => {
|
|
|
|
logger.debug('number of local search results:', records.length);
|
|
|
|
if (records.length === 0) {
|
|
|
|
return records;
|
|
|
|
}
|
|
|
|
const localClaims = records.map(record => { // format the data to match what lbry daemon would have returned
|
|
|
|
return { name: record.dataValues.name, claim_id: record.dataValues.claimId, height: record.dataValues.height };
|
|
|
|
});
|
|
|
|
return localClaims;
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
return error;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-08-08 02:08:03 +02:00
|
|
|
function createOpenGraphInfo ({ fileType, claimId, name, fileName, fileExt }) {
|
2017-08-05 02:41:06 +02:00
|
|
|
return {
|
2017-08-08 22:05:29 +02:00
|
|
|
embedUrl : `https://spee.ch/embed/${claimId}/${name}`,
|
2017-08-08 20:01:01 +02:00
|
|
|
showUrl : `https://spee.ch/${claimId}/${name}`,
|
|
|
|
source : `https://spee.ch/${claimId}/${name}${fileExt}`,
|
|
|
|
directFileUrl: `https://spee.ch/media/${fileName}`,
|
2017-08-05 02:41:06 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-07-17 22:16:11 +02:00
|
|
|
module.exports = {
|
|
|
|
serveFile ({ fileName, fileType, filePath }, res) {
|
|
|
|
logger.info(`serving file ${fileName}`);
|
|
|
|
// set default options
|
|
|
|
let options = {
|
|
|
|
headers: {
|
|
|
|
'X-Content-Type-Options': 'nosniff',
|
|
|
|
'Content-Type' : fileType,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
// adjust default options as needed
|
|
|
|
switch (fileType) {
|
|
|
|
case 'image/jpeg':
|
|
|
|
case 'image/gif':
|
|
|
|
case 'image/png':
|
|
|
|
case 'video/mp4':
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
logger.warn('sending file with unknown type as .jpeg');
|
|
|
|
options['headers']['Content-Type'] = 'image/jpeg';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// send the file
|
|
|
|
res.status(200).sendFile(filePath, options);
|
|
|
|
},
|
2017-08-02 03:58:13 +02:00
|
|
|
showFile (fileInfo, res) {
|
2017-08-05 02:41:06 +02:00
|
|
|
const openGraphInfo = createOpenGraphInfo(fileInfo);
|
|
|
|
res.status(200).render('show', { layout: 'show', fileInfo, openGraphInfo });
|
2017-08-01 02:02:39 +02:00
|
|
|
},
|
2017-08-02 03:58:13 +02:00
|
|
|
showFileLite (fileInfo, res) {
|
2017-08-05 02:41:06 +02:00
|
|
|
const openGraphInfo = createOpenGraphInfo(fileInfo);
|
|
|
|
res.status(200).render('showLite', { layout: 'show', fileInfo, openGraphInfo });
|
2017-08-01 02:02:39 +02:00
|
|
|
},
|
2017-08-04 06:59:22 +02:00
|
|
|
getClaimIdFromShortUrl (shortUrl, name) {
|
2017-08-02 22:16:39 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
2017-08-04 20:32:21 +02:00
|
|
|
logger.debug('getting claim_id from short url');
|
2017-08-02 22:16:39 +02:00
|
|
|
// use the daemon to check for claims list
|
|
|
|
lbryApi.getClaimsList(name)
|
|
|
|
.then(({ claims }) => {
|
2017-07-20 19:15:20 +02:00
|
|
|
logger.debug('Number of claims from getClaimsList:', claims.length);
|
2017-08-02 22:16:39 +02:00
|
|
|
// if no claims were found, check locally for possible claims
|
2017-07-20 19:15:20 +02:00
|
|
|
if (claims.length === 0) {
|
|
|
|
return checkLocalDbForClaims(name, shortUrl);
|
|
|
|
} else {
|
|
|
|
return claims;
|
|
|
|
}
|
|
|
|
})
|
2017-08-02 22:16:39 +02:00
|
|
|
// handle the claims list
|
|
|
|
.then(claims => {
|
|
|
|
logger.debug('Claims ready for filtering');
|
2017-07-19 18:11:08 +02:00
|
|
|
const regex = new RegExp(`^${shortUrl}`);
|
|
|
|
const filteredClaimsList = claims.filter(claim => {
|
|
|
|
return regex.test(claim.claim_id);
|
|
|
|
});
|
|
|
|
switch (filteredClaimsList.length) {
|
|
|
|
case 0:
|
|
|
|
reject(new Error('That is an invalid short url'));
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
resolve(filteredClaimsList[0].claim_id);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
const sortedClaimsList = filteredClaimsList.sort((a, b) => {
|
|
|
|
return a.height > b.height;
|
|
|
|
});
|
|
|
|
resolve(sortedClaimsList[0].claim_id);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
2017-08-04 21:58:42 +02:00
|
|
|
getShortUrlFromClaimId (claimId, height, name) {
|
2017-08-02 22:16:39 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
2017-08-04 20:32:21 +02:00
|
|
|
logger.debug('finding short url from claim_id');
|
2017-07-19 18:11:08 +02:00
|
|
|
// get a list of all the claims
|
2017-07-19 23:11:47 +02:00
|
|
|
lbryApi.getClaimsList(name)
|
2017-07-19 18:11:08 +02:00
|
|
|
// find the smallest possible unique url for this claim
|
|
|
|
.then(({ claims }) => {
|
2017-08-04 21:58:42 +02:00
|
|
|
const shortUrl = determineShortUrl(claimId, height, claims);
|
2017-07-19 18:11:08 +02:00
|
|
|
resolve(shortUrl);
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
reject(error);
|
|
|
|
});
|
2017-07-17 22:16:11 +02:00
|
|
|
});
|
2017-07-19 23:11:47 +02:00
|
|
|
},
|
2017-07-17 22:16:11 +02:00
|
|
|
};
|