Add zstd compression to all API Traffic
This commit is contained in:
parent
f7daacf0d4
commit
b7bc04a045
@ -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/)
|
- [Html Agility Pack (HAP)](https://html-agility-pack.net/)
|
||||||
- [Soenneker.Utils.String.NeedlemanWunsch](https://github.com/soenneker/soenneker.utils.string.needlemanwunsch)
|
- [Soenneker.Utils.String.NeedlemanWunsch](https://github.com/soenneker/soenneker.utils.string.needlemanwunsch)
|
||||||
- [Sixlabors.ImageSharp](https://docs-v2.sixlabors.com/articles/imagesharp/index.html#license)
|
- [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 🦈
|
- 💙 Blåhaj 🦈
|
||||||
|
|
||||||
<p align="right">(<a href="#readme-top">back to top</a>)</p>
|
<p align="right">(<a href="#readme-top">back to top</a>)</p>
|
||||||
|
@ -5,6 +5,7 @@ using System.Text.RegularExpressions;
|
|||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using SixLabors.ImageSharp;
|
using SixLabors.ImageSharp;
|
||||||
using SixLabors.ImageSharp.Formats.Png;
|
using SixLabors.ImageSharp.Formats.Png;
|
||||||
|
using ZstdSharp;
|
||||||
|
|
||||||
namespace Tranga.Server;
|
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-Headers", "Content-Type, Accept, X-Requested-With");
|
||||||
response.AddHeader("Access-Control-Allow-Methods", "GET, POST, DELETE");
|
response.AddHeader("Access-Control-Allow-Methods", "GET, POST, DELETE");
|
||||||
response.AddHeader("Access-Control-Max-Age", "1728000");
|
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
|
try
|
||||||
{
|
{
|
||||||
if (content is Stream stream)
|
if (content is Stream stream)
|
||||||
{
|
{
|
||||||
response.ContentType = "text/plain";
|
response.ContentType = "text/plain";
|
||||||
stream.CopyTo(response.OutputStream);
|
response.AddHeader("Cache-Control", "no-store");
|
||||||
|
stream.CopyTo(compressor);
|
||||||
stream.Close();
|
stream.Close();
|
||||||
}else if (content is Image image)
|
}else if (content is Image image)
|
||||||
{
|
{
|
||||||
response.ContentType = image.Metadata.DecodedImageFormat?.DefaultMimeType ?? PngFormat.Instance.DefaultMimeType;
|
response.ContentType = image.Metadata.DecodedImageFormat?.DefaultMimeType ?? PngFormat.Instance.DefaultMimeType;
|
||||||
response.AddHeader("Cache-Control", "max-age=600");
|
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();
|
image.Dispose();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
response.ContentType = "application/json";
|
response.ContentType = "application/json";
|
||||||
response.AddHeader("Cache-Control", "no-store");
|
response.AddHeader("Cache-Control", "no-store");
|
||||||
response.OutputStream.Write(content is not null
|
if(content is not null)
|
||||||
? Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(content))
|
new MemoryStream(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(content))).CopyTo(compressor);
|
||||||
: Array.Empty<byte>());
|
else
|
||||||
|
compressor.Write(Array.Empty<byte>());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
compressor.Flush();
|
||||||
response.OutputStream.Close();
|
response.OutputStream.Close();
|
||||||
}
|
}
|
||||||
catch (HttpListenerException e)
|
catch (HttpListenerException e)
|
||||||
|
@ -16,6 +16,7 @@
|
|||||||
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.5" />
|
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.5" />
|
||||||
<PackageReference Include="Soenneker.Utils.String.NeedlemanWunsch" Version="2.1.301" />
|
<PackageReference Include="Soenneker.Utils.String.NeedlemanWunsch" Version="2.1.301" />
|
||||||
<PackageReference Include="System.Drawing.Common" Version="9.0.0-preview.7.24405.4" />
|
<PackageReference Include="System.Drawing.Common" Version="9.0.0-preview.7.24405.4" />
|
||||||
|
<PackageReference Include="ZstdSharp.Port" Version="0.8.1" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
Loading…
Reference in New Issue
Block a user