diff --git a/src/ClientServer/Config/AppConfiguration.cs b/src/ClientServer/Config/AppConfiguration.cs index 7ed50c4aadc509a2ac4b54072320f4d54267593d..06386791934d4e57eb54557f941bd9d5fb19c76f 100644 --- a/src/ClientServer/Config/AppConfiguration.cs +++ b/src/ClientServer/Config/AppConfiguration.cs @@ -144,6 +144,9 @@ namespace ClientServer.Helpers /// </summary> public static List<string> DeployOriginUrls = new List<string>(); + + //5 mb? + public static long MaxUploadFileSizeInByte = 1024 * 1024 * 5; //--- methods --- @@ -351,6 +354,18 @@ namespace ClientServer.Helpers overwriteCount++; } } + + if (string.IsNullOrWhiteSpace(ConfigurationRoot[Constants.MaxUploadFileSizeInByteKey]) == false) + { + var temp = ConfigurationRoot[Constants.MaxUploadFileSizeInByteKey]; + long tempValue; + if (long.TryParse(temp, out tempValue)) + { + //keep the setting + AppConfiguration.MaxUploadFileSizeInByte = tempValue; + overwriteCount++; + } + } Console.WriteLine($"[INFO] {overwriteCount} app settings overwritten by appsettings.json"); } diff --git a/src/ClientServer/Config/Constants.cs b/src/ClientServer/Config/Constants.cs index c5f33145cac872b4673e305f6e0bef2b8dd25dba..3acea413b8305d1e691c7738b344d67450e5ffe6 100644 --- a/src/ClientServer/Config/Constants.cs +++ b/src/ClientServer/Config/Constants.cs @@ -189,6 +189,9 @@ namespace ClientServer.Helpers public static string DeployOriginUrlsKey = "DeployOriginUrls"; + public static string MaxUploadFileSizeInByteKey = "MaxUploadFileSizeInByte"; + + //--- some core constants --- diff --git a/src/ClientServer/Helpers/Files.cs b/src/ClientServer/Helpers/Files.cs index 080eeda25ea5f4d498629fea299f4aa370f3a6e8..0626930bd921bc14c8d95a81edb972bbc5e534f1 100644 --- a/src/ClientServer/Helpers/Files.cs +++ b/src/ClientServer/Helpers/Files.cs @@ -305,6 +305,29 @@ namespace ClientServer.Helpers return fileDict; } + + const int k_base = 1024; + private static readonly string[] sizesBases = new[] {"Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"}; + + //from http://stackoverflow.com/questions/15900485/correct-way-to-convert-size-in-bytes-to-kb-mb-gb-in-javascript + /// <summary> + /// converts the given size to a more human readable size + /// </summary> + /// <param name="sizeInBytes"></param> + public static string GetHumanReadableSize(long sizeInBytes, int decimalPlaces = 2) + { + if (sizeInBytes == 0) return "0 Byte"; + + //e.g. 1024^x = sizeInBytes ( + //how often can we fit in 1024? this is the size base + int sizeBaseIndex = (int)Math.Floor(Math.Log(sizeInBytes, k_base)); + + //get the remainder e.g. 1025/1024 --> 1.0009765625 + double value = sizeInBytes / Math.Pow(k_base, sizeBaseIndex); + + + return $"{Math.Round(value, decimalPlaces)} {sizesBases[sizeBaseIndex]}"; + } } diff --git a/src/ClientServer/Startup.cs b/src/ClientServer/Startup.cs index 89c618e873dfd39b07c705382c2eea5bb89d6183..03bae7dbbc891fc8e92dfc1f82ddc8071a67f4b7 100644 --- a/src/ClientServer/Startup.cs +++ b/src/ClientServer/Startup.cs @@ -126,7 +126,10 @@ namespace ClientServer //}); - //IDataAccessProvider + services.Configure<FormOptions>(o => + { + o.MultipartBodyLengthLimit = AppConfiguration.MaxUploadFileSizeInByte; + }); // Add framework services. services.AddMvc(options => @@ -215,7 +218,23 @@ namespace ClientServer context.Response.ContentType = "application/json"; } - await next.Invoke(); + try + { + await next.Invoke(); + } + catch (InvalidDataException e) //only catches this exception type + { +// Console.WriteLine(e); + context.Response.ContentType = "application/json"; + context.Response.StatusCode = 200; + await + context.Response.WriteAsync( + Jc.Serialize( + new BasicResponse(ResponseCode.ServerError, $"file content size (multipart body) exceeded the limit of " + + $"{AppConfiguration.MaxUploadFileSizeInByte} Bytes (~{Files.GetHumanReadableSize(AppConfiguration.MaxUploadFileSizeInByte)})"))); + return; + } + // Do logging or other work that doesn't write to the Response. }); diff --git a/src/ClientServer/appsettings.json b/src/ClientServer/appsettings.json index 53a31a7e6e9512e17c03d4b8d81f25bf328d8917..52d6736f3ef98dee1af57dbc0fc95c2c06444a03 100644 --- a/src/ClientServer/appsettings.json +++ b/src/ClientServer/appsettings.json @@ -22,6 +22,7 @@ "IsSlave": false, "EnsureFileUploadDirStructure": false, "OnlySecureFlaggedCookies": false, - "IsInitControllerEnabled": true + "IsInitControllerEnabled": true, + "MaxUploadFileSizeInByte": "1048576" }