LANCommander/LANCommander.Server/UI/Pages/Settings/Tools/Index.razor

202 lines
6.3 KiB
Text

@page "/Settings/Tools"
@using Hangfire
@using LANCommander.Helpers
@using LANCommander.Server.Jobs.Background
@using LANCommander.Server.Services.Exceptions
@using ZiggyCreatures.Caching.Fusion
@inject ArchiveService ArchiveService
@inject PageService PageService
@inject UpdateService UpdateService
@inject StorageLocationService StorageLocationService
@inject IMessageService MessageService
@inject IFusionCache FusionCache
@inject ILogger<Index> Logger
@attribute [Authorize(Roles = RoleService.AdministratorRoleName)]
<PageHeader Title="Tools" />
<PageContent>
<h3>Recalculate File Sizes</h3>
<p>
Some file sizes are cached in the database. Click the button below to scan through all files and recalculate their size.
</p>
<Button Type="ButtonType.Primary" OnClick="RecalculateFileSizes" Loading="RecalculatingFileSizes">Recalculate</Button>
<Divider />
<h3>Clear Cache</h3>
<p>
A cached list of games is sent to the clients on import. Click the button below to bust this cache if you don't see your changes.
</p>
<Button Type="ButtonType.Primary" OnClick="ClearCache">Clear Cache</Button>
<Divider />
<h3>Download Clients</h3>
<p>Check for any client releases for the current version and download them to this server.</p>
<Button Type="ButtonType.Primary" OnClick="DownloadLaunchers" Loading="DownloadingLaunchers">Download</Button>
<Divider />
<h3>Missing Archives</h3>
<p>List and fix all archives that are missing their backing files.</p>
<a href="/Settings/Tools/MissingArchives" class="ant-btn ant-btn-primary">View Missing Archives</a>
<Divider />
<h3>Orphaned Files</h3>
<p>Find and delete any files that don't exist in the database and may be taking up unnecessary disk space.</p>
<a href="/Settings/Tools/OrphanedFiles" class="ant-btn ant-btn-primary">View Orphaned Files</a>
<Divider />
<h3>Active Sessions</h3>
<p>List and delete any active user play sessions.</p>
<a href="/Settings/Tools/ActiveSessions" class="ant-btn ant-btn-primary">View Active Sessions</a>
<Divider />
<h3>Regenerate Page Routes</h3>
<p>Fix any page routes that seem to be out of date or corrupt.</p>
<Button Type="ButtonType.Primary" OnClick="FixPageRoutes" Loading="FixingPageRoutes">Fix Routes</Button>
<Divider />
<h3>Regenerate Thumbnails</h3>
<p>Clear out the existing thumbnail cache and generate replacements.</p>
<Button Type="ButtonType.Primary" OnClick="RegenerateThumbnails" Loading="RegeneratingThumbnails">Regenerate</Button>
</PageContent>
@code {
bool RecalculatingFileSizes = false;
bool DownloadingLaunchers = false;
bool FixingPageRoutes = false;
bool RegeneratingThumbnails = false;
async Task RecalculateFileSizes()
{
RecalculatingFileSizes = true;
await Task.Yield();
await InvokeAsync(StateHasChanged);
var archives = await ArchiveService.AsNoTracking().GetAsync();
foreach (var archive in archives)
{
try
{
var path = await ArchiveService.GetArchiveFileLocationAsync(archive);
if (File.Exists(path))
{
archive.CompressedSize = await ArchiveService.GetCompressedSizeAsync(archive);
archive.UncompressedSize = await ArchiveService.GetUncompressedSizeAsync(archive);
await ArchiveService.UpdateAsync(archive);
}
}
catch (Exception ex)
{
Logger?.LogError(ex, "Could not recalculate file size for archive {ArchiveId}", archive.Id);
}
}
MessageService.Success("File sizes recalculated!");
RecalculatingFileSizes = false;
await Task.Yield();
await InvokeAsync(StateHasChanged);
}
async Task ClearCache()
{
await FusionCache.RemoveByTagAsync("Games");
await FusionCache.RemoveByTagAsync("Depot");
await FusionCache.RemoveByTagAsync("Library");
await FusionCache.RemoveByTagAsync("Archives");
MessageService.Success("Cache cleared!");
}
async Task DownloadLaunchers()
{
DownloadingLaunchers = true;
await Task.Yield();
await InvokeAsync(StateHasChanged);
try
{
await UpdateService.DownloadLatestLauncherReleaseAsync();
MessageService.Success("Launchers downloaded!");
}
catch (ReleaseNotFoundException ex)
{
MessageService.Error($"Launchers could not be downloaded, unknown release {ex.Version}");
}
catch (Exception ex)
{
Logger?.LogError(ex, "An unknown error occurred while trying to download launchers");
MessageService.Error("An unknown error occurred");
}
DownloadingLaunchers = false;
await Task.Yield();
await InvokeAsync(StateHasChanged);
}
async Task FixPageRoutes()
{
FixingPageRoutes = true;
await Task.Yield();
await InvokeAsync(StateHasChanged);
try
{
await PageService.FixRoutesAsync();
MessageService.Success("Routes fixed!");
}
catch (Exception ex)
{
MessageService.Error("Could not fix all routes!");
}
FixingPageRoutes = false;
await Task.Yield();
await InvokeAsync(StateHasChanged);
}
async Task RegenerateThumbnails()
{
RegeneratingThumbnails = true;
await Task.Yield();
await InvokeAsync(StateHasChanged);
var storageLocations = await StorageLocationService.GetAsync();
foreach (var storageLocation in storageLocations.Where(l => l.Type == SDK.Enums.StorageLocationType.Media))
{
var thumbnailFiles = Directory.GetFiles(storageLocation.Path).Where(f => f.EndsWith(".Thumb"));
foreach (var thumbnailFile in thumbnailFiles)
FileHelpers.DeleteIfExists(thumbnailFile);
}
BackgroundJob.Enqueue<GenerateThumbnailsJob>(x => x.ExecuteAsync());
RegeneratingThumbnails = false;
await Task.Yield();
await InvokeAsync(StateHasChanged);
MessageService.Success("Cache has been cleared and is being regenerated in the background");
}
}