streaming rar support

This commit is contained in:
13xforever 2019-08-16 00:01:29 +05:00
parent 373a3d1800
commit d04e817ebf

View File

@ -4,8 +4,9 @@ using System.IO.Pipelines;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using CompatApiClient.Utils;
using CompatBot.Utils;
using SharpCompress.Archives.Rar;
using SharpCompress.Readers.Rar;
namespace CompatBot.EventHandlers.LogParsing.ArchiveHandlers
{
@ -21,9 +22,6 @@ namespace CompatBot.EventHandlers.LogParsing.ArchiveHandlers
if (header.Length >= Header.Length && header.Slice(0, Header.Length).SequenceEqual(Header)
|| fileName.EndsWith(".rar", StringComparison.InvariantCultureIgnoreCase))
{
if (fileSize > Config.AttachmentSizeLimit)
return (false, $"Log size is too large: {fileSize.AsStorageUnit()} (max allowed is {Config.AttachmentSizeLimit.AsStorageUnit()})");
var firstEntry = Encoding.ASCII.GetString(header);
if (!firstEntry.Contains(".log", StringComparison.InvariantCultureIgnoreCase))
return (false, "Archive doesn't contain any logs.");
@ -38,12 +36,8 @@ namespace CompatBot.EventHandlers.LogParsing.ArchiveHandlers
{
try
{
using (var fileStream = new FileStream(Path.GetTempFileName(), FileMode.Create, FileAccess.ReadWrite, FileShare.Read, 16384, FileOptions.Asynchronous | FileOptions.RandomAccess | FileOptions.DeleteOnClose))
{
await sourceStream.CopyToAsync(fileStream, 16384, cancellationToken).ConfigureAwait(false);
fileStream.Seek(0, SeekOrigin.Begin);
using (var rarArchive = RarArchive.Open(fileStream))
using (var rarReader = rarArchive.ExtractAllEntries())
using (var statsStream = new BufferCopyStream(sourceStream))
using (var rarReader = RarReader.Open(statsStream))
while (rarReader.MoveToNextEntry())
if (!rarReader.Entry.IsDirectory
&& rarReader.Entry.Key.EndsWith(".log", StringComparison.InvariantCultureIgnoreCase)
@ -59,7 +53,9 @@ namespace CompatBot.EventHandlers.LogParsing.ArchiveHandlers
var memory = writer.GetMemory(Config.MinimumBufferSize);
read = await rarStream.ReadAsync(memory, cancellationToken);
writer.Advance(read);
SourcePosition = statsStream.Position;
flushed = await writer.FlushAsync(cancellationToken).ConfigureAwait(false);
SourcePosition = statsStream.Position;
} while (read > 0 && !(flushed.IsCompleted || flushed.IsCanceled || cancellationToken.IsCancellationRequested));
}
writer.Complete();
@ -67,7 +63,6 @@ namespace CompatBot.EventHandlers.LogParsing.ArchiveHandlers
}
Config.Log.Warn("No rar entries that match the log criteria");
}
}
catch (Exception e)
{
Config.Log.Error(e, "Error filling the log pipe");