Files
archived-discord-bot/CompatBot/EventHandlers/LogParsing/ArchiveHandlers/GzipHandler.cs
Ilya 72dbc4074a Some performance and code optimizations (#955)
* upgrade deps, remove wrong ppu hashes

* upgrade compiler packages

will require container pull after build

* replace Regex with compiler-generated versions

* use new collection initialization syntax

* configure global defaults for regex

* bump min amd driver version

fixes #950

* add macos version check

fixes #948

* fix #954 (!sudo log date)
2024-05-18 14:26:34 +01:00

61 lines
2.2 KiB
C#

using System;
using System.IO;
using System.IO.Compression;
using System.IO.Pipelines;
using System.Threading;
using System.Threading.Tasks;
using CompatBot.Utils;
namespace CompatBot.EventHandlers.LogParsing.ArchiveHandlers;
internal sealed class GzipHandler: IArchiveHandler
{
private static readonly byte[] Header = [0x1F, 0x8B, 0x08];
public long LogSize { get; private set; }
public long SourcePosition { get; private set; }
public (bool result, string? reason) CanHandle(string fileName, int fileSize, ReadOnlySpan<byte> header)
{
if (header.Length >= Header.Length)
{
if (header[..Header.Length].SequenceEqual(Header))
return (true, null);
}
else if (fileName.EndsWith(".log.gz", StringComparison.InvariantCultureIgnoreCase)
&& !fileName.Contains("tty.log", StringComparison.InvariantCultureIgnoreCase))
return (true, null);
return (false, null);
}
public async Task FillPipeAsync(Stream sourceStream, PipeWriter writer, CancellationToken cancellationToken)
{
await using var statsStream = new BufferCopyStream(sourceStream);
await using var gzipStream = new GZipStream(statsStream, CompressionMode.Decompress);
try
{
int read;
FlushResult flushed;
do
{
var memory = writer.GetMemory(Config.MinimumBufferSize);
read = await gzipStream.ReadAsync(memory, cancellationToken);
writer.Advance(read);
SourcePosition = statsStream.Position;
flushed = await writer.FlushAsync(cancellationToken).ConfigureAwait(false);
} while (read > 0 && !(flushed.IsCompleted || flushed.IsCanceled || cancellationToken.IsCancellationRequested));
var buf = statsStream.GetBufferedBytes();
if (buf.Length > 3)
LogSize = BitConverter.ToInt32(buf.AsSpan(buf.Length - 4, 4));
}
catch (Exception e)
{
Config.Log.Error(e, "Error filling the log pipe");
await writer.CompleteAsync(e);
return;
}
await writer.CompleteAsync();
}
}