diff --git a/README.md b/README.md index f10ff84..fe0010b 100644 --- a/README.md +++ b/README.md @@ -91,6 +91,7 @@ That is why I wanted to create my own project, in a language I understand, and t - [Html Agility Pack (HAP)](https://html-agility-pack.net/) - [Soenneker.Utils.String.NeedlemanWunsch](https://github.com/soenneker/soenneker.utils.string.needlemanwunsch) - [Sixlabors.ImageSharp](https://docs-v2.sixlabors.com/articles/imagesharp/index.html#license) +- [zstd-wrapper](https://github.com/oleg-st/ZstdSharp) [zstd](https://github.com/facebook/zstd) - 💙 Blåhaj 🦈

(back to top)

diff --git a/Tranga/Server/Server.cs b/Tranga/Server/Server.cs index 68e326c..37fc5a4 100644 --- a/Tranga/Server/Server.cs +++ b/Tranga/Server/Server.cs @@ -5,6 +5,7 @@ using System.Text.RegularExpressions; using Newtonsoft.Json; using SixLabors.ImageSharp; using SixLabors.ImageSharp.Formats.Png; +using ZstdSharp; namespace Tranga.Server; @@ -205,32 +206,36 @@ public partial class Server : GlobalBase, IDisposable response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With"); response.AddHeader("Access-Control-Allow-Methods", "GET, POST, DELETE"); response.AddHeader("Access-Control-Max-Age", "1728000"); - response.AppendHeader("Access-Control-Allow-Origin", "*"); - - + response.AddHeader("Access-Control-Allow-Origin", "*"); + response.AddHeader("Content-Encoding", "zstd"); + + using CompressionStream compressor = new (response.OutputStream, 5); try { if (content is Stream stream) { response.ContentType = "text/plain"; - stream.CopyTo(response.OutputStream); + response.AddHeader("Cache-Control", "no-store"); + stream.CopyTo(compressor); stream.Close(); }else if (content is Image image) { response.ContentType = image.Metadata.DecodedImageFormat?.DefaultMimeType ?? PngFormat.Instance.DefaultMimeType; response.AddHeader("Cache-Control", "max-age=600"); - image.Save(response.OutputStream, image.Metadata.DecodedImageFormat ?? PngFormat.Instance); + image.Save(compressor, image.Metadata.DecodedImageFormat ?? PngFormat.Instance); image.Dispose(); } else { response.ContentType = "application/json"; response.AddHeader("Cache-Control", "no-store"); - response.OutputStream.Write(content is not null - ? Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(content)) - : Array.Empty()); + if(content is not null) + new MemoryStream(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(content))).CopyTo(compressor); + else + compressor.Write(Array.Empty()); } + compressor.Flush(); response.OutputStream.Close(); } catch (HttpListenerException e) diff --git a/Tranga/Tranga.csproj b/Tranga/Tranga.csproj index 70fa5ef..10fb726 100644 --- a/Tranga/Tranga.csproj +++ b/Tranga/Tranga.csproj @@ -16,6 +16,7 @@ +