forked from equinor/sara
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThermalReferenceMetadataController.cs
More file actions
240 lines (225 loc) · 8.57 KB
/
Copy pathThermalReferenceMetadataController.cs
File metadata and controls
240 lines (225 loc) · 8.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
using api.Controllers.Models;
using api.Database.Models;
using api.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace api.Controllers;
[ApiController]
[Route("[controller]")]
public class ThermalReferenceMetadataController(
ILogger<ThermalReferenceMetadataController> logger,
IThermalReferenceMetadataService thermalReferenceMetadataService,
IThermalImageService thermalImageService,
IConfiguration configuration
) : ControllerBase
{
[HttpGet]
[Authorize(Roles = Role.Any)]
[ProducesResponseType(typeof(IList<ThermalReferenceMetadata>), StatusCodes.Status200OK)]
public async Task<ActionResult<IList<ThermalReferenceMetadata>>> GetThermalReferenceMetadatas()
{
try
{
var thermalReferenceMetadatas =
await thermalReferenceMetadataService.GetThermalReferenceMetadatas();
return Ok(thermalReferenceMetadatas);
}
catch (Exception ex)
{
logger.LogError(ex, "Error during GET of thermal reference metadata");
return StatusCode(
StatusCodes.Status500InternalServerError,
"An error occurred while retrieving thermal reference metadata"
);
}
}
[HttpGet("id/{id}")]
[Authorize(Roles = Role.Any)]
[ProducesResponseType(typeof(ThermalReferenceMetadata), StatusCodes.Status200OK)]
public async Task<ActionResult<ThermalReferenceMetadata>> GetThermalReferenceMetadataById(
[FromRoute] Guid id
)
{
try
{
var thermalReferenceMetadata = await thermalReferenceMetadataService.ReadById(id);
if (thermalReferenceMetadata is null)
{
return NotFound($"Could not find thermal reference metadata with id {id}");
}
return Ok(thermalReferenceMetadata);
}
catch (Exception ex)
{
logger.LogError(ex, "Error during GET of thermal reference metadata by id");
return StatusCode(
StatusCodes.Status500InternalServerError,
"An error occurred while retrieving the thermal reference metadata"
);
}
}
[HttpPost]
[Authorize(Roles = Role.Any)]
[ProducesResponseType(typeof(ThermalReferenceMetadata), StatusCodes.Status200OK)]
public async Task<ActionResult<ThermalReferenceMetadata>> CreateThermalReferenceMetadata(
[FromBody] ThermalReferenceMetadataInput input
)
{
try
{
var (imageLocation, polygonLocation) = BuildReferenceLocations(
input.ReferenceBlobStorageDirectory
);
var thermalReferenceMetadata =
await thermalReferenceMetadataService.CreateThermalReferenceMetadata(
input,
imageLocation,
polygonLocation
);
return Ok(thermalReferenceMetadata);
}
catch (ArgumentException ex)
{
logger.LogWarning(ex, "Conflicting thermal reference metadata create request");
return Conflict(ex.Message);
}
catch (Exception ex)
{
logger.LogError(ex, "Error during creation of thermal reference metadata");
return StatusCode(
StatusCodes.Status500InternalServerError,
"An error occurred while creating the thermal reference metadata"
);
}
}
[HttpPut("id/{id}")]
[Authorize(Roles = Role.Any)]
[ProducesResponseType(typeof(ThermalReferenceMetadata), StatusCodes.Status200OK)]
public async Task<ActionResult<ThermalReferenceMetadata>> UpdateThermalReferenceMetadata(
[FromRoute] Guid id,
[FromBody] ThermalReferenceMetadataInput input
)
{
try
{
var (imageLocation, polygonLocation) = BuildReferenceLocations(
input.ReferenceBlobStorageDirectory
);
var thermalReferenceMetadata =
await thermalReferenceMetadataService.UpdateThermalReferenceMetadata(
id,
input,
imageLocation,
polygonLocation
);
return Ok(thermalReferenceMetadata);
}
catch (KeyNotFoundException ex)
{
logger.LogWarning(ex, "Thermal reference metadata not found during update");
return NotFound(ex.Message);
}
catch (ArgumentException ex)
{
logger.LogWarning(ex, "Conflicting thermal reference metadata update request");
return Conflict(ex.Message);
}
catch (Exception ex)
{
logger.LogError(ex, "Error during update of thermal reference metadata");
return StatusCode(
StatusCodes.Status500InternalServerError,
"An error occurred while updating the thermal reference metadata"
);
}
}
[HttpDelete("id/{id}")]
[Authorize(Roles = Role.Any)]
[ProducesResponseType(StatusCodes.Status200OK)]
public async Task<ActionResult> DeleteThermalReferenceMetadata([FromRoute] Guid id)
{
try
{
await thermalReferenceMetadataService.RemoveThermalReferenceMetadata(id);
return Ok("Thermal reference metadata removed successfully");
}
catch (KeyNotFoundException ex)
{
logger.LogWarning(ex, "Thermal reference metadata not found during delete");
return NotFound(ex.Message);
}
catch (Exception ex)
{
logger.LogError(ex, "Error during removal of thermal reference metadata");
return StatusCode(
StatusCodes.Status500InternalServerError,
"An error occurred while removing the thermal reference metadata"
);
}
}
[HttpGet("id/{id}/image")]
[Authorize(Roles = Role.Any)]
[ProducesResponseType(typeof(FileContentResult), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<ActionResult> GetThermalReferenceImage([FromRoute] Guid id)
{
try
{
var metadata = await thermalReferenceMetadataService.ReadById(id);
if (metadata is null)
{
return NotFound($"Could not find thermal reference metadata with id {id}");
}
var result = await thermalImageService.GetThermalImageDataAsync(
metadata.ReferenceImageBlobStorageLocation
);
Response.Headers["X-Image-Width"] = result.Width.ToString();
Response.Headers["X-Image-Height"] = result.Height.ToString();
Response.Headers["X-Temperature-Min"] = result.MinTemperature.ToString(
"G9",
System.Globalization.CultureInfo.InvariantCulture
);
Response.Headers["X-Temperature-Max"] = result.MaxTemperature.ToString(
"G9",
System.Globalization.CultureInfo.InvariantCulture
);
Response.Headers.Append(
"Access-Control-Expose-Headers",
"X-Image-Width, X-Image-Height, X-Temperature-Min, X-Temperature-Max"
);
return File(result.FloatBytes, "application/octet-stream");
}
catch (Exception ex)
{
logger.LogError(ex, "Error generating thermal reference image for id {Id}", id);
return StatusCode(
StatusCodes.Status500InternalServerError,
"An error occurred while generating the thermal reference image"
);
}
}
private (
BlobStorageLocation imageLocation,
BlobStorageLocation polygonLocation
) BuildReferenceLocations(BlobDirectoryInput directoryInput)
{
var storageAccount =
configuration["Storage:ThermalReferenceStorageAccount"]
?? throw new InvalidOperationException(
"Storage:ThermalReferenceStorageAccount is not configured"
);
var imageLocation = new BlobStorageLocation
{
StorageAccount = storageAccount,
BlobContainer = directoryInput.BlobContainer,
BlobName = $"{directoryInput.BlobName}/reference_image.tiff",
};
var polygonLocation = new BlobStorageLocation
{
StorageAccount = storageAccount,
BlobContainer = directoryInput.BlobContainer,
BlobName = $"{directoryInput.BlobName}/reference_polygon.json",
};
return (imageLocation, polygonLocation);
}
}