using Microsoft.AspNetCore.Mvc; using XZ.NET; using static CompServ.CompServConst; namespace CompServ.Worker { [ApiController] public class WorkerController : ControllerBase { [HttpGet] [Route(ApiPathWorkerRootForCheckAlive)] public async Task CheckAlive() => await Task.FromResult(Content(AliveMessageWorker)); [HttpPost] [Route(ApiPathWorkerCompress)] public async Task Compress() { Interlocked.Increment(ref StatusHolder.RequestIn); try { var req = await HttpContext.Request.ExtractCompressRequestAsync(); Interlocked.Add(ref StatusHolder.BytesIn, req.DataToCompress.Count); var msOutput = new MemoryStream(); var xz = new XZOutputStream(msOutput, req.Threads, (uint)req.Level, true); await xz.WriteAsync(req.DataToCompress); xz.Close(); Interlocked.Add(ref StatusHolder.BytesOut, msOutput.Length); msOutput.Position = 0; var fileStreamResult = File(msOutput, "compressed/xz"); Interlocked.Increment(ref StatusHolder.RequestSuccess); return fileStreamResult; } finally { Interlocked.Increment(ref StatusHolder.RequestOut); } } [HttpPost] [Route(ApiPathWorkerDecompress)] public async Task Decompress() { Interlocked.Increment(ref StatusHolder.RequestIn); try { var msInput = HttpContext.Request.ContentLength.HasValue ? new MemoryStream((int)HttpContext.Request.ContentLength.Value) : new MemoryStream(); await HttpContext.Request.Body.CopyToAsync(msInput); msInput.Position = 0; var xz = new XZInputStream(msInput); var msOutput = new MemoryStream(); await xz.CopyToAsync(msOutput); xz.Close(); msOutput.Position = 0; var fileStreamResult = File(msOutput, "binary/data"); Interlocked.Increment(ref StatusHolder.RequestSuccess); return fileStreamResult; } finally { Interlocked.Increment(ref StatusHolder.RequestOut); } } } }