spee.ch/routes/sockets-routes.js

42 lines
1.5 KiB
JavaScript
Raw Normal View History

var publishController = require('../controllers/publishController.js');
module.exports = function(app, siofu, hostedContentPath, 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();
uploader.dir = hostedContentPath;
2017-06-01 04:48:09 +02:00
uploader.listen(socket);
uploader.on("start", function(event){
// server side test to make sure file is not a bad file type
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){
2017-06-17 04:31:31 +02:00
console.log("uploaded ", event.file.name);
2017-06-01 04:48:09 +02:00
if (event.file.success){
socket.emit("publish-status", "file upload successfully completed");
2017-06-17 04:31:31 +02:00
publishController.publish(event.file.meta.name, event.file.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-17 04:31:31 +02:00
// to-do: remove the file
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;
}