Skip to content

Commit

Permalink
Merge pull request #6 from Schrodinger-AI/fix/log-and-valid
Browse files Browse the repository at this point in the history
fix: fix log and valid
  • Loading branch information
ymsdgr authored Apr 2, 2024
2 parents e5a2892 + a7276f0 commit 48cc5f7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 26 deletions.
8 changes: 4 additions & 4 deletions appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
"Error": "Error"
},
"Path": "logs",
"FileName": "log-",
"FileExtension": "txt",
"FileSizeLimitBytes": 10485760,
"RetainedFileCountLimit": 5,
"FileExtension": "log",
"FileName": "WaterMarkLog-",
"FileSizeLimitBytes": 2147483648,
"RetainedFileCountLimit": 3,
"FlushInterval": "00:00:30",
"Append": true
}
Expand Down
29 changes: 7 additions & 22 deletions controllers/WatermarkController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ public ImageController(ILogger<ImageController> logger, WatermarkSettings waterm
[HttpPost("process")]
public IActionResult AddWatermark([FromBody] WatermarkApiSchema.WatermarkRequest request)
{
if (string.IsNullOrEmpty(request.SourceImage) || string.IsNullOrEmpty(request.Watermark.Text))
{
_logger.LogWarning("invalid input while processing the image. The SourceImage: {SourceImage}, Watermark: {Watermark}", request.SourceImage, request.Watermark.Text);
return StatusCode(422, new { error = "invalid input" });
}

try
{
// Convert input Base64 string to byte array
Expand All @@ -42,18 +48,6 @@ public IActionResult AddWatermark([FromBody] WatermarkApiSchema.WatermarkRequest
imageSmall.Mutate(x => x.Resize(_resizeSettings.Width, _resizeSettings.Height));
var resizedBase64 = AddWatermark(imageSmall, request.Watermark.Text);

var logDetails = new WatermarkApiSchema.LogDetails
{
Timestamp = $"{DateTimeOffset.Now:yyyy-MM-dd HH:mm:ss.fff zzz}",
RequestMethod = "POST",
RequestBody = request,
RequestUrl = "/image/process",
StatusCode = 200,
Response = outputBase64
};

_logger.LogInformation("Image processed successfully. {@LogDetails}", logDetails);

return Ok(new
{
ProcessedImage = $"data:image/webp;base64,{outputBase64}",
Expand All @@ -62,16 +56,7 @@ public IActionResult AddWatermark([FromBody] WatermarkApiSchema.WatermarkRequest
}
catch (Exception ex)
{
var logDetails = new WatermarkApiSchema.LogDetails
{
Timestamp = $"{DateTimeOffset.Now:yyyy-MM-dd HH:mm:ss.fff zzz}",
RequestMethod = "POST",
RequestBody = request,
RequestUrl = "/image/process",
StatusCode = 500,
Response = ex.Message
};
_logger.LogError("An error occurred while processing the image. {@LogDetails}", logDetails);
_logger.LogError("An error occurred while processing the image. The SourceImage: {SourceImage}, Watermark: {Watermark}, ErrorMessage: {ErrorMessage}", request.SourceImage, request.Watermark.Text, ex.Message);
var errorResponse = new
{
error = ex.Message
Expand Down

0 comments on commit 48cc5f7

Please sign in to comment.