sendfile works

see #25
This commit is contained in:
adam 2025-06-04 16:28:53 -04:00
parent 9ad7520c61
commit c35a512dbd
5 changed files with 34 additions and 8 deletions

View File

@ -224,4 +224,15 @@ public class Behaver
return await iprotocol.SendFile(channel, path, accompanyingText);
}
public async Task<int> SendFile(Guid channelId, string base64dData, string filename, string accompanyingText)
{
var channel = Rememberer.ChannelDetail(channelId);
if (channel == null)
return 404;
var iprotocol = fetchInterface(channel);
if (iprotocol == null)
return 404;
return await iprotocol.SendFile(channel, base64dData, filename, accompanyingText);
}
}

View File

@ -27,7 +27,7 @@ public class DiscordInterface : ProtocolInterface
private bool eventsSignedUp = false;
private static readonly SemaphoreSlim discordChannelSetup = new(1, 1);
private Channel protocolAsChannel;
public override Channel SelfChannel { get => protocolAsChannel;}
public override Channel SelfChannel { get => protocolAsChannel; }
public async Task Init(string config)
{
@ -434,7 +434,7 @@ public class DiscordInterface : ProtocolInterface
return 503;
}
}
public override async Task<int> SendFile(Channel channel, string path, string accompanyingText)
public override async Task<int> SendFile(Channel channel, string base64dData, string filename, string accompanyingText)
{
var dcCh = await client.GetChannelAsync(ulong.Parse(channel.ExternalId));
if (dcCh == null)
@ -444,7 +444,10 @@ public class DiscordInterface : ProtocolInterface
if (dcCh is IMessageChannel msgChannel)
{
await msgChannel.SendFileAsync(path, TruncateText(accompanyingText, channel.MaxTextChars));
using (var ms = new MemoryStream(Convert.FromBase64String(base64dData)))
{
await msgChannel.SendFileAsync(ms, filename, TruncateText(accompanyingText, channel.MaxTextChars));
}
return 200;
}
else

View File

@ -7,7 +7,16 @@ public abstract class ProtocolInterface
public static string Protocol { get; }
public abstract Channel SelfChannel { get; }
public abstract Task<int> SendMessage(Channel channel, string text);
public abstract Task<int> SendFile(Channel channel, string path, string accompanyingText);
public virtual async Task<int> SendFile(Channel channel, string path, string accompanyingText)
{
if (!File.Exists(path))
{
return 404;
}
var fstring = Convert.ToBase64String(File.ReadAllBytes(path));
return await SendFile(channel, fstring, Path.GetFileName(path), accompanyingText);
}
public abstract Task<int> SendFile(Channel channel, string base64dData, string filename, string accompanyingText);
public abstract Task<int> React(Message message, string reaction);
public abstract Task<int> Reply(Message message, string text);
}

View File

@ -295,7 +295,7 @@ public class TwitchInterface : ProtocolInterface
Task.Run(() => { client.SendMessage(channel.ExternalId, text); });
return 200;
}
public override async Task<int> SendFile(Channel channel, string path, string accompanyingText)
public override async Task<int> SendFile(Channel channel, string base64dData, string filename, string accompanyingText)
{
return 405;
}

View File

@ -45,15 +45,18 @@ public class InternalAPIProtocolController : ControllerBase
public class extraSpecialObjectReadGlorifiedTupleFor_SendFile
{
public Guid channelId; public string path; public string accompanyingText;
public Guid channelId;
public string accompanyingText;
public string base64dData;
public string filename;
}
[HttpPost]
[Route("SendFile")]
[Produces("application/json")]
public IActionResult SendFile([FromBody] extraSpecialObjectReadGlorifiedTupleFor_SendFile param)
{
Console.WriteLine($"SendFile- {param.channelId}, {param.path}, {param.accompanyingText}");
return StatusCode(Behaver.Instance.SendFile(param.channelId, param.path, param.accompanyingText).Result);
Console.WriteLine($"SendFile- {param.channelId}, {param.filename} (base64'd, {param.base64dData?.Length} chars), {param.accompanyingText}");
return StatusCode(Behaver.Instance.SendFile(param.channelId, param.base64dData, param.filename, param.accompanyingText).Result);
}
public class extraSpecialObjectReadGlorifiedTupleFor_ReactToMessage