<commit_before>﻿import redis = require("redis");
import express = require("express");
import http = require("http");
import socketio = require("socket.io");
import fs = require("fs");
import path = require("path");

//Vorlon
import iwsc = require("./vorlon.IWebServerComponent");
import tools = require("./vorlon.tools");
import vorloncontext = require("../config/vorlon.servercontext");

export module VORLON {
    export class Server implements iwsc.VORLON.IWebServerComponent {
        private _sessions: vorloncontext.VORLON.SessionManager;
        public dashboards = new Array<SocketIO.Socket>();

        private _io: any;
        private _log: vorloncontext.VORLON.ILogger;
        private httpConfig: vorloncontext.VORLON.IHttpConfig;
        private pluginsConfig: vorloncontext.VORLON.IPluginsProvider;
        private baseURLConfig: vorloncontext.VORLON.IBaseURLConfig;

        constructor(context: vorloncontext.VORLON.IVorlonServerContext) {
            this.baseURLConfig = context.baseURLConfig;
            this.httpConfig = context.httpConfig;
            this.pluginsConfig = context.plugins;
            this._log = context.logger;
            this._sessions = context.sessions;
        }
        
        public noCache(res:any){
            //Add header no-cache
            res.header('Cache-Control', 'private, no-cache, no-store, must-revalidate');
            res.header('Expires', '-1');
            res.header('Pragma', 'no-cache');
        }

        public addRoutes(app: express.Express, passport: any): void {
            app.get(this.baseURLConfig.baseURL + "/api/createsession", (req: any, res: any) => {
                this.json(res, this.guid());
            });

            app.get(this.baseURLConfig.baseURL + "/api/reset/:idSession", (req: any, res: any) => {
                var session = this._sessions.get(req.params.idSession);
                if (session && session.connectedClients) {
                    for (var client in session.connectedClients) {
                        delete session.connectedClients[client];
                    }
                }
                this._sessions.remove(req.params.idSession);
                
                this.noCache(res);
                res.writeHead(200, {});
                res.end(); 
            });

            app.get(this.baseURLConfig.baseURL + "/api/getclients/:idSession", (req: any, res: any) => {
                var session = this._sessions.get(req.params.idSession);
                var clients = new Array();
                if (session != null) {
                    var nbClients = 0;
                    for (var client in session.connectedClients) {
                        var currentclient = session.connectedClients[client];
                        if (currentclient.opened) {
                            clients.push(currentclient.data);
                            nbClients++;
                        }
                    }
                    this._sessions.update(req.params.idSession, session);
                    this._log.debug("API : GetClients nb client " + nbClients + " in session " + req.params.idSession, { type: "API", session: req.params.idSession });
                }
                else {
                    this._log.warn("API : No client in session " + req.params.idSession, { type: "API", session: req.params.idSession });
                }
               
                this.noCache(res);
                this.json(res, clients);
            });

            app.get(this.baseURLConfig.baseURL + "/vorlon.max.js/", (req: any, res: any) => {
                res.redirect("/vorlon.max.js/default");
            });

            app.get(this.baseURLConfig.baseURL + "/vorlon.max.js/:idsession", (req: any, res: any) => {
                this._sendVorlonJSFile(false, req, res);
            });

            app.get(this.baseURLConfig.baseURL + "/vorlon.js", (req: any, res: any) => {
                res.redirect(this.baseURLConfig.baseURL + "/vorlon.js/default");
            });

            app.get(this.baseURLConfig.baseURL + "/vorlon.js/:idsession", (req: any, res: any) => {
                this._sendVorlonJSFile(true, req, res);
            });

            app.get(this.baseURLConfig.baseURL + "/vorlon.max.autostartdisabled.js", (req: any, res: any) => {
                this._sendVorlonJSFile(false, req, res, false);
            });

            app.get(this.baseURLConfig.baseURL + "/vorlon.autostartdisabled.js", (req: any, res: any) => {
                this._sendVorlonJSFile(true, req, res, false);
            });

            app.get(this.baseURLConfig.baseURL + "/getplugins/:idsession", (req: any, res: any) => {
                this._sendConfigJson(req, res);
            });
            
            app.post(this.baseURLConfig.baseURL + "/setplugin/:pluginid/name", (req: any, res: any) => {
                this.setPluginName(req, res);
            });
            
            app.post(this.baseURLConfig.baseURL + "/setplugin/positions", (req: any, res: any) => {
                this.setPluginsPosition(req, res);
            });
            
            app.get(this.baseURLConfig.baseURL + "/setplugin/:pluginid/state", (req: any, res: any) => {
                this.setPluginState(req, res);
            });
            
            app.post(this.baseURLConfig.baseURL + "/setplugin/:pluginid/panel", (req: any, res: any) => {
                this.setPluginPanel(req, res);
            });
            
            app.get(this.baseURLConfig.baseURL + "/vorlon.node.max.js/", (req: any, res: any) => {
                res.redirect("/vorlon.node.max.js/default");
            });
            
            app.get(this.baseURLConfig.baseURL + "/vorlon.node.max.js/:idsession", (req: any, res: any) => {
                this._sendVorlonJSFile(false, req, res, false, true);
            });
            
            app.get(this.baseURLConfig.baseURL + "/vorlon.node.js/", (req: any, res: any) => {
                res.redirect("/vorlon.node.js/default");
            });

            app.get(this.baseURLConfig.baseURL + "/vorlon.node.js/:idsession", (req: any, res: any) => {
                this._sendVorlonJSFile(true, req, res, false, true);
            });
        }

