Add question image media support

This commit is contained in:
hz
2026-07-11 03:12:46 -05:00
parent 5ed96ae3b8
commit 9c8775d740
5 changed files with 215 additions and 3 deletions
@@ -14,12 +14,18 @@ public class QuestionsController : ControllerBase
private readonly QuestionStore _questions;
private readonly QuestionAttemptStore _attempts;
private readonly RatingStore _ratings;
private readonly QuestionMediaStore _media;
public QuestionsController(QuestionStore questions, QuestionAttemptStore attempts, RatingStore ratings)
public QuestionsController(
QuestionStore questions,
QuestionAttemptStore attempts,
RatingStore ratings,
QuestionMediaStore media)
{
_questions = questions;
_attempts = attempts;
_ratings = ratings;
_media = media;
}
[HttpGet("systems")]
@@ -101,6 +107,48 @@ public class QuestionsController : ControllerBase
});
}
[HttpPost("media")]
[Authorize(Roles = "SUPER")]
[RequestSizeLimit(5_000_000)]
public async Task<IActionResult> UploadMedia(IFormFile file)
{
if (file is null || file.Length == 0)
return BadRequest("file required");
if (file.Length > 5_000_000)
return BadRequest("file must be 5 MB or smaller");
var contentType = file.ContentType.Trim().ToLowerInvariant();
var allowedTypes = new HashSet<string>
{
"image/png",
"image/jpeg",
"image/webp"
};
if (!allowedTypes.Contains(contentType))
return BadRequest("file must be a png, jpeg, or webp image");
await using var stream = file.OpenReadStream();
using var memory = new MemoryStream();
await stream.CopyToAsync(memory);
var asset = await _media.CreateAsync(file.FileName, contentType, memory.ToArray());
return Ok(new
{
id = asset.Id ?? "",
url = $"/api/questions/media/{asset.Id}"
});
}
[HttpGet("media/{id}")]
public async Task<IActionResult> GetMedia(string id)
{
var asset = await _media.GetAsync(id.Trim());
if (asset is null)
return NotFound();
return File(asset.Bytes, asset.ContentType);
}
[HttpPost("{externalId}/skip")]
[Authorize(Roles = "USER,SUPER")]
public async Task<IActionResult> Skip(string externalId)