Allow user saves storage path and max size to be configurable in settings

dhcp-server^2
Pat Hartl 2023-09-13 19:32:36 -05:00
parent 4476fbed5f
commit 4d7d0df9c0
9 changed files with 89 additions and 13 deletions

View File

@ -18,12 +18,14 @@ namespace LANCommander.Controllers.Api
private readonly GameSaveService GameSaveService;
private readonly GameService GameService;
private readonly UserManager<User> UserManager;
private readonly LANCommanderSettings Settings;
public GameSavesController(GameSaveService gameSaveService, GameService gameService, UserManager<User> userManager)
{
GameSaveService = gameSaveService;
GameService = gameService;
UserManager = userManager;
Settings = SettingService.GetSettings();
}
[HttpGet("{id}")]
@ -65,7 +67,7 @@ namespace LANCommander.Controllers.Api
public async Task<IActionResult> Upload(Guid id, [FromForm] SaveUpload save)
{
// Arbitrary file size limit of 25MB
if (save.File.Length > (ByteSizeLib.ByteSize.BytesInMebiByte * 25))
if (save.File.Length > (ByteSizeLib.ByteSize.BytesInMebiByte * Settings.UserSaves.MaxSize))
return BadRequest("Save file archive is too large");
var game = await GameService.Get(id);

View File

@ -1,4 +1,5 @@
using System.ComponentModel.DataAnnotations.Schema;
using LANCommander.Services;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text.Json.Serialization;
namespace LANCommander.Data.Models
@ -19,7 +20,9 @@ namespace LANCommander.Data.Models
public string GetUploadPath()
{
return Path.Combine("Saves", UserId.ToString(), GameId.ToString(), Id.ToString());
var settings = SettingService.GetSettings();
return Path.Combine(settings.UserSaves.StoragePath, UserId.ToString(), GameId.ToString(), Id.ToString());
}
}
}

View File

@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Identity;
using LANCommander.Services;
using Microsoft.AspNetCore.Identity;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text.Json.Serialization;
@ -53,7 +54,9 @@ namespace LANCommander.Data.Models
public string GetGameSaveUploadPath()
{
return Path.Combine("Saves", Id.ToString());
var settings = SettingService.GetSettings();
return Path.Combine(settings.UserSaves.StoragePath, Id.ToString());
}
}
}

View File

@ -16,6 +16,7 @@
public LANCommanderTheme Theme { get; set; } = LANCommanderTheme.Light;
public LANCommanderAuthenticationSettings Authentication { get; set; } = new LANCommanderAuthenticationSettings();
public LANCommanderUserSaveSettings UserSaves { get; set; } = new LANCommanderUserSaveSettings();
public LANCommanderArchiveSettings Archives { get; set; } = new LANCommanderArchiveSettings();
public LANCommanderIPXRelaySettings IPXRelay { get; set; } = new LANCommanderIPXRelaySettings();
}
@ -32,6 +33,12 @@
public int PasswordRequiredLength { get; set; } = 8;
}
public class LANCommanderUserSaveSettings
{
public string StoragePath { get; set; } = "Saves";
public int MaxSize { get; set; } = 25;
}
public class LANCommanderArchiveSettings
{
public bool EnablePatching { get; set; } = false;

View File

@ -39,7 +39,7 @@
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 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(settings.UserSaves.StoragePath).EnumerateFiles().Sum(f => f.Length);
Data = new object[]
{

View File

@ -8,6 +8,7 @@
<MenuItem RouterLink="/Settings/General">General</MenuItem>
<MenuItem RouterLink="/Settings/Users">Users</MenuItem>
<MenuItem RouterLink="/Settings/Authentication">Authentication</MenuItem>
<MenuItem RouterLink="/Settings/Archives">User Saves</MenuItem>
<MenuItem RouterLink="/Settings/Archives">Archives</MenuItem>
<MenuItem RouterLink="/Settings/IPXRelay">IPX Relay</MenuItem>
<MenuItem RouterLink="/Settings/Tools" RouterMatch="@NavLinkMatch.Prefix">Tools</MenuItem>

View File

@ -0,0 +1,59 @@
@page "/Settings/UserSaves"
@using LANCommander.Components.FileManagerComponents;
@using LANCommander.Models;
@layout SettingsLayout
@inject SettingService SettingService
@inject IMessageService MessageService
@attribute [Authorize(Roles = "Administrator")]
<PageHeader Title="User Saves" />
<div style="padding: 0 24px;">
<Form Model="Settings" Layout="@FormLayout.Vertical">
<FormItem Label="Storage Path">
<FilePicker Root="@RootPath" EntrySelectable="x => x is FileManagerDirectory" @bind-Value="@context.UserSaves.StoragePath" OkText="Select Path" Title="Choose Path" OnSelected="OnPathSelected" />
</FormItem>
<FormItem Label="Max Size (MB)">
<AntDesign.InputNumber @bind-Value="context.UserSaves.MaxSize" Min="1" DefaultValue="25" />
</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.UserSaves.StoragePath = path;
}
}

View File

@ -190,8 +190,8 @@ if (!Directory.Exists(settings.Archives.StoragePath))
if (!Directory.Exists("Icon"))
Directory.CreateDirectory("Icon");
if (!Directory.Exists("Saves"))
Directory.CreateDirectory("Saves");
if (!Directory.Exists(settings.UserSaves.StoragePath))
Directory.CreateDirectory(settings.UserSaves.StoragePath);
if (!Directory.Exists("Snippets"))
Directory.CreateDirectory("Snippets");

View File

@ -1,16 +1,17 @@
using LANCommander.Data;
using LANCommander.Data.Models;
using LANCommander.Helpers;
using LANCommander.Models;
namespace LANCommander.Services
{
public class GameSaveService : BaseDatabaseService<GameSave>
{
private readonly SettingService SettingService;
private readonly LANCommanderSettings Settings;
public GameSaveService(DatabaseContext dbContext, IHttpContextAccessor httpContextAccessor, SettingService settingService) : base(dbContext, httpContextAccessor)
public GameSaveService(DatabaseContext dbContext, IHttpContextAccessor httpContextAccessor) : base(dbContext, httpContextAccessor)
{
SettingService = settingService;
Settings = SettingService.GetSettings();
}
public override Task Delete(GameSave entity)
@ -36,14 +37,14 @@ namespace LANCommander.Services
var save = Get(gs => gs.Id == id).FirstOrDefault();
if (save == null)
return null;;
return null;
return GetSavePath(save);
}
public string GetSavePath(GameSave save)
{
return Path.Combine("Save", save.UserId.ToString(), $"{save.Id}.zip");
return Path.Combine(Settings.UserSaves.StoragePath, save.UserId.ToString(), $"{save.Id}.zip");
}
}
}