Show routes #72
5 changed files with 58 additions and 16 deletions
|
@ -6,14 +6,13 @@ const { postToStats, sendGoogleAnalytics } = require('../controllers/statsContro
|
||||||
function serveFile ({ fileName, fileType, filePath }, res) {
|
function serveFile ({ fileName, fileType, filePath }, res) {
|
||||||
logger.info(`serving file ${fileName}`);
|
logger.info(`serving file ${fileName}`);
|
||||||
// set default options
|
// set default options
|
||||||
const options = {
|
let options = {
|
||||||
headers: {
|
headers: {
|
||||||
'X-Content-Type-Options': 'nosniff',
|
'X-Content-Type-Options': 'nosniff',
|
||||||
'Content-Type' : fileType,
|
'Content-Type' : fileType,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
// adjust default options as needed
|
// adjust default options as needed
|
||||||
// eslint-disable-next-line camelcase
|
|
||||||
switch (fileType) {
|
switch (fileType) {
|
||||||
case 'image/jpeg':
|
case 'image/jpeg':
|
||||||
break;
|
break;
|
||||||
|
@ -35,10 +34,36 @@ function serveFile ({ fileName, fileType, filePath }, res) {
|
||||||
function servePage ({ fileName, fileType, filePath }, res) {
|
function servePage ({ fileName, fileType, filePath }, res) {
|
||||||
logger.info(`serving show page for ${fileName}`);
|
logger.info(`serving show page for ${fileName}`);
|
||||||
// set default options
|
// 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
|
// send file
|
||||||
res.status(200).render('show', showOptions);
|
res.status(200).render('show', options);
|
||||||
}
|
}
|
||||||
|
|
||||||
function sendAnalyticsAndLog (headers, ip, originalUrl) {
|
function sendAnalyticsAndLog (headers, ip, originalUrl) {
|
||||||
|
|
|
@ -5,8 +5,9 @@ const errorHandlers = require('../helpers/libraries/errorHandlers.js');
|
||||||
const { postToStats } = require('../controllers/statsController.js');
|
const { postToStats } = require('../controllers/statsController.js');
|
||||||
|
|
||||||
module.exports = (app, siofu, hostedContentPath) => {
|
module.exports = (app, siofu, hostedContentPath) => {
|
||||||
const http = require('http').Server(app);
|
const http = require('http');
|
||||||
const io = require('socket.io')(http);
|
const server = http.Server(app);
|
||||||
|
const io = require('socket.io')(server);
|
||||||
|
|
||||||
io.on('connection', socket => {
|
io.on('connection', socket => {
|
||||||
logger.silly('a user connected via sockets');
|
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
|
// 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
|
// handle disconnect
|
||||||
socket.on('disconnect', () => {
|
socket.on('disconnect', () => {
|
||||||
logger.silly('a user disconnected via sockets');
|
logger.silly('a user disconnected via sockets');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
return http;
|
return server;
|
||||||
};
|
};
|
||||||
|
|
|
@ -61,13 +61,13 @@ require('./routes/serve-routes.js')(app);
|
||||||
require('./routes/home-routes.js')(app);
|
require('./routes/home-routes.js')(app);
|
||||||
|
|
||||||
// require socket.io routes
|
// 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
|
// sync sequelize
|
||||||
// wrap the server in socket.io to intercept incoming sockets requests
|
// wrap the server in socket.io to intercept incoming sockets requests
|
||||||
// start server
|
// start server
|
||||||
db.sequelize.sync().then(() => {
|
db.sequelize.sync().then(() => {
|
||||||
http.listen(PORT, () => {
|
server.listen(PORT, () => {
|
||||||
winston.info('Trusting proxy?', app.get('trust proxy'));
|
winston.info('Trusting proxy?', app.get('trust proxy'));
|
||||||
winston.info(`Server is listening on PORT ${PORT}`);
|
winston.info(`Server is listening on PORT ${PORT}`);
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,4 +1,19 @@
|
||||||
<div class="panel">
|
<div class="panel">
|
||||||
<p> asset goes here </p>
|
<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>
|
||||||
|
|
||||||
</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');
|
||||||
|
|
||||||
|
</script>
|
|
@ -9,6 +9,3 @@
|
||||||
</div>
|
</div>
|
||||||
{{> footer}}
|
{{> footer}}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script src="/socket.io/socket.io.js"></script>
|
|
||||||
<script src="/siofu/client.js"></script>
|
|
Loading…
Reference in a new issue