Skip to content
Snippets Groups Projects
Commit 25aceeba authored by Janis Daniel Dähne's avatar Janis Daniel Dähne
Browse files

- limited file upload size to default 5MB

parent 2173544c
No related branches found
No related tags found
No related merge requests found
......@@ -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");
}
......
......@@ -189,6 +189,9 @@ namespace ClientServer.Helpers
public static string DeployOriginUrlsKey = "DeployOriginUrls";
public static string MaxUploadFileSizeInByteKey = "MaxUploadFileSizeInByte";
//--- some core constants ---
......
......@@ -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]}";
}
}
......
......@@ -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.
});
......
......@@ -22,6 +22,7 @@
"IsSlave": false,
"EnsureFileUploadDirStructure": false,
"OnlySecureFlaggedCookies": false,
"IsInitControllerEnabled": true
"IsInitControllerEnabled": true,
"MaxUploadFileSizeInByte": "1048576"
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment