Moved archive functionality to a separate controller
parent
7e309205c2
commit
d65ef4a280
|
@ -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<IActionResult> Add(Guid? id)
|
||||||
|
{
|
||||||
|
if (id == null || Context.Games == null)
|
||||||
|
return NotFound();
|
||||||
|
|
||||||
|
using (Repository<Game> repo = new Repository<Game>(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<IActionResult> Add(Guid? id, Archive archive)
|
||||||
|
{
|
||||||
|
archive.Id = Guid.Empty;
|
||||||
|
|
||||||
|
using (Repository<Game> gameRepo = new Repository<Game>(Context, HttpContext))
|
||||||
|
{
|
||||||
|
var game = await gameRepo.Find(id.GetValueOrDefault());
|
||||||
|
|
||||||
|
using (Repository<Archive> archiveRepo = new Repository<Archive>(Context, HttpContext))
|
||||||
|
{
|
||||||
|
archive.Game = game;
|
||||||
|
|
||||||
|
archive = await archiveRepo.Add(archive);
|
||||||
|
await archiveRepo.SaveChanges();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return RedirectToAction("Edit", "Games", new { id = id });
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<IActionResult> Download(Guid id)
|
||||||
|
{
|
||||||
|
using (Repository<Archive> repo = new Repository<Archive>(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<IActionResult> Delete(Guid? id)
|
||||||
|
{
|
||||||
|
Guid gameId;
|
||||||
|
|
||||||
|
using (Repository<Archive> repo = new Repository<Archive>(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 });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -168,48 +168,6 @@ namespace LANCommander.Controllers
|
||||||
return RedirectToAction(nameof(Index));
|
return RedirectToAction(nameof(Index));
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IActionResult> AddArchive(Guid? id)
|
|
||||||
{
|
|
||||||
if (id == null || Context.Games == null)
|
|
||||||
return NotFound();
|
|
||||||
|
|
||||||
using (Repository<Game> repo = new Repository<Game>(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<IActionResult> AddArchive(Guid? id, Archive archive)
|
|
||||||
{
|
|
||||||
archive.Id = Guid.Empty;
|
|
||||||
|
|
||||||
using (Repository<Game> gameRepo = new Repository<Game>(Context, HttpContext))
|
|
||||||
{
|
|
||||||
var game = await gameRepo.Find(id.GetValueOrDefault());
|
|
||||||
|
|
||||||
using (Repository<Archive> archiveRepo = new Repository<Archive>(Context, HttpContext))
|
|
||||||
{
|
|
||||||
archive.Game = game;
|
|
||||||
|
|
||||||
archive = await archiveRepo.Add(archive);
|
|
||||||
await archiveRepo.SaveChanges();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return RedirectToAction(nameof(Index));
|
|
||||||
}
|
|
||||||
|
|
||||||
private bool GameExists(Guid id)
|
private bool GameExists(Guid id)
|
||||||
{
|
{
|
||||||
return (Context.Games?.Any(e => e.Id == id)).GetValueOrDefault();
|
return (Context.Games?.Any(e => e.Id == id)).GetValueOrDefault();
|
||||||
|
|
|
@ -62,6 +62,11 @@ namespace LANCommander.Data
|
||||||
return entity;
|
return entity;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void Delete(T entity)
|
||||||
|
{
|
||||||
|
Context.Remove(entity);
|
||||||
|
}
|
||||||
|
|
||||||
public async Task SaveChanges()
|
public async Task SaveChanges()
|
||||||
{
|
{
|
||||||
await Context.SaveChangesAsync();
|
await Context.SaveChangesAsync();
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -10,7 +10,7 @@
|
||||||
<hr />
|
<hr />
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col-md-6">
|
<div class="col-md-6">
|
||||||
<form asp-action="AddArchive" enctype="multipart/form-data" >
|
<form asp-action="Add" enctype="multipart/form-data" >
|
||||||
<fieldset>
|
<fieldset>
|
||||||
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
|
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
|
||||||
<div class="form-group">
|
<div class="form-group">
|
|
@ -92,8 +92,8 @@
|
||||||
<td>@ByteSizeLib.ByteSize.FromBytes(new FileInfo(System.IO.Path.Combine("Upload", archive.ObjectKey)).Length)</td>
|
<td>@ByteSizeLib.ByteSize.FromBytes(new FileInfo(System.IO.Path.Combine("Upload", archive.ObjectKey)).Length)</td>
|
||||||
<td>
|
<td>
|
||||||
<div class="btn-list flex-nowrap">
|
<div class="btn-list flex-nowrap">
|
||||||
<a asp-action="DownloadArchive" asp-route-id="@archive.Id" class="btn">Download</a>
|
<a asp-action="Download" asp-controller="Archives" asp-route-id="@archive.Id" class="btn">Download</a>
|
||||||
<a asp-action="DeleteArchive" asp-route-id="@archive.Id" class="btn btn-danger">Delete</a>
|
<a asp-action="Delete" asp-controller="Archives" asp-route-id="@archive.Id" class="btn btn-danger">Delete</a>
|
||||||
</div>
|
</div>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
@ -108,7 +108,7 @@
|
||||||
<p class="empty-title">No Archives</p>
|
<p class="empty-title">No Archives</p>
|
||||||
<p class="empty-subtitle text-muted">There have been no archives uploaded for this game.</p>
|
<p class="empty-subtitle text-muted">There have been no archives uploaded for this game.</p>
|
||||||
<div class="empty-action">
|
<div class="empty-action">
|
||||||
<a asp-action="AddArchive" asp-route-id="@Model.Id" class="btn btn-primary">Upload Archive</a>
|
<a asp-action="Add" asp-controller="Archives" asp-route-id="@Model.Id" class="btn btn-primary">Upload Archive</a>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue