Skip to content

Commit

Permalink
Add exception handling to blob store
Browse files Browse the repository at this point in the history
  • Loading branch information
andchiind committed Dec 18, 2024
1 parent 5c97282 commit cc15480
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 19 deletions.
2 changes: 1 addition & 1 deletion backend/api.test/Mocks/BlobServiceMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Api.Test.Mocks
{
public class MockBlobService : IBlobService
{
public async Task<byte[]> DownloadBlob(string blobName, string containerName, string accountName)
public async Task<byte[]?> DownloadBlob(string blobName, string containerName, string accountName)
{
using var memoryStream = new System.IO.MemoryStream();
await Task.CompletedTask;
Expand Down
2 changes: 1 addition & 1 deletion backend/api.test/Mocks/MapServiceMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class MockMapService : IMapService
return new MapMetadata();
}

public async Task<byte[]> FetchMapImage(string mapName, string installationCode)
public async Task<byte[]?> FetchMapImage(string mapName, string installationCode)
{
await Task.Run(() => Thread.Sleep(1));
string filePath = Directory.GetCurrentDirectory() + "Images/MockMapImage.png";
Expand Down
5 changes: 4 additions & 1 deletion backend/api/Controllers/InspectionController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@ public async Task<ActionResult<Inspection>> 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)
Expand Down
14 changes: 5 additions & 9 deletions backend/api/Controllers/MapController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,11 @@ public class MapController(IMapService mapService) : ControllerBase
[ProducesResponseType(StatusCodes.Status502BadGateway)]
public async Task<ActionResult<byte[]>> 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");
}
}
}
15 changes: 12 additions & 3 deletions backend/api/Services/BlobService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Api.Services
{
public interface IBlobService
{
public Task<byte[]> DownloadBlob(string blobName, string containerName, string accountName);
public Task<byte[]?> DownloadBlob(string blobName, string containerName, string accountName);

public AsyncPageable<BlobItem> FetchAllBlobs(string containerName, string accountName);

Expand All @@ -20,13 +20,22 @@ public interface IBlobService

public class BlobService(ILogger<BlobService> logger, IOptions<AzureAdOptions> azureOptions) : IBlobService
{
public async Task<byte[]> DownloadBlob(string blobName, string containerName, string accountName)
public async Task<byte[]?> 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();
}
Expand Down
4 changes: 2 additions & 2 deletions backend/api/Services/InspectionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Api.Services
{
public interface IInspectionService
{
public Task<byte[]> FetchInpectionImage(string inpectionName, string installationCode, string storageAccount);
public Task<byte[]?> FetchInpectionImage(string inpectionName, string installationCode, string storageAccount);
public Task<Inspection> UpdateInspectionStatus(string isarTaskId, IsarTaskStatus isarTaskStatus);
public Task<Inspection?> ReadByIsarTaskId(string id, bool readOnly = true);
public Task<Inspection?> AddFinding(InspectionFindingQuery inspectionFindingsQuery, string isarTaskId);
Expand All @@ -31,7 +31,7 @@ public class InspectionService(FlotillaDbContext context, ILogger<InspectionServ
{
public const string ServiceName = "IDA";

public async Task<byte[]> FetchInpectionImage(string inpectionName, string installationCode, string storageAccount)
public async Task<byte[]?> FetchInpectionImage(string inpectionName, string installationCode, string storageAccount)
{
return await blobService.DownloadBlob(inpectionName, installationCode, storageAccount);
}
Expand Down
4 changes: 2 additions & 2 deletions backend/api/Services/MapService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Api.Services
{
public interface IMapService
{
public Task<byte[]> FetchMapImage(string mapName, string installationCode);
public Task<byte[]?> FetchMapImage(string mapName, string installationCode);
public Task<MapMetadata?> ChooseMapFromPositions(IList<Position> positions, string installationCode);
public Task<MapMetadata?> ChooseMapFromMissionRunTasks(MissionRun mission);
}
Expand All @@ -17,7 +17,7 @@ public class MapService(ILogger<MapService> logger,
IOptions<MapBlobOptions> blobOptions,
IBlobService blobService) : IMapService
{
public async Task<byte[]> FetchMapImage(string mapName, string installationCode)
public async Task<byte[]?> FetchMapImage(string mapName, string installationCode)
{
return await blobService.DownloadBlob(mapName, installationCode, blobOptions.Value.StorageAccount);
}
Expand Down

0 comments on commit cc15480

Please sign in to comment.