Esempio n. 1
0
 private static Task UploadFile(UploadCallbackHandler uploadHandler, string userName, string connectionId, string roomName, string fileName, string contentType, Stream value)
 {
     return(uploadHandler.Upload(userName,
                                 connectionId,
                                 roomName,
                                 fileName,
                                 contentType,
                                 value));
 }
Esempio n. 2
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)
                    };

                    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);
            };
        }
Esempio n. 3
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);
            };
        }
Esempio n. 4
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);
            };
        }