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"
This commit is contained in:
parent
b1578dde69
commit
3849880b33
31
Program.cs
31
Program.cs
@ -10,8 +10,6 @@ using Discord.WebSocket;
|
|||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using ImageMagick;
|
|
||||||
using ImageMagick.Formats;
|
|
||||||
|
|
||||||
namespace silverworker_discord
|
namespace silverworker_discord
|
||||||
{
|
{
|
||||||
@ -140,20 +138,31 @@ namespace silverworker_discord
|
|||||||
{
|
{
|
||||||
var request = WebRequest.Create(att.Url);
|
var request = WebRequest.Create(att.Url);
|
||||||
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
|
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
|
||||||
using (var convertedStream = new MemoryStream())
|
if(!Directory.Exists("tmp"))
|
||||||
using (var image = new MagickImage(response.GetResponseStream()))
|
|
||||||
{
|
{
|
||||||
image.Write(convertedStream, MagickFormat.Jpeg);
|
Directory.CreateDirectory("tmp");
|
||||||
convertedStream.Position = 0;
|
}
|
||||||
Console.WriteLine($"converted to stream {convertedStream.Length} bytes long");
|
using (Stream output = File.OpenWrite("tmp/" + att.Filename))
|
||||||
await message.Channel.SendFileAsync(convertedStream, "heiccup.jpg", "converted from jpeg-but-apple to jpeg");
|
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)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
await message.Channel.SendMessageAsync("¯\\_(ツ)_/¯");
|
await botChatterChannel.SendMessageAsync(JsonConvert.SerializeObject(e, Formatting.Indented));
|
||||||
Console.Error.Write(e);
|
Console.Error.WriteLine(JsonConvert.SerializeObject(e, Formatting.Indented));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private async void detiktokify(Uri link, SocketUserMessage message)
|
private async void detiktokify(Uri link, SocketUserMessage message)
|
||||||
|
79
externalProcess.cs
Normal file
79
externalProcess.cs
Normal file
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -8,7 +8,6 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="discord.net" Version="2.4.0" />
|
<PackageReference Include="discord.net" Version="2.4.0" />
|
||||||
<PackageReference Include="Magick.NET-Q16-AnyCPU" Version="8.4.0" />
|
|
||||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="5.0.0" />
|
<PackageReference Include="Microsoft.Extensions.Configuration" Version="5.0.0" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="5.0.0" />
|
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="5.0.0" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="5.0.0" />
|
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="5.0.0" />
|
||||||
|
Loading…
Reference in New Issue
Block a user