Added max file size upload option. Switched from stream upload to multipart form content file upload

This commit is contained in:
2025-03-07 13:31:30 +01:00
parent 9fb1667bf0
commit f23320eb1c
5 changed files with 36 additions and 3 deletions

View File

@@ -10,6 +10,7 @@ public class AppConfiguration
public AuthenticationConfig Authentication { get; set; } = new();
public DevelopmentConfig Development { get; set; } = new();
public ClientConfig Client { get; set; } = new();
public KestrelConfig Kestrel { get; set; } = new();
public class ClientConfig
{
@@ -48,4 +49,9 @@ public class AppConfiguration
{
public bool EnableApiDocs { get; set; } = false;
}
public class KestrelConfig
{
public int UploadLimit { get; set; } = 100;
}
}

View File

@@ -3,8 +3,10 @@ using ICSharpCode.SharpZipLib.GZip;
using ICSharpCode.SharpZipLib.Tar;
using ICSharpCode.SharpZipLib.Zip;
using Microsoft.AspNetCore.Mvc;
using MoonCore.Exceptions;
using MoonCore.Extended.PermFilter;
using MoonCore.Helpers;
using Moonlight.ApiServer.Configuration;
using Moonlight.Shared.Http.Requests.Admin.Sys.Files;
using Moonlight.Shared.Http.Responses.Admin.Sys;
@@ -65,7 +67,11 @@ public class FilesController : Controller
[HttpPost("create")]
public async Task Create([FromQuery] string path)
{
var stream = Request.Body;
if (Request.Form.Files.Count != 1)
throw new HttpApiException("You need to provide exactly one file", 400);
var file = Request.Form.Files[0];
var stream = file.OpenReadStream();
var safePath = SanitizePath(path);
var physicalPath = PathBuilder.File(BaseDirectory, safePath);

View File

@@ -71,6 +71,7 @@ public class Startup
await CreateWebApplicationBuilder();
await ConfigureKestrel();
await RegisterAppConfiguration();
await RegisterLogging();
await RegisterBase();
@@ -183,6 +184,20 @@ public class Startup
return Task.CompletedTask;
}
private Task ConfigureKestrel()
{
WebApplicationBuilder.WebHost.ConfigureKestrel(kestrelOptions =>
{
var maxUploadInBytes = ByteConverter
.FromMegaBytes(Configuration.Kestrel.UploadLimit)
.Bytes;
kestrelOptions.Limits.MaxRequestBodySize = maxUploadInBytes;
});
return Task.CompletedTask;
}
#endregion
#region Plugin Loading

View File

@@ -47,7 +47,13 @@ public class SysFileSystemProvider : IFileSystemProvider, ICompressFileSystemPro
}
public async Task Create(string path, Stream stream)
=> await HttpApiClient.PostStream($"{BaseApiUrl}/create?path={path}", stream);
{
var content = new MultipartFormDataContent();
content.Add(new StreamContent(stream), "file", "x");
await HttpApiClient.Post($"{BaseApiUrl}/create?path={path}", content);
}
public async Task Move(string oldPath, string newPath)
=> await HttpApiClient.Post($"{BaseApiUrl}/move?oldPath={oldPath}&newPath={newPath}");

View File

@@ -27,7 +27,7 @@
<PackageReference Include="MoonCore" Version="1.8.5" />
<PackageReference Include="MoonCore.Blazor" Version="1.2.9" />
<PackageReference Include="MoonCore.PluginFramework" Version="1.0.5"/>
<PackageReference Include="MoonCore.Blazor.Tailwind" Version="1.3.3" />
<PackageReference Include="MoonCore.Blazor.Tailwind" Version="1.3.4" />
</ItemGroup>
<!--