Exemplo n.º 1
0
        private static async Task MyVideosGet(HttpContext context)
        {
            CommonController.SetHTMLContentType(context);

            var userTask = UserSessions.GetLoggedInUser(context);

            using var connection = Database.OpenNewConnection();
            var user = await userTask;

            if (user != null)
            {
                const int countPerPage = 20;
                int       page         = 1;
                if (context.Request.Query.ContainsKey("page"))
                {
                    Int32.TryParse(context.Request.Query["page"], out page);
                }

                int offset = (page - 1) * countPerPage;

                var numVideos = await NumVideosForUser(user.userid, connection);

                var VideosTask = VideosForUser(user.userid, countPerPage, offset, connection);

                var pagination = new Pagination(numVideos, countPerPage, offset);

                var templateContext = new TemplateContext(new { videos = await VideosTask, pagination });

                await context.Response.WriteAsync(await HTMLRenderer.Render(context, "Templates\\myVideos.liquid", templateContext));
            }
            else
            {
                await CommonController.Write404(context);
            }
        }
Exemplo n.º 2
0
        private static async Task VideoGet(HttpContext context)
        {
            CommonController.SetHTMLContentType(context);

            if (GuidHelpers.TryDecode(context.Request.Query["id"], out var videoId))
            {
                using var connection = Database.OpenNewConnection();
                var video = await GetVideo(videoId, connection);

                var user = await UserSessions.GetLoggedInUser(context);

                if (video != null && UserCanViewVideo(video, user))
                {
                    bool userOwnsVideo = UserOwnsVideo(video, user.userid);
                    var  relatedVideos = new List <Video>();

                    var templateContext = new TemplateContext(new { video, relatedVideos, userOwnsVideo });
                    await context.Response.WriteAsync(await HTMLRenderer.Render(context, "Templates\\video.liquid", templateContext));
                    await AddVideoView(video.id, context, connection);
                }
                else
                {
                    await CommonController.Write404(context);
                }
            }
            else
            {
                await CommonController.Write404(context);
            }
        }
Exemplo n.º 3
0
        private static async Task EditVideoGet(HttpContext context)
        {
            CommonController.SetHTMLContentType(context);

            var userTask = UserSessions.GetLoggedInUser(context);

            using var connection = Database.OpenNewConnection();
            var user = await userTask;

            if (user != null && GuidHelpers.TryDecode(context.Request.Query["id"], out var videoId))
            {
                var video = await GetVideo(videoId, connection);

                if (UserOwnsVideo(video, user.userid))
                {
                    var templateContext = new TemplateContext(new { video });
                    await context.Response.WriteAsync(await HTMLRenderer.Render(context, "Templates\\editVideo.liquid", templateContext));
                }
                else
                {
                    await CommonController.Write404(context);
                }
            }
            else
            {
                await CommonController.Write404(context);
            }
        }
Exemplo n.º 4
0
        private static async Task SearchPost(HttpContext context)
        {
            CommonController.SetHTMLContentType(context);

            var searchQuery = context.Request.Query["q"].ToString();

            if (!String.IsNullOrEmpty(searchQuery))
            {
                using var connection = Database.OpenNewConnection();

                var normalizedQuery = searchQuery.NormalizeForSearch();

                var channels = await FindUsersFuzzy(normalizedQuery, 3, connection);

                var videos = await FindVideosFuzzy(normalizedQuery, 20, connection);

                bool hasResults = channels.Any() || videos.Any();

                var templateContext = new TemplateContext(new { channels, videos, searchQuery, hasResults });
                await context.Response.WriteAsync(await HTMLRenderer.Render(context, "Templates\\search.liquid", templateContext));
            }
            else
            {
                await context.Response.WriteAsync(await HTMLRenderer.Render(context, "Templates\\search.liquid", null));
            }
        }
        private static async Task InstallGet(HttpContext context)
        {
            CommonController.SetHTMLContentType(context);
            var playerURL = GetLatestPlayerInstallerURL();
            var editorURL = GetLatestEditorInstallerURL();

            var templateContext = new TemplateContext(new { playerURL, editorURL });
            await context.Response.WriteAsync(await HTMLRenderer.Render(context, "Templates\\install.liquid", templateContext));
        }
Exemplo n.º 6
0
        private static async Task ResetPasswordFinishGet(HttpContext context)
        {
            SetHTMLContentType(context);

            var args  = context.Request.Query;
            var model = new ResetPasswordModel
            {
                email = args["email"].ToString().NormalizeEmail(),
                token = args["token"].ToString(),
            };

            //TODO(Simon): Show HTML. Put token in hidden form element
            var templateContext = new TemplateContext(model);
            await context.Response.WriteAsync(await HTMLRenderer.Render(context, "Templates\\resetPasswordFinish.liquid", templateContext));
        }
