5.2 ImageController.GetImage
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
40
src/FrameProcessor/Controllers/ImageController.cs
Normal file
40
src/FrameProcessor/Controllers/ImageController.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
using FrameProcessor.Configuration;
|
||||
using FrameProcessor.Domain;
|
||||
using FrameProcessor.Storage;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace FrameProcessor.Controllers;
|
||||
|
||||
[ApiController]
|
||||
public sealed class ImageController : ControllerBase
|
||||
{
|
||||
private readonly FramesRegistry _frames;
|
||||
private readonly ImageStore _store;
|
||||
|
||||
public ImageController(FramesRegistry frames, ImageStore store)
|
||||
{
|
||||
_frames = frames;
|
||||
_store = store;
|
||||
}
|
||||
|
||||
[HttpGet("/i/{mac}.png")]
|
||||
public IActionResult GetImage(string mac)
|
||||
{
|
||||
if (!MacAddress.TryParse(mac, out var macAddress) || !_frames.TryGetByMac(macAddress, out _))
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
if (!_store.TryGetPath(macAddress, out var path))
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
var mtime = System.IO.File.GetLastWriteTimeUtc(path);
|
||||
Response.Headers.CacheControl = "no-store";
|
||||
Response.Headers.ETag = $"\"{mtime.Ticks:x}\"";
|
||||
|
||||
var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
|
||||
return File(stream, "image/png");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user