From 56ad4ce089ff3185c5ed7109f852a84d0ab25e4b Mon Sep 17 00:00:00 2001 From: bill bittner Date: Wed, 5 Jul 2017 18:26:33 -0700 Subject: [PATCH] show route tested and working on jpegs --- helpers/libraries/errorHandlers.js | 7 +++-- public/assets/css/allStyle.css | 2 +- public/assets/css/componentStyle.css | 8 ++++++ routes/api-routes.js | 12 ++++----- routes/serve-routes.js | 38 +++------------------------- routes/sockets-routes.js | 37 +++++++++++++++++++++++++-- views/partials/asset.handlebars | 34 +++++++++++++++++-------- 7 files changed, 82 insertions(+), 56 deletions(-) diff --git a/helpers/libraries/errorHandlers.js b/helpers/libraries/errorHandlers.js index e07632a5..b7ea5a37 100644 --- a/helpers/libraries/errorHandlers.js +++ b/helpers/libraries/errorHandlers.js @@ -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) { diff --git a/public/assets/css/allStyle.css b/public/assets/css/allStyle.css index 1feb4c45..cb258dea 100644 --- a/public/assets/css/allStyle.css +++ b/public/assets/css/allStyle.css @@ -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; diff --git a/public/assets/css/componentStyle.css b/public/assets/css/componentStyle.css index ef8a795b..d1603d2d 100644 --- a/public/assets/css/componentStyle.css +++ b/public/assets/css/componentStyle.css @@ -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 */ diff --git a/routes/api-routes.js b/routes/api-routes.js index a49c5e58..46b90530 100644 --- a/routes/api-routes.js +++ b/routes/api-routes.js @@ -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 diff --git a/routes/serve-routes.js b/routes/serve-routes.js index 870e5061..e9333814 100644 --- a/routes/serve-routes.js +++ b/routes/serve-routes.js @@ -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 diff --git a/routes/sockets-routes.js b/routes/sockets-routes.js index 6f6f72e8..edd6e6f6 100644 --- a/routes/sockets-routes.js +++ b/routes/sockets-routes.js @@ -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', () => { diff --git a/views/partials/asset.handlebars b/views/partials/asset.handlebars index c343d11d..e1035962 100644 --- a/views/partials/asset.handlebars +++ b/views/partials/asset.handlebars @@ -1,19 +1,31 @@
-

asset goes here

-
- {{#if type.mp4}} -

It's a video!

- {{else}} -

It's not a video

- {{/if}} +
-
\ No newline at end of file