Exemplo n.º 7
0
        private static async Task ResetPasswordFinishPost(HttpContext context)
        {
            SetHTMLContentType(context);
            using var connection = Database.OpenNewConnection();

            var form  = context.Request.Form;
            var model = new ResetPasswordModel
            {
                email = form["email"].ToString().NormalizeEmail(),
                token = form["token"].ToString(),
            };

            string password        = form["password"].ToString();
            string confirmPassword = form["password-confirmation"].ToString();
            int    userid          = await UserIdFromEmail(model.email, connection);

            if (await AuthenticatePasswordResetToken(userid, model.token, connection))
            {
                var(success, result) = ValidatePassword(password, confirmPassword);
                if (!success)
                {
                    model.error = result;
                    var templateContext = new TemplateContext(model);
                    await context.Response.WriteAsync(await HTMLRenderer.Render(context, "Templates\\resetPasswordFinish.liquid", templateContext));

                    return;
                }

                if (await UpdatePassword(model.email, password, connection))
                {
                    await context.Response.WriteAsync(await HTMLRenderer.Render(context, "Templates\\resetPasswordFinishSuccess.liquid", null));
                    await DeletePasswordResetToken(userid, model.token, connection);
                }
                else
                {
                    model.error = "An unknown error happened while resetting this password. Please try again later.";
                    var templateContext = new TemplateContext(model);
                    await context.Response.WriteAsync(await HTMLRenderer.Render(context, "Templates\\resetPasswordFinish.liquid", templateContext));
                }
            }
            else
            {
                //NOTE(Simon): Do not tell exact reason, could be an attack vector
                model.error = "This password reset token is not valid.";
                var templateContext = new TemplateContext(model);
                await context.Response.WriteAsync(await HTMLRenderer.Render(context, "Templates\\resetPasswordFinish.liquid", templateContext));
            }
        }
Exemplo n.º 8
0
        public void ConfigureServices(IServiceCollection services)
        {
            services.Configure <FormOptions>(config => { config.MultipartBodyLengthLimit = long.MaxValue; });

            router = new Router();

            EmailClient.InitCredentials();

            HTMLRenderer.RegisterLayout(BaseLayout.Web, "Templates/base.liquid");

            CheckForFfmpeg();

            CreateDataDirectoryIfNeeded();

            RegisterGlobalExceptionLogger();
        }
Exemplo n.º 9
0
        private static async Task UserGet(HttpContext context)
        {
            CommonController.SetHTMLContentType(context);

            var username = context.Request.Query["name"].ToString();

            if (!String.IsNullOrEmpty(username))
            {
                using var connection = Database.OpenNewConnection();
                var user = await UserController.UserFromUsername(username, connection);

                if (user != null)
                {
                    const int countPerPage = 20;
                    int       page         = 1;
                    if (context.Request.Query.ContainsKey("page"))
                    {
                        Int32.TryParse(context.Request.Query["page"], out page);
                    }

                    int offset = (page - 1) * countPerPage;

                    var numVideos = await NumPublicVideosForUser(user.userid, connection);

                    var VideosTask = PublicVideosForUser(user.userid, countPerPage, offset, connection);

                    var pagination = new Pagination(numVideos, countPerPage, offset);

                    var templateContext = new TemplateContext(new { videos = await VideosTask, user, pagination });

                    await context.Response.WriteAsync(await HTMLRenderer.Render(context, "Templates\\user.liquid", templateContext));
                }
                else
                {
                    await CommonController.Write404(context);
                }
            }
            else
            {
                await CommonController.Write404(context);
            }
        }
Exemplo n.º 10
0
        private static async Task UpdatePasswordPost(HttpContext context)
        {
            SetHTMLContentType(context);

            string currentPassword      = context.Request.Form["current-password"];
            string password             = context.Request.Form["new-password"];
            string passwordConfirmation = context.Request.Form["password-confirmation"];

            var(success, result) = ValidatePassword(password, passwordConfirmation);

            if (!success)
            {
                var templateContext = new TemplateContext(new { success = false, message = result });
                await context.Response.WriteAsync(await HTMLRenderer.Render(context, "Templates\\settings.liquid", templateContext));
            }
            else
            {
                using var connection = Database.OpenNewConnection();
                var user = await UserSessions.GetLoggedInUser(context);

                var currentPassCorrect = await AuthenticateUser(user.email, currentPassword, connection);

                if (currentPassCorrect)
                {
                    if (await UpdatePassword(user.email, password, connection))
                    {
                        var templateContext = new TemplateContext(new { success = true, message = "" });
                        await context.Response.WriteAsync(await HTMLRenderer.Render(context, "Templates\\settings.liquid", templateContext));
                    }
                    else
                    {
                        var templateContext = new TemplateContext(new { success = false, message = "Something went wrong while updating password. Please try again later" });
                        await context.Response.WriteAsync(await HTMLRenderer.Render(context, "Templates\\settings.liquid", templateContext));
                    }
                }
                else
                {
                    var templateContext = new TemplateContext(new { success = false, message = "Current password is wrong" });
                    await context.Response.WriteAsync(await HTMLRenderer.Render(context, "Templates\\settings.liquid", templateContext));
                }
            }
        }
