Files
archived-discord-bot/CompatBot/LogParsing/SourceHandlers/ZipHandler.cs
13xforever 7fd7d09973 RPCS3 Compatibility Bot reimplemented in C# for .NET Core
RPCS3 Compatibility Bot reimplemented in C# for .NET Core

Current status of this PR:
* tested and targeted for .NET Core 2.1
* all functionality is either on par or improved compared to the python version
* compatibility with current bot.db should be preserved in all upgrade scenarios
* some bot management commands were changed (now under !sudo bot)
* standard help generator for the new discord client is ... different;
  compatibility with old format could be restored through custom formatter if needed
* everything has been split in more loosely tied components for easier extensibility and maintenance
* log parsing has been rewritten and should work ~2x as fast
2018-07-20 09:22:28 +02:00

84 lines
3.6 KiB
C#

using System;
using System.Buffers;
using System.IO;
using System.IO.Compression;
using System.IO.Pipelines;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using CompatBot.Utils;
using DSharpPlus.Entities;
namespace CompatBot.LogParsing.SourceHandlers
{
public class ZipHandler: ISourceHandler
{
private static readonly ArrayPool<byte> bufferPool = ArrayPool<byte>.Create(1024, 16);
public async Task<bool> CanHandleAsync(DiscordAttachment attachment)
{
if (!attachment.FileName.EndsWith(".zip", StringComparison.InvariantCultureIgnoreCase))
return false;
try
{
using (var client = HttpClientFactory.Create())
using (var stream = await client.GetStreamAsync(attachment.Url).ConfigureAwait(false))
{
var buf = bufferPool.Rent(1024);
var read = await stream.ReadBytesAsync(buf).ConfigureAwait(false);
var firstEntry = Encoding.ASCII.GetString(new ReadOnlySpan<byte>(buf, 0, read));
var result = firstEntry.Contains(".log", StringComparison.InvariantCultureIgnoreCase);
bufferPool.Return(buf);
return result;
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return false;
}
}
public async Task FillPipeAsync(DiscordAttachment attachment, PipeWriter writer)
{
try
{
using (var fileStream = new FileStream(Path.GetTempFileName(), FileMode.Create, FileAccess.ReadWrite, FileShare.Read, 16384, FileOptions.Asynchronous | FileOptions.RandomAccess | FileOptions.DeleteOnClose))
{
using (var client = HttpClientFactory.Create())
using (var downloadStream = await client.GetStreamAsync(attachment.Url).ConfigureAwait(false))
await downloadStream.CopyToAsync(fileStream, 16384, Config.Cts.Token).ConfigureAwait(false);
fileStream.Seek(0, SeekOrigin.Begin);
using (var zipArchive = new ZipArchive(fileStream, ZipArchiveMode.Read))
{
var logEntry = zipArchive.Entries.FirstOrDefault(e => e.Name.EndsWith(".log", StringComparison.InvariantCultureIgnoreCase));
if (logEntry == null)
throw new InvalidOperationException("No zip entries that match the log criteria");
using (var zipStream = logEntry.Open())
{
int read;
FlushResult flushed;
do
{
var memory = writer.GetMemory(Config.MinimumBufferSize);
read = await zipStream.ReadAsync(memory, Config.Cts.Token);
writer.Advance(read); //todo: test that .Advance(0) works as expected
flushed = await writer.FlushAsync(Config.Cts.Token).ConfigureAwait(false);
} while (read > 0 && !(flushed.IsCompleted || flushed.IsCanceled || Config.Cts.IsCancellationRequested));
}
}
}
}
catch (Exception e)
{
Console.WriteLine(e);
writer.Complete(e);
return;
}
writer.Complete();
}
}
}