spee.ch/routes/sockets-routes.js

73 lines
3 KiB
JavaScript
Raw Normal View History

const logger = require('winston');
const publishController = require('../controllers/publishController.js');
2017-08-03 02:13:02 +02:00
const publishHelpers = require('../helpers/publishHelpers.js');
const errorHandlers = require('../helpers/errorHandlers.js');
2017-06-30 07:26:29 +02:00
const { postToStats } = require('../controllers/statsController.js');
module.exports = (app, siofu, hostedContentPath) => {
2017-07-05 22:51:25 +02:00
const http = require('http');
const server = http.Server(app);
const io = require('socket.io')(server);
2017-05-25 07:50:02 +02:00
io.on('connection', socket => {
logger.silly('a user connected via sockets');
// attach upload listeners
const uploader = new siofu();
uploader.dir = hostedContentPath;
uploader.listen(socket);
// listener for when file upload starts
uploader.on('start', ({ file }) => {
2017-06-30 07:26:29 +02:00
// log
2017-06-20 05:22:23 +02:00
logger.info('client started an upload:', file.name);
// server side test to make sure file is not a bad file type
if (/\.exe$/.test(file.name)) {
uploader.abort(file.id, socket);
}
});
// listener for when file upload encounters an error
uploader.on('error', ({ error }) => {
logger.error('an error occured while uploading', error);
2017-09-07 20:58:52 +02:00
postToStats('PUBLISH', '/', null, null, null, error);
socket.emit('publish-status', error);
});
// listener for when file has been uploaded
uploader.on('saved', ({ file }) => {
if (file.success) {
2017-06-20 05:22:23 +02:00
logger.debug(`Client successfully uploaded ${file.name}`);
2017-07-10 18:32:37 +02:00
socket.emit('publish-status', 'File upload successfully completed. Your image is being published to LBRY (this might take a second)...');
/*
NOTE: need to validate that client has the credentials to the channel they chose
otherwise they could circumvent security client side.
*/
// prepare the publish parameters
2017-09-21 18:18:19 +02:00
const publishParams = publishHelpers.createPublishParams(file.meta.name, file.pathName, file.meta.title, file.meta.description, file.meta.license, file.meta.nsfw, file.meta.channel);
2017-09-28 17:47:02 +02:00
logger.debug(publishParams);
// publish the file
2017-08-16 20:00:17 +02:00
publishController.publish(publishParams, file.name, file.meta.type)
.then(result => {
2017-09-07 20:58:52 +02:00
postToStats('PUBLISH', '/', null, null, null, 'success');
2017-08-16 20:00:17 +02:00
socket.emit('publish-complete', { name: publishParams.name, result });
})
.catch(error => {
error = errorHandlers.handlePublishError(error);
2017-09-07 20:58:52 +02:00
postToStats('PUBLISH', '/', null, null, null, error);
2017-08-16 20:00:17 +02:00
socket.emit('publish-failure', error);
});
} else {
logger.error(`An error occurred in uploading the client's file`);
2017-07-10 18:32:37 +02:00
socket.emit('publish-failure', 'File uploaded, but with errors');
2017-09-07 20:58:52 +02:00
postToStats('PUBLISH', '/', null, null, null, 'File uploaded, but with errors');
2017-09-21 18:18:19 +02:00
// to-do: remove the file, if not done automatically
}
});
// handle disconnect
socket.on('disconnect', () => {
logger.silly('a user disconnected via sockets');
});
});
2017-07-05 22:51:25 +02:00
return server;
};