summaryrefslogtreecommitdiff
path: root/Controllers
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 02:23:14 +1000
commit16aa1d68777f4d5a6f41df6612f2a4535394d334 (patch)
tree935381730c715bcfac4345de19dd2a0771966c97 /Controllers
parent79d383f6f48df0078e6ba13239f9c4c94fbc9ea6 (diff)
Added API endpoint to get media items by file checksum
Diffstat (limited to 'Controllers')
-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();