        private setPluginState(req: any, res: any) {
            var pluginid = req.params.pluginid;
            this.pluginsConfig.setPluginState(pluginid, (err) => {
                if (err) {
                    this._log.error("SET_PLUGIN_STATE : " + err);
                    res.header('Content-Type', 'application/json');
                    return res.send({'error': true});
                }

                res.header('Content-Type', 'application/json');
                return res.send({'error': false});
            });
        }

        private setPluginsPosition(req: any, res: any) {
            var positions = req.body.positions;
            this.pluginsConfig.setPluginsPosition(positions, (err) => {
                if (err) {
                    this._log.error("SET_PLUGINS_POSITION : " + err);
                    res.header('Content-Type', 'application/json');
                    return res.send({'error': true});
                }

                res.header('Content-Type', 'application/json');
                return res.send({'error': false});
            });<commit_msg>Icon support plugins list API<commit_after>﻿import redis = require("redis");
import express = require("express");
import http = require("http");
import socketio = require("socket.io");
import fs = require("fs");
import path = require("path");

//Vorlon
import iwsc = require("./vorlon.IWebServerComponent");
import tools = require("./vorlon.tools");
import vorloncontext = require("../config/vorlon.servercontext");

export module VORLON {
    export class Server implements iwsc.VORLON.IWebServerComponent {
        private _sessions: vorloncontext.VORLON.SessionManager;
        public dashboards = new Array<SocketIO.Socket>();

        private _io: any;
        private _log: vorloncontext.VORLON.ILogger;
        private httpConfig: vorloncontext.VORLON.IHttpConfig;
        private pluginsConfig: vorloncontext.VORLON.IPluginsProvider;
        private baseURLConfig: vorloncontext.VORLON.IBaseURLConfig;

        constructor(context: vorloncontext.VORLON.IVorlonServerContext) {
            this.baseURLConfig = context.baseURLConfig;
            this.httpConfig = context.httpConfig;
            this.pluginsConfig = context.plugins;
            this._log = context.logger;
            this._sessions = context.sessions;
        }
        
        public noCache(res:any){
            //Add header no-cache
            res.header('Cache-Control', 'private, no-cache, no-store, must-revalidate');
            res.header('Expires', '-1');
            res.header('Pragma', 'no-cache');
        }

        public addRoutes(app: express.Express, passport: any): void {
            app.get(this.baseURLConfig.baseURL + "/api/createsession", (req: any, res: any) => {
                this.json(res, this.guid());
            });

            app.get(this.baseURLConfig.baseURL + "/api/reset/:idSession", (req: any, res: any) => {
                var session = this._sessions.get(req.params.idSession);
                if (session && session.connectedClients) {
                    for (var client in session.connectedClients) {
                        delete session.connectedClients[client];
                    }
                }
                this._sessions.remove(req.params.idSession);
                
                this.noCache(res);
                res.writeHead(200, {});
                res.end(); 
            });

            app.get(this.baseURLConfig.baseURL + "/api/getclients/:idSession", (req: any, res: any) => {
                var session = this._sessions.get(req.params.idSession);
                var clients = new Array();
                if (session != null) {
                    var nbClients = 0;
                    for (var client in session.connectedClients) {
                        var currentclient = session.connectedClients[client];
                        if (currentclient.opened) {
                            clients.push(currentclient.data);
                            nbClients++;
                        }
                    }
                    this._sessions.update(req.params.idSession, session);
                    this._log.debug("API : GetClients nb client " + nbClients + " in session " + req.params.idSession, { type: "API", session: req.params.idSession });
                }
                else {
                    this._log.warn("API : No client in session " + req.params.idSession, { type: "API", session: req.params.idSession });
                }
               
                this.noCache(res);
                this.json(res, clients);
            });

            app.get(this.baseURLConfig.baseURL + "/vorlon.max.js/", (req: any, res: any) => {
                res.redirect("/vorlon.max.js/default");
            });

            app.get(this.baseURLConfig.baseURL + "/vorlon.max.js/:idsession", (req: any, res: any) => {
                this._sendVorlonJSFile(false, req, res);
            });

            app.get(this.baseURLConfig.baseURL + "/vorlon.js", (req: any, res: any) => {
                res.redirect(this.baseURLConfig.baseURL + "/vorlon.js/default");
            });

            app.get(this.baseURLConfig.baseURL + "/vorlon.js/:idsession", (req: any, res: any) => {
                this._sendVorlonJSFile(true, req, res);
            });

            app.get(this.baseURLConfig.baseURL + "/vorlon.max.autostartdisabled.js", (req: any, res: any) => {
                this._sendVorlonJSFile(false, req, res, false);
            });

            app.get(this.baseURLConfig.baseURL + "/vorlon.autostartdisabled.js", (req: any, res: any) => {
                this._sendVorlonJSFile(true, req, res, false);
            });

            app.get(this.baseURLConfig.baseURL + "/getplugins/:idsession", (req: any, res: any) => {
                this._sendConfigJson(req, res);
            });
            
            app.post(this.baseURLConfig.baseURL + "/setplugin/:pluginid/name", (req: any, res: any) => {
                this.setPluginName(req, res);
            });
            
            app.post(this.baseURLConfig.baseURL + "/setplugin/positions", (req: any, res: any) => {
                this.setPluginsPosition(req, res);
            });
            
            app.get(this.baseURLConfig.baseURL + "/setplugin/:pluginid/state", (req: any, res: any) => {
                this.setPluginState(req, res);
            });
            
            app.post(this.baseURLConfig.baseURL + "/setplugin/:pluginid/panel", (req: any, res: any) => {
                this.setPluginPanel(req, res);
            });
            
            app.get(this.baseURLConfig.baseURL + "/getplugin/:pluginfolder/icon", (req: any, res: any) => {
                this.sendPluginIcon(req, res);
            });
            
            app.get(this.baseURLConfig.baseURL + "/vorlon.node.max.js/", (req: any, res: any) => {
                res.redirect("/vorlon.node.max.js/default");
            });
            
            app.get(this.baseURLConfig.baseURL + "/vorlon.node.max.js/:idsession", (req: any, res: any) => {
                this._sendVorlonJSFile(false, req, res, false, true);
            });
            
            app.get(this.baseURLConfig.baseURL + "/vorlon.node.js/", (req: any, res: any) => {
                res.redirect("/vorlon.node.js/default");
            });

            app.get(this.baseURLConfig.baseURL + "/vorlon.node.js/:idsession", (req: any, res: any) => {
                this._sendVorlonJSFile(true, req, res, false, true);
            });
        }

        private sendPluginIcon(req: any, res: any) {
            var pluginfolder = req.params.pluginfolder;
            try {
                var icon = fs.readFileSync(path.join(__dirname, "../public/vorlon/plugins/" + pluginfolder + "/icon.png"));
                res.writeHead(200, {'Content-Type': 'image/png' });
                res.end(icon, 'binary');
            } catch (err) {
                res.writeHead(404);
                res.end();
            }    
        }

        private setPluginState(req: any, res: any) {
            var pluginid = req.params.pluginid;
            this.pluginsConfig.setPluginState(pluginid, (err) => {
                if (err) {
                    this._log.error("SET_PLUGIN_STATE : " + err);
                    res.header('Content-Type', 'application/json');
                    return res.send({'error': true});
                }

                res.header('Content-Type', 'application/json');
                return res.send({'error': false});
            });
        }

        private setPluginsPosition(req: any, res: any) {
            var positions = req.body.positions;
            this.pluginsConfig.setPluginsPosition(positions, (err) => {
                if (err) {
                    this._log.error("SET_PLUGINS_POSITION : " + err);
                    res.header('Content-Type', 'application/json');
                    return res.send({'error': true});
                }

                res.header('Content-Type', 'application/json');
                return res.send({'error': false});
            });
{'fraction_non_alphanumeric': 0.13881664499349805, 'fraction_numerical': 0.0010403120936280884, 'mean_word_length': 1.681952920662598, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 46, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4868208', 'n_tokens_mistral': 4426, 'n_tokens_neox': 4273, 'n_words': 750}