Exemplo n.º 11
0
        private static async Task ResetPasswordStartPost(HttpContext context)
        {
            SetHTMLContentType(context);

            using var connection = Database.OpenNewConnection();

            var    form  = context.Request.Form;
            string email = form["email"].ToString().NormalizeEmail();

            var userExistsTask = UserExists(email, connection);

            await context.Response.WriteAsync(await HTMLRenderer.Render(context, "Templates\\resetPasswordSuccess.liquid", null));

            if (await userExistsTask)
            {
                var token = await CreatePasswordResetToken(email, connection);

                await EmailClient.SendPasswordResetMail(email, token);
            }
        }
Exemplo n.º 12
0
        private static async Task RegisterPost(HttpContext context)
        {
            SetHTMLContentType(context);

            string result;
            bool   success;

            var model = new RegisterModel
            {
                username = context.Request.Form["username"].ToString(),
                email    = context.Request.Form["email"].ToString().NormalizeEmail(),
                error    = ""
            };

            try
            {
                (success, result) = await RegisterWithForm(context);
            }
            catch (Exception e)
            {
                model.error = "An unknown error happened while registering this account. Please try again later.";
                var templateContext = new TemplateContext(model);
                await context.Response.WriteAsync(await HTMLRenderer.Render(context, "Templates\\register.liquid", templateContext));

                return;
            }

            if (!success)
            {
                model.error = result;
                var templateContext = new TemplateContext(model);
                await context.Response.WriteAsync(await HTMLRenderer.Render(context, "Templates\\register.liquid", templateContext));
            }
            else
            {
                UserSessions.SetSessionCookie(context, result);
                await context.Response.WriteAsync(await HTMLRenderer.Render(context, "Templates\\registerSuccess.liquid", null));
            }
        }
Exemplo n.º 13
0
        private static async Task IndexGet(HttpContext context)
        {
            CommonController.SetHTMLContentType(context);
            var tabString = context.Request.Query["tab"].ToString();
            var tab       = tabString switch
            {
                "new" => IndexTab.New,
                "popular" => IndexTab.Popular,
                _ => IndexTab.MostWatched
            };

            int count  = 20;
            int offset = 0;

            using var connection = Database.OpenNewConnection();

            var videos = await GetIndexVideos(tab, count, offset, connection);

            var templateContext = new TemplateContext(new { videos, tab = tab.ToString() });

            await context.Response.WriteAsync(await HTMLRenderer.Render(context, "Templates\\index.liquid", templateContext));
        }
Exemplo n.º 14
0
        private static async Task VerifyEmailGet(HttpContext context)
        {
            SetHTMLContentType(context);
            using var connection = Database.OpenNewConnection();

            var    args  = context.Request.Query;
            string email = args["email"].ToString().NormalizeEmail();
            string token = args["token"].ToString();

            int userid = await UserIdFromEmail(email, connection);

            bool success = await VerifyEmail(userid, token, connection);

            if (success)
            {
                await context.Response.WriteAsync(await HTMLRenderer.Render(context, "Templates\\verifyEmailSuccess.liquid", null));
            }
            else
            {
                await context.Response.WriteAsync(await HTMLRenderer.Render(context, "Templates\\verifyEmailFailure.liquid", null));
            }
        }
Exemplo n.º 15
0
        private static async Task LoginPost(HttpContext context)
        {
            SetHTMLContentType(context);

            string result;
            bool   success;

            var model = new LoginModel
            {
                email = context.Request.Form["email"].ToString().NormalizeEmail(),
            };

            try
            {
                (success, result) = await LoginWithForm(context);
            }
            catch (Exception e)
            {
                model.error = "An unknown error happened while logging in. Please try again later.";
                var templateContext = new TemplateContext(model);
                await context.Response.WriteAsync(await HTMLRenderer.Render(context, "Templates\\login.liquid", templateContext));

                return;
            }

            if (!success)
            {
                model.error = result;
                var templateContext = new TemplateContext(model);
                await context.Response.WriteAsync(await HTMLRenderer.Render(context, "Templates\\login.liquid", templateContext));
            }
            else
            {
                UserSessions.SetSessionCookie(context, result);
                context.Response.Redirect("/");
            }
        }
Exemplo n.º 16
0
        private static async Task RegisterGet(HttpContext context)
        {
            SetHTMLContentType(context);

            await context.Response.WriteAsync(await HTMLRenderer.Render(context, "Templates\\register.liquid", null));
        }
Exemplo n.º 17
0
 private static async Task CookiePolicy(HttpContext context)
 {
     SetHTMLContentType(context);
     await context.Response.WriteAsync(await HTMLRenderer.Render(context, "Templates\\cookiePolicy.liquid", null));
 }