61 lines
2.5 KiB
C#
61 lines
2.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Kreta.Enums.ManualEnums.SystemSettings;
|
|
|
|
namespace Kreta.BusinessLogic.Logic.SystemSettings
|
|
{
|
|
public static class SystemSettingsLogic
|
|
{
|
|
public static long MaxFileSizeMB { get; } = 4;
|
|
public static long AllowedMaxHeight { get; } = 140;
|
|
public static long AllowedMaxWidth { get; } = 980;
|
|
|
|
private static Dictionary<string, string> allowedFileTypes =
|
|
new Dictionary<string, string>() {
|
|
{ ".bmp", "image/bmp" },
|
|
{ ".jpg", "image/jpeg" },
|
|
{ ".jpeg", "image/jpeg" },
|
|
{ ".png", "image/png" },
|
|
};
|
|
|
|
public static SystemSettingsFejlecLablecSzerkesztoHibakEnum ImageValidate(string content)
|
|
{
|
|
var sumLength = 0;
|
|
var sumWidth = 0;
|
|
var maxHeight = 0;
|
|
var imgTags = System.Text.RegularExpressions.Regex.Matches(content, "<img(.*?)src=\"(?<src>(.*?))\"(.*?)/>");
|
|
foreach (System.Text.RegularExpressions.Match imgTag in imgTags)
|
|
{
|
|
var group = imgTag.Groups["src"];
|
|
if (group.Success)
|
|
{
|
|
string[] parts = group.Value.Split(new string[] { ";", ":", "," }, StringSplitOptions.None);
|
|
if (!allowedFileTypes.ContainsValue(parts[1]))
|
|
{
|
|
return SystemSettingsFejlecLablecSzerkesztoHibakEnum.AFajlKiterjeszteseVagyTipusaNemMegfelelo;
|
|
}
|
|
var bytes = Convert.FromBase64String(parts[3]);
|
|
sumLength += bytes.Length;
|
|
using (System.Drawing.Image image = System.Drawing.Image.FromStream(new System.IO.MemoryStream(bytes)))
|
|
{
|
|
maxHeight = Math.Max(maxHeight, image.Height);
|
|
sumWidth += image.Width;
|
|
}
|
|
}
|
|
}
|
|
if (sumLength > (MaxFileSizeMB * 1024 * 1024))
|
|
{
|
|
return SystemSettingsFejlecLablecSzerkesztoHibakEnum.HibaTortentXMBNalNemLehetNagyobbAFajlMerete;
|
|
}
|
|
if (maxHeight > AllowedMaxHeight)
|
|
{
|
|
return SystemSettingsFejlecLablecSzerkesztoHibakEnum.AKepMagassagaMeghaladjaAMegengedettMeretet;
|
|
}
|
|
if (sumWidth > AllowedMaxWidth)
|
|
{
|
|
return SystemSettingsFejlecLablecSzerkesztoHibakEnum.AzOsszesKepSzelessegeMeghaladjaAMegengedettMeretet;
|
|
}
|
|
return 0;
|
|
}
|
|
}
|
|
}
|