2017-06-15 20:15:13 +02:00
|
|
|
var socketHelpers = require('../helpers/socketHelpers.js');
|
|
|
|
|
|
|
|
module.exports = function(app, path, siofu, hostedContentPath, ua, googleAnalyticsId) {
|
2017-05-25 09:59:22 +02:00
|
|
|
var http = require('http').Server(app);
|
|
|
|
var io = require('socket.io')(http);
|
2017-06-13 04:09:39 +02:00
|
|
|
|
2017-05-25 07:50:02 +02:00
|
|
|
io.on('connection', function(socket){
|
2017-06-13 04:09:39 +02:00
|
|
|
console.log('a user connected via sockets');
|
2017-06-13 21:12:41 +02:00
|
|
|
// create visitor record
|
|
|
|
var visitor = ua(googleAnalyticsId, {https: true});
|
2017-06-03 05:56:33 +02:00
|
|
|
// attach upload listeners
|
2017-06-01 04:48:09 +02:00
|
|
|
var uploader = new siofu();
|
2017-06-15 20:15:13 +02:00
|
|
|
uploader.dir = hostedContentPath; //path.join(__dirname, '../../Uploads');
|
2017-06-01 04:48:09 +02:00
|
|
|
uploader.listen(socket);
|
2017-06-14 21:53:55 +02:00
|
|
|
uploader.on("start", function(event){
|
2017-06-15 20:15:13 +02:00
|
|
|
// server side test to make sure file is not a bad file type
|
2017-06-14 21:53:55 +02:00
|
|
|
if (/\.exe$/.test(event.file.name)) {
|
|
|
|
uploader.abort(event.file.id, socket);
|
|
|
|
}
|
|
|
|
});
|
2017-06-01 04:48:09 +02:00
|
|
|
uploader.on("error", function(event){
|
|
|
|
console.log("an error occured while uploading", event.error);
|
|
|
|
socket.emit("publish-status", event.error)
|
2017-06-14 21:53:55 +02:00
|
|
|
});
|
2017-06-01 04:48:09 +02:00
|
|
|
uploader.on("saved", function(event){
|
2017-06-15 20:15:13 +02:00
|
|
|
console.log("saved ", event.file.name, "deets:", event.file);
|
2017-06-01 04:48:09 +02:00
|
|
|
if (event.file.success){
|
|
|
|
socket.emit("publish-status", "file upload successfully completed");
|
2017-06-15 20:15:13 +02:00
|
|
|
socketHelpers.publish(event.file.meta.name, event.file.pathName, event.file.meta.type, event.file.meta.license, event.file.meta.nsfw, socket, visitor)
|
2017-06-01 01:44:45 +02:00
|
|
|
} else {
|
2017-06-03 09:41:02 +02:00
|
|
|
socket.emit("publish-failure", "file uploaded, but with errors")
|
2017-06-01 04:48:09 +02:00
|
|
|
};
|
2017-06-01 01:44:45 +02:00
|
|
|
});
|
2017-05-25 07:50:02 +02:00
|
|
|
// handle disconnect
|
|
|
|
socket.on('disconnect', function(){
|
|
|
|
console.log('user disconnected');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return http;
|
|
|
|
}
|