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 }); } } }