From 38142c91297f5001e1525fc8b34d02a9937b507c Mon Sep 17 00:00:00 2001 From: Pat Hartl Date: Fri, 1 Sep 2023 11:40:29 -0500 Subject: [PATCH] Add tool to delete orphaned files --- LANCommander/Models/OrphanedFile.cs | 9 +++ .../Pages/Settings/Tools/OrphanedFiles.razor | 76 +++++++++++++++++++ LANCommander/Pages/Settings/ToolsIndex.razor | 6 ++ LANCommander/Services/GameService.cs | 8 +- 4 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 LANCommander/Models/OrphanedFile.cs create mode 100644 LANCommander/Pages/Settings/Tools/OrphanedFiles.razor diff --git a/LANCommander/Models/OrphanedFile.cs b/LANCommander/Models/OrphanedFile.cs new file mode 100644 index 0000000..5865a30 --- /dev/null +++ b/LANCommander/Models/OrphanedFile.cs @@ -0,0 +1,9 @@ +namespace LANCommander.Models +{ + public class OrphanedFile + { + public string Path { get; set; } + public long Size { get; set; } + public DateTime CreatedOn { get; set; } + } +} diff --git a/LANCommander/Pages/Settings/Tools/OrphanedFiles.razor b/LANCommander/Pages/Settings/Tools/OrphanedFiles.razor new file mode 100644 index 0000000..356da46 --- /dev/null +++ b/LANCommander/Pages/Settings/Tools/OrphanedFiles.razor @@ -0,0 +1,76 @@ +@page "/Settings/Tools/OrphanedFiles" +@using LANCommander.Helpers; +@using LANCommander.Models; +@layout SettingsLayout +@inject ArchiveService ArchiveService; + + + +
+

+ These files exist on the server, but aren't linked in the database. Use this tool to identify and delete orphaned files. +

+ + + + + @ByteSizeLib.ByteSize.FromBytes(context.Size).ToString() + + + + + + +
+
+ + @code { + ICollection Orphans = new List(); + bool Loading = true; + + protected override async Task OnInitializedAsync() + { + await LoadData(); + } + + async Task LoadData() + { + Loading = true; + + Orphans = new List(); + + var archives = await ArchiveService.Get(); + var archiveFiles = archives.Select(a => ArchiveService.GetArchiveFileLocation(a)); + var files = Directory.GetFiles("Upload"); + + foreach (var file in files.Where(f => !archiveFiles.Contains(f))) + { + var fileInfo = new FileInfo(file); + + Orphans.Add(new OrphanedFile + { + Path = file, + Size = fileInfo.Length, + CreatedOn = fileInfo.CreationTime + }); + } + + Orphans = Orphans.OrderByDescending(f => f.Size).ToList(); + + Loading = false; + } + + async Task Delete(OrphanedFile file) + { + FileHelpers.DeleteIfExists(file.Path); + + Orphans.Remove(file); + + StateHasChanged(); + } +} diff --git a/LANCommander/Pages/Settings/ToolsIndex.razor b/LANCommander/Pages/Settings/ToolsIndex.razor index 63219f0..0f171ce 100644 --- a/LANCommander/Pages/Settings/ToolsIndex.razor +++ b/LANCommander/Pages/Settings/ToolsIndex.razor @@ -27,6 +27,12 @@

Missing Archives

List and fix all archives that are missing their backing files.

View Missing Archives + + + +

Orphaned Files

+

Find and delete any files that don't exist in the database and may be taking up unnecessary disk space.

+ View Orphaned Files diff --git a/LANCommander/Services/GameService.cs b/LANCommander/Services/GameService.cs index 09786c3..ae7ea16 100644 --- a/LANCommander/Services/GameService.cs +++ b/LANCommander/Services/GameService.cs @@ -11,16 +11,20 @@ namespace LANCommander.Services { public class GameService : BaseDatabaseService { - public GameService(DatabaseContext dbContext, IHttpContextAccessor httpContextAccessor) : base(dbContext, httpContextAccessor) + private readonly ArchiveService ArchiveService; + + public GameService(DatabaseContext dbContext, IHttpContextAccessor httpContextAccessor, ArchiveService archiveService) : base(dbContext, httpContextAccessor) { + ArchiveService = archiveService; } public override async Task Delete(Game game) { foreach (var archive in game.Archives.OrderByDescending(a => a.CreatedOn)) { + await ArchiveService.Delete(archive); + FileHelpers.DeleteIfExists($"Icon/{game.Id}.png".ToPath()); - FileHelpers.DeleteIfExists($"Upload/{archive.ObjectKey}".ToPath()); } await base.Delete(game);