diff --git a/Behaver.cs b/Behaver.cs index 3bfcec6..cd16a9f 100644 --- a/Behaver.cs +++ b/Behaver.cs @@ -224,4 +224,15 @@ public class Behaver return await iprotocol.SendFile(channel, path, accompanyingText); } + public async Task 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); + } } diff --git a/ProtocolInterfaces/DiscordInterface/DiscordInterface.cs b/ProtocolInterfaces/DiscordInterface/DiscordInterface.cs index 0135811..c005f3d 100644 --- a/ProtocolInterfaces/DiscordInterface/DiscordInterface.cs +++ b/ProtocolInterfaces/DiscordInterface/DiscordInterface.cs @@ -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 SendFile(Channel channel, string path, string accompanyingText) + public override async Task 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 diff --git a/ProtocolInterfaces/ProtocolInterface.cs b/ProtocolInterfaces/ProtocolInterface.cs index d31c063..5bf2b45 100644 --- a/ProtocolInterfaces/ProtocolInterface.cs +++ b/ProtocolInterfaces/ProtocolInterface.cs @@ -7,7 +7,16 @@ public abstract class ProtocolInterface public static string Protocol { get; } public abstract Channel SelfChannel { get; } public abstract Task SendMessage(Channel channel, string text); - public abstract Task SendFile(Channel channel, string path, string accompanyingText); + public virtual async Task 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 SendFile(Channel channel, string base64dData, string filename, string accompanyingText); public abstract Task React(Message message, string reaction); public abstract Task Reply(Message message, string text); } diff --git a/ProtocolInterfaces/TwitchInterface/TwitchInterface.cs b/ProtocolInterfaces/TwitchInterface/TwitchInterface.cs index 05022b8..7dc8c5a 100644 --- a/ProtocolInterfaces/TwitchInterface/TwitchInterface.cs +++ b/ProtocolInterfaces/TwitchInterface/TwitchInterface.cs @@ -295,7 +295,7 @@ public class TwitchInterface : ProtocolInterface Task.Run(() => { client.SendMessage(channel.ExternalId, text); }); return 200; } - public override async Task SendFile(Channel channel, string path, string accompanyingText) + public override async Task SendFile(Channel channel, string base64dData, string filename, string accompanyingText) { return 405; } diff --git a/WebInterface/Controllers/api/InternalAPIProtocolController.cs b/WebInterface/Controllers/api/InternalAPIProtocolController.cs index 939cc52..5fba873 100644 --- a/WebInterface/Controllers/api/InternalAPIProtocolController.cs +++ b/WebInterface/Controllers/api/InternalAPIProtocolController.cs @@ -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