diff --git a/backend/api.test/Mocks/BlobServiceMock.cs b/backend/api.test/Mocks/BlobServiceMock.cs index 97a2dc7b..ded5b749 100644 --- a/backend/api.test/Mocks/BlobServiceMock.cs +++ b/backend/api.test/Mocks/BlobServiceMock.cs @@ -7,7 +7,7 @@ namespace Api.Test.Mocks { public class MockBlobService : IBlobService { - public async Task DownloadBlob(string blobName, string containerName, string accountName) + public async Task DownloadBlob(string blobName, string containerName, string accountName) { using var memoryStream = new System.IO.MemoryStream(); await Task.CompletedTask; diff --git a/backend/api.test/Mocks/MapServiceMock.cs b/backend/api.test/Mocks/MapServiceMock.cs index 946c33bc..38cc24dd 100644 --- a/backend/api.test/Mocks/MapServiceMock.cs +++ b/backend/api.test/Mocks/MapServiceMock.cs @@ -21,7 +21,7 @@ public class MockMapService : IMapService return new MapMetadata(); } - public async Task FetchMapImage(string mapName, string installationCode) + public async Task FetchMapImage(string mapName, string installationCode) { await Task.Run(() => Thread.Sleep(1)); string filePath = Directory.GetCurrentDirectory() + "Images/MockMapImage.png"; diff --git a/backend/api/Controllers/InspectionController.cs b/backend/api/Controllers/InspectionController.cs index ce8fa303..6d2e8974 100644 --- a/backend/api/Controllers/InspectionController.cs +++ b/backend/api/Controllers/InspectionController.cs @@ -96,7 +96,10 @@ public async Task> GetInspectionImageById([FromRoute] s try { - byte[] inspectionStream = await inspectionService.FetchInpectionImage(inspectionData.BlobName, inspectionData.BlobContainer, inspectionData.StorageAccount); + byte[]? inspectionStream = await inspectionService.FetchInpectionImage(inspectionData.BlobName, inspectionData.BlobContainer, inspectionData.StorageAccount); + + if (inspectionStream == null) return NotFound($"Could not retrieve inspection with task Id {taskId}"); + return File(inspectionStream, "image/png"); } catch (Azure.RequestFailedException) diff --git a/backend/api/Controllers/MapController.cs b/backend/api/Controllers/MapController.cs index aa9b41d3..4d819c6d 100644 --- a/backend/api/Controllers/MapController.cs +++ b/backend/api/Controllers/MapController.cs @@ -24,15 +24,11 @@ public class MapController(IMapService mapService) : ControllerBase [ProducesResponseType(StatusCodes.Status502BadGateway)] public async Task> GetMap([FromRoute] string installationCode, string mapName) { - try - { - byte[] mapStream = await mapService.FetchMapImage(mapName, installationCode); - return File(mapStream, "image/png"); - } - catch (RequestFailedException) - { - return NotFound("Could not find map for this area"); - } + byte[]? mapStream = await mapService.FetchMapImage(mapName, installationCode); + + if (mapStream == null) return NotFound($"Could not retrieve map '{mapName}' in installation {installationCode}"); + + return File(mapStream, "image/png"); } } } diff --git a/backend/api/Services/BlobService.cs b/backend/api/Services/BlobService.cs index d0591b5d..5c7cdf6a 100644 --- a/backend/api/Services/BlobService.cs +++ b/backend/api/Services/BlobService.cs @@ -11,7 +11,7 @@ namespace Api.Services { public interface IBlobService { - public Task DownloadBlob(string blobName, string containerName, string accountName); + public Task DownloadBlob(string blobName, string containerName, string accountName); public AsyncPageable FetchAllBlobs(string containerName, string accountName); @@ -20,13 +20,22 @@ public interface IBlobService public class BlobService(ILogger logger, IOptions azureOptions) : IBlobService { - public async Task DownloadBlob(string blobName, string containerName, string accountName) + public async Task DownloadBlob(string blobName, string containerName, string accountName) { var blobContainerClient = GetBlobContainerClient(containerName, accountName); var blobClient = blobContainerClient.GetBlobClient(blobName); using var memoryStream = new MemoryStream(); - await blobClient.DownloadToAsync(memoryStream); + try + { + await blobClient.DownloadToAsync(memoryStream); + } + catch (RequestFailedException) + { + logger.LogWarning("Failed to download blob {blobName} from container {containerName}", blobName, containerName); + return null; + } + return memoryStream.ToArray(); } diff --git a/backend/api/Services/InspectionService.cs b/backend/api/Services/InspectionService.cs index 36e8f7c8..243ff7ac 100644 --- a/backend/api/Services/InspectionService.cs +++ b/backend/api/Services/InspectionService.cs @@ -13,7 +13,7 @@ namespace Api.Services { public interface IInspectionService { - public Task FetchInpectionImage(string inpectionName, string installationCode, string storageAccount); + public Task FetchInpectionImage(string inpectionName, string installationCode, string storageAccount); public Task UpdateInspectionStatus(string isarTaskId, IsarTaskStatus isarTaskStatus); public Task ReadByIsarTaskId(string id, bool readOnly = true); public Task AddFinding(InspectionFindingQuery inspectionFindingsQuery, string isarTaskId); @@ -31,7 +31,7 @@ public class InspectionService(FlotillaDbContext context, ILogger FetchInpectionImage(string inpectionName, string installationCode, string storageAccount) + public async Task FetchInpectionImage(string inpectionName, string installationCode, string storageAccount) { return await blobService.DownloadBlob(inpectionName, installationCode, storageAccount); } diff --git a/backend/api/Services/MapService.cs b/backend/api/Services/MapService.cs index eab82504..9cbe7e1e 100644 --- a/backend/api/Services/MapService.cs +++ b/backend/api/Services/MapService.cs @@ -8,7 +8,7 @@ namespace Api.Services { public interface IMapService { - public Task FetchMapImage(string mapName, string installationCode); + public Task FetchMapImage(string mapName, string installationCode); public Task ChooseMapFromPositions(IList positions, string installationCode); public Task ChooseMapFromMissionRunTasks(MissionRun mission); } @@ -17,7 +17,7 @@ public class MapService(ILogger logger, IOptions blobOptions, IBlobService blobService) : IMapService { - public async Task FetchMapImage(string mapName, string installationCode) + public async Task FetchMapImage(string mapName, string installationCode) { return await blobService.DownloadBlob(mapName, installationCode, blobOptions.Value.StorageAccount); }