diff --git a/Marco.Pms.Services/Controllers/ImageController.cs b/Marco.Pms.Services/Controllers/ImageController.cs index 2a1b057..fedc067 100644 --- a/Marco.Pms.Services/Controllers/ImageController.cs +++ b/Marco.Pms.Services/Controllers/ImageController.cs @@ -272,5 +272,44 @@ namespace Marco.Pms.Services.Controllers return Ok(ApiResponse.SuccessResponse(response, "Images for provided batchId fetched successfully", 200)); } + [HttpGet("{documentId}")] + public async Task GetImage(Guid documentId) + { + // Log the start of the image fetch process + _logger.LogInfo("GetImage called for DocumentId: {DocumentId}", documentId); + + // Step 1: Get the currently logged-in employee (for future use like permission checks or auditing) + var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync(); + + // Step 2: Fetch the document from the database based on the provided ID + var document = await _context.Documents.FirstOrDefaultAsync(d => d.Id == documentId); + + // Step 3: If document doesn't exist, return a 400 Bad Request response + if (document == null) + { + _logger.LogWarning("Document not found for DocumentId: {DocumentId}", documentId); + return BadRequest(ApiResponse.ErrorResponse("Document not found", "Document not found", 400)); + } + + // Step 4: Generate pre-signed URLs for thumbnail and full image (if keys exist) + string? thumbnailUrl = document.ThumbS3Key != null + ? _s3Service.GeneratePreSignedUrlAsync(document.ThumbS3Key) + : null; + + string? imageUrl = document.S3Key != null + ? _s3Service.GeneratePreSignedUrlAsync(document.S3Key) + : null; + + // Step 5: Prepare the response object + var response = new + { + ThumbnailUrl = thumbnailUrl, + ImageUrl = imageUrl + }; + + // Step 6: Log successful fetch and return the result + _logger.LogInfo("Image fetched successfully for DocumentId: {DocumentId}", documentId); + return Ok(ApiResponse.SuccessResponse(response, "Image fetched successfully", 200)); + } } }