Example #1
0
        public HomeModule(UploadCallbackHandler uploadHandler)
        {
            Get["/"] = _ =>
            {
                if (Context.CurrentUser != null)
                {
                    var viewModel = new SettingsViewModel
                    {
                        GoogleAnalytics = ConfigurationManager.AppSettings["jabbr:googleAnalytics"],
                        Sha = ConfigurationManager.AppSettings["jabbr:releaseSha"],
                        Branch = ConfigurationManager.AppSettings["jabbr:releaseBranch"],
                        Time = ConfigurationManager.AppSettings["jabbr:releaseTime"],
                        DebugMode = (bool)Context.Items["_debugMode"],
                        Version = Constants.JabbRVersion
                    };

                    return View["index", viewModel];
                }

                return Response.AsRedirect(String.Format("~/account/login?returnUrl={0}", HttpUtility.UrlEncode(Request.Path)));
            };

            Post["/upload"] = _ =>
            {
                if (Context.CurrentUser == null)
                {
                    return 403;
                }

                string roomName = Request.Form.room;
                string connectionId = Request.Form.connectionId;
                HttpFile file = Request.Files.First();

                // This blocks since we're not using nancy's async support yet
                uploadHandler.Upload(Context.CurrentUser.UserName,
                                     connectionId,
                                     roomName,
                                     file.Name,
                                     file.ContentType,
                                     file.Value).Wait();

                return 200;
            };
        }
Example #2
0
        public HomeModule()
        {
            Get["/"] = _ =>
            {
                if (Context.CurrentUser != null)
                {
                    var viewModel = new SettingsViewModel
                    {
                        GoogleAnalytics = ConfigurationManager.AppSettings["jabbr:googleAnalytics"],
                        Sha = ConfigurationManager.AppSettings["jabbr:releaseSha"],
                        Branch = ConfigurationManager.AppSettings["jabbr:releaseBranch"],
                        Time = ConfigurationManager.AppSettings["jabbr:releaseTime"]
                    };

                    return View["index", viewModel];
                }

                return Response.AsRedirect(String.Format("~/account/login?returnUrl={0}", HttpUtility.UrlEncode(Request.Path)));
            };
        }
Example #3
0
        public HomeModule(IAuthenticationTokenService authService)
        {
            Get["/"] = _ =>
            {
                if (Context.CurrentUser != null)
                {
                    var viewModel = new SettingsViewModel
                    {
                        GoogleAnalytics = ConfigurationManager.AppSettings["googleAnalytics"],
                        Sha = ConfigurationManager.AppSettings["googleAnalytics"],
                        Branch = ConfigurationManager.AppSettings["releaseBranch"],
                        Time = ConfigurationManager.AppSettings["releaseTime"]
                    };

                    return View["index", viewModel];
                }

                return Response.AsRedirect("~/account/login");
            };
        }
Example #4
0
        public HomeModule(UploadCallbackHandler uploadHandler)
        {
            Get["/"] = _ =>
            {
                if (IsAuthenticated)
                {
                    var viewModel = new SettingsViewModel
                    {
                        GoogleAnalytics = ConfigurationManager.AppSettings["jabbr:googleAnalytics"],
                        Sha = ConfigurationManager.AppSettings["jabbr:releaseSha"],
                        Branch = ConfigurationManager.AppSettings["jabbr:releaseBranch"],
                        Time = ConfigurationManager.AppSettings["jabbr:releaseTime"],
                        DebugMode = (bool)Context.Items["_debugMode"],
                        Version = Constants.JabbRVersion
                    };

                    return View["index", viewModel];
                }

                if (Principal.HasPartialIdentity())
                {
                    // If the user is partially authenticated then take them to the register page
                    return Response.AsRedirect("~/account/register");
                }

                return HttpStatusCode.Unauthorized;
            };

            Get["/monitor"] = _ =>
            {
                ClaimsPrincipal principal = Principal;

                if (principal == null ||
                    !principal.HasClaim(JabbRClaimTypes.Admin))
                {
                    return 403;
                }

                return View["monitor"];
            };

            Post["/upload"] = _ =>
            {
                if (!IsAuthenticated)
                {
                    return 403;
                }

                string roomName = Request.Form.room;
                string connectionId = Request.Form.connectionId;
                HttpFile file = Request.Files.First();

                // This blocks since we're not using nancy's async support yet
                UploadFile(
                    uploadHandler,
                    Principal.GetUserId(),
                    connectionId,
                    roomName,
                    file.Name,
                    file.ContentType,
                    file.Value).Wait();

                return 200;
            };

            Post["/upload-clipboard"] = _ =>
                {
                    if (!IsAuthenticated)
                    {
                        return 403;
                    }

                    string roomName = Request.Form.room;
                    string connectionId = Request.Form.connectionId;
                    string file = Request.Form.file;
                    string fileName = "clipboard_" + Guid.NewGuid().ToString("N");
                    string contentType = "image/jpeg";

                    var info = Regex.Match(file, @"data:image/(?<type>.+?);base64,(?<data>.+)");

                    var binData = Convert.FromBase64String(info.Groups["data"].Value);
                    contentType = info.Groups["type"].Value;

                    fileName = fileName + "." + contentType.Substring(contentType.IndexOf("/") + 1);

                    UploadFile(
                        uploadHandler,
                        Principal.GetUserId(),
                        connectionId,
                        roomName,
                        fileName,
                        contentType,
                        new MemoryStream(binData)).Wait();

                    return 200;
                };
        }
