From f4fe34e4615df36fef1cde3c7cf9d0ddbbe98b30 Mon Sep 17 00:00:00 2001 From: MattMo Date: Wed, 1 Mar 2023 17:14:52 -0800 Subject: [PATCH] Added WithPreCompressedFile extension. Bumped package version to 1.3.5 --- Rest.Net/HttpListenerResponseExtensions.cs | 28 ++++++++++++++++++++++ Rest.Net/Rest.Net.csproj | 2 +- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/Rest.Net/HttpListenerResponseExtensions.cs b/Rest.Net/HttpListenerResponseExtensions.cs index dc2ff38..8e376ef 100644 --- a/Rest.Net/HttpListenerResponseExtensions.cs +++ b/Rest.Net/HttpListenerResponseExtensions.cs @@ -198,6 +198,34 @@ namespace MontoyaTech.Rest.Net return response; } + /// + /// Sets the response content type to a precompressed file and writes the file contents to the response. + /// + /// + /// + /// + /// + /// + /// + public static HttpListenerResponse WithPreCompressedFile(this HttpListenerResponse response, string filePath, byte[] content, string mimeType = null) + { + if (string.IsNullOrWhiteSpace(filePath)) + throw new ArgumentException("filePath must not be null or empty"); + + if (string.IsNullOrWhiteSpace(mimeType)) + mimeType = Path.GetExtension(filePath).GetMimeType(); + + response.ContentType = mimeType; + response.Headers.Add("Content-Deposition", $@"attachment; filename=""{Path.GetFileName(filePath)}"""); + response.Headers.Add("Content-Encoding", "gzip"); + response.SendChunked = true; + + using (var responseStream = response.OutputStream) + responseStream.Write(content, 0, content.Length); + + return response; + } + /// /// Sets the response content type to html and writes the given html to it. /// diff --git a/Rest.Net/Rest.Net.csproj b/Rest.Net/Rest.Net.csproj index 50659b8..b72ca9f 100644 --- a/Rest.Net/Rest.Net.csproj +++ b/Rest.Net/Rest.Net.csproj @@ -17,7 +17,7 @@ MontoyaTech.Rest.Net MontoyaTech.Rest.Net True - 1.3.4 + 1.3.5 Logo_Symbol_Black_Outline.png