diff --git a/LANCommander/Controllers/Api/GameSavesController.cs b/LANCommander/Controllers/Api/GameSavesController.cs index e9800c9..2717de5 100644 --- a/LANCommander/Controllers/Api/GameSavesController.cs +++ b/LANCommander/Controllers/Api/GameSavesController.cs @@ -18,12 +18,14 @@ namespace LANCommander.Controllers.Api private readonly GameSaveService GameSaveService; private readonly GameService GameService; private readonly UserManager UserManager; + private readonly LANCommanderSettings Settings; public GameSavesController(GameSaveService gameSaveService, GameService gameService, UserManager userManager) { GameSaveService = gameSaveService; GameService = gameService; UserManager = userManager; + Settings = SettingService.GetSettings(); } [HttpGet("{id}")] @@ -65,7 +67,7 @@ namespace LANCommander.Controllers.Api public async Task 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); diff --git a/LANCommander/Data/Models/GameSave.cs b/LANCommander/Data/Models/GameSave.cs index 1e232d3..a42f4e8 100644 --- a/LANCommander/Data/Models/GameSave.cs +++ b/LANCommander/Data/Models/GameSave.cs @@ -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()); } } } diff --git a/LANCommander/Data/Models/User.cs b/LANCommander/Data/Models/User.cs index 7c98774..8cd057b 100644 --- a/LANCommander/Data/Models/User.cs +++ b/LANCommander/Data/Models/User.cs @@ -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()); } } } diff --git a/LANCommander/Models/Settings.cs b/LANCommander/Models/Settings.cs index 4002199..fc10f1c 100644 --- a/LANCommander/Models/Settings.cs +++ b/LANCommander/Models/Settings.cs @@ -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; diff --git a/LANCommander/Pages/Dashboard/Charts/StorageUsage.razor b/LANCommander/Pages/Dashboard/Charts/StorageUsage.razor index 3913cea..be31623 100644 --- a/LANCommander/Pages/Dashboard/Charts/StorageUsage.razor +++ b/LANCommander/Pages/Dashboard/Charts/StorageUsage.razor @@ -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[] { diff --git a/LANCommander/Pages/Settings/SettingsLayout.razor b/LANCommander/Pages/Settings/SettingsLayout.razor index 6386acd..fa3b888 100644 --- a/LANCommander/Pages/Settings/SettingsLayout.razor +++ b/LANCommander/Pages/Settings/SettingsLayout.razor @@ -8,6 +8,7 @@ General Users Authentication + User Saves Archives IPX Relay Tools diff --git a/LANCommander/Pages/Settings/UserSaves.razor b/LANCommander/Pages/Settings/UserSaves.razor new file mode 100644 index 0000000..e00e81f --- /dev/null +++ b/LANCommander/Pages/Settings/UserSaves.razor @@ -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")] + + + +
+
+ + + + + + + + + + + +
+
+ +@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; + } +} diff --git a/LANCommander/Program.cs b/LANCommander/Program.cs index 15e5697..afa8f52 100644 --- a/LANCommander/Program.cs +++ b/LANCommander/Program.cs @@ -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"); diff --git a/LANCommander/Services/GameSaveService.cs b/LANCommander/Services/GameSaveService.cs index d838359..65e6a57 100644 --- a/LANCommander/Services/GameSaveService.cs +++ b/LANCommander/Services/GameSaveService.cs @@ -1,16 +1,17 @@ using LANCommander.Data; using LANCommander.Data.Models; using LANCommander.Helpers; +using LANCommander.Models; namespace LANCommander.Services { public class GameSaveService : BaseDatabaseService { - 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"); } } }