Example #5
0
        public HomeModule(ApplicationSettings settings,
                          IJabbrConfiguration configuration,
                          IConnectionManager connectionManager,
                          IJabbrRepository jabbrRepository)
        {
            Get["/"] = _ =>
            {
                if (IsAuthenticated)
                {
                    var viewModel = new SettingsViewModel
                    {
                        GoogleAnalytics = settings.GoogleAnalytics,
                        Sha = configuration.DeploymentSha,
                        Branch = configuration.DeploymentBranch,
                        Time = configuration.DeploymentTime,
                        DebugMode = (bool)Context.Items["_debugMode"],
                        Version = Constants.JabbRVersion,
                        IsAdmin = Principal.HasClaim(JabbRClaimTypes.Admin),
                        ClientLanguageResources = BuildClientResources(),
                        MaxMessageLength = settings.MaxMessageLength
                    };

                    return View["index", viewModel];
                }

                if (Principal != null && Principal.HasPartialIdentity())
                {
                    // If the user is partially authenticated then take them to the register page
                    return Response.AsRedirect("~/account/register");
                }

                return HttpStatusCode.Unauthorized;
            };

            Get["/monitor"] = _ =>
            {
                ClaimsPrincipal principal = Principal;

                if (principal == null ||
                    !principal.HasClaim(JabbRClaimTypes.Admin))
                {
                    return HttpStatusCode.Forbidden;
                }

                return View["monitor"];
            };

            Get["/status", runAsync: true] = async (_, token) =>
            {
                var model = new StatusViewModel();

                // Try to send a message via SignalR
                // NOTE: Ideally we'd like to actually receive a message that we send, but right now
                // that would require a full client instance. SignalR 2.1.0 plans to add a feature to
                // easily support this on the server.
                var signalrStatus = new SystemStatus { SystemName = "SignalR messaging" };
                model.Systems.Add(signalrStatus);

                try
                {
                    var hubContext = connectionManager.GetHubContext<Chat>();
                    await (Task)hubContext.Clients.Client("doesn't exist").noMethodCalledThis();
                    
                    signalrStatus.SetOK();
                }
                catch (Exception ex)
                {
                    signalrStatus.SetException(ex.GetBaseException());
                }

                // Try to talk to database
                var dbStatus = new SystemStatus { SystemName = "Database" };
                model.Systems.Add(dbStatus);

                try
                {
                    var roomCount = jabbrRepository.Rooms.Count();
                    dbStatus.SetOK();
                }
                catch (Exception ex)
                {
                    dbStatus.SetException(ex.GetBaseException());
                }

                // Try to talk to azure storage
                var azureStorageStatus = new SystemStatus { SystemName = "Azure Upload storage" };
                model.Systems.Add(azureStorageStatus);

                try
                {
                    if (!String.IsNullOrEmpty(settings.AzureblobStorageConnectionString))
                    {
                        var azure = new AzureBlobStorageHandler(settings);
                        UploadResult result;
                        using (var stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes("test")))
                        {
                            result = await azure.UploadFile("statusCheck.txt", "text/plain", stream);
                        }

                        azureStorageStatus.SetOK();
                    }
                    else
                    {
                        azureStorageStatus.StatusMessage = "Not configured";
                    }
                }
                catch (Exception ex)
                {
                    azureStorageStatus.SetException(ex.GetBaseException());
                }

                //try to talk to local storage
                var localStorageStatus = new SystemStatus { SystemName = "Local Upload storage" };
                model.Systems.Add(localStorageStatus);

                try
                {
                    if (!String.IsNullOrEmpty(settings.LocalFileSystemStoragePath) && !String.IsNullOrEmpty(settings.LocalFileSystemStorageUriPrefix))
                    {
                        var local = new LocalFileSystemStorageHandler(settings);
                        UploadResult localResult;
                        using (var stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes("test")))
                        {
                            localResult = await local.UploadFile("statusCheck.txt", "text/plain", stream);
                        }

                        localStorageStatus.SetOK();
                    }
                    else
                    {
                        localStorageStatus.StatusMessage = "Not configured";
                    }
                }
                catch (Exception ex)
                {
                    localStorageStatus.SetException(ex.GetBaseException());
                }

                // Force failure
                if (Context.Request.Query.fail)
                {
                    var failedSystem = new SystemStatus { SystemName = "Forced failure" };
                    failedSystem.SetException(new ApplicationException("Forced failure for test purposes"));
                    model.Systems.Add(failedSystem);
                }

                var view = View["status", model];

                if (!model.AllOK)
                {
                    return view.WithStatusCode(HttpStatusCode.InternalServerError);
                }

                return view;
            };
        }
