2024-06-03 17:21:11 +00:00
|
|
|
|
using DeviceRepair.Utils;
|
|
|
|
|
using System.IO;
|
2024-06-02 16:38:52 +00:00
|
|
|
|
using System.IO.Compression;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Web.Http;
|
|
|
|
|
|
|
|
|
|
namespace DeviceRepair.Api.Models
|
|
|
|
|
{
|
2024-06-03 17:21:11 +00:00
|
|
|
|
public class GzipCompressedResult : IHttpActionResult
|
2024-06-02 16:38:52 +00:00
|
|
|
|
{
|
2024-06-03 17:21:11 +00:00
|
|
|
|
private readonly string _content;
|
2024-06-02 16:38:52 +00:00
|
|
|
|
private readonly HttpRequestMessage _request;
|
|
|
|
|
|
2024-06-03 17:21:11 +00:00
|
|
|
|
public GzipCompressedResult(string content, HttpRequestMessage request)
|
2024-06-02 16:38:52 +00:00
|
|
|
|
{
|
2024-06-03 17:21:11 +00:00
|
|
|
|
_content = content;
|
2024-06-02 16:38:52 +00:00
|
|
|
|
_request = request;
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-03 17:21:11 +00:00
|
|
|
|
public async Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
|
2024-06-02 16:38:52 +00:00
|
|
|
|
{
|
2024-06-03 17:21:11 +00:00
|
|
|
|
var response = new HttpResponseMessage(HttpStatusCode.OK);
|
|
|
|
|
var contentBytes = Encoding.UTF8.GetBytes(_content).Compress();
|
2024-06-02 16:38:52 +00:00
|
|
|
|
|
2024-06-03 17:21:11 +00:00
|
|
|
|
using (var outputStream = new MemoryStream())
|
2024-06-02 16:38:52 +00:00
|
|
|
|
{
|
2024-06-03 17:21:11 +00:00
|
|
|
|
using (var gzipStream = new GZipStream(outputStream, CompressionMode.Compress))
|
|
|
|
|
{
|
|
|
|
|
await gzipStream.WriteAsync(contentBytes, 0, contentBytes.Length, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
response.Content = new ByteArrayContent(outputStream.ToArray());
|
|
|
|
|
response.Content.Headers.ContentEncoding.Add("gzip");
|
|
|
|
|
response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
|
2024-06-02 16:38:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-03 17:21:11 +00:00
|
|
|
|
response.RequestMessage = _request;
|
|
|
|
|
return response;
|
2024-06-02 16:38:52 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|