From d4560905eddb2f5434534cd49056ec429e4a8f55 Mon Sep 17 00:00:00 2001 From: bill bittner Date: Wed, 5 Jul 2017 12:23:55 -0700 Subject: [PATCH 01/11] build the handlebars template and serve/show route fork --- routes/serve-routes.js | 32 +++++++++++++++++++++---- views/partials/asset.handlebars | 4 ++++ views/partials/assetLinks.handlebars | 4 ++++ views/partials/assetMetadata.handlebars | 4 ++++ views/show.handlebars | 14 +++++++++++ 5 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 views/partials/asset.handlebars create mode 100644 views/partials/assetLinks.handlebars create mode 100644 views/partials/assetMetadata.handlebars create mode 100644 views/show.handlebars diff --git a/routes/serve-routes.js b/routes/serve-routes.js index fee6b68f..124c1ac2 100644 --- a/routes/serve-routes.js +++ b/routes/serve-routes.js @@ -32,7 +32,17 @@ 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}`); + // set default options + let showOptions; + + // send file + res.status(200).render('show', showOptions); +} + function sendAnalyticsAndLog (headers, ip, originalUrl) { + logger.verbose('headers', headers); // google analytics sendGoogleAnalytics('serve', headers, ip, originalUrl); // logging @@ -52,8 +62,15 @@ module.exports = (app) => { res.status(307).render('noClaims'); return; } - postToStats('serve', originalUrl, ip, 'success'); - serveFile(fileInfo, res); + // serve the file or the show route + const mimetypes = headers['accept'].split(','); + if (mimetypes.includes('text/html')) { + postToStats('show', originalUrl, ip, 'success'); + servePage(fileInfo, res); + } else { + postToStats('serve', originalUrl, ip, 'success'); + serveFile(fileInfo, res); + } }) .catch(error => { errorHandlers.handleRequestError('serve', originalUrl, ip, error, res); @@ -71,8 +88,15 @@ module.exports = (app) => { res.status(307).render('noClaims'); return; } - postToStats('serve', originalUrl, ip, 'success'); - serveFile(fileInfo, res); + // serve the file or the show route + const mimetypes = headers['accept'].split(','); + if (mimetypes.includes('text/html')) { + postToStats('show', originalUrl, ip, 'success'); + servePage(fileInfo, res); + } else { + postToStats('serve', originalUrl, ip, 'success'); + serveFile(fileInfo, res); + } }) .catch(error => { errorHandlers.handleRequestError('serve', originalUrl, ip, error, res); diff --git a/views/partials/asset.handlebars b/views/partials/asset.handlebars new file mode 100644 index 00000000..79158d4a --- /dev/null +++ b/views/partials/asset.handlebars @@ -0,0 +1,4 @@ +
+

asset goes here

+ +
\ No newline at end of file diff --git a/views/partials/assetLinks.handlebars b/views/partials/assetLinks.handlebars new file mode 100644 index 00000000..99ad20c4 --- /dev/null +++ b/views/partials/assetLinks.handlebars @@ -0,0 +1,4 @@ +
+

links

+

links go here

+
\ No newline at end of file diff --git a/views/partials/assetMetadata.handlebars b/views/partials/assetMetadata.handlebars new file mode 100644 index 00000000..6dd584d1 --- /dev/null +++ b/views/partials/assetMetadata.handlebars @@ -0,0 +1,4 @@ +
+

metadata

+

metadata goes here

+
\ No newline at end of file diff --git a/views/show.handlebars b/views/show.handlebars new file mode 100644 index 00000000..a60ff292 --- /dev/null +++ b/views/show.handlebars @@ -0,0 +1,14 @@ +
+ {{> topBar}} +
+ {{> asset}} +
+ + {{> footer}} +
+ + + \ No newline at end of file -- 2.45.2 From 41e524694131307a8fe30bd9c4d624f90914bff4 Mon Sep 17 00:00:00 2001 From: bill bittner Date: Wed, 5 Jul 2017 13:51:25 -0700 Subject: [PATCH 02/11] updated route to send html to client --- routes/serve-routes.js | 35 ++++++++++++++++++++++++++++----- routes/sockets-routes.js | 11 ++++++++--- server.js | 4 ++-- views/partials/asset.handlebars | 19 ++++++++++++++++-- views/show.handlebars | 5 +---- 5 files changed, 58 insertions(+), 16 deletions(-) diff --git a/routes/serve-routes.js b/routes/serve-routes.js index 124c1ac2..870e5061 100644 --- a/routes/serve-routes.js +++ b/routes/serve-routes.js @@ -6,14 +6,13 @@ const { postToStats, sendGoogleAnalytics } = require('../controllers/statsContro function serveFile ({ fileName, fileType, filePath }, res) { logger.info(`serving file ${fileName}`); // set default options - const options = { + let options = { headers: { 'X-Content-Type-Options': 'nosniff', 'Content-Type' : fileType, }, }; // adjust default options as needed - // eslint-disable-next-line camelcase switch (fileType) { case 'image/jpeg': break; @@ -35,10 +34,36 @@ function serveFile ({ fileName, fileType, filePath }, res) { function servePage ({ fileName, fileType, filePath }, res) { logger.info(`serving show page for ${fileName}`); // set default options - let showOptions; - + 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', showOptions); + res.status(200).render('show', options); } function sendAnalyticsAndLog (headers, ip, originalUrl) { diff --git a/routes/sockets-routes.js b/routes/sockets-routes.js index 7ca4b092..6f6f72e8 100644 --- a/routes/sockets-routes.js +++ b/routes/sockets-routes.js @@ -5,8 +5,9 @@ const errorHandlers = require('../helpers/libraries/errorHandlers.js'); const { postToStats } = require('../controllers/statsController.js'); module.exports = (app, siofu, hostedContentPath) => { - const http = require('http').Server(app); - const io = require('socket.io')(http); + const http = require('http'); + const server = http.Server(app); + const io = require('socket.io')(server); io.on('connection', socket => { logger.silly('a user connected via sockets'); @@ -55,11 +56,15 @@ module.exports = (app, siofu, hostedContentPath) => { // to-do: remove the file if not done automatically } }); + // handle asset requests + socket.on('asset-request', (msg) => { + console.log('received a msg:', msg); + }); // handle disconnect socket.on('disconnect', () => { logger.silly('a user disconnected via sockets'); }); }); - return http; + return server; }; diff --git a/server.js b/server.js index 0df1158c..e8274d5c 100644 --- a/server.js +++ b/server.js @@ -61,13 +61,13 @@ require('./routes/serve-routes.js')(app); require('./routes/home-routes.js')(app); // require socket.io routes -const http = require('./routes/sockets-routes.js')(app, siofu, hostedContentPath); +const server = require('./routes/sockets-routes.js')(app, siofu, hostedContentPath); // sync sequelize // wrap the server in socket.io to intercept incoming sockets requests // start server db.sequelize.sync().then(() => { - http.listen(PORT, () => { + server.listen(PORT, () => { winston.info('Trusting proxy?', app.get('trust proxy')); winston.info(`Server is listening on PORT ${PORT}`); }); diff --git a/views/partials/asset.handlebars b/views/partials/asset.handlebars index 79158d4a..c343d11d 100644 --- a/views/partials/asset.handlebars +++ b/views/partials/asset.handlebars @@ -1,4 +1,19 @@

asset goes here

- -
\ No newline at end of file +
+ {{#if type.mp4}} +

It's a video!

+ {{else}} +

It's not a video

+ {{/if}} +
+ + + + + \ No newline at end of file diff --git a/views/show.handlebars b/views/show.handlebars index a60ff292..dbcd3a32 100644 --- a/views/show.handlebars +++ b/views/show.handlebars @@ -8,7 +8,4 @@ {{> assetMetadata}} {{> footer}} - - - - \ No newline at end of file + \ No newline at end of file -- 2.45.2 From 56ad4ce089ff3185c5ed7109f852a84d0ab25e4b Mon Sep 17 00:00:00 2001 From: bill bittner Date: Wed, 5 Jul 2017 18:26:33 -0700 Subject: [PATCH 03/11] 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 -- 2.45.2 From 90da7a3637cefc64426004c84049c8575584cee9 Mon Sep 17 00:00:00 2001 From: bill bittner Date: Wed, 5 Jul 2017 18:38:22 -0700 Subject: [PATCH 04/11] show route tested and working on mp4 --- public/assets/css/componentStyle.css | 2 +- routes/sockets-routes.js | 9 +++++---- views/partials/asset.handlebars | 3 ++- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/public/assets/css/componentStyle.css b/public/assets/css/componentStyle.css index d1603d2d..2f09f62f 100644 --- a/public/assets/css/componentStyle.css +++ b/public/assets/css/componentStyle.css @@ -14,7 +14,7 @@ /* show routes */ -.show-image { +.show-image, .show-video { width: 100%; padding-top: 1em; } diff --git a/routes/sockets-routes.js b/routes/sockets-routes.js index edd6e6f6..be9f4da1 100644 --- a/routes/sockets-routes.js +++ b/routes/sockets-routes.js @@ -67,21 +67,22 @@ module.exports = (app, siofu, hostedContentPath) => { }; // set the data const fileExtension = filePath.substring(filePath.lastIndexOf('.')); + console.log(fileExtension); let data = { type : null, buffer: assetBuffer.toString('base64'), }; switch (fileExtension) { - case 'jpeg' || 'jpg': + case '.jpeg' || '.jpg': data['type'] = 'image/jpeg'; break; - case 'gif': + case '.gif': data['type'] = 'image/gif'; break; - case 'png': + case '.png': data['type'] = 'image/png'; break; - case 'mp4': + case '.mp4': data['type'] = 'video/mp4'; break; default: diff --git a/views/partials/asset.handlebars b/views/partials/asset.handlebars index e1035962..85be4b89 100644 --- a/views/partials/asset.handlebars +++ b/views/partials/asset.handlebars @@ -1,5 +1,6 @@
+

video loading...

@@ -23,7 +24,7 @@ break; case 'video/mp4': const base64video = 'data:' + data.type + ';base64,' + data.buffer; - document.getElementById("asset-placeholder").innerHTML = '
\ No newline at end of file + + + + \ No newline at end of file diff --git a/views/partials/assetInfo.handlebars b/views/partials/assetInfo.handlebars new file mode 100644 index 00000000..ad4ec8ac --- /dev/null +++ b/views/partials/assetInfo.handlebars @@ -0,0 +1,69 @@ +
+

Title

+

{{fileInfo.name}} +

+ +
+

Metadata

+ + + + + + + + + + + + + + + + + + +
+ + \ No newline at end of file diff --git a/views/partials/assetLinks.handlebars b/views/partials/assetLinks.handlebars deleted file mode 100644 index 378fd893..00000000 --- a/views/partials/assetLinks.handlebars +++ /dev/null @@ -1,13 +0,0 @@ -
-

links

- - - - - - - - - - -
\ No newline at end of file diff --git a/views/partials/assetMetadata.handlebars b/views/partials/assetMetadata.handlebars deleted file mode 100644 index e0a60fd1..00000000 --- a/views/partials/assetMetadata.handlebars +++ /dev/null @@ -1,21 +0,0 @@ -
-

metadata

- - - - - - - - - - - - - - - - - - -
\ No newline at end of file diff --git a/views/show.handlebars b/views/show.handlebars index 03ee170a..fe8935dc 100644 --- a/views/show.handlebars +++ b/views/show.handlebars @@ -4,40 +4,7 @@ {{> asset}} {{> footer}} - - - - \ No newline at end of file + \ No newline at end of file diff --git a/views/showLite.handlebars b/views/showLite.handlebars index e0ccfb74..e30e03a2 100644 --- a/views/showLite.handlebars +++ b/views/showLite.handlebars @@ -6,7 +6,6 @@