Example #6
0
        public HomeModule(ApplicationSettings settings,
                          IJabbrConfiguration configuration,
                          UploadCallbackHandler uploadHandler,
                          IConnectionManager connectionManager,
                          IJabbrRepository jabbrRepository)
        {
            Get["/"] = _ =>
            {
                if (IsAuthenticated)
                {
                    var viewModel = new SettingsViewModel
                    {
                        GoogleAnalytics = settings.GoogleAnalytics,
                        Sha = configuration.DeploymentSha,
                        Branch = configuration.DeploymentBranch,
                        Time = configuration.DeploymentTime,
                        DebugMode = (bool)Context.Items["_debugMode"],
                        Version = Constants.JabbRVersion,
                        IsAdmin = Principal.HasClaim(JabbRClaimTypes.Admin),
                        ClientLanguageResources = BuildClientResources()
                    };

                    return View["index", viewModel];
                }

                if (Principal != null && Principal.HasPartialIdentity())
                {
                    // If the user is partially authenticated then take them to the register page
                    return Response.AsRedirect("~/account/register");
                }

                return HttpStatusCode.Unauthorized;
            };

            Get["/monitor"] = _ =>
            {
                ClaimsPrincipal principal = Principal;

                if (principal == null ||
                    !principal.HasClaim(JabbRClaimTypes.Admin))
                {
                    return HttpStatusCode.Forbidden;
                }

                return View["monitor"];
            };

            Get["/status"] = _ =>
            {
                var model = new StatusViewModel();

                // Try to send a message via SignalR
                // NOTE: Ideally we'd like to actually receive a message that we send, but right now
                // that would require a full client instance. SignalR 2.1.0 plans to add a feature to
                // easily support this on the server.
                var signalrStatus = new SystemStatus { SystemName = "SignalR messaging" };
                model.Systems.Add(signalrStatus);

                try
                {
                    var hubContext = connectionManager.GetHubContext<Chat>();
                    var sendTask = (Task)hubContext.Clients.Client("doesn't exist").noMethodCalledThis();
                    sendTask.Wait();

                    signalrStatus.SetOK();
                }
                catch (Exception ex)
                {
                    signalrStatus.SetException(ex.GetBaseException());
                }

                // Try to talk to database
                var dbStatus = new SystemStatus { SystemName = "Database" };
                model.Systems.Add(dbStatus);

                try
                {
                    var roomCount = jabbrRepository.Rooms.Count();
                    dbStatus.SetOK();
                }
                catch (Exception ex)
                {
                    dbStatus.SetException(ex.GetBaseException());
                }

                // Try to talk to storage
                var azureStorageStatus = new SystemStatus { SystemName = "Upload storage" };
                model.Systems.Add(azureStorageStatus);

                try
                {
                    if (!String.IsNullOrEmpty(settings.AzureblobStorageConnectionString))
                    {
                        var azure = new AzureBlobStorageHandler(settings);
                        UploadResult result;
                        using (var stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes("test")))
                        {
                            result = azure.UploadFile("statusCheck.txt", "text/plain", stream)
                                          .Result;
                        }
                        azureStorageStatus.SetOK();
                    }
                    else
                    {
                        azureStorageStatus.StatusMessage = "Not configured";
                    }
                }
                catch (Exception ex)
                {
                    azureStorageStatus.SetException(ex.GetBaseException());
                }

                // Force failure
                if (Context.Request.Query.fail)
                {
                    var failedSystem = new SystemStatus { SystemName = "Forced failure" };
                    failedSystem.SetException(new ApplicationException("Forced failure for test purposes"));
                    model.Systems.Add(failedSystem);
                }

                var view = View["status", model];

                if (!model.AllOK)
                {
                    return view.WithStatusCode(HttpStatusCode.InternalServerError);
                }

                return view;
            };

            Post["/upload-file"] = _ =>
                {
                    if (!IsAuthenticated)
                    {
                        return 403;
                    }

                    string roomName = Request.Form.room;
                    string connectionId = Request.Form.connectionId;
                    string file = Request.Form.file;
                    //string fileName = "clipboard_" + Guid.NewGuid().ToString("N");
                    string fileName = Request.Form.filename;
                    string contentType = Request.Form.type;
                    byte[] binData = null;

                    var info = Regex.Match(file, @"data:(?:(?<unkown>.+?)/(?<type>.+?))?;base64,(?<data>.+)");

                    binData = Convert.FromBase64String(info.Groups["data"].Value);
                    contentType = info.Groups["type"].Value;

                    if (String.IsNullOrWhiteSpace(contentType))
                    {
                        contentType = "application/octet-stream";
                    }

                    UploadFile(
                        uploadHandler,
                        Principal.GetUserId(),
                        connectionId,
                        roomName,
                        fileName,
                        contentType,
                        new MemoryStream(binData)).Wait();

                    return 200;
                };
        }
