spee.ch/routes/sockets-routes.js

38 lines
1.3 KiB
JavaScript
Raw Normal View History

2017-06-13 12:12:41 -07:00
module.exports = function(app, path, siofu, socketHelpers, ua, googleAnalyticsId) {
var http = require('http').Server(app);
var io = require('socket.io')(http);
2017-06-12 19:09:39 -07:00
2017-05-24 22:50:02 -07:00
io.on('connection', function(socket){
2017-06-12 19:09:39 -07:00
console.log('a user connected via sockets');
2017-06-13 12:12:41 -07:00
// create visitor record
var visitor = ua(googleAnalyticsId, {https: true});
2017-06-02 20:56:33 -07:00
// attach upload listeners
2017-05-31 19:48:09 -07:00
var uploader = new siofu();
2017-05-31 20:29:16 -07:00
uploader.dir = path.join(__dirname, '../../Uploads');
2017-05-31 19:48:09 -07:00
uploader.listen(socket);
uploader.on("start", function(event){
if (/\.exe$/.test(event.file.name)) {
uploader.abort(event.file.id, socket);
}
});
2017-05-31 19:48:09 -07:00
uploader.on("error", function(event){
console.log("an error occured while uploading", event.error);
socket.emit("publish-status", event.error)
});
2017-05-31 19:48:09 -07: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 12:12:41 -07:00
socketHelpers.publish(event.file.meta.name, event.file.pathName, event.file.meta.license, event.file.meta.nsfw, socket, visitor)
2017-05-31 16:44:45 -07:00
} else {
2017-06-03 00:41:02 -07:00
socket.emit("publish-failure", "file uploaded, but with errors")
2017-05-31 19:48:09 -07:00
};
2017-05-31 16:44:45 -07:00
});
2017-05-24 22:50:02 -07:00
// handle disconnect
socket.on('disconnect', function(){
console.log('user disconnected');
});
});
return http;
}