diff --git a/ReplayBrowser/Helpers/ReplayHelper.cs b/ReplayBrowser/Helpers/ReplayHelper.cs index 5b60874..954cfb9 100644 --- a/ReplayBrowser/Helpers/ReplayHelper.cs +++ b/ReplayBrowser/Helpers/ReplayHelper.cs @@ -200,13 +200,32 @@ public async Task GetTotalReplayCount() return await _context.Replays.CountAsync(); } + public async Task GetReplay(string @operator, string server, int id, AuthenticationState authstate) + { + Log.Information($"Looking for replay: {@operator}, {server}, {id}"); + + var replay = await _context.Replays + .AsNoTracking() + .Include(r => r.RoundParticipants!) + .ThenInclude(p => p.Players) + .OrderByDescending(r => r.Duration) + .FirstOrDefaultAsync(r => r.ServerId == @operator && r.ServerName == server && r.RoundId == id); + + if (replay == null) + return null; + + var caller = await _accountService.GetAccount(authstate); + replay = FilterReplay(replay, caller); + return replay; + } + public async Task GetReplay(int id, AuthenticationState authstate) { var replay = await _context.Replays .AsNoTracking() .Include(r => r.RoundParticipants!) .ThenInclude(p => p.Players) - .FirstOrDefaultAsync(r => r.Id == id); + .SingleOrDefaultAsync(r => r.Id == id); if (replay == null) return null; diff --git a/ReplayBrowser/Pages/Shared/Layout/App.razor b/ReplayBrowser/Pages/Shared/Layout/App.razor index 191c6d7..3f09354 100644 --- a/ReplayBrowser/Pages/Shared/Layout/App.razor +++ b/ReplayBrowser/Pages/Shared/Layout/App.razor @@ -17,6 +17,33 @@ + + diff --git a/ReplayBrowser/Pages/Shared/Layout/ErrorCode.razor b/ReplayBrowser/Pages/Shared/Layout/ErrorCode.razor index 4125673..9e21972 100644 --- a/ReplayBrowser/Pages/Shared/Layout/ErrorCode.razor +++ b/ReplayBrowser/Pages/Shared/Layout/ErrorCode.razor @@ -7,6 +7,7 @@ switch (Code) { case 404: + Error - Not Found Should there be a page here? Contribute to the project on GitHub! break; case 500: + Error - Internal Server Error -
-
@_nameFormatted
- @if (ReplayData.Map == null) - { -

Maps: @string.Join(", ", ReplayData.Maps!)

- } - else - { -

Map: @ReplayData.Map

- } -

Gamemode: @ReplayData.Gamemode

+
+
@(ReplayData.ServerName ?? ReplayData.ServerId ?? "Unknown!")#@ReplayData.RoundId
- +
@@ -70,38 +79,8 @@ protected override async void OnInitialized() { await base.OnInitializedAsync(); - // Get the date and server name of the replay using regex - var fileName = Path.GetFileName(ReplayData.Link); - if (fileName == null) - { - return; - } - var storageUrl = ReplayParserService.GetStorageUrlFromReplayLink(ReplayData.Link); - var matchName = storageUrl.ServerNameRegexCompiled.Match(fileName); - var matchDate = storageUrl.ReplayRegexCompiled.Match(fileName); - if (matchName.Success && matchDate.Success) - { - if (string.IsNullOrWhiteSpace(matchName.Groups[1].Value)) - { - // wut? Fallback to server id - _nameFormatted = $"{ReplayData.ServerId} - {matchDate.Groups[1].Value}"; - } - else - { - _nameFormatted = $"{matchName.Groups[1].Value} - {matchDate.Groups[1].Value}"; - } - } - else - { - if (ReplayData.ServerName != null) - { - _nameFormatted = ReplayData.ServerName; - } - else - { - _nameFormatted = ReplayData.ServerId; - } - } + + _nameFormatted = $"{ReplayData.ServerName ?? ReplayData.ServerId} #{ReplayData!.RoundId}"; var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync(); var account = await AccountService.GetAccount(authState); diff --git a/ReplayBrowser/Pages/Shared/SearchBar.razor b/ReplayBrowser/Pages/Shared/SearchBar.razor index 383a942..2527f77 100644 --- a/ReplayBrowser/Pages/Shared/SearchBar.razor +++ b/ReplayBrowser/Pages/Shared/SearchBar.razor @@ -63,7 +63,7 @@ display: none !important; } - .btn { + .search-container .btn { margin-right: 0.5rem; } diff --git a/ReplayBrowser/Pages/ViewReplay.razor b/ReplayBrowser/Pages/ViewReplay.razor deleted file mode 100644 index 0f3fa23..0000000 --- a/ReplayBrowser/Pages/ViewReplay.razor +++ /dev/null @@ -1,80 +0,0 @@ -@page "/replay/{id:int}" -@using Microsoft.AspNetCore.Components.Authorization -@using Microsoft.AspNetCore.Components.Web -@using ReplayBrowser.Data.Models -@using ReplayBrowser.Helpers -@using ReplayBrowser.Pages.Shared -@using ReplayBrowser.Services.ReplayParser - -@inject AuthenticationStateProvider AuthenticationStateProvider -@inject ReplayHelper ReplayHelper -@inject ReplayParserService ReplayParserService -@inject NavigationManager NavigationManager - -Replay viewer -@if (Replay == null) -{ -

Loading...

-} -else -{ - - - -} - -@code { - [Parameter] - public int Id { get; set; } - public Replay? Replay { get; set; } - - private string GetTitle() - { - var fileName = Path.GetFileName(Replay!.Link); - var storageUrl = ReplayParserService.GetStorageUrlFromReplayLink(Replay.Link); - var matchName = storageUrl.ServerNameRegexCompiled.Match(fileName); - var matchDate = storageUrl.ReplayRegexCompiled.Match(fileName); - - var nameFormatted = ""; - var dateFormatted = ""; - - if (matchName.Success && matchDate.Success) - { - nameFormatted = $"{matchName.Groups[1].Value} - {matchDate.Groups[1].Value.Replace("_","-")}"; - dateFormatted = matchDate.Groups[1].Value.Replace("_", "-"); - if (string.IsNullOrWhiteSpace(matchName.Groups[1].Value)) - { - nameFormatted = $"{Replay.ServerId} - {matchDate.Groups[1].Value}"; - } - else - { - nameFormatted = $"{matchName.Groups[1].Value} - {matchDate.Groups[1].Value}"; - } - } - else - { - nameFormatted = Replay.ServerName ?? Replay.ServerId; - } - - return $"Round {Replay.RoundId} - {nameFormatted}"; - } - - private string GetDescription() - { - return "Round " + Replay!.RoundId + " played on " + Replay.Map + " on " + Replay.Date?.ToString("yyyy-MM-dd HH:mm:ss"); - } - - protected override async Task OnInitializedAsync() - { - var authstate = await AuthenticationStateProvider.GetAuthenticationStateAsync(); - Replay = await ReplayHelper.GetReplay(Id, authstate)!; - if (Replay == null) - { - var currentUrl = Uri.EscapeDataString(NavigationManager.Uri); - NavigationManager.NavigateTo($"/error/404?url={currentUrl}"); - } - } -} \ No newline at end of file diff --git a/ReplayBrowser/Pages/ViewReplayBase.razor b/ReplayBrowser/Pages/ViewReplayBase.razor new file mode 100644 index 0000000..b2f06b0 --- /dev/null +++ b/ReplayBrowser/Pages/ViewReplayBase.razor @@ -0,0 +1,61 @@ +@page "/replay/{id:int}" +@using Microsoft.AspNetCore.Components.Authorization +@using Microsoft.AspNetCore.Components.Web +@using ReplayBrowser.Data.Models +@using ReplayBrowser.Helpers +@using ReplayBrowser.Pages.Shared +@using ReplayBrowser.Services.ReplayParser + +@inject AuthenticationStateProvider AuthenticationStateProvider +@inject ReplayHelper ReplayHelper +@inject ReplayParserService ReplayParserService +@inject NavigationManager NavigationManager +@inject IHttpContextAccessor HttpContextAccessor + +Replay viewer +@if (Replay == null) +{ + +} +else +{ + + + +} + +@code { + [Parameter] + public int Id { get; set; } + + public Replay? Replay { get; set; } + + protected string GetTitle() + { + // Not null because the function is used only when Replay IS NOT null + return $"Round #{Replay!.RoundId} - {Replay.ServerName ?? Replay.ServerId} - {Replay.Date}"; + } + + protected string GetDescription() + { + return "Round " + Replay!.RoundId + " played on " + Replay.Map + " on " + Replay.Date?.ToString("yyyy-MM-dd HH:mm:ss"); + } + + protected virtual Task GetReplay(AuthenticationState? state) + { + return ReplayHelper.GetReplay((int) Id!, state!); + } + + protected override async Task OnInitializedAsync() + { + Replay = await GetReplay(await AuthenticationStateProvider.GetAuthenticationStateAsync()); + + if (Replay is not null) + return; + + HttpContextAccessor.HttpContext!.Response.StatusCode = 404; + } +} \ No newline at end of file diff --git a/ReplayBrowser/Pages/ViewReplayFriendly.razor b/ReplayBrowser/Pages/ViewReplayFriendly.razor new file mode 100644 index 0000000..844ef6d --- /dev/null +++ b/ReplayBrowser/Pages/ViewReplayFriendly.razor @@ -0,0 +1,32 @@ +@page "/replay/{operator}/{server}/{RoundId:int}" +@inherits ViewReplayBase + +@using Microsoft.AspNetCore.Components.Authorization +@using Microsoft.AspNetCore.Components.Web +@using ReplayBrowser.Data.Models +@using ReplayBrowser.Helpers +@using ReplayBrowser.Pages.Shared +@using ReplayBrowser.Services.ReplayParser + +@inject AuthenticationStateProvider AuthenticationStateProvider +@inject ReplayHelper ReplayHelper +@inject ReplayParserService ReplayParserService +@inject NavigationManager NavigationManager + +@{ + base.BuildRenderTree(__builder); +} + +@code { + [Parameter] + required public string Operator { get; set; } + [Parameter] + required public string Server { get; set; } + [Parameter] + required public int RoundId { get; set; } + + protected override Task GetReplay(AuthenticationState? state) + { + return ReplayHelper.GetReplay(Operator, Server, RoundId, state!); + } +} \ No newline at end of file diff --git a/ReplayBrowser/Startup.cs b/ReplayBrowser/Startup.cs index 29315bb..b4df66d 100644 --- a/ReplayBrowser/Startup.cs +++ b/ReplayBrowser/Startup.cs @@ -230,12 +230,6 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env) } } - app.Use((context, next) => - { - context.Request.Scheme = "https"; - return next(); - }); - #if RELEASE app.UseExceptionHandler("/error", createScopeForErrors: true); app.UseHsts(); @@ -248,9 +242,11 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env) app.UseHttpsRedirection(); #endif - app.UseStaticFiles(); - + app.UseForwardedHeaders(); app.UseRouting(); + app.UseCors(); + + app.UseStaticFiles(); app.UseAuthentication(); app.UseAuthorization(); @@ -270,10 +266,5 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env) name: "default", pattern: "api/{controller=Home}/{action=Index}/{id?}"); }); - - app.UseHttpsRedirection(); - app.UseForwardedHeaders(); - app.UseRouting(); - app.UseCors(); } } \ No newline at end of file