Example #7
0
        public HomeModule(ApplicationSettings settings,
                          IJabbrConfiguration configuration,
                          UploadCallbackHandler uploadHandler)
        {
            Get["/"] = _ =>
            {
                if (IsAuthenticated)
                {
                    var viewModel = new SettingsViewModel
                    {
                        GoogleAnalytics = settings.GoogleAnalytics,
                        Sha = configuration.DeploymentSha,
                        Branch = configuration.DeploymentBranch,
                        Time = configuration.DeploymentTime,
                        DebugMode = (bool)Context.Items["_debugMode"],
                        Version = Constants.JabbRVersion,
                        IsAdmin = Principal.HasClaim(JabbRClaimTypes.Admin),
                        ClientLanguageResources = BuildClientResources()
                    };

                    return View["index", viewModel];
                }

                if (Principal.HasPartialIdentity())
                {
                    // If the user is partially authenticated then take them to the register page
                    return Response.AsRedirect("~/account/register");
                }

                return HttpStatusCode.Unauthorized;
            };

            Get["/monitor"] = _ =>
            {
                ClaimsPrincipal principal = Principal;

                if (principal == null ||
                    !principal.HasClaim(JabbRClaimTypes.Admin))
                {
                    return 403;
                }

                return View["monitor"];
            };

            Post["/upload-file"] = _ =>
                {
                    if (!IsAuthenticated)
                    {
                        return 403;
                    }

                    string roomName = Request.Form.room;
                    string connectionId = Request.Form.connectionId;
                    string file = Request.Form.file;
                    //string fileName = "clipboard_" + Guid.NewGuid().ToString("N");
                    string fileName = Request.Form.filename;
                    string contentType = Request.Form.type;
                    byte[] binData = null;

                    var info = Regex.Match(file, @"data:(?:(?<unkown>.+?)/(?<type>.+?))?;base64,(?<data>.+)");

                    binData = Convert.FromBase64String(info.Groups["data"].Value);
                    contentType = info.Groups["type"].Value;

                    if (String.IsNullOrWhiteSpace(contentType))
                    {
                        contentType = "application/octet-stream";
                    }

                    UploadFile(
                        uploadHandler,
                        Principal.GetUserId(),
                        connectionId,
                        roomName,
                        fileName,
                        contentType,
                        new MemoryStream(binData)).Wait();

                    return 200;
                };
        }