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 DeleteVideoConfirmGet(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)) { await DeleteVideo(video.id, connection); context.Response.Redirect("/my_videos"); } else { await CommonController.Write404(context); } } else { await CommonController.Write404(context); } }
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); } }
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); } }
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); } }
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)); }
private static async Task EditVideoPost(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 form = context.Request.Form; video.title = form["title"]; video.description = form["description"]; //TODO(Simon): Deduplicate tags. Should be cleaned by frontend, but may be malicious data. string[] tags = form["tags"].ToString().Split(','); var deduplicatedTags = new HashSet <string>(tags); video.tags = deduplicatedTags.ToList(); if (Int32.TryParse(form["privacy"], out var privacyInt)) { video.privacy = (VideoPrivacy)privacyInt; } await AddOrUpdateVideo(video, connection); context.Response.Redirect("/my_videos"); } else { await CommonController.Write404(context); } } else { await CommonController.Write404(context); } }
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); } }
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)); }
private static async Task UpdateVideoPrivacyPost(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)) { if (Int32.TryParse(context.Request.Form["video-privacy"], out int privacy)) { await SetVideoPrivacy(video.id, (VideoPrivacy)privacy, connection); } } } context.Response.Redirect("/my_videos"); }