Short urls #89

Merged
bones7242 merged 6 commits from short-urls into master 2017-07-18 04:18:47 +02:00
12 changed files with 185 additions and 105 deletions

View file

@ -7,7 +7,7 @@
}, },
"Database": { "Database": {
"MySqlConnectionUri": "none", "MySqlConnectionUri": "none",
"DownloadDirectory": "/home/lbry/Downloads/" "DownloadDirectory": "/home/ubuntu/Downloads/"
}, },
"Logging": { "Logging": {
"LogLevel": "silly" "LogLevel": "silly"

View file

@ -3,9 +3,10 @@ const db = require('../models');
const logger = require('winston'); const logger = require('winston');
const getAllFreePublicClaims = require('../helpers/functions/getAllFreePublicClaims.js'); const getAllFreePublicClaims = require('../helpers/functions/getAllFreePublicClaims.js');
const isFreePublicClaim = require('../helpers/functions/isFreePublicClaim.js'); const isFreePublicClaim = require('../helpers/functions/isFreePublicClaim.js');
const { validateClaimId } = require('../helpers/libraries/serveHelpers.js');
function updateFileIfNeeded (uri, claimName, claimId, localOutpoint, localHeight) { function updateFileIfNeeded (uri, claimName, claimId, localOutpoint, localHeight) {
logger.debug(`A mysql record was found for ${claimName}:${claimId}. Initiating resolve to check outpoint.`); logger.debug(`Initiating resolve to check outpoint for ${claimName}:${claimId}.`);
// 1. resolve claim // 1. resolve claim
lbryApi lbryApi
.resolveUri(uri) .resolveUri(uri)
@ -34,7 +35,7 @@ function updateFileIfNeeded (uri, claimName, claimId, localOutpoint, localHeight
} }
}) })
.catch(error => { .catch(error => {
logger.error(`error resolving "${uri}" >> `, error); logger.error(error);
}); });
} }
@ -110,6 +111,41 @@ function getClaimAndHandleResponse (uri, address, height, resolve, reject) {
}); });
} }
function getClaimAndReturnResponse (uri, address, height) {
const deferred = new Promise((resolve, reject) => {
lbryApi
.getClaim(uri)
.then(({ name, claim_id, outpoint, file_name, download_path, mime_type, metadata }) => {
// create entry in the db
logger.silly(`creating new File record`);
db.File
.create({
name,
claimId : claim_id,
address, // note: passed as an arguent, not from this 'get' call
outpoint,
height, // note: passed as an arguent, not from this 'get' call
fileName: file_name,
filePath: download_path,
fileType: mime_type,
nsfw : metadata.stream.metadata.nsfw,
})
.then(result => {
logger.debug('successfully created File record');
resolve(result); // note: result.dataValues ?
})
.catch(error => {
logger.error('sequelize create error', error);
reject(error);
});
})
.catch(error => {
reject(error);
});
});
return deferred;
}
module.exports = { module.exports = {
getClaimByName (claimName) { getClaimByName (claimName) {
const deferred = new Promise((resolve, reject) => { const deferred = new Promise((resolve, reject) => {
@ -155,35 +191,36 @@ module.exports = {
}, },
getClaimByClaimId (name, claimId) { getClaimByClaimId (name, claimId) {
const deferred = new Promise((resolve, reject) => { const deferred = new Promise((resolve, reject) => {
const uri = `${name}#${claimId}`; let uri;
// 1. check locally for the claim validateClaimId(name, claimId) // 1. validate the claim id & retrieve the full claim id if needed
db.File .then(validClaimId => { // 2. check locally for the claim
.findOne({ where: { name, claimId } }) logger.debug('valid claim id:', validClaimId);
.then(claim => { uri = `${name}#${validClaimId}`;
// 2. if a match is found locally, serve it return db.File.findOne({ where: { name, claimId: validClaimId } });
if (claim) { })
// serve the file .then(result => {
resolve(claim.dataValues); // 3. if a match is found locally, serve that claim
// trigger an update if needed if (result) {
updateFileIfNeeded(uri, name, claimId, claim.dataValues.outpoint, claim.dataValues.outpoint); logger.debug('Result found in File table:', result.dataValues);
// 2. otherwise use daemon to retrieve it // return the data for the file to be served
resolve(result.dataValues);
// update the file, as needed
updateFileIfNeeded(uri, name, claimId, result.dataValues.outpoint, result.dataValues.outpoint);
// 3. if a match was not found use the daemon to retrieve the claim & return the db data once it is created
} else { } else {
// 3. resolve the Uri logger.debug('No result found in File table');
lbryApi lbryApi
.resolveUri(uri) .resolveUri(uri)
.then(result => { .then(result => {
// check to make sure the result is a claim if (!result.claim) { // check to make sure the result is a claim
if (!result.claim) {
logger.debug('resolve did not return a claim'); logger.debug('resolve did not return a claim');
resolve(null); resolve(null);
return;
} }
// 4. check to see if the claim is free & public if (isFreePublicClaim(result.claim)) { // check to see if the claim is free & public
if (isFreePublicClaim(result.claim)) { // get claim and serve
// 5. get claim and serve resolve(getClaimAndReturnResponse(uri, result.claim.address, result.claim.height));
getClaimAndHandleResponse(uri, result.claim.address, result.claim.height, resolve, reject);
} else { } else {
reject(null); resolve(null);
} }
}) })
.catch(error => { .catch(error => {

View file

@ -36,7 +36,7 @@ module.exports = {
}); });
}) })
.catch(error => { .catch(error => {
logger.error('sequelize error', error); logger.error('Sequelize error', error);
}); });
}, },
sendGoogleAnalytics (action, headers, ip, originalUrl) { sendGoogleAnalytics (action, headers, ip, originalUrl) {

View file

@ -55,7 +55,6 @@ module.exports = (claimName) => {
resolve(orderedPublicClaims); resolve(orderedPublicClaims);
}) })
.catch(error => { .catch(error => {
logger.error('error received from lbryApi.getClaimsList', error);
reject(error); reject(error);
}); });
}); });

View file

@ -0,0 +1,68 @@
const logger = require('winston');
const db = require('../../models');
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':
break;
case 'image/gif':
break;
case 'image/png':
break;
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);
},
validateClaimId (name, claimId) {
const deferred = new Promise((resolve, reject) => {
logger.debug('claim id length:', claimId.length);
// make sure the claim id is 40 characters
if (claimId.length === 40) {
logger.debug('Claim Id length is valid.');
resolve(claimId);
// if the claim id is shorter than 40, check the db for the full claim id
} else if (claimId.length === 1) {
logger.debug(`Finding claim id for "${name}" "${claimId}"`);
db.File
.findOne({
where: {
name,
claimId: { $like: `${claimId}%` },
},
})
.then(file => {
// if no results were found, throw an error
if (!file) {
reject(new Error('That is not a valid short URL.'));
}
// if a result was found, resolve with the full claim id
logger.debug('Full claim id:', file.dataValues.claimId);
resolve(file.dataValues.claimId);
})
.catch(error => {
reject(error);
});
} else {
logger.error('The Claim Id was neither 40 nor 1 character in length');
reject(new Error('That Claim Id is not valid.'));
}
});
return deferred;
},
};

View file

@ -59,6 +59,10 @@ button.copy-button {
float: right; float: right;
} }
.share-option {
margin-bottom: 1em;
}
.metadata-table { .metadata-table {
font-size: small; font-size: small;
border-collapse: collapse; border-collapse: collapse;

View file

@ -2,47 +2,17 @@ const logger = require('winston');
const { getClaimByClaimId, getClaimByName } = require('../controllers/serveController.js'); const { getClaimByClaimId, getClaimByName } = require('../controllers/serveController.js');
const { postToStats, sendGoogleAnalytics } = require('../controllers/statsController.js'); const { postToStats, sendGoogleAnalytics } = require('../controllers/statsController.js');
const errorHandlers = require('../helpers/libraries/errorHandlers.js'); const errorHandlers = require('../helpers/libraries/errorHandlers.js');
const { serveFile } = require('../helpers/libraries/serveHelpers.js');
function 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':
break;
case 'image/gif':
break;
case 'image/png':
break;
case 'video/mp4':
break;
default:
logger.warn('sending file with unknown type as .jpeg');
options['headers']['Content-Type'] = 'image/jpeg';
break;
}
// send file
res.status(200).sendFile(filePath, options);
}
function sendAnalyticsAndLog (headers, ip, originalUrl) {
// google analytics
sendGoogleAnalytics('serve', headers, ip, originalUrl);
}
module.exports = (app) => { module.exports = (app) => {
// route to serve a specific asset // route to serve a specific asset
app.get('/:name/:claim_id', ({ headers, ip, originalUrl, params }, res) => { app.get('/:name/:claim_id', ({ headers, ip, originalUrl, params }, res) => {
sendAnalyticsAndLog(headers, ip, originalUrl); // google analytics
sendGoogleAnalytics('serve', headers, ip, originalUrl);
// begin image-serve processes // begin image-serve processes
getClaimByClaimId(params.name, params.claim_id) getClaimByClaimId(params.name, params.claim_id)
.then(fileInfo => { .then(fileInfo => {
logger.debug('file info:', fileInfo);
// check to make sure a file was found // check to make sure a file was found
if (!fileInfo) { if (!fileInfo) {
res.status(307).render('noClaims'); res.status(307).render('noClaims');
@ -67,9 +37,10 @@ module.exports = (app) => {
errorHandlers.handleRequestError('serve', originalUrl, ip, error, res); errorHandlers.handleRequestError('serve', originalUrl, ip, error, res);
}); });
}); });
// route to serve the winning claim // route to serve the winning asset at a claim
app.get('/:name', ({ headers, ip, originalUrl, params }, res) => { app.get('/:name', ({ headers, ip, originalUrl, params }, res) => {
sendAnalyticsAndLog(headers, ip, originalUrl); // google analytics
sendGoogleAnalytics('serve', headers, ip, originalUrl);
// begin image-serve processes // begin image-serve processes
getClaimByName(params.name) getClaimByName(params.name)
.then(fileInfo => { .then(fileInfo => {

View file

@ -5,13 +5,13 @@ const siofu = require('socketio-file-upload');
const expressHandlebars = require('express-handlebars'); const expressHandlebars = require('express-handlebars');
const Handlebars = require('handlebars'); const Handlebars = require('handlebars');
const config = require('config'); const config = require('config');
const winston = require('winston'); const logger = require('winston');
const hostedContentPath = config.get('Database.DownloadDirectory'); const hostedContentPath = config.get('Database.DownloadDirectory');
// configure logging // configure logging
const logLevel = config.get('Logging.LogLevel'); const logLevel = config.get('Logging.LogLevel');
require('./config/loggerSetup.js')(winston, logLevel); require('./config/loggerSetup.js')(logger, logLevel);
// set port // set port
const PORT = 3000; const PORT = 3000;
@ -29,7 +29,7 @@ app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
app.use(siofu.router); app.use(siofu.router);
app.use((req, res, next) => { // logging middleware app.use((req, res, next) => { // logging middleware
winston.verbose(`Request on ${req.originalUrl} from ${req.ip}`); logger.verbose(`Request on ${req.originalUrl} from ${req.ip}`);
next(); next();
}); });
@ -74,6 +74,9 @@ const hbs = expressHandlebars.create({
return options.inverse(this); return options.inverse(this);
} }
}, },
firstCharacter (word) {
return word.substring(0, 1);
},
}, },
}); });
app.engine('handlebars', hbs.engine); app.engine('handlebars', hbs.engine);
@ -94,10 +97,10 @@ const server = require('./routes/sockets-routes.js')(app, siofu, hostedContentPa
db.sequelize.sync() db.sequelize.sync()
.then(() => { .then(() => {
server.listen(PORT, () => { server.listen(PORT, () => {
winston.info('Trusting proxy?', app.get('trust proxy')); logger.info('Trusting proxy?', app.get('trust proxy'));
winston.info(`Server is listening on PORT ${PORT}`); logger.info(`Server is listening on PORT ${PORT}`);
}); });
}) })
.catch((error) => { .catch((error) => {
winston.log('Error syncing sequelize db:', error); logger.log('Error syncing sequelize db:', error);
}); });

View file

@ -4,45 +4,45 @@
</div> </div>
<div class="panel links"> <div class="panel links">
<h2 class="subheader">Links</h2> <h2 class="subheader">Links</h2>
{{!--direct link to asset--}} {{!--short direct link to asset--}}
<a href="/{{fileInfo.name}}/{{fileInfo.claimId}}">Direct Link</a> <div class="share-option">
<a href="/{{fileInfo.name}}/{{firstCharacter fileInfo.claimId}}">Direct Link</a>
<div class="input-error" id="input-error-copy-direct-link" hidden="true"></div> <div class="input-error" id="input-error-copy-direct-link" hidden="true"></div>
<br/> <br/>
<input type="text" id="direct-link" class="link" readonly spellcheck="false" value="https://spee.ch/{{fileInfo.name}}/{{fileInfo.claimId}}" onclick="select()"/> <input type="text" id="direct-link" class="link" readonly spellcheck="false" value="https://spee.ch/{{fileInfo.name}}/{{firstCharacter fileInfo.claimId}}" onclick="select()"/>
<button class="copy-button" data-elementtocopy="direct-link" onclick="copyToClipboard(event)">copy</button> <button class="copy-button" data-elementtocopy="direct-link" onclick="copyToClipboard(event)">copy</button>
</div>
{{!-- link to show route for asset--}}
<div class="share-option">
<a href="/show/{{fileInfo.name}}/{{firstCharacter fileInfo.claimId}}">Details Link</a>
<div class="input-error" id="input-error-copy-show-link" hidden="true"></div>
</br>
<input type="text" id="show-link" class="link" readonly onclick="select()" spellcheck="false" value="https://spee.ch/show/{{fileInfo.name}}/{{firstCharacter fileInfo.claimId}}"/>
<button class="copy-button" data-elementtocopy="show-link" onclick="copyToClipboard(event)">copy</button>
</div>
{{!-- html text for embedding asset--}}
<div class="share-option">
Embed HTML
<div class="input-error" id="input-error-copy-embed-text" hidden="true"></div>
<br/> <br/>
<br/> {{#ifConditional fileInfo.fileType '===' 'video/mp4'}}
<input type="text" id="embed-text" class="link" readonly onclick="select()" spellcheck="false" value='&lt;video width="100%" controls>&lt;source src="https://spee.ch/{{fileInfo.name}}/{{fileInfo.claimId}}" />&lt;/video>'/>
{{else}}
<input type="text" id="embed-text" class="link" readonly onclick="select()" spellcheck="false" value='&lt;img src="https://spee.ch/{{fileInfo.name}}/{{fileInfo.claimId}}" />'/>
{{/ifConditional}}
<button class="copy-button" data-elementtocopy="embed-text" onclick="copyToClipboard(event)">copy</button>
</div>
{{!--markdown text using asset--}} {{!--markdown text using asset--}}
{{#ifConditional fileInfo.fileType '===' 'video/mp4'}} {{#ifConditional fileInfo.fileType '===' 'video/mp4'}}
{{else}} {{else}}
<div class="share-option">
Markdown Markdown
<div class="input-error" id="input-error-copy-markdown-text" hidden="true"></div> <div class="input-error" id="input-error-copy-markdown-text" hidden="true"></div>
<br/> <br/>
<input type="text" id="markdown-text" class="link" readonly onclick="select()" spellcheck="false" value='![{{fileInfo.name}}](https://spee.ch/{{fileInfo.name}}/{{fileInfo.claimId}})'/> <input type="text" id="markdown-text" class="link" readonly onclick="select()" spellcheck="false" value='![{{fileInfo.name}}](https://spee.ch/{{fileInfo.name}}/{{fileInfo.claimId}})'/>
<button class="copy-button" data-elementtocopy="markdown-text" onclick="copyToClipboard(event)">copy</button> <button class="copy-button" data-elementtocopy="markdown-text" onclick="copyToClipboard(event)">copy</button>
<br/> </div>
<br/>
{{/ifConditional}} {{/ifConditional}}
{{!-- html text for embedding asset--}}
Embed HTML
<div class="input-error" id="input-error-copy-embed-text" hidden="true"></div>
<br/>
{{#ifConditional fileInfo.fileType '===' 'video/mp4'}}
<input type="text" id="embed-text" class="link" readonly onclick="select()" spellcheck="false" value='&lt;video autoplay controls>&lt;source src="https://spee.ch/{{fileInfo.name}}/{{fileInfo.claimId}}" />&lt;/video>'/>
{{else}}
<input type="text" id="embed-text" class="link" readonly onclick="select()" spellcheck="false" value='&lt;img src="https://spee.ch/{{fileInfo.name}}/{{fileInfo.claimId}}" />'/>
{{/ifConditional}}
<button class="copy-button" data-elementtocopy="embed-text" onclick="copyToClipboard(event)">copy</button>
<br/>
<br/>
{{!-- link to show route for asset--}}
<a href="/show/{{fileInfo.name}}/{{fileInfo.claimId}}">Details Link</a>
<div class="input-error" id="input-error-copy-show-link" hidden="true"></div>
</br>
<input type="text" id="show-link" class="link" readonly onclick="select()" spellcheck="false" value="https://spee.ch/show/{{fileInfo.name}}/{{fileInfo.claimId}}"/>
<button class="copy-button" data-elementtocopy="show-link" onclick="copyToClipboard(event)">copy</button>
<br/>
<br/>
</div> </div>
<div class="panel"> <div class="panel">
<h2 class="subheader">Metadata</h2> <h2 class="subheader">Metadata</h2>

View file

@ -3,6 +3,7 @@
{{#each trendingAssets}} {{#each trendingAssets}}
{{#if this.nsfw}} {{#if this.nsfw}}
{{else }} {{else }}
<a href="/show/{{this.name}}/{{this.claimId}}">
{{#ifConditional this.fileType '===' 'video/mp4'}} {{#ifConditional this.fileType '===' 'video/mp4'}}
<video class="asset-small" controls> <video class="asset-small" controls>
<source src="/api/streamFile/{{this.fileName}}"> <source src="/api/streamFile/{{this.fileName}}">
@ -10,10 +11,9 @@
Your browser does not support the <code>video</code> element. Your browser does not support the <code>video</code> element.
</video> </video>
{{else}} {{else}}
<a href="/show/{{this.name}}/{{this.claimId}}">
<img class="asset-small" src="/api/streamFile/{{this.fileName}}" /> <img class="asset-small" src="/api/streamFile/{{this.fileName}}" />
</a>
{{/ifConditional}} {{/ifConditional}}
</a>
{{/if}} {{/if}}
{{/each}} {{/each}}
</div> </div>

View file

@ -10,11 +10,6 @@
</div> </div>
<script type ="text/javascript"> <script type ="text/javascript">
function focusThisInput(event){
}
function copyToClipboard(event){ function copyToClipboard(event){
var elementToCopy = event.target.dataset.elementtocopy; var elementToCopy = event.target.dataset.elementtocopy;
var element = document.getElementById(elementToCopy); var element = document.getElementById(elementToCopy);

View file

@ -1,11 +1,14 @@
<div id="asset-placeholder"> <div id="asset-placeholder">
<a href="/show/{{fileInfo.name}}/{{fileInfo.claimId}}">
{{#ifConditional fileInfo.fileType '===' 'video/mp4'}} {{#ifConditional fileInfo.fileType '===' 'video/mp4'}}
<video class="show-asset-lite" autoplay controls> <video class="show-asset-lite" autoplay controls>
<source src="/api/streamFile/{{fileInfo.fileName}}"> <source src="/api/streamFile/{{fileInfo.fileName}}">
{{!--fallback--}} {{!--fallback--}}
Your browser does not support the <code>video</code> element. Your browser does not support the <code>video</code> element.
</video> </video>
{{else}} {{else}}
<img class="show-asset-lite" src="/api/streamFile/{{fileInfo.fileName}}" alt="{{fileInfo.fileName}}"/> <img class="show-asset-lite" src="/api/streamFile/{{fileInfo.fileName}}" alt="{{fileInfo.fileName}}"/>
{{/ifConditional}} {{/ifConditional}}
</a>
</div> </div>