Added compressed response extensions and added more to the example program. Bumped package version to 1.3.3
This commit is contained in:
@@ -6,6 +6,8 @@ using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.IO;
|
||||
using Newtonsoft.Json;
|
||||
using System.IO.Compression;
|
||||
using System.IO.Pipes;
|
||||
|
||||
namespace MontoyaTech.Rest.Net
|
||||
{
|
||||
@@ -30,6 +32,26 @@ namespace MontoyaTech.Rest.Net
|
||||
return response;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the response content type to text and writes the given text compressed to it.
|
||||
/// </summary>
|
||||
/// <param name="response"></param>
|
||||
/// <param name="text"></param>
|
||||
/// <returns>This response.</returns>
|
||||
public static HttpListenerResponse WithCompressedText(this HttpListenerResponse response, string text)
|
||||
{
|
||||
response.ContentType = "text/plain; charset=utf-8";
|
||||
response.Headers.Add("Content-Encoding", "gzip");
|
||||
|
||||
var bytes = Encoding.UTF8.GetBytes(text);
|
||||
|
||||
using (var responseStream = response.OutputStream)
|
||||
using (var compressedStream = new GZipStream(responseStream, CompressionMode.Compress, true))
|
||||
compressedStream.Write(bytes, 0, bytes.Length);
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the response content type to json and serializes the object as json and writes it.
|
||||
/// </summary>
|
||||
@@ -46,6 +68,26 @@ namespace MontoyaTech.Rest.Net
|
||||
return response;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the response content type to json and writes the given json compressed to it.
|
||||
/// </summary>
|
||||
/// <param name="response"></param>
|
||||
/// <param name="obj"></param>
|
||||
/// <returns>This response.</returns>
|
||||
public static HttpListenerResponse WithCompressedJson(this HttpListenerResponse response, object obj)
|
||||
{
|
||||
response.ContentType = "application/json; charset=utf-8";
|
||||
response.Headers.Add("Content-Encoding", "gzip");
|
||||
|
||||
var bytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(obj));
|
||||
|
||||
using (var responseStream = response.OutputStream)
|
||||
using (var compressedStream = new GZipStream(responseStream, CompressionMode.Compress, true))
|
||||
compressedStream.Write(bytes, 0, bytes.Length);
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the response content type to a file and writes the given file content to the response.
|
||||
/// </summary>
|
||||
@@ -72,6 +114,34 @@ namespace MontoyaTech.Rest.Net
|
||||
return response;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the response content type to a file and compresses the given file content to the response.
|
||||
/// </summary>
|
||||
/// <param name="response"></param>
|
||||
/// <param name="filePath">The path of the file to send.</param>
|
||||
/// <param name="mimeType">The mime type of the file to send, if null, it will be auto detected if possible.</param>
|
||||
/// <returns>This response.</returns>
|
||||
public static HttpListenerResponse WithCompressedFile(this HttpListenerResponse response, string filePath, 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)
|
||||
using (var compressedStream = new GZipStream(responseStream, CompressionMode.Compress, true))
|
||||
using (var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
|
||||
fileStream.CopyTo(compressedStream);
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the response content type to html and writes the given html to it.
|
||||
/// </summary>
|
||||
|
@@ -17,7 +17,7 @@
|
||||
<AssemblyName>MontoyaTech.Rest.Net</AssemblyName>
|
||||
<RootNamespace>MontoyaTech.Rest.Net</RootNamespace>
|
||||
<GenerateDocumentationFile>True</GenerateDocumentationFile>
|
||||
<Version>1.3.2</Version>
|
||||
<Version>1.3.3</Version>
|
||||
<PackageReleaseNotes></PackageReleaseNotes>
|
||||
<PackageIcon>Logo_Symbol_Black_Outline.png</PackageIcon>
|
||||
</PropertyGroup>
|
||||
|
Reference in New Issue
Block a user