@@ -29,6 +29,7 @@ public async Task<ActionResult<BaseResponse>> GeneratePdf(PdfGenerationRequestDT
2929 BaseResponse response = new BaseResponse ( ResponseStatus . Fail ) ;
3030 string ? htmlTemplateFilePath = null ;
3131 string ? outputFilePath = null ;
32+ bool cleanupResources = this . _configuration . GetValue ( "CONFIG:CLEAN_RESOURCES_GENERATED_BY_BASE64_STRINGS" , false ) ;
3233
3334 try
3435 {
@@ -37,15 +38,15 @@ public async Task<ActionResult<BaseResponse>> GeneratePdf(PdfGenerationRequestDT
3738 throw new BadHttpRequestException ( "Request body cannot be null" ) ;
3839 }
3940
40- string tempPath = this . _configuration . GetSection ( "TEMPORARY_FILE_PATHS:TEMP" ) . Value
41+ string tempPath = this . _configuration . GetValue < string > ( "TEMPORARY_FILE_PATHS:TEMP" )
4142 ?? throw new InvalidOperationException ( "Configuration TEMPORARY_FILE_PATHS:TEMP is missing." ) ;
42- string inputPath = this . _configuration . GetSection ( "TEMPORARY_FILE_PATHS:INPUT" ) . Value
43+ string inputPath = this . _configuration . GetValue < string > ( "TEMPORARY_FILE_PATHS:INPUT" )
4344 ?? throw new InvalidOperationException ( "Configuration TEMPORARY_FILE_PATHS:INPUT is missing." ) ;
44- string htmlPath = this . _configuration . GetSection ( "TEMPORARY_FILE_PATHS:HTML" ) . Value
45+ string htmlPath = this . _configuration . GetValue < string > ( "TEMPORARY_FILE_PATHS:HTML" )
4546 ?? throw new InvalidOperationException ( "Configuration TEMPORARY_FILE_PATHS:HTML is missing." ) ;
46- string outputPath = this . _configuration . GetSection ( "TEMPORARY_FILE_PATHS:OUTPUT" ) . Value
47+ string outputPath = this . _configuration . GetValue < string > ( "TEMPORARY_FILE_PATHS:OUTPUT" )
4748 ?? throw new InvalidOperationException ( "Configuration TEMPORARY_FILE_PATHS:OUTPUT is missing." ) ;
48- string pdfPath = this . _configuration . GetSection ( "TEMPORARY_FILE_PATHS:PDF" ) . Value
49+ string pdfPath = this . _configuration . GetValue < string > ( "TEMPORARY_FILE_PATHS:PDF" )
4950 ?? throw new InvalidOperationException ( "Configuration TEMPORARY_FILE_PATHS:PDF is missing." ) ;
5051
5152
@@ -125,26 +126,29 @@ await PdfDocumentGenerator.GeneratePdf(
125126 }
126127 finally
127128 {
128- if ( htmlTemplateFilePath != null && System . IO . File . Exists ( htmlTemplateFilePath ) )
129+ if ( cleanupResources )
129130 {
130- try
131+ if ( htmlTemplateFilePath != null && System . IO . File . Exists ( htmlTemplateFilePath ) )
131132 {
132- System . IO . File . Delete ( htmlTemplateFilePath ) ;
133+ try
134+ {
135+ System . IO . File . Delete ( htmlTemplateFilePath ) ;
136+ }
137+ catch ( Exception ex )
138+ {
139+ this . _logger . LogError ( $ "Error in deleting file at path { htmlTemplateFilePath } : { ex . Message } ") ;
140+ }
133141 }
134- catch ( Exception ex )
142+ if ( outputFilePath != null && System . IO . File . Exists ( outputFilePath ) )
135143 {
136- this . _logger . LogError ( $ "Error in deleting file at path { htmlTemplateFilePath } : { ex . Message } ") ;
137- }
138- }
139- if ( outputFilePath != null && System . IO . File . Exists ( outputFilePath ) )
140- {
141- try
142- {
143- System . IO . File . Delete ( outputFilePath ) ;
144- }
145- catch ( Exception ex )
146- {
147- this . _logger . LogError ( $ "Error in deleting file at path { outputFilePath } : { ex . Message } ") ;
144+ try
145+ {
146+ System . IO . File . Delete ( outputFilePath ) ;
147+ }
148+ catch ( Exception ex )
149+ {
150+ this . _logger . LogError ( $ "Error in deleting file at path { outputFilePath } : { ex . Message } ") ;
151+ }
148152 }
149153 }
150154 }
@@ -158,6 +162,7 @@ public async Task<ActionResult<BaseResponse>> GeneratePdfUsingEjs(PdfGenerationR
158162 BaseResponse response = new BaseResponse ( ResponseStatus . Fail ) ;
159163 string ? ejsTemplateFilePath = null ;
160164 string ? outputFilePath = null ;
165+ bool cleanupResources = this . _configuration . GetValue ( "CONFIG:CLEAN_RESOURCES_GENERATED_BY_BASE64_STRINGS" , false ) ;
161166
162167 try
163168 {
@@ -166,15 +171,15 @@ public async Task<ActionResult<BaseResponse>> GeneratePdfUsingEjs(PdfGenerationR
166171 throw new BadHttpRequestException ( "Request body cannot be null" ) ;
167172 }
168173
169- string tempPath = this . _configuration . GetSection ( "TEMPORARY_FILE_PATHS:TEMP" ) . Value
174+ string tempPath = this . _configuration . GetValue < string > ( "TEMPORARY_FILE_PATHS:TEMP" )
170175 ?? throw new InvalidOperationException ( "Configuration TEMPORARY_FILE_PATHS:TEMP is missing." ) ;
171- string inputPath = this . _configuration . GetSection ( "TEMPORARY_FILE_PATHS:INPUT" ) . Value
176+ string inputPath = this . _configuration . GetValue < string > ( "TEMPORARY_FILE_PATHS:INPUT" )
172177 ?? throw new InvalidOperationException ( "Configuration TEMPORARY_FILE_PATHS:INPUT is missing." ) ;
173- string ejsPath = this . _configuration . GetSection ( "TEMPORARY_FILE_PATHS:EJS" ) . Value
178+ string ejsPath = this . _configuration . GetValue < string > ( "TEMPORARY_FILE_PATHS:EJS" )
174179 ?? throw new InvalidOperationException ( "Configuration TEMPORARY_FILE_PATHS:EJS is missing." ) ;
175- string outputPath = this . _configuration . GetSection ( "TEMPORARY_FILE_PATHS:OUTPUT" ) . Value
180+ string outputPath = this . _configuration . GetValue < string > ( "TEMPORARY_FILE_PATHS:OUTPUT" )
176181 ?? throw new InvalidOperationException ( "Configuration TEMPORARY_FILE_PATHS:OUTPUT is missing." ) ;
177- string pdfPath = this . _configuration . GetSection ( "TEMPORARY_FILE_PATHS:PDF" ) . Value
182+ string pdfPath = this . _configuration . GetValue < string > ( "TEMPORARY_FILE_PATHS:PDF" )
178183 ?? throw new InvalidOperationException ( "Configuration TEMPORARY_FILE_PATHS:PDF is missing." ) ;
179184
180185 // Generate filepath to save base64 html template
@@ -253,26 +258,29 @@ await PdfDocumentGenerator.GeneratePdf(
253258 }
254259 finally
255260 {
256- if ( ejsTemplateFilePath != null && System . IO . File . Exists ( ejsTemplateFilePath ) )
257- {
258- try
259- {
260- System . IO . File . Delete ( ejsTemplateFilePath ) ;
261- }
262- catch ( Exception ex )
263- {
264- this . _logger . LogError ( $ "Error in deleting file at path { ejsTemplateFilePath } : { ex . Message } ") ;
265- }
266- }
267- if ( outputFilePath != null && System . IO . File . Exists ( outputFilePath ) )
261+ if ( cleanupResources )
268262 {
269- try
263+ if ( ejsTemplateFilePath != null && System . IO . File . Exists ( ejsTemplateFilePath ) )
270264 {
271- System . IO . File . Delete ( outputFilePath ) ;
265+ try
266+ {
267+ System . IO . File . Delete ( ejsTemplateFilePath ) ;
268+ }
269+ catch ( Exception ex )
270+ {
271+ this . _logger . LogError ( $ "Error in deleting file at path { ejsTemplateFilePath } : { ex . Message } ") ;
272+ }
272273 }
273- catch ( Exception ex )
274+ if ( outputFilePath != null && System . IO . File . Exists ( outputFilePath ) )
274275 {
275- this . _logger . LogError ( $ "Error in deleting file at path { outputFilePath } : { ex . Message } ") ;
276+ try
277+ {
278+ System . IO . File . Delete ( outputFilePath ) ;
279+ }
280+ catch ( Exception ex )
281+ {
282+ this . _logger . LogError ( $ "Error in deleting file at path { outputFilePath } : { ex . Message } ") ;
283+ }
276284 }
277285 }
278286 }
0 commit comments