@@ -27,7 +27,7 @@ public static class ServiceCollectionExtensions
2727 /// <param name="configuration">Configuration.</param>
2828 public static IServiceCollection AddFileValidator ( this IServiceCollection services , IConfiguration configuration )
2929 {
30- return services . AddFileValidator ( options => configuration . GetSection ( DefaultSectionName ) . Bind ( options ) ) ;
30+ return services . AddFileValidator ( configuration , DefaultSectionName ) ;
3131 }
3232
3333 /// <summary>
@@ -38,47 +38,80 @@ public static IServiceCollection AddFileValidator(this IServiceCollection servic
3838 /// <param name="sectionName">Section name.</param>
3939 public static IServiceCollection AddFileValidator ( this IServiceCollection services , IConfiguration configuration , string sectionName )
4040 {
41- return services . AddFileValidator ( options => configuration . GetSection ( sectionName ) . Bind ( options ) ) ;
41+ ArgumentNullException . ThrowIfNull ( configuration ) ;
42+ ArgumentException . ThrowIfNullOrEmpty ( sectionName ) ;
43+
44+ var section = configuration . GetSection ( sectionName ) ;
45+ if ( section is null )
46+ {
47+ throw new InvalidOperationException ( $ "Configuration section '{ sectionName } ' not found.") ;
48+ }
49+
50+ var settings = new FileValidatorSettingsConfiguration ( ) ;
51+ section . Bind ( settings ) ;
52+
53+ var scannerSection = section . GetSection ( "Scanner" ) ;
54+ if ( scannerSection . Exists ( ) )
55+ {
56+ var scannerType = scannerSection [ "ScannerType" ] ;
57+ if ( string . IsNullOrWhiteSpace ( scannerType ) )
58+ {
59+ throw new InvalidOperationException ( "ScannerType must be specified in the configuration section." ) ;
60+ }
61+
62+ settings . Scanner = new ScannerRegistration ( ) ;
63+ settings . Scanner . ScannerType = scannerType ;
64+ settings . Scanner . OptionsConfiguration = scannerSection . GetSection ( "Options" ) ;
65+ }
66+
67+ ConfigureFromSettings ( services , settings ) ;
68+ return services ;
4269 }
4370
4471 /// <summary>
4572 /// Adds the File Validator services to the specified <see cref="IServiceCollection"/> with custom configuration options.
4673 /// </summary>
4774 /// <param name="services">Service collection.</param>
48- /// <param name="options">Configuration options .</param>
75+ /// <param name="options">File validator configuration .</param>
4976 public static IServiceCollection AddFileValidator ( this IServiceCollection services , Action < FileValidatorSettingsConfiguration > options )
5077 {
51- ArgumentNullException . ThrowIfNull ( services ) ;
5278 ArgumentNullException . ThrowIfNull ( options ) ;
5379
54- // Validate and setup configuration options.
55- services . AddSingleton < IValidateOptions < FileValidatorConfiguration > ,
56- FileValidatorConfigurationOptionsValidator > ( ) ;
80+ var settings = new FileValidatorSettingsConfiguration ( ) ;
81+ options ( settings ) ;
5782
58- services . Configure ( options ) ;
83+ ConfigureFromSettings ( services , settings ) ;
84+ return services ;
85+ }
86+
87+ /// <summary>
88+ /// Configures services from settings.
89+ /// </summary>
90+ /// <param name="services">Service collection.</param>
91+ /// <param name="settings">File valiator settings.</param>
92+ private static void ConfigureFromSettings ( IServiceCollection services , FileValidatorSettingsConfiguration settings )
93+ {
94+ ArgumentNullException . ThrowIfNull ( settings ) ;
5995
6096 services . AddOptions < FileValidatorConfiguration > ( )
61- . Configure < IOptions < FileValidatorSettingsConfiguration > > ( ( cfg , settings ) =>
97+ . Configure ( config =>
6298 {
6399 // Convert from FileValidatorSettingsConfiguration to FileValidatorConfiguration.
64- cfg . SupportedFileTypes = settings . Value . SupportedFileTypes ;
65- cfg . ThrowExceptionOnInvalidFile = settings . Value . ThrowExceptionOnInvalidFile ;
100+ config . SupportedFileTypes = settings . SupportedFileTypes ;
101+ config . ThrowExceptionOnInvalidFile = settings . ThrowExceptionOnInvalidFile ;
66102
67- if ( settings . Value . FileSizeLimit != - 1 )
103+ if ( settings . FileSizeLimit != - 1 )
68104 {
69- cfg . FileSizeLimit = settings . Value . FileSizeLimit ;
105+ config . FileSizeLimit = settings . FileSizeLimit ;
70106 }
71- else if ( ! string . IsNullOrWhiteSpace ( settings . Value . UnitFileSizeLimit ) )
107+ else if ( ! string . IsNullOrWhiteSpace ( settings . UnitFileSizeLimit ) )
72108 {
73- cfg . FileSizeLimit = ByteSize . Parse ( settings . Value . UnitFileSizeLimit ) ;
109+ config . FileSizeLimit = ByteSize . Parse ( settings . UnitFileSizeLimit ) ;
74110 }
75111 } )
76112 . ValidateOnStart ( ) ;
77113
78- // Register antimalware scanner.
79- var settings = new FileValidatorSettingsConfiguration ( ) ;
80- options ( settings ) ;
81-
114+ // Register antimalware scanner (if any).
82115 RegisterConfiguredScanner ( services , settings . Scanner ) ;
83116
84117 // Register the FileValidator service.
@@ -96,8 +129,6 @@ public static IServiceCollection AddFileValidator(this IServiceCollection servic
96129 // No antimalware scanner registered.
97130 return new FileValidator ( configuration ) ;
98131 } ) ;
99-
100- return services ;
101132 }
102133
103134 /// <summary>
0 commit comments