2017-06-19 22:10:06 +02:00
|
|
|
const logger = require('winston');
|
2017-10-05 23:48:08 +02:00
|
|
|
const { publish } = require('../controllers/publishController.js');
|
|
|
|
const { createPublishParams } = require('../helpers/publishHelpers.js');
|
2017-10-07 03:04:20 +02:00
|
|
|
const { useObjectPropertiesIfNoKeys } = require('../helpers/errorHandlers.js');
|
2017-06-30 07:26:29 +02:00
|
|
|
const { postToStats } = require('../controllers/statsController.js');
|
2017-06-15 20:15:13 +02:00
|
|
|
|
2017-06-28 05:25:36 +02:00
|
|
|
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
|
|
|
|
2017-06-17 22:51:30 +02:00
|
|
|
io.on('connection', socket => {
|
2017-06-19 22:10:06 +02:00
|
|
|
logger.silly('a user connected via sockets');
|
2017-06-17 22:51:30 +02:00
|
|
|
// attach upload listeners
|
2017-06-19 18:37:35 +02:00
|
|
|
const uploader = new siofu();
|
|
|
|
uploader.dir = hostedContentPath;
|
|
|
|
uploader.listen(socket);
|
2017-06-28 00:53:53 +02:00
|
|
|
// listener for when file upload starts
|
2017-06-17 22:51:30 +02:00
|
|
|
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);
|
2017-06-17 22:51:30 +02:00
|
|
|
// server side test to make sure file is not a bad file type
|
|
|
|
if (/\.exe$/.test(file.name)) {
|
2017-06-19 18:37:35 +02:00
|
|
|
uploader.abort(file.id, socket);
|
2017-06-17 22:51:30 +02:00
|
|
|
}
|
2017-06-19 18:37:35 +02:00
|
|
|
});
|
2017-06-28 00:53:53 +02:00
|
|
|
// listener for when file upload encounters an error
|
2017-06-17 22:51:30 +02:00
|
|
|
uploader.on('error', ({ error }) => {
|
2017-06-19 22:10:06 +02:00
|
|
|
logger.error('an error occured while uploading', error);
|
2017-09-07 20:58:52 +02:00
|
|
|
postToStats('PUBLISH', '/', null, null, null, error);
|
2017-06-19 18:37:35 +02:00
|
|
|
socket.emit('publish-status', error);
|
|
|
|
});
|
2017-06-28 00:53:53 +02:00
|
|
|
// listener for when file has been uploaded
|
2017-06-17 22:51:30 +02:00
|
|
|
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)...');
|
2017-10-10 03:29:40 +02:00
|
|
|
// /*
|
|
|
|
// NOTE: need to validate that client has the credentials to the channel they chose
|
|
|
|
// otherwise they could circumvent security.
|
|
|
|
// */
|
|
|
|
let thumbnail;
|
|
|
|
if (file.meta.thumbnail) {
|
|
|
|
thumbnail = file.meta.thumbnail;
|
|
|
|
} else {
|
|
|
|
thumbnail = null;
|
|
|
|
}
|
|
|
|
let channelName;
|
|
|
|
if (file.meta.channel) {
|
|
|
|
channelName = file.meta.channel;
|
|
|
|
} else {
|
|
|
|
channelName = null;
|
|
|
|
}
|
2017-06-26 19:02:14 +02:00
|
|
|
// prepare the publish parameters
|
2017-10-10 03:29:40 +02:00
|
|
|
const publishParams = createPublishParams(file.pathName, file.meta.name, file.meta.title, file.meta.description, file.meta.license, file.meta.nsfw, thumbnail, channelName);
|
|
|
|
logger.debug('publish parameters:', publishParams);
|
2017-06-26 19:02:14 +02:00
|
|
|
// publish the file
|
2017-10-05 23:48:08 +02:00
|
|
|
publish(publishParams, file.name, file.meta.type)
|
2017-08-16 20:00:17 +02:00
|
|
|
.then(result => {
|
|
|
|
socket.emit('publish-complete', { name: publishParams.name, result });
|
2017-10-03 19:05:09 +02:00
|
|
|
postToStats('PUBLISH', '/', null, null, null, 'success');
|
2017-08-16 20:00:17 +02:00
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
socket.emit('publish-failure', error);
|
2017-10-03 19:05:09 +02:00
|
|
|
logger.error('Publish Error:', useObjectPropertiesIfNoKeys(error));
|
|
|
|
postToStats('PUBLISH', '/', null, null, null, error);
|
2017-08-16 20:00:17 +02:00
|
|
|
});
|
2017-06-17 22:51:30 +02:00
|
|
|
} else {
|
2017-07-10 18:32:37 +02:00
|
|
|
socket.emit('publish-failure', 'File uploaded, but with errors');
|
2017-10-03 19:05:09 +02:00
|
|
|
logger.error(`An error occurred in uploading the client's file`);
|
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
|
2017-06-17 22:51:30 +02:00
|
|
|
}
|
2017-06-19 18:37:35 +02:00
|
|
|
});
|
2017-06-17 22:51:30 +02:00
|
|
|
// handle disconnect
|
|
|
|
socket.on('disconnect', () => {
|
2017-06-19 22:10:06 +02:00
|
|
|
logger.silly('a user disconnected via sockets');
|
2017-06-19 18:37:35 +02:00
|
|
|
});
|
|
|
|
});
|
2017-06-17 22:51:30 +02:00
|
|
|
|
2017-07-05 22:51:25 +02:00
|
|
|
return server;
|
2017-06-19 18:37:35 +02:00
|
|
|
};
|