From d65ef4a2805f8ea3fba3b4fec84bbf734b2fdd56 Mon Sep 17 00:00:00 2001 From: Pat Hartl Date: Wed, 4 Jan 2023 00:36:49 -0600 Subject: [PATCH] Moved archive functionality to a separate controller --- Controllers/ArchivesController.cs | 93 +++++++++++++++++++ Controllers/GamesController.cs | 42 --------- Data/Repository.cs | 5 + Extensions/StringExtensions.cs | 14 +++ .../AddArchive.cshtml => Archives/Add.cshtml} | 2 +- Views/Games/Edit.cshtml | 6 +- 6 files changed, 116 insertions(+), 46 deletions(-) create mode 100644 Controllers/ArchivesController.cs create mode 100644 Extensions/StringExtensions.cs rename Views/{Games/AddArchive.cshtml => Archives/Add.cshtml} (98%) diff --git a/Controllers/ArchivesController.cs b/Controllers/ArchivesController.cs new file mode 100644 index 0000000..c1270a7 --- /dev/null +++ b/Controllers/ArchivesController.cs @@ -0,0 +1,93 @@ +using LANCommander.Data; +using LANCommander.Data.Models; +using LANCommander.Extensions; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; + +namespace LANCommander.Controllers +{ + [Authorize] + public class ArchivesController : Controller + { + private readonly DatabaseContext Context; + + public ArchivesController(DatabaseContext context) + { + Context = context; + } + + public async Task Add(Guid? id) + { + if (id == null || Context.Games == null) + return NotFound(); + + using (Repository repo = new Repository(Context, HttpContext)) + { + var game = await repo.Find(id.GetValueOrDefault()); + + Archive lastVersion = null; + + if (game.Archives != null && game.Archives.Count > 0) + lastVersion = game.Archives.OrderByDescending(a => a.CreatedOn).FirstOrDefault(); + + return View(new Archive() + { + Game = game, + LastVersion = lastVersion, + }); + } + } + + [HttpPost] + public async Task Add(Guid? id, Archive archive) + { + archive.Id = Guid.Empty; + + using (Repository gameRepo = new Repository(Context, HttpContext)) + { + var game = await gameRepo.Find(id.GetValueOrDefault()); + + using (Repository archiveRepo = new Repository(Context, HttpContext)) + { + archive.Game = game; + + archive = await archiveRepo.Add(archive); + await archiveRepo.SaveChanges(); + } + } + return RedirectToAction("Edit", "Games", new { id = id }); + } + + public async Task Download(Guid id) + { + using (Repository repo = new Repository(Context, HttpContext)) + { + var archive = await repo.Find(id); + + var content = new FileStream(Path.Combine("Upload", archive.ObjectKey), FileMode.Open, FileAccess.Read, FileShare.Read); + + return File(content, "application/octet-stream", $"{archive.Game.Title.SanitizeFilename()}.zip"); + } + } + + public async Task Delete(Guid? id) + { + Guid gameId; + + using (Repository repo = new Repository(Context, HttpContext)) + { + var archive = await repo.Find(id.GetValueOrDefault()); + + gameId = archive.Game.Id; + + System.IO.File.Delete(Path.Combine("Upload", archive.ObjectKey)); + + repo.Delete(archive); + + await repo.SaveChanges(); + } + + return RedirectToAction("Edit", "Games", new { id = gameId }); + } + } +} diff --git a/Controllers/GamesController.cs b/Controllers/GamesController.cs index 3d48846..80b59b8 100644 --- a/Controllers/GamesController.cs +++ b/Controllers/GamesController.cs @@ -168,48 +168,6 @@ namespace LANCommander.Controllers return RedirectToAction(nameof(Index)); } - public async Task AddArchive(Guid? id) - { - if (id == null || Context.Games == null) - return NotFound(); - - using (Repository repo = new Repository(Context, HttpContext)) - { - var game = await repo.Find(id.GetValueOrDefault()); - - Archive lastVersion = null; - - if (game.Archives != null && game.Archives.Count > 0) - lastVersion = game.Archives.OrderByDescending(a => a.CreatedOn).FirstOrDefault(); - - return View(new Archive() - { - Game = game, - LastVersion = lastVersion, - }); - } - } - - [HttpPost] - public async Task AddArchive(Guid? id, Archive archive) - { - archive.Id = Guid.Empty; - - using (Repository gameRepo = new Repository(Context, HttpContext)) - { - var game = await gameRepo.Find(id.GetValueOrDefault()); - - using (Repository archiveRepo = new Repository(Context, HttpContext)) - { - archive.Game = game; - - archive = await archiveRepo.Add(archive); - await archiveRepo.SaveChanges(); - } - } - return RedirectToAction(nameof(Index)); - } - private bool GameExists(Guid id) { return (Context.Games?.Any(e => e.Id == id)).GetValueOrDefault(); diff --git a/Data/Repository.cs b/Data/Repository.cs index 09dad9f..a4b79cd 100644 --- a/Data/Repository.cs +++ b/Data/Repository.cs @@ -62,6 +62,11 @@ namespace LANCommander.Data return entity; } + public void Delete(T entity) + { + Context.Remove(entity); + } + public async Task SaveChanges() { await Context.SaveChangesAsync(); diff --git a/Extensions/StringExtensions.cs b/Extensions/StringExtensions.cs new file mode 100644 index 0000000..dec341f --- /dev/null +++ b/Extensions/StringExtensions.cs @@ -0,0 +1,14 @@ +using System.Text.RegularExpressions; + +namespace LANCommander.Extensions +{ + public static class StringExtensions + { + public static string SanitizeFilename(this string filename, string replacement = "") + { + var removeInvalidChars = new Regex($"[{Regex.Escape(new string(Path.GetInvalidFileNameChars()))}]", RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.CultureInvariant); + + return removeInvalidChars.Replace(filename, replacement); + } + } +} diff --git a/Views/Games/AddArchive.cshtml b/Views/Archives/Add.cshtml similarity index 98% rename from Views/Games/AddArchive.cshtml rename to Views/Archives/Add.cshtml index a73c107..78d951f 100644 --- a/Views/Games/AddArchive.cshtml +++ b/Views/Archives/Add.cshtml @@ -10,7 +10,7 @@
-
+
diff --git a/Views/Games/Edit.cshtml b/Views/Games/Edit.cshtml index 9fc383e..c5f15a3 100644 --- a/Views/Games/Edit.cshtml +++ b/Views/Games/Edit.cshtml @@ -92,8 +92,8 @@ @ByteSizeLib.ByteSize.FromBytes(new FileInfo(System.IO.Path.Combine("Upload", archive.ObjectKey)).Length) @@ -108,7 +108,7 @@

No Archives

There have been no archives uploaded for this game.

}