UApi: partial work on filling in wrappers for non-json endpoints

This commit is contained in:
Rory&
2026-02-26 08:59:44 +01:00
parent e30e18b89d
commit 70034a410d
2 changed files with 98 additions and 0 deletions

View File

@@ -0,0 +1,40 @@
using Microsoft.AspNetCore.Mvc;
using Spacebar.Interop.Authentication.AspNetCore;
using Spacebar.Models.Db.Contexts;
using Spacebar.Models.Db.Models;
using Spacebar.UApi.Controllers.Messages;
using Spacebar.UApi.Services;
namespace Spacebar.UApi.Controllers;
[ApiController]
[Route("/api/v{_}/guilds/{guildId}/stickers/")]
public class GuildStickerController(ILogger<MessagesController> logger, SpacebarDbContext db, SpacebarAspNetAuthenticationService authService, UApiConfiguration cfg, PermissionService permService) : ControllerBase {
// TODO proper response type
[HttpPost]
public async Task<Sticker> UploadGuildSticker(string guildId, MultipartFormDataContent content) {
var sticker = new Sticker() {
GuildId = guildId
};
foreach (var item in content) {
switch (item.Headers.ContentDisposition.Name.Trim('"')) {
case "name":
sticker.Name = await item.ReadAsStringAsync();
break;
case "description":
sticker.Description = await item.ReadAsStringAsync();
break;
case "tags":
sticker.Tags = await item.ReadAsStringAsync();
break;
case "file":
var fileContent = await item.ReadAsStreamAsync();
break;
}
}
return sticker;
}
}

View File

@@ -0,0 +1,58 @@
using System.Text.Json.Nodes;
using System.Text.RegularExpressions;
using ArcaneLibs;
using Microsoft.AspNetCore.Mvc;
using Spacebar.Interop.Authentication.AspNetCore;
using Spacebar.Models.Db.Contexts;
using Spacebar.UApi.Services;
namespace Spacebar.UApi.Controllers.Messages;
[ApiController]
[Route("/api/v{_}/channels/{channelId}/messages")]
public partial class MessagesController(ILogger<MessagesController> logger, SpacebarDbContext db, SpacebarAspNetAuthenticationService authService, UApiConfiguration cfg) : ControllerBase {
[Consumes("multipart/form-data")]
[HttpPost]
public async Task CreateMessageWithAttachments(string channelId, MultipartFormDataContent formData) {
// Generic proxy doesnt work with multipart/form-data for some reason, so handle them specially
JsonObject jsonPayload = null!;
foreach (var content in formData)
{
if (content.Headers.ContentDisposition?.Name == "payload_json") {
jsonPayload = await content.ReadFromJsonAsync<JsonObject>();
break;
}
if (FileNameRegex().IsMatch(content.Headers.ContentDisposition?.Name ?? "")) {
break;
}
throw new InvalidOperationException("Invalid multipart/form-data payload: missing payload_json or file attachments");
}
var client = new StreamingHttpClient();
var requestMessage = new HttpRequestMessage(
new HttpMethod(Request.Method),
cfg.FallbackApiEndpoint + Request.Path + Request.QueryString
) {
Content = new StreamContent(Request.Body)
};
Console.WriteLine(requestMessage.RequestUri);
var responseMessage = await client.SendUnhandledAsync(requestMessage, CancellationToken.None);
Response.StatusCode = (int)responseMessage.StatusCode;
foreach (var header in responseMessage.Headers) Response.Headers[header.Key] = header.Value.ToArray();
foreach (var header in responseMessage.Content.Headers) Response.Headers[header.Key] = header.Value.ToArray();
await responseMessage.Content.CopyToAsync(Response.Body);
}
[GeneratedRegex(@"files\[\d+\]")]
private static partial Regex FileNameRegex();
private struct CloudUploadTask {
public int index;
public Task<string> task;
}
}