diff --git a/Iceshrimp.Backend/Controllers/Mastodon/MediaController.cs b/Iceshrimp.Backend/Controllers/Mastodon/MediaController.cs index 0b5e27c2..3f39b6ab 100644 --- a/Iceshrimp.Backend/Controllers/Mastodon/MediaController.cs +++ b/Iceshrimp.Backend/Controllers/Mastodon/MediaController.cs @@ -2,6 +2,7 @@ using System.Diagnostics.CodeAnalysis; using System.Net; using System.Net.Mime; using Iceshrimp.Backend.Controllers.Mastodon.Attributes; +using Iceshrimp.Backend.Controllers.Mastodon.Renderers; using Iceshrimp.Backend.Controllers.Mastodon.Schemas; using Iceshrimp.Backend.Controllers.Mastodon.Schemas.Entities; using Iceshrimp.Backend.Controllers.Shared.Attributes; @@ -40,7 +41,7 @@ public class MediaController(DriveService driveSvc, DatabaseContext db) : Contro MimeType = request.File.ContentType }; var file = await driveSvc.StoreFileAsync(request.File.OpenReadStream(), user, rq); - return RenderAttachment(file); + return AttachmentRenderer.Render(file); } [HttpPut("/api/v1/media/{id}")] @@ -56,7 +57,7 @@ public class MediaController(DriveService driveSvc, DatabaseContext db) : Contro file.Comment = request.Description; await db.SaveChangesAsync(); - return RenderAttachment(file); + return AttachmentRenderer.Render(file); } [HttpGet("/api/v1/media/{id}")] @@ -68,7 +69,7 @@ public class MediaController(DriveService driveSvc, DatabaseContext db) : Contro var file = await db.DriveFiles.FirstOrDefaultAsync(p => p.Id == id && p.User == user) ?? throw GracefulException.RecordNotFound(); - return RenderAttachment(file); + return AttachmentRenderer.Render(file); } [HttpPut("/api/v2/media/{id}")] @@ -76,20 +77,4 @@ public class MediaController(DriveService driveSvc, DatabaseContext db) : Contro [ProducesErrors(HttpStatusCode.NotFound)] public IActionResult FallbackMediaRoute([SuppressMessage("ReSharper", "UnusedParameter.Global")] string id) => throw GracefulException.NotFound("This endpoint is not implemented, but some clients expect a 404 here."); - - private static AttachmentEntity RenderAttachment(DriveFile file) - { - return new AttachmentEntity - { - Id = file.Id, - Type = AttachmentEntity.GetType(file.Type), - Url = file.AccessUrl, - Blurhash = file.Blurhash, - Description = file.Comment, - PreviewUrl = file.ThumbnailAccessUrl, - RemoteUrl = file.Uri, - Sensitive = file.IsSensitive - //Metadata = TODO, - }; - } } \ No newline at end of file diff --git a/Iceshrimp.Backend/Controllers/Mastodon/Renderers/AttachmentRenderer.cs b/Iceshrimp.Backend/Controllers/Mastodon/Renderers/AttachmentRenderer.cs new file mode 100644 index 00000000..cb6ec83b --- /dev/null +++ b/Iceshrimp.Backend/Controllers/Mastodon/Renderers/AttachmentRenderer.cs @@ -0,0 +1,23 @@ +using Iceshrimp.Backend.Controllers.Mastodon.Schemas.Entities; +using Iceshrimp.Backend.Core.Database.Tables; + +namespace Iceshrimp.Backend.Controllers.Mastodon.Renderers; + +public static class AttachmentRenderer +{ + public static AttachmentEntity Render(DriveFile file) => new() + { + Id = file.Id, + Type = AttachmentEntity.GetType(file.Type), + Url = file.AccessUrl, + Blurhash = file.Blurhash, + PreviewUrl = file.ThumbnailAccessUrl, + Description = file.Comment, + RemoteUrl = file.Uri, + Sensitive = file.IsSensitive, + // + Metadata = file.Properties is { Height: { } height, Width: { } width } + ? new AttachmentMetadata(width, height) + : null + }; +} diff --git a/Iceshrimp.Backend/Controllers/Mastodon/Renderers/NoteRenderer.cs b/Iceshrimp.Backend/Controllers/Mastodon/Renderers/NoteRenderer.cs index d78dad52..767e2a59 100644 --- a/Iceshrimp.Backend/Controllers/Mastodon/Renderers/NoteRenderer.cs +++ b/Iceshrimp.Backend/Controllers/Mastodon/Renderers/NoteRenderer.cs @@ -255,18 +255,7 @@ public class NoteRenderer( if (notes.Count == 0) return []; var ids = notes.SelectMany(n => n.FileIds).Distinct(); return await db.DriveFiles.Where(p => ids.Contains(p.Id)) - .Select(f => new AttachmentEntity - { - Id = f.Id, - Url = f.AccessUrl, - Blurhash = f.Blurhash, - PreviewUrl = f.ThumbnailAccessUrl, - Description = f.Comment, - Metadata = null, - RemoteUrl = f.Uri, - Type = AttachmentEntity.GetType(f.Type), - Sensitive = f.IsSensitive - }) + .Select(f => AttachmentRenderer.Render(f)) .ToListAsync(); } @@ -275,18 +264,7 @@ public class NoteRenderer( var ids = fileIds.Distinct().ToList(); if (ids.Count == 0) return []; return await db.DriveFiles.Where(p => ids.Contains(p.Id)) - .Select(f => new AttachmentEntity - { - Id = f.Id, - Url = f.AccessUrl, - Blurhash = f.Blurhash, - PreviewUrl = f.ThumbnailAccessUrl, - Description = f.Comment, - Metadata = null, - RemoteUrl = f.Uri, - Type = AttachmentEntity.GetType(f.Type), - Sensitive = f.IsSensitive - }) + .Select(f => AttachmentRenderer.Render(f)) .ToListAsync(); } diff --git a/Iceshrimp.Backend/Controllers/Mastodon/Schemas/Entities/AttachmentEntity.cs b/Iceshrimp.Backend/Controllers/Mastodon/Schemas/Entities/AttachmentEntity.cs index 508cc922..c9ca5ebf 100644 --- a/Iceshrimp.Backend/Controllers/Mastodon/Schemas/Entities/AttachmentEntity.cs +++ b/Iceshrimp.Backend/Controllers/Mastodon/Schemas/Entities/AttachmentEntity.cs @@ -47,7 +47,15 @@ public enum AttachmentType Audio } -public class AttachmentMetadata +public class AttachmentMetadata(int width, int height) { - //TODO -} \ No newline at end of file + [J("original")] public OriginalAttachmentMetadata Original => new(width, height); +} + +public class OriginalAttachmentMetadata(int width, int height) +{ + [J("width")] public int Width => width; + [J("height")] public int Height => height; + [J("size")] public string Size => $"{width}x{height}"; + [J("aspect")] public float Aspect => (float)width / height; +}