Add ability to specify storage path for archives

dhcp-server
Pat Hartl 2023-09-12 19:41:25 -05:00
parent c725afc362
commit 026efd586a
10 changed files with 91 additions and 22 deletions

View File

@ -1,6 +1,8 @@
using LANCommander.Data; using LANCommander.Data;
using LANCommander.Data.Models; using LANCommander.Data.Models;
using LANCommander.Extensions; using LANCommander.Extensions;
using LANCommander.Models;
using LANCommander.Services;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
@ -12,7 +14,8 @@ namespace LANCommander.Controllers.Api
[ApiController] [ApiController]
public class ArchivesController : ControllerBase public class ArchivesController : ControllerBase
{ {
private DatabaseContext Context; private readonly DatabaseContext Context;
private readonly LANCommanderSettings Settings = SettingService.GetSettings();
public ArchivesController(DatabaseContext context) public ArchivesController(DatabaseContext context)
{ {
@ -47,7 +50,7 @@ namespace LANCommander.Controllers.Api
if (archive == null) if (archive == null)
return NotFound(); return NotFound();
var filename = Path.Combine("Upload", archive.ObjectKey); var filename = Path.Combine(Settings.Archives.StoragePath, archive.ObjectKey);
if (!System.IO.File.Exists(filename)) if (!System.IO.File.Exists(filename))
return NotFound(); return NotFound();

View File

@ -15,6 +15,7 @@ namespace LANCommander.Controllers.Api
public class GamesController : ControllerBase public class GamesController : ControllerBase
{ {
private readonly GameService GameService; private readonly GameService GameService;
private readonly LANCommanderSettings Settings = SettingService.GetSettings();
public GamesController(GameService gameService) public GamesController(GameService gameService)
{ {
@ -59,7 +60,7 @@ namespace LANCommander.Controllers.Api
var archive = game.Archives.OrderByDescending(a => a.CreatedOn).First(); var archive = game.Archives.OrderByDescending(a => a.CreatedOn).First();
var filename = Path.Combine("Upload", archive.ObjectKey); var filename = Path.Combine(Settings.Archives.StoragePath, archive.ObjectKey);
if (!System.IO.File.Exists(filename)) if (!System.IO.File.Exists(filename))
return NotFound(); return NotFound();

View File

@ -1,4 +1,5 @@
using LANCommander.Models; using LANCommander.Models;
using LANCommander.Services;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
@ -8,18 +9,18 @@ namespace LANCommander.Controllers.Api
[ApiController] [ApiController]
public class UploadController : ControllerBase public class UploadController : ControllerBase
{ {
private const string UploadDirectory = "Upload"; private readonly LANCommanderSettings Settings = SettingService.GetSettings();
[HttpPost("Init")] [HttpPost("Init")]
public string Init() public string Init()
{ {
var key = Guid.NewGuid().ToString(); var key = Guid.NewGuid().ToString();
if (!Directory.Exists(UploadDirectory)) if (!Directory.Exists(Settings.Archives.StoragePath))
Directory.CreateDirectory(UploadDirectory); Directory.CreateDirectory(Settings.Archives.StoragePath);
if (!System.IO.File.Exists(Path.Combine(UploadDirectory, key))) if (!System.IO.File.Exists(Path.Combine(Settings.Archives.StoragePath, key)))
System.IO.File.Create(Path.Combine(UploadDirectory, key)).Close(); System.IO.File.Create(Path.Combine(Settings.Archives.StoragePath, key)).Close();
return key; return key;
} }
@ -27,7 +28,7 @@ namespace LANCommander.Controllers.Api
[HttpPost("Chunk")] [HttpPost("Chunk")]
public async Task Chunk([FromForm] ChunkUpload chunk) public async Task Chunk([FromForm] ChunkUpload chunk)
{ {
var filePath = Path.Combine(UploadDirectory, chunk.Key.ToString()); var filePath = Path.Combine(Settings.Archives.StoragePath, chunk.Key.ToString());
if (!System.IO.File.Exists(filePath)) if (!System.IO.File.Exists(filePath))
throw new Exception("Destination file not initialized."); throw new Exception("Destination file not initialized.");

View File

@ -12,6 +12,7 @@ namespace LANCommander.Controllers
public class DownloadController : Controller public class DownloadController : Controller
{ {
private readonly ArchiveService ArchiveService; private readonly ArchiveService ArchiveService;
private readonly LANCommanderSettings Settings = SettingService.GetSettings();
public DownloadController(ArchiveService archiveService) public DownloadController(ArchiveService archiveService)
{ {
@ -25,7 +26,7 @@ namespace LANCommander.Controllers
if (archive == null) if (archive == null)
return NotFound(); return NotFound();
var filename = Path.Combine("Upload", archive.ObjectKey); var filename = Path.Combine(Settings.Archives.StoragePath, archive.ObjectKey);
if (!System.IO.File.Exists(filename)) if (!System.IO.File.Exists(filename))
return NotFound(); return NotFound();

View File

@ -1,4 +1,5 @@
using LANCommander.Models; using LANCommander.Models;
using LANCommander.Services;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
@ -10,20 +11,19 @@ namespace LANCommander.Controllers
public class UploadController : Controller public class UploadController : Controller
{ {
protected readonly Logger Logger = LogManager.GetCurrentClassLogger(); protected readonly Logger Logger = LogManager.GetCurrentClassLogger();
private readonly LANCommanderSettings Settings = SettingService.GetSettings();
private const string UploadDirectory = "Upload";
public JsonResult Init() public JsonResult Init()
{ {
var key = Guid.NewGuid().ToString(); var key = Guid.NewGuid().ToString();
if (!Directory.Exists(UploadDirectory)) if (!Directory.Exists(Settings.Archives.StoragePath))
Directory.CreateDirectory(UploadDirectory); Directory.CreateDirectory(Settings.Archives.StoragePath);
if (!System.IO.File.Exists(Path.Combine(UploadDirectory, key))) if (!System.IO.File.Exists(Path.Combine(Settings.Archives.StoragePath, key)))
System.IO.File.Create(Path.Combine(UploadDirectory, key)).Close(); System.IO.File.Create(Path.Combine(Settings.Archives.StoragePath, key)).Close();
else else
System.IO.File.Delete(Path.Combine(UploadDirectory, key)); System.IO.File.Delete(Path.Combine(Settings.Archives.StoragePath, key));
return Json(new return Json(new
{ {
@ -33,7 +33,7 @@ namespace LANCommander.Controllers
public async Task<IActionResult> Chunk([FromForm] ChunkUpload chunk) public async Task<IActionResult> Chunk([FromForm] ChunkUpload chunk)
{ {
var filePath = Path.Combine(UploadDirectory, chunk.Key.ToString()); var filePath = Path.Combine(Settings.Archives.StoragePath, chunk.Key.ToString());
if (!System.IO.File.Exists(filePath)) if (!System.IO.File.Exists(filePath))
return BadRequest("Destination file not initialized."); return BadRequest("Destination file not initialized.");

View File

@ -35,6 +35,7 @@
public class LANCommanderArchiveSettings public class LANCommanderArchiveSettings
{ {
public bool EnablePatching { get; set; } = false; public bool EnablePatching { get; set; } = false;
public string StoragePath { get; set; } = "Uploads";
} }
public class LANCommanderIPXRelaySettings public class LANCommanderIPXRelaySettings

View File

@ -32,12 +32,13 @@
protected override async Task OnInitializedAsync() protected override async Task OnInitializedAsync()
{ {
var settings = SettingService.GetSettings();
var drives = DriveInfo.GetDrives(); var drives = DriveInfo.GetDrives();
var root = Path.GetPathRoot(System.Reflection.Assembly.GetExecutingAssembly().Location); var root = Path.GetPathRoot(System.Reflection.Assembly.GetExecutingAssembly().Location);
var totalStorageSize = drives.Where(d => d.IsReady && d.Name == root).Sum(d => d.TotalSize); var totalStorageSize = drives.Where(d => d.IsReady && d.Name == root).Sum(d => d.TotalSize);
var totalAvailableFreeSpace = drives.Where(d => d.IsReady && d.Name == root).Sum(d => d.AvailableFreeSpace); var totalAvailableFreeSpace = drives.Where(d => d.IsReady && d.Name == root).Sum(d => d.AvailableFreeSpace);
var totalUploadDirectorySize = new DirectoryInfo("Upload").EnumerateFiles().Sum(f => f.Length); var totalUploadDirectorySize = new DirectoryInfo(settings.Archives.StoragePath).EnumerateFiles().Sum(f => f.Length);
var totalSaveDirectorySize = new DirectoryInfo("Saves").EnumerateFiles().Sum(f => f.Length); var totalSaveDirectorySize = new DirectoryInfo("Saves").EnumerateFiles().Sum(f => f.Length);
Data = new object[] Data = new object[]

View File

@ -0,0 +1,59 @@
@page "/Settings/Archives"
@using LANCommander.Components.FileManagerComponents;
@using LANCommander.Models;
@layout SettingsLayout
@inject SettingService SettingService
@inject IMessageService MessageService
@attribute [Authorize(Roles = "Administrator")]
<PageHeader Title="Archives" />
<div style="padding: 0 24px;">
<Form Model="Settings" Layout="@FormLayout.Vertical">
<FormItem Label="Enable Patching">
<Switch @bind-Checked="context.Archives.EnablePatching" />
</FormItem>
<FormItem Label="Storage Path">
<FilePicker Root="@RootPath" EntrySelectable="x => x is FileManagerDirectory" @bind-Value="@context.Archives.StoragePath" OkText="Select Path" Title="Choose Path" OnSelected="OnPathSelected" />
</FormItem>
<FormItem>
<Button OnClick="Save" Type="@ButtonType.Primary">Save</Button>
</FormItem>
</Form>
</div>
@code {
LANCommanderSettings Settings;
string RootPath = Path.GetPathRoot(Directory.GetCurrentDirectory());
protected override async Task OnInitializedAsync()
{
Settings = SettingService.GetSettings();
}
private void Save()
{
try
{
SettingService.SaveSettings(Settings);
MessageService.Success("Settings saved!");
}
catch
{
MessageService.Error("An unknown error occurred.");
}
}
private void OnPathSelected(string path)
{
var appPath = Directory.GetCurrentDirectory();
if (path != null && path.StartsWith(appPath))
path = path.Substring(appPath.Length).TrimStart(Path.DirectorySeparatorChar).TrimEnd(Path.DirectorySeparatorChar);
Settings.Archives.StoragePath = path;
}
}

View File

@ -34,6 +34,8 @@
ICollection<OrphanedFile> Orphans = new List<OrphanedFile>(); ICollection<OrphanedFile> Orphans = new List<OrphanedFile>();
bool Loading = true; bool Loading = true;
private readonly LANCommanderSettings Settings = SettingService.GetSettings();
protected override async Task OnInitializedAsync() protected override async Task OnInitializedAsync()
{ {
await LoadData(); await LoadData();
@ -47,7 +49,7 @@
var archives = await ArchiveService.Get(); var archives = await ArchiveService.Get();
var archiveFiles = archives.Select(a => ArchiveService.GetArchiveFileLocation(a)); var archiveFiles = archives.Select(a => ArchiveService.GetArchiveFileLocation(a));
var files = Directory.GetFiles("Upload"); var files = Directory.GetFiles(Settings.Archives.StoragePath);
foreach (var file in files.Where(f => !archiveFiles.Contains(f))) foreach (var file in files.Where(f => !archiveFiles.Contains(f)))
{ {

View File

@ -184,8 +184,8 @@ app.UseEndpoints(endpoints =>
}); });
Logger.Debug("Ensuring required directories exist"); Logger.Debug("Ensuring required directories exist");
if (!Directory.Exists("Upload")) if (!Directory.Exists(settings.Archives.StoragePath))
Directory.CreateDirectory("Upload"); Directory.CreateDirectory(settings.Archives.StoragePath);
if (!Directory.Exists("Icon")) if (!Directory.Exists("Icon"))
Directory.CreateDirectory("Icon"); Directory.CreateDirectory("Icon");