Skip to content

Commit 2298678

Browse files
committed
feat: add flag for deciding cleaning up of input and output files
1 parent 1df9fd3 commit 2298678

File tree

3 files changed

+76
-66
lines changed

3 files changed

+76
-66
lines changed

OsmoDoc.API/Controllers/PdfController.cs

Lines changed: 50 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

OsmoDoc.API/Controllers/WordController.cs

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public async Task<ActionResult<BaseResponse>> GenerateWord(WordGenerationRequest
3333
BaseResponse response = new BaseResponse(ResponseStatus.Fail);
3434
string? docxTemplateFilePath = null;
3535
string? outputFilePath = null;
36+
bool cleanupResources = this._configuration.GetValue("CONFIG:CLEAN_RESOURCES_GENERATED_BY_BASE64_STRINGS", false);
3637

3738
try
3839
{
@@ -46,17 +47,14 @@ public async Task<ActionResult<BaseResponse>> GenerateWord(WordGenerationRequest
4647
throw new BadHttpRequestException("Document data is required");
4748
}
4849

49-
string tempPath = this._configuration.GetSection("TEMPORARY_FILE_PATHS:TEMP").Value
50+
string tempPath = this._configuration.GetValue<string>("TEMPORARY_FILE_PATHS:TEMP")
5051
?? throw new InvalidOperationException("Configuration TEMPORARY_FILE_PATHS:TEMP is missing.");
51-
string inputPath = this._configuration.GetSection("TEMPORARY_FILE_PATHS:INPUT").Value
52+
string inputPath = this._configuration.GetValue<string>("TEMPORARY_FILE_PATHS:INPUT")
5253
?? throw new InvalidOperationException("Configuration TEMPORARY_FILE_PATHS:INPUT is missing.");
53-
string wordPath = this._configuration.GetSection("TEMPORARY_FILE_PATHS:WORD").Value
54+
string wordPath = this._configuration.GetValue<string>("TEMPORARY_FILE_PATHS:WORD")
5455
?? throw new InvalidOperationException("Configuration TEMPORARY_FILE_PATHS:WORD is missing.");
55-
string outputPath = this._configuration.GetSection("TEMPORARY_FILE_PATHS:OUTPUT").Value
56+
string outputPath = this._configuration.GetValue<string>("TEMPORARY_FILE_PATHS:OUTPUT")
5657
?? throw new InvalidOperationException("Configuration TEMPORARY_FILE_PATHS:OUTPUT is missing.");
57-
string imagesPath = this._configuration.GetSection("TEMPORARY_FILE_PATHS:IMAGES").Value
58-
?? throw new InvalidOperationException("Configuration TEMPORARY_FILE_PATHS:IMAGES is missing.");
59-
6058

6159
// Generate filepath to save base64 docx template
6260
docxTemplateFilePath = Path.Combine(
@@ -147,26 +145,29 @@ await WordDocumentGenerator.GenerateDocumentByTemplate(
147145
}
148146
finally
149147
{
150-
if (docxTemplateFilePath != null && System.IO.File.Exists(docxTemplateFilePath))
151-
{
152-
try
153-
{
154-
System.IO.File.Delete(docxTemplateFilePath);
155-
}
156-
catch (Exception ex)
157-
{
158-
this._logger.LogError($"Error in deleting file at path {docxTemplateFilePath}: {ex.Message}");
159-
}
160-
}
161-
if (outputFilePath != null && System.IO.File.Exists(outputFilePath))
148+
if (cleanupResources)
162149
{
163-
try
150+
if (docxTemplateFilePath != null && System.IO.File.Exists(docxTemplateFilePath))
164151
{
165-
System.IO.File.Delete(outputFilePath);
152+
try
153+
{
154+
System.IO.File.Delete(docxTemplateFilePath);
155+
}
156+
catch (Exception ex)
157+
{
158+
this._logger.LogError($"Error in deleting file at path {docxTemplateFilePath}: {ex.Message}");
159+
}
166160
}
167-
catch (Exception ex)
161+
if (outputFilePath != null && System.IO.File.Exists(outputFilePath))
168162
{
169-
this._logger.LogError($"Error in deleting file at path {outputFilePath}: {ex.Message}");
163+
try
164+
{
165+
System.IO.File.Delete(outputFilePath);
166+
}
167+
catch (Exception ex)
168+
{
169+
this._logger.LogError($"Error in deleting file at path {outputFilePath}: {ex.Message}");
170+
}
170171
}
171172
}
172173
}

OsmoDoc.API/appsettings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
},
2727
"CONFIG": {
2828
"REQUEST_BODY_SIZE_LIMIT_BYTES": 52428800,
29-
"UPLOAD_FILE_SIZE_LIMIT_BYTES": 5242880
29+
"UPLOAD_FILE_SIZE_LIMIT_BYTES": 5242880,
30+
"CLEAN_RESOURCES_GENERATED_BY_BASE64_STRINGS": false
3031
},
3132
"AllowedHosts": "*"
3233
}

0 commit comments

Comments
 (0)