ZUGFeRDRESTService: Add check for file size

This commit is contained in:
Jonathan Jenne 2023-02-02 13:42:56 +01:00
parent 52dae95373
commit 1c7e620c87
4 changed files with 36 additions and 5 deletions

View File

@ -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; }
}

View File

@ -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<String, XmlItemProperty> _propertyMap;
private readonly Dictionary<string, XmlItemProperty> _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);

View File

@ -21,6 +21,9 @@
<Reference Include="DigitalData.Modules.Database">
<HintPath>..\..\..\DDModules\Database\bin\Debug\DigitalData.Modules.Database.dll</HintPath>
</Reference>
<Reference Include="DigitalData.Modules.Filesystem">
<HintPath>..\..\..\DDModules\Filesystem\bin\Debug\DigitalData.Modules.Filesystem.dll</HintPath>
</Reference>
<Reference Include="DigitalData.Modules.Interfaces">
<HintPath>..\..\..\DDModules\Interfaces\bin\Debug\DigitalData.Modules.Interfaces.dll</HintPath>
</Reference>

View File

@ -17,6 +17,7 @@
"Database": "<FIREBIRDSERVER-IP>:<FIREBIRDSERVER-DBPATH>",
"Username": "<FIREBIRDSERVER-USERNAME>",
"Password": "<FIREBIRDSERVER-PASSWORD>"
}
},
"MaxFileSizeInMegabytes": 25
}
}