diff --git a/backend/api/Controllers/InspectionController.cs b/backend/api/Controllers/InspectionController.cs index ce8fa303..9851c52f 100644 --- a/backend/api/Controllers/InspectionController.cs +++ b/backend/api/Controllers/InspectionController.cs @@ -1,10 +1,8 @@ -using System.Globalization; -using Api.Controllers.Models; -using Api.Database.Models; - +using Api.Controllers.Models; using Api.Services; using Api.Services.MissionLoaders; using Api.Services.Models; +using Api.Utilities; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; @@ -55,53 +53,31 @@ public async Task> Create([FromRoute] string } /// - /// Lookup the inspection image for task with specified isarTaskId + /// Lookup the inspection image for task with specified isarInspectionId /// /// + /// Retrieves the inspection image associated with the given ISAR Inspection ID. /// - [HttpGet] + [HttpGet("{isarInspectionId}")] [Authorize(Roles = Role.User)] - [Route("{installationCode}/{taskId}/taskId")] - [ProducesResponseType(typeof(Inspection), StatusCodes.Status200OK)] + [ProducesResponseType(typeof(FileContentResult), StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status400BadRequest)] - [ProducesResponseType(StatusCodes.Status409Conflict)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] [ProducesResponseType(StatusCodes.Status403Forbidden)] + [ProducesResponseType(StatusCodes.Status404NotFound)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] - public async Task> GetInspectionImageById([FromRoute] string installationCode, string taskId) + public async Task GetInspectionImageByIsarInspectionId([FromRoute] string isarInspectionId) { - Inspection? inspection; - try - { - inspection = await inspectionService.ReadByIsarTaskId(taskId, readOnly: true); - if (inspection == null) return NotFound($"Could not find inspection for task with Id {taskId}."); - - } - catch (Exception e) - { - logger.LogError(e, $"Error while finding an inspection with task Id {taskId}"); - return StatusCode(StatusCodes.Status500InternalServerError); - } - - if (inspection.IsarInspectionId == null) return NotFound($"Could not find isar inspection Id {inspection.IsarInspectionId} for Inspection with task ID {taskId}."); - - var inspectionData = await inspectionService.GetInspectionStorageInfo(inspection.IsarInspectionId); - - if (inspectionData == null) return NotFound($"Could not find inspection data for inspection with isar Id {inspection.IsarInspectionId}."); - - if (!inspectionData.BlobContainer.ToLower(CultureInfo.CurrentCulture).Equals(installationCode.ToLower(CultureInfo.CurrentCulture), StringComparison.Ordinal)) - { - return NotFound($"Could not find inspection data for inspection with isar Id {inspection.IsarInspectionId} because blob name {inspectionData.BlobName} does not match installation {installationCode}."); - } try { - byte[] inspectionStream = await inspectionService.FetchInpectionImage(inspectionData.BlobName, inspectionData.BlobContainer, inspectionData.StorageAccount); + byte[] inspectionStream = await inspectionService.FetchInpectionImageFromIsarInspectionId(isarInspectionId); return File(inspectionStream, "image/png"); } - catch (Azure.RequestFailedException) + catch (InspectionNotFoundException e) { - return NotFound($"Could not find inspection blob {inspectionData.BlobName} in container {inspectionData.BlobContainer} and storage account {inspectionData.StorageAccount}."); + logger.LogError(e, "{ErrorMessage}", e.Message); + return NotFound($"Could not find inspection image with ISAR Inspection ID {isarInspectionId}"); } } } diff --git a/backend/api/Services/InspectionService.cs b/backend/api/Services/InspectionService.cs index 36e8f7c8..ed078f5f 100644 --- a/backend/api/Services/InspectionService.cs +++ b/backend/api/Services/InspectionService.cs @@ -9,15 +9,18 @@ using Api.Utilities; using Microsoft.EntityFrameworkCore; using Microsoft.Identity.Abstractions; + + + + namespace Api.Services { public interface IInspectionService { - public Task FetchInpectionImage(string inpectionName, string installationCode, string storageAccount); + public Task FetchInpectionImageFromIsarInspectionId(string isarInspectionId); public Task UpdateInspectionStatus(string isarTaskId, IsarTaskStatus isarTaskStatus); public Task ReadByIsarTaskId(string id, bool readOnly = true); public Task AddFinding(InspectionFindingQuery inspectionFindingsQuery, string isarTaskId); - public Task GetInspectionStorageInfo(string inspectionId); } @@ -31,9 +34,12 @@ public class InspectionService(FlotillaDbContext context, ILogger FetchInpectionImage(string inpectionName, string installationCode, string storageAccount) + public async Task FetchInpectionImageFromIsarInspectionId(string isarInspectionId) { - return await blobService.DownloadBlob(inpectionName, installationCode, storageAccount); + + var inspectionData = await GetInspectionStorageInfo(isarInspectionId) + ?? throw new InspectionNotFoundException($"Could not find inspection data for inspection with ISAR Inspection Id {isarInspectionId}."); + return await blobService.DownloadBlob(inspectionData.BlobName, inspectionData.BlobContainer, inspectionData.StorageAccount); } public async Task UpdateInspectionStatus(string isarTaskId, IsarTaskStatus isarTaskStatus) @@ -110,7 +116,7 @@ private IQueryable GetInspections(bool readOnly = true) return inspection; } - public async Task GetInspectionStorageInfo(string inspectionId) + private async Task GetInspectionStorageInfo(string inspectionId) { string relativePath = $"InspectionData/{inspectionId}/inspection-data-storage-location"; @@ -123,6 +129,12 @@ private IQueryable GetInspections(bool readOnly = true) } ); + if (response.StatusCode == HttpStatusCode.OK) + { + var inspectionData = await response.Content.ReadFromJsonAsync() ?? throw new JsonException("Failed to deserialize inspection data from IDA."); + return inspectionData; + } + if (response.StatusCode == HttpStatusCode.Accepted) { logger.LogInformation("Inspection data storage location for inspection with Id {inspectionId} is not yet available", inspectionId); @@ -131,7 +143,7 @@ private IQueryable GetInspections(bool readOnly = true) if (response.StatusCode == HttpStatusCode.InternalServerError) { - logger.LogError("Inetrnal server error when trying to get inspection data for inspection with Id {inspectionId}", inspectionId); + logger.LogError("Internal server error when trying to get inspection data for inspection with Id {inspectionId}", inspectionId); return null; } @@ -141,11 +153,15 @@ private IQueryable GetInspections(bool readOnly = true) return null; } - var inspectionData = await response.Content.ReadFromJsonAsync< - IDAInspectionDataResponse - >() ?? throw new JsonException("Failed to deserialize inspection data from IDA."); + if (response.StatusCode == HttpStatusCode.UnprocessableEntity) + { + logger.LogError("Anonymization workflow failed for inspection with Id {inspectionId}", inspectionId); + return null; + } + + logger.LogError("Unexpected error when trying to get inspection data for inspection with Id {inspectionId}", inspectionId); + return null; - return inspectionData; } } }