diff --git a/WEBSERVICES/ZUGFeRDRESTService/Config.cs b/WEBSERVICES/ZUGFeRDRESTService/Config.cs index f9734783..4a43548e 100644 --- a/WEBSERVICES/ZUGFeRDRESTService/Config.cs +++ b/WEBSERVICES/ZUGFeRDRESTService/Config.cs @@ -5,6 +5,7 @@ public string Name { get; set; } public string LogPath { get; set; } public string MSSQLConnectionString { get; set; } + public string MaxFileSizeInMegabytes { get; set; } public FirebirdConfig Firebird { get; set; } } diff --git a/WEBSERVICES/ZUGFeRDRESTService/Controllers/ValidationController.cs b/WEBSERVICES/ZUGFeRDRESTService/Controllers/ValidationController.cs index 77299759..e186c6fc 100644 --- a/WEBSERVICES/ZUGFeRDRESTService/Controllers/ValidationController.cs +++ b/WEBSERVICES/ZUGFeRDRESTService/Controllers/ValidationController.cs @@ -9,6 +9,7 @@ using static DigitalData.Modules.Interfaces.Exceptions; using static DigitalData.Modules.Interfaces.ZUGFeRDInterface; using static DigitalData.Modules.Interfaces.PropertyValues; using System.Data.SqlClient; +using Microsoft.Extensions.Configuration; namespace ZUGFeRDRESTService.Controllers { @@ -22,19 +23,25 @@ namespace ZUGFeRDRESTService.Controllers public const string ADDED_WHO = "ZUGFeRD REST Service"; public const string MESSAGEID_DOMAIN = "test.wisag.de"; + private const int MAX_FILE_SIZE_DEFAULT = 25; + private readonly ZUGFeRDInterface _zugferd; private readonly IDatabase _database; private readonly DigitalData.Modules.Logging.LogConfig _logConfig; private readonly DigitalData.Modules.Logging.Logger _logger; + private readonly DigitalData.Modules.Filesystem.File _file; private readonly PropertyValues _props; - private readonly Dictionary _propertyMap; + private readonly Dictionary _propertyMap; - public ValidationController(ILogging logging, IDatabase database) + private readonly int _MaxFileSizeInMegabytes; + + public ValidationController(ILogging logging, IDatabase database, IConfiguration Config) { _logConfig = logging.LogConfig; _logger = _logConfig.GetLogger(); + _file = new DigitalData.Modules.Filesystem.File(_logConfig); _logger.Debug("Validation Controller initializing"); @@ -47,6 +54,14 @@ namespace ZUGFeRDRESTService.Controllers _zugferd = new ZUGFeRDInterface(_logConfig, oGDPictureKey); _props = new PropertyValues(_logConfig); + var oAppConfig = Config.GetSection("Config"); + + if (int.TryParse(oAppConfig["MaxFileSizeInMegabytes"], out _MaxFileSizeInMegabytes)) + { + _logger.Info("Configuration MaxFileSizeInMegabytes was not set. Using default value [{0}]", MAX_FILE_SIZE_DEFAULT); + _MaxFileSizeInMegabytes = MAX_FILE_SIZE_DEFAULT; + } + _logger.Debug("Validation Controller initialized!"); } @@ -61,12 +76,22 @@ namespace ZUGFeRDRESTService.Controllers _logger.Info("Start processing request to ValidationController"); object oDocument; - PropertyValues.CheckPropertyValuesResult oResult = new PropertyValues.CheckPropertyValuesResult(); + CheckPropertyValuesResult oResult = new CheckPropertyValuesResult(); try { using Stream oStream = file.OpenReadStream(); { + _logger.Info("Checking Filesize of file [{0}]", file.FileName); + + bool oFileSizeIsOK = _file.TestFileSizeIsLessThanMaxFileSize(file.FileName, _MaxFileSizeInMegabytes); + + if (oFileSizeIsOK == false) + { + throw new ZUGFeRDExecption(ErrorType.FileTooBig, + string.Format("Die hochgeladene Datei überschreitet die zulässige Dateigröße [{0}].", _MaxFileSizeInMegabytes)); + } + _logger.Info("Extracting ZUGFeRD Data from file [{0}]", file.FileName); oDocument = _zugferd.ExtractZUGFeRDFileWithGDPicture(oStream); @@ -137,6 +162,7 @@ namespace ZUGFeRDRESTService.Controllers ErrorType.NoZugferd => "Die hochgeladene Datei ist keine ZUGFeRD-Rechnung.", ErrorType.NoValidZugferd => "Die hochgeladene Datei ist keine gültige ZUGFeRD-Rechnung.", ErrorType.MissingProperties => "Die hochgeladene Datei ist keine gültige ZUGFeRD-Rechnung, es fehlen einige Metadaten", + ErrorType.FileTooBig => string.Format("Die hochgeladene Datei überschreitet die zulässige Dateigröße [{0}].", _MaxFileSizeInMegabytes), _ => "Die hochgeladene Datei kann nicht validiert werden.", }; @@ -239,7 +265,7 @@ namespace ZUGFeRDRESTService.Controllers new SqlParameter("@CREATEDWHO", ADDED_WHO), new SqlParameter("@GROUP_COUNTER", pProperty.GroupCounter), new SqlParameter("@SPEC_NAME", pProperty.TableColumn), - new SqlParameter("@IS_REQUIRED", pProperty.ISRequired) + new SqlParameter("@IS_REQUIRED", pProperty.IsRequired) }; var oCommand = new SqlCommand(oSql); diff --git a/WEBSERVICES/ZUGFeRDRESTService/ZUGFeRDRESTService.csproj b/WEBSERVICES/ZUGFeRDRESTService/ZUGFeRDRESTService.csproj index 311cfff2..15d1c7eb 100644 --- a/WEBSERVICES/ZUGFeRDRESTService/ZUGFeRDRESTService.csproj +++ b/WEBSERVICES/ZUGFeRDRESTService/ZUGFeRDRESTService.csproj @@ -21,6 +21,9 @@ ..\..\..\DDModules\Database\bin\Debug\DigitalData.Modules.Database.dll + + ..\..\..\DDModules\Filesystem\bin\Debug\DigitalData.Modules.Filesystem.dll + ..\..\..\DDModules\Interfaces\bin\Debug\DigitalData.Modules.Interfaces.dll diff --git a/WEBSERVICES/ZUGFeRDRESTService/appsettings.json b/WEBSERVICES/ZUGFeRDRESTService/appsettings.json index 2d200b5a..e3766070 100644 --- a/WEBSERVICES/ZUGFeRDRESTService/appsettings.json +++ b/WEBSERVICES/ZUGFeRDRESTService/appsettings.json @@ -17,6 +17,7 @@ "Database": ":", "Username": "", "Password": "" - } + }, + "MaxFileSizeInMegabytes": 25 } }