[backend/masto-client] Return attachment metadata when available (ISH-657)
This commit is contained in:
parent
911480afcb
commit
7dc3143089
4 changed files with 40 additions and 46 deletions
|
@ -2,6 +2,7 @@ using System.Diagnostics.CodeAnalysis;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Mime;
|
using System.Net.Mime;
|
||||||
using Iceshrimp.Backend.Controllers.Mastodon.Attributes;
|
using Iceshrimp.Backend.Controllers.Mastodon.Attributes;
|
||||||
|
using Iceshrimp.Backend.Controllers.Mastodon.Renderers;
|
||||||
using Iceshrimp.Backend.Controllers.Mastodon.Schemas;
|
using Iceshrimp.Backend.Controllers.Mastodon.Schemas;
|
||||||
using Iceshrimp.Backend.Controllers.Mastodon.Schemas.Entities;
|
using Iceshrimp.Backend.Controllers.Mastodon.Schemas.Entities;
|
||||||
using Iceshrimp.Backend.Controllers.Shared.Attributes;
|
using Iceshrimp.Backend.Controllers.Shared.Attributes;
|
||||||
|
@ -40,7 +41,7 @@ public class MediaController(DriveService driveSvc, DatabaseContext db) : Contro
|
||||||
MimeType = request.File.ContentType
|
MimeType = request.File.ContentType
|
||||||
};
|
};
|
||||||
var file = await driveSvc.StoreFileAsync(request.File.OpenReadStream(), user, rq);
|
var file = await driveSvc.StoreFileAsync(request.File.OpenReadStream(), user, rq);
|
||||||
return RenderAttachment(file);
|
return AttachmentRenderer.Render(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPut("/api/v1/media/{id}")]
|
[HttpPut("/api/v1/media/{id}")]
|
||||||
|
@ -56,7 +57,7 @@ public class MediaController(DriveService driveSvc, DatabaseContext db) : Contro
|
||||||
file.Comment = request.Description;
|
file.Comment = request.Description;
|
||||||
await db.SaveChangesAsync();
|
await db.SaveChangesAsync();
|
||||||
|
|
||||||
return RenderAttachment(file);
|
return AttachmentRenderer.Render(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet("/api/v1/media/{id}")]
|
[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) ??
|
var file = await db.DriveFiles.FirstOrDefaultAsync(p => p.Id == id && p.User == user) ??
|
||||||
throw GracefulException.RecordNotFound();
|
throw GracefulException.RecordNotFound();
|
||||||
|
|
||||||
return RenderAttachment(file);
|
return AttachmentRenderer.Render(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPut("/api/v2/media/{id}")]
|
[HttpPut("/api/v2/media/{id}")]
|
||||||
|
@ -76,20 +77,4 @@ public class MediaController(DriveService driveSvc, DatabaseContext db) : Contro
|
||||||
[ProducesErrors(HttpStatusCode.NotFound)]
|
[ProducesErrors(HttpStatusCode.NotFound)]
|
||||||
public IActionResult FallbackMediaRoute([SuppressMessage("ReSharper", "UnusedParameter.Global")] string id) =>
|
public IActionResult FallbackMediaRoute([SuppressMessage("ReSharper", "UnusedParameter.Global")] string id) =>
|
||||||
throw GracefulException.NotFound("This endpoint is not implemented, but some clients expect a 404 here.");
|
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,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
}
|
|
@ -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
|
||||||
|
};
|
||||||
|
}
|
|
@ -255,18 +255,7 @@ public class NoteRenderer(
|
||||||
if (notes.Count == 0) return [];
|
if (notes.Count == 0) return [];
|
||||||
var ids = notes.SelectMany(n => n.FileIds).Distinct();
|
var ids = notes.SelectMany(n => n.FileIds).Distinct();
|
||||||
return await db.DriveFiles.Where(p => ids.Contains(p.Id))
|
return await db.DriveFiles.Where(p => ids.Contains(p.Id))
|
||||||
.Select(f => new AttachmentEntity
|
.Select(f => AttachmentRenderer.Render(f))
|
||||||
{
|
|
||||||
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
|
|
||||||
})
|
|
||||||
.ToListAsync();
|
.ToListAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -275,18 +264,7 @@ public class NoteRenderer(
|
||||||
var ids = fileIds.Distinct().ToList();
|
var ids = fileIds.Distinct().ToList();
|
||||||
if (ids.Count == 0) return [];
|
if (ids.Count == 0) return [];
|
||||||
return await db.DriveFiles.Where(p => ids.Contains(p.Id))
|
return await db.DriveFiles.Where(p => ids.Contains(p.Id))
|
||||||
.Select(f => new AttachmentEntity
|
.Select(f => AttachmentRenderer.Render(f))
|
||||||
{
|
|
||||||
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
|
|
||||||
})
|
|
||||||
.ToListAsync();
|
.ToListAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -47,7 +47,15 @@ public enum AttachmentType
|
||||||
Audio
|
Audio
|
||||||
}
|
}
|
||||||
|
|
||||||
public class AttachmentMetadata
|
public class AttachmentMetadata(int width, int height)
|
||||||
{
|
{
|
||||||
//TODO
|
[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;
|
||||||
}
|
}
|
Loading…
Add table
Reference in a new issue