From 3849880b331a214ca173bd21f18ebde80b9f8981 Mon Sep 17 00:00:00 2001 From: "Adam R. Grey" Date: Wed, 17 Nov 2021 22:30:51 -0500 Subject: [PATCH] deheic with external imagemagick "oh hey magick.net lets you not have to install imagemagick, but you still have to manage this library as though it was a dependency, only now it mysteriously doesn't work sometimes" --- Program.cs | 31 +++++++++------ externalProcess.cs | 79 +++++++++++++++++++++++++++++++++++++ silverworker-discord.csproj | 1 - 3 files changed, 99 insertions(+), 12 deletions(-) create mode 100644 externalProcess.cs diff --git a/Program.cs b/Program.cs index c0cb63f..86bd5e3 100644 --- a/Program.cs +++ b/Program.cs @@ -10,8 +10,6 @@ using Discord.WebSocket; using Microsoft.Extensions.Configuration; using Newtonsoft.Json; using System.Text; -using ImageMagick; -using ImageMagick.Formats; namespace silverworker_discord { @@ -140,20 +138,31 @@ namespace silverworker_discord { var request = WebRequest.Create(att.Url); HttpWebResponse response = request.GetResponse() as HttpWebResponse; - using (var convertedStream = new MemoryStream()) - using (var image = new MagickImage(response.GetResponseStream())) + if(!Directory.Exists("tmp")) { - image.Write(convertedStream, MagickFormat.Jpeg); - convertedStream.Position = 0; - Console.WriteLine($"converted to stream {convertedStream.Length} bytes long"); - await message.Channel.SendFileAsync(convertedStream, "heiccup.jpg", "converted from jpeg-but-apple to jpeg"); + Directory.CreateDirectory("tmp"); + } + using (Stream output = File.OpenWrite("tmp/" + att.Filename)) + using (Stream input = response.GetResponseStream()) + { + input.CopyTo(output); + } + if(ExternalProcess.GoPlz("convert", $"tmp/{att.Filename} tmp/{att.Filename}.jpg")) + { + await message.Channel.SendFileAsync($"tmp/{att.Filename}.jpg", "converted from jpeg-but-apple to jpeg"); + File.Delete($"tmp/{att.Filename}"); + File.Delete($"tmp/{att.Filename}.jpg"); + } + else + { + await botChatterChannel.SendMessageAsync("convert failed :("); + Console.Error.WriteLine("convert failed :("); } } - catch (Exception e) { - await message.Channel.SendMessageAsync("¯\\_(ツ)_/¯"); - Console.Error.Write(e); + await botChatterChannel.SendMessageAsync(JsonConvert.SerializeObject(e, Formatting.Indented)); + Console.Error.WriteLine(JsonConvert.SerializeObject(e, Formatting.Indented)); } } private async void detiktokify(Uri link, SocketUserMessage message) diff --git a/externalProcess.cs b/externalProcess.cs new file mode 100644 index 0000000..e366534 --- /dev/null +++ b/externalProcess.cs @@ -0,0 +1,79 @@ +using System; +using System.Linq; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Text; +using System.Text.RegularExpressions; + +namespace silverworker_discord +{ + public class ExternalProcess + { + public static bool GoPlz(string commandPath, string commandArguments) + { + var process = readableProcess(commandPath, commandArguments); + var outputData = new StringBuilder(); + process.OutputDataReceived += new DataReceivedEventHandler((s, e) => + { + outputData.Append(e.Data); + }); + var errorData = new StringBuilder(); + process.ErrorDataReceived += new DataReceivedEventHandler((s, e) => + { + errorData.Append(e.Data); + }); + + try + { + process.Start(); + process.BeginErrorReadLine(); + process.BeginOutputReadLine(); + process.WaitForExit(); + } + catch(Exception e) + { + var dumpDir = $"fail{DateTime.Now.ToFileTimeUtc()}"; + var outputFilename = $"{dumpDir}/output0.log"; + var errorFilename = $"{dumpDir}/error0.err"; + if(!Directory.Exists(dumpDir)) + { + Directory.CreateDirectory(dumpDir); + } + else + { + var i = 0; + foreach(var file in Directory.GetFiles(dumpDir)) + { + var thisNummatch = Regex.Matches(Path.GetFileNameWithoutExtension(file), "[^\\d](\\d+)\\.err$").LastOrDefault().Value; + if(!string.IsNullOrWhiteSpace(thisNummatch)) + { + var thisNumval = 0; + if(int.TryParse(thisNummatch, out thisNumval) && thisNumval > i) + { + i = thisNumval; + } + } + } + outputFilename = $"{dumpDir}/output{i}.log"; + errorFilename = $"{dumpDir}/error{i}.err"; + } + File.WriteAllText(outputFilename, outputData.ToString()); + File.WriteAllText(errorFilename, errorFilename.ToString()); + return false; + } + return true; + } + private static Process readableProcess(string commandPath, string commandArguments) + { + var pi = new ProcessStartInfo(commandPath, commandArguments); + pi.UseShellExecute = false; + pi.CreateNoWindow = true; + pi.RedirectStandardError = true; + pi.RedirectStandardOutput = true; + var process = new Process(); + process.StartInfo = pi; + return process; + } + } +} \ No newline at end of file diff --git a/silverworker-discord.csproj b/silverworker-discord.csproj index 3b4c268..9b37191 100644 --- a/silverworker-discord.csproj +++ b/silverworker-discord.csproj @@ -8,7 +8,6 @@ -