diff options
Diffstat (limited to 'Controllers/ApiMediaController.cs')
| -rw-r--r-- | Controllers/ApiMediaController.cs | 24 |
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(); |
