Skip to content

Commit

Permalink
Fix compression
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanBout committed Nov 26, 2024
1 parent de9ee0e commit a08b0a1
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
4 changes: 2 additions & 2 deletions SimpleCDN/CDNLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,13 @@ public class CDNLoader(IWebHostEnvironment environment, IOptionsMonitor<CDNConfi
};

// attempt to compress the file if it's not already compressed
/*if (!file.IsCompressed)
if (!file.IsCompressed)
{
var contentSpan = content.content.AsSpan();
bool compressed = GZipHelpers.TryCompress(ref contentSpan);
file.Content = contentSpan.ToArray();
file.IsCompressed = compressed;
}*/
}

_cache[absolutePath] = file;

Expand Down
8 changes: 5 additions & 3 deletions SimpleCDN/Helpers/GZipHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ public class GZipHelpers
public static bool TryCompress(ref Span<byte> data)
{
using var memoryStream = new MemoryStream();
using var gzipStream = new GZipStream(memoryStream, CompressionMode.Compress);
gzipStream.Write(data);
gzipStream.Flush();
using (var gzipStream = new GZipStream(memoryStream, CompressionMode.Compress, true))
{
gzipStream.Write(data);
}

if (memoryStream.Length >= data.Length)
return false;

memoryStream.Position = 0;
var read = memoryStream.Read(data);

data = data[..read];
Expand Down
1 change: 1 addition & 0 deletions SimpleCDN/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using SimpleCDN;
using SimpleCDN.Configuration;
using System.IO.Compression;

var builder = WebApplication.CreateSlimBuilder(args);

Expand Down

0 comments on commit a08b0a1

Please sign in to comment.