Cleaned up ArchivesController
This commit is contained in:
parent
bf8c7752cb
commit
ed83489683
1 changed files with 52 additions and 78 deletions
|
@ -2,6 +2,7 @@
|
||||||
using LANCommander.Data.Models;
|
using LANCommander.Data.Models;
|
||||||
using LANCommander.Extensions;
|
using LANCommander.Extensions;
|
||||||
using LANCommander.Models;
|
using LANCommander.Models;
|
||||||
|
using LANCommander.Services;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using System.IO.Compression;
|
using System.IO.Compression;
|
||||||
|
@ -14,20 +15,22 @@ namespace LANCommander.Controllers
|
||||||
public class ArchivesController : Controller
|
public class ArchivesController : Controller
|
||||||
{
|
{
|
||||||
private readonly DatabaseContext Context;
|
private readonly DatabaseContext Context;
|
||||||
|
private readonly GameService GameService;
|
||||||
|
private readonly ArchiveService ArchiveService;
|
||||||
|
|
||||||
public ArchivesController(DatabaseContext context)
|
public ArchivesController(DatabaseContext context, GameService gameService, ArchiveService archiveService)
|
||||||
{
|
{
|
||||||
Context = context;
|
Context = context;
|
||||||
|
GameService = gameService;
|
||||||
|
ArchiveService = archiveService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IActionResult> Add(Guid? id)
|
public async Task<IActionResult> Add(Guid? id)
|
||||||
{
|
{
|
||||||
if (id == null || Context.Games == null)
|
if (id == null)
|
||||||
return NotFound();
|
return NotFound();
|
||||||
|
|
||||||
using (Repository<Game> repo = new Repository<Game>(Context, HttpContext))
|
var game = await GameService.Get(id.GetValueOrDefault());
|
||||||
{
|
|
||||||
var game = await repo.Find(id.GetValueOrDefault());
|
|
||||||
|
|
||||||
Archive lastVersion = null;
|
Archive lastVersion = null;
|
||||||
|
|
||||||
|
@ -40,16 +43,13 @@ namespace LANCommander.Controllers
|
||||||
LastVersion = lastVersion,
|
LastVersion = lastVersion,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public async Task<IActionResult> Add(Guid? id, Archive archive)
|
public async Task<IActionResult> Add(Guid? id, Archive archive)
|
||||||
{
|
{
|
||||||
archive.Id = Guid.Empty;
|
archive.Id = Guid.Empty;
|
||||||
|
|
||||||
using (Repository<Game> gameRepo = new Repository<Game>(Context, HttpContext))
|
var game = await GameService.Get(id.GetValueOrDefault());
|
||||||
{
|
|
||||||
var game = await gameRepo.Find(id.GetValueOrDefault());
|
|
||||||
|
|
||||||
archive.Game = game;
|
archive.Game = game;
|
||||||
|
|
||||||
|
@ -58,54 +58,36 @@ namespace LANCommander.Controllers
|
||||||
|
|
||||||
if (ModelState.IsValid)
|
if (ModelState.IsValid)
|
||||||
{
|
{
|
||||||
using (Repository<Archive> archiveRepo = new Repository<Archive>(Context, HttpContext))
|
await ArchiveService.Update(archive);
|
||||||
{
|
|
||||||
archive = await archiveRepo.Add(archive);
|
|
||||||
await archiveRepo.SaveChanges();
|
|
||||||
}
|
|
||||||
|
|
||||||
return RedirectToAction("Edit", "Games", new { id = id });
|
return RedirectToAction("Edit", "Games", new { id = id });
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return View(archive);
|
return View(archive);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IActionResult> Download(Guid id)
|
public async Task<IActionResult> Download(Guid id)
|
||||||
{
|
{
|
||||||
using (Repository<Archive> repo = new Repository<Archive>(Context, HttpContext))
|
var archive = await ArchiveService.Get(id);
|
||||||
{
|
|
||||||
var archive = await repo.Find(id);
|
|
||||||
|
|
||||||
var content = new FileStream(Path.Combine("Upload", archive.ObjectKey), FileMode.Open, FileAccess.Read, FileShare.Read);
|
var content = new FileStream($"Upload/{archive.ObjectKey}".ToPath(), FileMode.Open, FileAccess.Read, FileShare.Read);
|
||||||
|
|
||||||
return File(content, "application/octet-stream", $"{archive.Game.Title.SanitizeFilename()}.zip");
|
return File(content, "application/octet-stream", $"{archive.Game.Title.SanitizeFilename()}.zip");
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<IActionResult> Delete(Guid? id)
|
public async Task<IActionResult> Delete(Guid? id)
|
||||||
{
|
{
|
||||||
Guid gameId;
|
var archive = await ArchiveService.Get(id.GetValueOrDefault());
|
||||||
|
var gameId = archive.Game.Id;
|
||||||
|
|
||||||
using (Repository<Archive> repo = new Repository<Archive>(Context, HttpContext))
|
await ArchiveService.Delete(archive);
|
||||||
{
|
|
||||||
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 });
|
return RedirectToAction("Edit", "Games", new { id = gameId });
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IActionResult> Validate(Guid id, Archive archive)
|
public async Task<IActionResult> Validate(Guid id, Archive archive)
|
||||||
{
|
{
|
||||||
var path = Path.Combine("Upload", id.ToString());
|
var path = $"Upload/{id}".ToPath();
|
||||||
|
|
||||||
string manifestContents = String.Empty;
|
string manifestContents = String.Empty;
|
||||||
long compressedSize = 0;
|
long compressedSize = 0;
|
||||||
|
@ -163,25 +145,18 @@ namespace LANCommander.Controllers
|
||||||
return BadRequest("The manifest file is invalid or corrupt.");
|
return BadRequest("The manifest file is invalid or corrupt.");
|
||||||
}
|
}
|
||||||
|
|
||||||
using (var repo = new Repository<Game>(Context, HttpContext))
|
var game = await GameService.Get(archive.Game.Id);
|
||||||
{
|
|
||||||
var game = await repo.Find(archive.Game.Id);
|
|
||||||
|
|
||||||
if (game == null)
|
if (game == null)
|
||||||
return BadRequest("The related game is missing or corrupt.");
|
return BadRequest("The related game is missing or corrupt.");
|
||||||
|
|
||||||
archive.Game = game;
|
archive.Game = game;
|
||||||
}
|
|
||||||
|
|
||||||
using (var repo = new Repository<Archive>(Context, HttpContext))
|
|
||||||
{
|
|
||||||
archive.Id = Guid.Empty;
|
archive.Id = Guid.Empty;
|
||||||
archive.CompressedSize = compressedSize;
|
archive.CompressedSize = compressedSize;
|
||||||
archive.UncompressedSize = uncompressedSize;
|
archive.UncompressedSize = uncompressedSize;
|
||||||
archive.ObjectKey = id.ToString();
|
archive.ObjectKey = id.ToString();
|
||||||
|
|
||||||
archive = await repo.Add(archive);
|
archive = await ArchiveService.Update(archive);
|
||||||
await repo.SaveChanges();
|
|
||||||
|
|
||||||
return Json(new
|
return Json(new
|
||||||
{
|
{
|
||||||
|
@ -191,4 +166,3 @@ namespace LANCommander.Controllers
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue