summaryrefslogtreecommitdiff
path: root/Controllers
diff options
context:
space:
mode:
Diffstat (limited to 'Controllers')
-rw-r--r--Controllers/ApiMediaController.cs24
-rw-r--r--Controllers/ApiUserController.cs2
2 files changed, 25 insertions, 1 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();
diff --git a/Controllers/ApiUserController.cs b/Controllers/ApiUserController.cs
index 3230218..c581956 100644
--- a/Controllers/ApiUserController.cs
+++ b/Controllers/ApiUserController.cs
@@ -92,7 +92,7 @@ public class ApiUserController : Controller {
[HttpDelete("{userId}")]
public async Task<IActionResult> DeleteUserAsync([FromRoute] Guid userId) {
- if(userId == HBContext.AdminUser)
+ if(userId == InternalTag.AdminUser)
throw new ApiModels.ArgumentException("Cannot delete the admin user");
using var db = dbFactory.CreateDbContext();