spee.ch/routes/sockets-routes.js

38 lines
1.3 KiB
JavaScript
Raw Normal View History

2017-06-13 21:12:41 +02:00
module.exports = function(app, path, siofu, socketHelpers, ua, googleAnalyticsId) {
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-01 05:29:16 +02:00
uploader.dir = path.join(__dirname, '../../Uploads');
2017-06-01 04:48:09 +02:00
uploader.listen(socket);
uploader.on("start", function(event){
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-01 04:48:09 +02:00
uploader.on("saved", function(event){
console.log("saved " + event.file.name);
if (event.file.success){
socket.emit("publish-status", "file upload successfully completed");
2017-06-13 21:12:41 +02:00
socketHelpers.publish(event.file.meta.name, event.file.pathName, 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;
}