Files
archived-discord-bot/CompatApiClient/Compression/CompressedContent.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

54 lines
1.6 KiB
C#

using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
namespace CompatApiClient.Compression
{
public class CompressedContent : HttpContent
{
private readonly HttpContent content;
private readonly ICompressor compressor;
public CompressedContent(HttpContent content, ICompressor compressor)
{
if (content == null)
throw new ArgumentNullException(nameof(content));
if (compressor == null)
throw new ArgumentNullException(nameof(compressor));
this.content = content;
this.compressor = compressor;
AddHeaders();
}
protected override bool TryComputeLength(out long length)
{
length = -1;
return false;
}
protected override async Task SerializeToStreamAsync(Stream stream, TransportContext context)
{
if (stream == null)
throw new ArgumentNullException(nameof(stream));
using (content)
{
var contentStream = await content.ReadAsStreamAsync().ConfigureAwait(false);
var compressedLength = await compressor.CompressAsync(contentStream, stream).ConfigureAwait(false);
Headers.ContentLength = compressedLength;
}
}
private void AddHeaders()
{
foreach (var header in content.Headers)
Headers.TryAddWithoutValidation(header.Key, header.Value);
Headers.ContentEncoding.Add(compressor.EncodingType);
Headers.ContentLength = null;
}
}
}