summaryrefslogtreecommitdiff
path: root/Controllers/ApiMediaController.cs
diff options
context:
space:
mode:
authorJake Mannens <jake@asger.xyz>2026-06-15 10:35:47 +1000
committerJake Mannens <jake@asger.xyz>2026-06-15 10:35:47 +1000
commite556de927015acd8d9934e68707a95901acfad8e (patch)
treec04e9601a15cff5f1fe9980e8f2ff41bae351635 /Controllers/ApiMediaController.cs
parentdf4bfad33e1b3fc2ce35af8000380029ecb444cf (diff)
Diffstat (limited to 'Controllers/ApiMediaController.cs')
-rw-r--r--Controllers/ApiMediaController.cs50
1 files changed, 28 insertions, 22 deletions
diff --git a/Controllers/ApiMediaController.cs b/Controllers/ApiMediaController.cs
index a1b07b1..bb6c81e 100644
--- a/Controllers/ApiMediaController.cs
+++ b/Controllers/ApiMediaController.cs
@@ -1,5 +1,6 @@
using HyperBooru.ApiModels;
using HyperBooru.Services;
+using HyperBooru.Util;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
@@ -26,7 +27,7 @@ public class ApiMediaController : Controller {
var media = await db.Media.FirstOrDefaultAsync(m => m.Guid == mediaId);
if(media is null)
- throw new ObjectNotFoundException(mediaId);
+ throw new ObjectNotFoundException([ mediaId ]);
return Ok((ApiModels.Media) media);
}
@@ -40,7 +41,7 @@ public class ApiMediaController : Controller {
.FirstOrDefaultAsync(m => m.Guid == mediaId);
if(media is null)
- throw new ObjectNotFoundException(mediaId);
+ throw new ObjectNotFoundException([ mediaId ]);
return Ok(media.UploadedFiles.Select(uf => (ApiModels.UploadedFile) uf).ToArray());
}
@@ -52,10 +53,10 @@ public class ApiMediaController : Controller {
var media = await db.Media.FirstOrDefaultAsync(m => m.Guid == updatedMedia.MediaId);
if(media is null)
- return NotFound();
+ throw new ObjectNotFoundException([ updatedMedia.MediaId ]);
- media.ShortDescription = updatedMedia.ShortDescription;
- media.LongDescription = updatedMedia.LongDescription;
+ media.ShortDescription = updatedMedia.ShortDescription?.NullIfEmpty();
+ media.LongDescription = updatedMedia.LongDescription?.NullIfEmpty();
await db.SaveChangesAsync();
await transaction.CommitAsync();
@@ -66,9 +67,9 @@ public class ApiMediaController : Controller {
[HttpPost]
public IActionResult Upload() {
if(Request.Form.Files.Count == 0)
- return BadRequest("No files");
+ throw new ApiModels.ArgumentException("No files");
if(Request.Form.Files.Count > 1)
- return BadRequest("More than one file supplied");
+ throw new ApiModels.ArgumentException("More than one file supplied");
var metadataString = Request.Form.Files
.First()
@@ -87,7 +88,7 @@ public class ApiMediaController : Controller {
metadata?.LastAccessTime,
metadata?.LastWriteTime,
metadata?.CreateTime,
- metadata?.Path,
+ metadata?.Path.NullIfEmpty(),
metadata?.PathType,
metadata?.Tags);
@@ -108,7 +109,7 @@ public class ApiMediaController : Controller {
.ThenInclude(td => td.ImplicitTags)
.FirstOrDefaultAsync(m => m.Guid == mediaId);
if(media is null)
- return NotFound();
+ throw new ObjectNotFoundException([ mediaId ]);
return Ok(media.Tags.Select(t => (ApiModels.TagDefinition) t.TagDefinition).ToArray());
}
@@ -121,13 +122,15 @@ public class ApiMediaController : Controller {
using var db = dbFactory.CreateDbContext();
using var transaction = await db.Database.BeginTransactionAsync();
+ var missing = new List<Guid>();
+
var media = await db.Media
.Include(m => m.Tags)
.ThenInclude(t => t.TagDefinition)
.ThenInclude(td => td.ImplicitTags)
.FirstOrDefaultAsync(m => m.Guid == mediaId);
if(media is null)
- return NotFound();
+ missing.Add(mediaId);
tagIds = tagIds.Distinct().ToArray();
@@ -135,8 +138,9 @@ public class ApiMediaController : Controller {
.Where(td => tagIds.Contains(td.Guid))
.ToArrayAsync();
- if(tags.Count() < tagIds.Count())
- return NotFound("Invalid tag IDs specified");
+ missing.AddRange(tagIds.Except(tags.Select(td => td.Guid)));
+ if(missing.Any())
+ throw new ObjectNotFoundException(missing);
media.Tags.AddRange(tags
.Where(td => !media.Tags.Select(t => t.TagDefinition.Guid).Contains(td.Guid))
@@ -156,23 +160,24 @@ public class ApiMediaController : Controller {
using var db = dbFactory.CreateDbContext();
using var transaction = await db.Database.BeginTransactionAsync();
+ var missing = new List<Guid>();
+
var media = await db.Media
.Include(m => m.Tags)
.ThenInclude(t => t.TagDefinition)
.ThenInclude(td => td.ImplicitTags)
.FirstOrDefaultAsync(m => m.Guid == mediaId);
if(media is null)
- return NotFound();
+ missing.Add(mediaId);
tagIds = tagIds.Distinct().Order().ToArray();
var tags = await db.TagDefinitions
.Where(td => tagIds.Contains(td.Guid))
.ToArrayAsync();
- var missingTags = tagIds.Except(tags.Select(td => td.Guid));
- var missingTagsString = string.Join(", ", missingTags.Select(t => t.ToString()));
- if(missingTags.Any())
- return BadRequest($"Invalid tag IDs specified: {missingTagsString}");
+ missing.AddRange(tagIds.Except(tags.Select(td => td.Guid)));
+ if(missing.Any())
+ throw new ObjectNotFoundException(missing);
media.Tags.AddRange(tags
.Where(td => !media.Tags.Select(t => t.TagDefinition.Guid).Contains(td.Guid))
@@ -195,20 +200,21 @@ public class ApiMediaController : Controller {
using var db = dbFactory.CreateDbContext();
using var transaction = await db.Database.BeginTransactionAsync();
+ var missing = new List<Guid>();
+
var media = await db.Media
.Include(m => m.Tags)
.ThenInclude(t => t.TagDefinition)
.ThenInclude(td => td.ImplicitTags)
.FirstOrDefaultAsync(m => m.Guid == mediaId);
if(media is null)
- return NotFound();
+ missing.Add(mediaId);
tagIds = tagIds.Distinct().Order().ToArray();
- var missingTags = tagIds.Except(media.Tags.Select(t => t.TagDefinition.Guid));
- var missingTagsString = string.Join(", ", missingTags.Select(t => t.ToString()));
- if(missingTags.Any())
- return BadRequest($"Media does not contain the following tags: {missingTagsString}");
+ missing.AddRange(tagIds.Except(media.Tags.Select(t => t.TagDefinition.Guid)));
+ if(missing.Any())
+ throw new ObjectNotFoundException(missing);
db.Tags.RemoveRange(
media.Tags.Where(t => tagIds.Contains(t.TagDefinition.Guid)));