summaryrefslogtreecommitdiff
path: root/Controllers/ApiMediaController.cs
diff options
context:
space:
mode:
authorJake Mannens <jake@asger.xyz>2026-06-21 21:02:44 +1000
committerJake Mannens <jake@asger.xyz>2026-06-25 12:26:07 +1000
commit713867bbda8322a0938d6b8f0c850752622fdd42 (patch)
tree1f8c4e58480471aecdee37e3f734c4c1211e16b1 /Controllers/ApiMediaController.cs
parent79d383f6f48df0078e6ba13239f9c4c94fbc9ea6 (diff)
Diffstat (limited to 'Controllers/ApiMediaController.cs')
-rw-r--r--Controllers/ApiMediaController.cs24
1 files changed, 24 insertions, 0 deletions
diff --git a/Controllers/ApiMediaController.cs b/Controllers/ApiMediaController.cs
index bb6c81e..8ca6121 100644
--- a/Controllers/ApiMediaController.cs
+++ b/Controllers/ApiMediaController.cs
@@ -5,6 +5,7 @@ using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Text.Json;
+using System.Text.RegularExpressions;
namespace HyperBooru.Controllers;
@@ -15,6 +16,9 @@ public class ApiMediaController : Controller {
private IDbContextFactory<HBContext> dbFactory;
private IMediaService mediaService;
+ private readonly Regex checksumRegex =
+ new Regex("^[0-9a-f]{32}$", RegexOptions.IgnoreCase | RegexOptions.Compiled);
+
public ApiMediaController(IDbContextFactory<HBContext> dbFactory, IMediaService mediaService) {
this.dbFactory = dbFactory;
this.mediaService = mediaService;
@@ -32,6 +36,26 @@ public class ApiMediaController : Controller {
return Ok((ApiModels.Media) media);
}
+ [HttpGet("checksum/{checksum}")]
+ public async Task<IActionResult> GetChecksum([FromRoute] string checksum) {
+ if(!checksumRegex.IsMatch(checksum))
+ throw new ApiModels.ArgumentException("Invalid checksum string");
+
+ using var db = dbFactory.CreateDbContext();
+
+ var media = await db.UploadedFiles
+ .Include(uf => uf.Media)
+ .Where(uf => uf.Checksum == checksum.ToLower())
+ .Select(uf => uf.Media)
+ .Distinct()
+ .ToArrayAsync();
+
+ if(media.Count() == 0)
+ throw new ObjectNotFoundException([ Guid.Empty ]);
+
+ return Ok(media.Select(m => (ApiModels.Media) m).ToArray());
+ }
+
[HttpGet("{mediaId}/files")]
public async Task<IActionResult> GetUploadedFiles([FromRoute] Guid mediaId) {
using var db = dbFactory.CreateDbContext();