1- using System ;
21using Contracts ;
32using Microsoft . AspNetCore . Builder ;
43using Microsoft . AspNetCore . Diagnostics ;
98using Microsoft . Extensions . Configuration ;
109using Microsoft . Extensions . DependencyInjection ;
1110using Microsoft . Extensions . Hosting ;
12- using Microsoft . OpenApi . Models ;
11+ using Microsoft . OpenApi ;
1312using Models . Events ;
1413using Models . Exceptions ;
1514using Models . Settings ;
1817using Services . Events ;
1918using Services . Events . Handlers ;
2019using Services . Repositories ;
20+ using System ;
2121
2222namespace FilesAPI
2323{
24- public class Startup
25- {
26- public Startup ( IConfiguration configuration )
27- {
28- Configuration = configuration ;
29- }
24+ public class Startup
25+ {
26+ public Startup ( IConfiguration configuration )
27+ {
28+ Configuration = configuration ;
29+ }
30+
31+ public IConfiguration Configuration { get ; }
32+
33+ // This method gets called by the runtime. Use this method to add services to the container.
34+ public void ConfigureServices ( IServiceCollection services )
35+ {
36+ // Configure Kestrel server options for unlimited file uploads
37+ services . Configure < KestrelServerOptions > ( options =>
38+ {
39+ options . Limits . MaxRequestBodySize = null ; // Remove limit completely
40+ options . Limits . MinRequestBodyDataRate = null ; // Remove timeout for slow uploads
41+ options . Limits . MinResponseDataRate = null ; // Remove timeout for slow downloads
42+ options . Limits . MaxConcurrentConnections = null ; // Remove connection limit
43+ options . Limits . MaxConcurrentUpgradedConnections = null ; // Remove upgraded connection limit
44+ } ) ;
45+
46+ // Note: IIS file size limits are configured via web.config See web.config for
47+ // maxAllowedContentLength and requestLimits configuration Configure form options for
48+ // unlimited file uploads
49+ services . Configure < FormOptions > ( options =>
50+ {
51+ options . ValueLengthLimit = int . MaxValue ; // Remove form value length limit
52+ options . MultipartBodyLengthLimit = long . MaxValue ; // Remove multipart body limit
53+ options . MultipartHeadersLengthLimit = int . MaxValue ; // Remove header length limit
54+ options . MultipartBoundaryLengthLimit = int . MaxValue ; // Remove boundary length limit
55+ options . KeyLengthLimit = int . MaxValue ; // Remove key length limit
56+ options . ValueCountLimit = int . MaxValue ; // Remove value count limit
57+ options . BufferBody = true ; // Buffer the request body
58+ options . MemoryBufferThreshold = int . MaxValue ; // Set memory buffer threshold
59+ } ) ;
60+ services . AddCors ( options =>
61+ {
62+ options . AddPolicy ( "AllowAll" ,
63+ builder =>
64+ {
65+ builder
66+ . AllowAnyOrigin ( )
67+ . AllowAnyMethod ( )
68+ . AllowAnyHeader ( ) ;
69+ } ) ;
70+ } ) ;
71+ services . AddSwaggerGen ( c =>
72+ {
73+ c . SwaggerDoc ( "v1" , new OpenApiInfo
74+ {
75+ Title = "Files API" ,
76+ Version = "v1"
77+ } ) ;
78+ } ) ;
79+ //App Settings Injection
80+ services . Configure < MongoDBAppSettings > ( Configuration . GetSection ( "MongoDBAppSettings" ) ) ;
81+ services . Configure < LiteDBAppSettings > ( Configuration . GetSection ( "LiteDBAppSettings" ) ) ;
3082
31- public IConfiguration Configuration { get ; }
83+ // Configure database services based on environment
84+ var useEmbeddedDatabase = Configuration . GetValue < bool > ( "USE_EMBEDDED_DATABASE" , false ) ||
85+ Environment . GetEnvironmentVariable ( "USE_EMBEDDED_DATABASE" ) == "true" ;
3286
33- // This method gets called by the runtime. Use this method to add services to the container.
34- public void ConfigureServices ( IServiceCollection services )
35- {
36- // Configure Kestrel server options for unlimited file uploads
37- services . Configure < KestrelServerOptions > ( options =>
38- {
39- options . Limits . MaxRequestBodySize = null ; // Remove limit completely
40- options . Limits . MinRequestBodyDataRate = null ; // Remove timeout for slow uploads
41- options . Limits . MinResponseDataRate = null ; // Remove timeout for slow downloads
42- options . Limits . MaxConcurrentConnections = null ; // Remove connection limit
43- options . Limits . MaxConcurrentUpgradedConnections = null ; // Remove upgraded connection limit
44- } ) ;
45-
46- // Note: IIS file size limits are configured via web.config
47- // See web.config for maxAllowedContentLength and requestLimits configuration
48- // Configure form options for unlimited file uploads
49- services . Configure < FormOptions > ( options =>
50- {
51- options . ValueLengthLimit = int . MaxValue ; // Remove form value length limit
52- options . MultipartBodyLengthLimit = long . MaxValue ; // Remove multipart body limit
53- options . MultipartHeadersLengthLimit = int . MaxValue ; // Remove header length limit
54- options . MultipartBoundaryLengthLimit = int . MaxValue ; // Remove boundary length limit
55- options . KeyLengthLimit = int . MaxValue ; // Remove key length limit
56- options . ValueCountLimit = int . MaxValue ; // Remove value count limit
57- options . BufferBody = true ; // Buffer the request body
58- options . MemoryBufferThreshold = int . MaxValue ; // Set memory buffer threshold
59- } ) ;
60- services . AddCors ( options =>
61- {
62- options . AddPolicy ( "AllowAll" ,
63- builder =>
64- {
65- builder
66- . AllowAnyOrigin ( )
67- . AllowAnyMethod ( )
68- . AllowAnyHeader ( ) ;
69- } ) ;
70- } ) ;
71- services . AddSwaggerGen ( c =>
72- {
73- c . SwaggerDoc ( "v1" , new OpenApiInfo
74- {
75- Title = "Files API" ,
76- Version = "v1"
77- } ) ;
78- } ) ;
79- //App Settings Injection
80- services . Configure < MongoDBAppSettings > ( Configuration . GetSection ( "MongoDBAppSettings" ) ) ;
81- services . Configure < LiteDBAppSettings > ( Configuration . GetSection ( "LiteDBAppSettings" ) ) ;
87+ if ( useEmbeddedDatabase )
88+ {
89+ // Use LiteDB for self-contained operation
90+ var databasePath = Environment . GetEnvironmentVariable ( "DATABASE_PATH" ) ??
91+ Configuration . GetValue < string > ( "DATABASE_PATH" , "./data/filesapi.db" ) ;
92+ var uploadsPath = Environment . GetEnvironmentVariable ( "UPLOADS_PATH" ) ??
93+ Configuration . GetValue < string > ( "UPLOADS_PATH" , "./uploads" ) ;
8294
83- // Configure database services based on environment
84- var useEmbeddedDatabase = Configuration . GetValue < bool > ( "USE_EMBEDDED_DATABASE" , false ) ||
85- Environment . GetEnvironmentVariable ( "USE_EMBEDDED_DATABASE" ) == "true" ;
86-
87- if ( useEmbeddedDatabase )
88- {
89- // Use LiteDB for self-contained operation
90- var databasePath = Environment . GetEnvironmentVariable ( "DATABASE_PATH" ) ??
91- Configuration . GetValue < string > ( "DATABASE_PATH" , "./data/filesapi.db" ) ;
92- var uploadsPath = Environment . GetEnvironmentVariable ( "UPLOADS_PATH" ) ??
93- Configuration . GetValue < string > ( "UPLOADS_PATH" , "./uploads" ) ;
94-
95- services . AddScoped < IStorageRepository > ( provider =>
96- new Services . Repositories . LiteDbStorageRepository ( databasePath , uploadsPath ) ) ;
97- services . AddScoped < IFileDetailsRepository > ( provider =>
98- new Services . Repositories . LiteDbFileDetailsRepository ( databasePath ) ) ;
99- services . AddScoped < IDownloadAnalyticsRepository > ( provider =>
100- new Services . Repositories . LiteDbDownloadAnalyticsRepository ( databasePath ) ) ;
101- }
102- else
103- {
104- // Use MongoDB for traditional operation
105- services . AddScoped < IStorageRepository , StorageRepository > ( ) ;
106- services . AddScoped < IFileDetailsRepository , FileDetailsRepository > ( ) ;
107- services . AddScoped < IDownloadAnalyticsRepository , Services . Repositories . MongoDbDownloadAnalyticsRepository > ( ) ;
108- }
95+ services . AddScoped < IStorageRepository > ( provider =>
96+ new Services . Repositories . LiteDbStorageRepository ( databasePath , uploadsPath ) ) ;
97+ services . AddScoped < IFileDetailsRepository > ( provider =>
98+ new Services . Repositories . LiteDbFileDetailsRepository ( databasePath ) ) ;
99+ services . AddScoped < IDownloadAnalyticsRepository > ( provider =>
100+ new Services . Repositories . LiteDbDownloadAnalyticsRepository ( databasePath ) ) ;
101+ }
102+ else
103+ {
104+ // Use MongoDB for traditional operation
105+ services . AddScoped < IStorageRepository , StorageRepository > ( ) ;
106+ services . AddScoped < IFileDetailsRepository , FileDetailsRepository > ( ) ;
107+ services . AddScoped < IDownloadAnalyticsRepository , Services . Repositories . MongoDbDownloadAnalyticsRepository > ( ) ;
108+ }
109109
110- //services.AddSingleton<IStorageService, FilesService>();
111- services . AddScoped < IStorageService , StorageService > ( ) ;
112- services . AddScoped < IAnalyticsService , AnalyticsService > ( ) ;
113- services . AddSingleton < ISettingsService , SettingsService > ( ) ;
110+ //services.AddSingleton<IStorageService, FilesService>();
111+ services . AddScoped < IStorageService , StorageService > ( ) ;
112+ services . AddScoped < IAnalyticsService , AnalyticsService > ( ) ;
113+ services . AddSingleton < ISettingsService , SettingsService > ( ) ;
114114
115- services . AddScoped < RecordDownloadHandler > ( ) ;
116- services . AddScoped < EventHandlerContainer > ( ) ;
117- EventHandlerContainer . Subscribe < FileDownloadedEvent , RecordDownloadHandler > ( ) ;
115+ services . AddScoped < RecordDownloadHandler > ( ) ;
116+ services . AddScoped < EventHandlerContainer > ( ) ;
117+ EventHandlerContainer . Subscribe < FileDownloadedEvent , RecordDownloadHandler > ( ) ;
118118
119- services . AddControllers ( ) ;
120- }
119+ services . AddControllers ( ) ;
120+ }
121121
122- // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
123- public void Configure ( IApplicationBuilder app , IWebHostEnvironment env )
124- {
125- app . UseCors ( "AllowAll" ) ;
126- app . UseStaticFiles ( ) ;
127- var swaggerUrl = "/swagger/v1/swagger.json" ;
128- if ( env . IsDevelopment ( ) )
129- {
130- app . UseDeveloperExceptionPage ( ) ;
131- }
122+ // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
123+ public void Configure ( IApplicationBuilder app , IWebHostEnvironment env )
124+ {
125+ app . UseCors ( "AllowAll" ) ;
126+ app . UseStaticFiles ( ) ;
127+ var swaggerUrl = "/swagger/v1/swagger.json" ;
128+ if ( env . IsDevelopment ( ) )
129+ {
130+ app . UseDeveloperExceptionPage ( ) ;
131+ }
132132
133- app . UseSwagger ( ) ;
134- app . UseSwaggerUI ( c =>
135- {
136- c . SwaggerEndpoint ( swaggerUrl , "Files API V1" ) ;
137- c . RoutePrefix = string . Empty ;
138- } ) ;
133+ app . UseSwagger ( ) ;
134+ app . UseSwaggerUI ( c =>
135+ {
136+ c . SwaggerEndpoint ( swaggerUrl , "Files API V1" ) ;
137+ c . RoutePrefix = string . Empty ;
138+ } ) ;
139139
140- app . UseHttpsRedirection ( ) ;
141- app . UseRouting ( ) ;
140+ app . UseHttpsRedirection ( ) ;
141+ app . UseRouting ( ) ;
142142
143- app . UseExceptionHandler ( a => a . Run ( async context =>
144- {
145- var exceptionHandlerPathFeature = context . Features . Get < IExceptionHandlerPathFeature > ( ) ;
146- var exception = exceptionHandlerPathFeature . Error ;
147- if ( exception is FilesApiException )
148- {
149- var result = JsonConvert . SerializeObject ( new { error = exception . Message } ) ;
150- context . Response . ContentType = "application/json" ;
151- context . Response . StatusCode = StatusCodes . Status400BadRequest ;
152- await context . Response . WriteAsync ( result ) ;
153- }
154- } ) ) ;
143+ app . UseExceptionHandler ( a => a . Run ( async context =>
144+ {
145+ var exceptionHandlerPathFeature = context . Features . Get < IExceptionHandlerPathFeature > ( ) ;
146+ var exception = exceptionHandlerPathFeature . Error ;
147+ if ( exception is FilesApiException )
148+ {
149+ var result = JsonConvert . SerializeObject ( new { error = exception . Message } ) ;
150+ context . Response . ContentType = "application/json" ;
151+ context . Response . StatusCode = StatusCodes . Status400BadRequest ;
152+ await context . Response . WriteAsync ( result ) ;
153+ }
154+ } ) ) ;
155155
156- app . UseEndpoints ( endpoints =>
157- {
158- endpoints . MapControllers ( ) ;
159- } ) ;
160- }
161- }
156+ app . UseEndpoints ( endpoints =>
157+ {
158+ endpoints . MapControllers ( ) ;
159+ } ) ;
160+ }
161+ }
162162}
0 commit comments