mirror of
https://github.com/spacebarchat/server.git
synced 2026-03-29 11:59:53 +00:00
UApi: partial work on filling in wrappers for non-json endpoints
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user