show route tested and working on jpegs

This commit is contained in:
bill bittner 2017-07-05 18:26:33 -07:00
parent 41e5246941
commit 56ad4ce089
7 changed files with 82 additions and 56 deletions

View file

@ -3,16 +3,19 @@ const { postToStats } = require('../../controllers/statsController.js');
module.exports = {
handleRequestError (action, originalUrl, ip, error, res) {
logger.error('Request Error >>', error.message);
logger.error('Request Error >>', error);
if (error.response) {
postToStats(action, originalUrl, ip, error.response.data.error.messsage);
res.status(error.response.status).send(error.response.data.error.message);
} else if (error.code === 'ECONNREFUSED') {
postToStats(action, originalUrl, ip, 'Connection refused. The daemon may not be running.');
res.status(503).send('Connection refused. The daemon may not be running.');
} else {
} else if (error.message) {
postToStats(action, originalUrl, ip, error);
res.status(400).send(error.message);
} else {
postToStats(action, originalUrl, ip, error);
res.status(400).send(error);
}
},
handlePublishError (error) {

View file

@ -43,7 +43,6 @@ footer {
.panel {
overflow: auto;
word-wrap: break-word;
margin-bottom: 1em;
}
.col-left, .col-right {
@ -73,6 +72,7 @@ h1 {
h2 {
font-size: medium;
margin-top: 1em;
border-top: 1px #999 solid;
background-color: lightgray;
padding: 6px;

View file

@ -12,6 +12,13 @@
margin: 2px 5px 2px 5px;
}
/* show routes */
.show-image {
width: 100%;
padding-top: 1em;
}
/* learn more */
.learn-more {
text-align: center;
@ -82,6 +89,7 @@
#image-preview {
display: none;
margin-bottom: 1em;
}
/* meme */

View file

@ -9,9 +9,9 @@ const { postToStats, sendGoogleAnalytics } = require('../controllers/statsContro
module.exports = app => {
// route to run a claim_list request on the daemon
app.get('/api/claim_list/:name', ({ ip, originalUrl, params }, res) => {
app.get('/api/claim_list/:name', ({ headers, ip, originalUrl, params }, res) => {
// google analytics
sendGoogleAnalytics('serve', ip, originalUrl);
sendGoogleAnalytics('serve', headers, ip, originalUrl);
// log
logger.verbose(`GET request on ${originalUrl} from ${ip}`);
// serve the content
@ -45,9 +45,9 @@ module.exports = app => {
});
});
// route to run a resolve request on the daemon
app.get('/api/resolve/:uri', ({ ip, originalUrl, params }, res) => {
app.get('/api/resolve/:uri', ({ headers, ip, originalUrl, params }, res) => {
// google analytics
sendGoogleAnalytics('serve', ip, originalUrl);
sendGoogleAnalytics('serve', headers, ip, originalUrl);
// log
logger.verbose(`GET request on ${originalUrl} from ${ip}`);
// serve content
@ -62,9 +62,9 @@ module.exports = app => {
});
});
// route to run a publish request on the daemon
app.post('/api/publish', multipartMiddleware, ({ body, files, ip, originalUrl }, res) => {
app.post('/api/publish', multipartMiddleware, ({ body, files, headers, ip, originalUrl }, res) => {
// google analytics
sendGoogleAnalytics('publish', ip, originalUrl);
sendGoogleAnalytics('publish', headers, ip, originalUrl);
// log
logger.verbose(`POST request on ${originalUrl} from ${ip}`);
// validate that a file was provided

View file

@ -23,7 +23,7 @@ function serveFile ({ fileName, fileType, filePath }, res) {
case 'video/mp4':
break;
default:
logger.warn('sending unknown file type as .jpeg');
logger.warn('sending file with unknown type as .jpeg');
options['headers']['Content-Type'] = 'image/jpeg';
break;
}
@ -31,43 +31,13 @@ function serveFile ({ fileName, fileType, filePath }, res) {
res.status(200).sendFile(filePath, options);
}
function servePage ({ fileName, fileType, filePath }, res) {
logger.info(`serving show page for ${fileName}`);
function servePage ({ fileName, filePath }, res) {
logger.debug(`serving show page for ${fileName}`);
// set default options
let options = {
name: fileName,
type: {
jpeg: false,
gif : false,
png : false,
mp4 : false,
},
path: filePath,
};
switch (fileType) {
case 'image/jpeg':
options['type']['jpeg'] = true;
break;
case 'image/gif':
options['type']['gif'] = true;
break;
case 'image/png':
options['type']['png'] = true;
break;
case 'video/mp4':
options['type']['mp4'] = true;
break;
default:
options['type']['jpeg'] = true;
logger.warn('sending show page with unknown file type');
break;
}
// send file
res.status(200).render('show', options);
res.status(200).render('show', { filePath });
}
function sendAnalyticsAndLog (headers, ip, originalUrl) {
logger.verbose('headers', headers);
// google analytics
sendGoogleAnalytics('serve', headers, ip, originalUrl);
// logging

View file

@ -1,4 +1,5 @@
const logger = require('winston');
const fs = require('fs');
const publishController = require('../controllers/publishController.js');
const publishHelpers = require('../helpers/libraries/publishHelpers.js');
const errorHandlers = require('../helpers/libraries/errorHandlers.js');
@ -57,8 +58,40 @@ module.exports = (app, siofu, hostedContentPath) => {
}
});
// handle asset requests
socket.on('asset-request', (msg) => {
console.log('received a msg:', msg);
socket.on('asset-request', filePath => {
logger.debug('received a request for the following file', filePath);
fs.readFile(filePath, (err, assetBuffer) => {
if (err) {
logger.error('fs.readFile error', err);
return;
};
// set the data
const fileExtension = filePath.substring(filePath.lastIndexOf('.'));
let data = {
type : null,
buffer: assetBuffer.toString('base64'),
};
switch (fileExtension) {
case 'jpeg' || 'jpg':
data['type'] = 'image/jpeg';
break;
case 'gif':
data['type'] = 'image/gif';
break;
case 'png':
data['type'] = 'image/png';
break;
case 'mp4':
data['type'] = 'video/mp4';
break;
default:
data['type'] = 'image/jpeg';
logger.warn('showing file with unknown type as jpeg');
break;
}
// send the asset
socket.emit('asset-transfer', data);
});
});
// handle disconnect
socket.on('disconnect', () => {

View file

@ -1,19 +1,31 @@
<div class="panel">
<p> asset goes here </p>
<div id="asset-placeholder">
{{#if type.mp4}}
<p>It's a video!</p>
{{else}}
<p>It's not a video</p>
{{/if}}
<div id="asset-placeholder" data-filepath="{{{filePath}}}">
</div>
</div>
<script src="/socket.io/socket.io.js"></script>
<script type="text/javascript">
var socket = io();
socket.emit('asset-request', 'ready for asset on 1234');
var filePath = document.getElementById('asset-placeholder').dataset.filepath;
console.log(filePath);
if (filePath) {
// send request for the file
socket.emit('asset-request', filePath);
}
// wait for the file to be sent
socket.on('asset-transfer', function(data) {
switch (data.type) {
case 'image/jpeg':
case 'image/gif':
case 'image/png':
const base64Image = 'data:' + data.type + ';base64,' + data.buffer;
document.getElementById("asset-placeholder").innerHTML = '<img class="show-image" src="' + base64Image + '"/>';
break;
case 'video/mp4':
const base64video = 'data:' + data.type + ';base64,' + data.buffer;
document.getElementById("asset-placeholder").innerHTML = '<video class="show-video" src="' + base64video + '"/>';
break;
default: break;
}
});
</script>