Add question image media support
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Bson.Serialization.Attributes;
|
||||
|
||||
namespace QuestionsApi.Models;
|
||||
|
||||
public class QuestionMediaAsset
|
||||
{
|
||||
[BsonId]
|
||||
[BsonRepresentation(BsonType.ObjectId)]
|
||||
public string? Id { get; set; }
|
||||
|
||||
public string FileName { get; set; } = "";
|
||||
public string ContentType { get; set; } = "";
|
||||
public byte[] Bytes { get; set; } = [];
|
||||
public DateTime CreatedUtc { get; set; } = DateTime.UtcNow;
|
||||
}
|
||||
@@ -11,6 +11,7 @@ builder.Services.AddControllers();
|
||||
builder.Services.AddSingleton<QuestionStore>();
|
||||
builder.Services.AddSingleton<QuestionAttemptStore>();
|
||||
builder.Services.AddSingleton<RatingStore>();
|
||||
builder.Services.AddSingleton<QuestionMediaStore>();
|
||||
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen(c =>
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
using MongoDB.Driver;
|
||||
using QuestionsApi.Models;
|
||||
|
||||
namespace QuestionsApi.Services;
|
||||
|
||||
public class QuestionMediaStore
|
||||
{
|
||||
private readonly IMongoCollection<QuestionMediaAsset> _col;
|
||||
|
||||
public QuestionMediaStore(IConfiguration cfg)
|
||||
{
|
||||
var cs = cfg["MongoDB:ConnectionString"] ?? "mongodb://127.0.0.1:27017";
|
||||
var dbName = cfg["MongoDB:DatabaseName"] ?? "GameDb";
|
||||
var client = new MongoClient(cs);
|
||||
var db = client.GetDatabase(dbName);
|
||||
_col = db.GetCollection<QuestionMediaAsset>("QuestionMediaAssets");
|
||||
}
|
||||
|
||||
public async Task<QuestionMediaAsset> CreateAsync(string fileName, string contentType, byte[] bytes)
|
||||
{
|
||||
var asset = new QuestionMediaAsset
|
||||
{
|
||||
FileName = fileName.Trim(),
|
||||
ContentType = contentType.Trim(),
|
||||
Bytes = bytes,
|
||||
CreatedUtc = DateTime.UtcNow
|
||||
};
|
||||
await _col.InsertOneAsync(asset);
|
||||
return asset;
|
||||
}
|
||||
|
||||
public async Task<QuestionMediaAsset?> GetAsync(string id) =>
|
||||
await _col.Find(asset => asset.Id == id).FirstOrDefaultAsync();
|
||||
}
|
||||
Reference in New Issue
Block a user