Skip to content

Commit

Permalink
Always download map from blob
Browse files Browse the repository at this point in the history
Earlier the map was downloaded to a local file and then read.
This lead to an issue when hosting the backend in aurora where it was
a readonly file system. This fix makes it so that the image is read
directly into memory before being passed to the frontend.
  • Loading branch information
oysand committed Feb 25, 2023
1 parent 8a4ff3b commit a736956
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 24 deletions.
4 changes: 1 addition & 3 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ RUN dotnet publish -c release -o /app
FROM mcr.microsoft.com/dotnet/aspnet:6.0
WORKDIR /app
COPY --from=build /app ./

# Create folder for temporary storing of maps
RUN mkdir -p /app/Database/Maps/
RUN apt-get update && apt-get install -y apt-utils libgdiplus libc6-dev

EXPOSE 8000

Expand Down
Binary file added backend/api.test/Mocks/Images/MockMapImage.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 5 additions & 2 deletions backend/api.test/Mocks/MapServiceMock.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Api.Database.Models;
Expand All @@ -14,10 +16,11 @@ public async Task<MissionMap> AssignMapToMission(string assetCode, List<PlannedT
return new MissionMap();
}

public async Task<string> FetchMapImage(string missionId)
public async Task<Image> FetchMapImage(string missionId)
{
await Task.Run(() => Thread.Sleep(1));
return "filepath";
string filePath = Directory.GetCurrentDirectory() + "Images/MockMapImage.png";
return Image.FromFile(filePath); ;
}
}
}
8 changes: 5 additions & 3 deletions backend/api/Controllers/MissionController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,11 @@ public async Task<ActionResult<byte[]>> GetMap([FromRoute] string id)
{
try
{
string filePath = await _mapService.FetchMapImage(id);
var returnFile = PhysicalFile(filePath, "image/png");
return returnFile;
var mapImage = await _mapService.FetchMapImage(id);

using var memoryStream = new MemoryStream();
mapImage.Save(memoryStream, mapImage.RawFormat);
return File(memoryStream.ToArray(), "image/png");
}
catch (Azure.RequestFailedException)
{
Expand Down
24 changes: 8 additions & 16 deletions backend/api/Services/MapService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Globalization;
using System.Drawing;
using System.Globalization;
using Api.Database.Context;
using Api.Database.Models;
using Api.Options;
Expand All @@ -13,7 +14,7 @@ namespace Api.Services
{
public interface IMapService
{
public abstract Task<string> FetchMapImage(string missionId);
public abstract Task<Image> FetchMapImage(string missionId);
public abstract Task<MissionMap> AssignMapToMission(
string assetCode,
List<PlannedTask> tasks
Expand All @@ -26,8 +27,6 @@ public class MapService : IMapService
private readonly IOptions<AzureAdOptions> _azureOptions;
private readonly IOptions<MapBlobOptions> _blobOptions;
private readonly FlotillaDbContext _dbContext;
private readonly string _localFilePath =
Directory.GetCurrentDirectory() + "/Database/Maps/map.png";

public MapService(
ILogger<MapService> logger,
Expand All @@ -42,7 +41,7 @@ FlotillaDbContext dbContext
_dbContext = dbContext;
}

public async Task<string> FetchMapImage(string missionId)
public async Task<Image> FetchMapImage(string missionId)
{
var currentMission = _dbContext.Missions.Find(missionId);
if (currentMission == null)
Expand Down Expand Up @@ -130,22 +129,15 @@ private BlobContainerClient GetBlobContainerClient(string asset)
return containerClient;
}

private async Task<string> DownloadMapImageFromBlobStorage(Mission currentMission)
private async Task<Image> DownloadMapImageFromBlobStorage(Mission currentMission)
{
var blobContainerClient = GetBlobContainerClient(
currentMission.AssetCode.ToLower(CultureInfo.CurrentCulture)
);
var blobClient = blobContainerClient.GetBlobClient(currentMission.Map.MapName);
try
{
await blobClient.DownloadToAsync(_localFilePath);
}
catch (RequestFailedException e)
{
_logger.LogError("Directory not found: {message}", e.Message);
throw e;
}
return _localFilePath;

using var stream = await blobClient.OpenReadAsync();
return Image.FromStream(stream);
}

private Boundary ExtractMapMetadata(BlobItem map)
Expand Down

0 comments on commit a736956

Please sign in to comment.