Last active
December 21, 2017 12:53
-
-
Save grumpydev/4605541 to your computer and use it in GitHub Desktop.
Sample processor for binary files, either from a byte array, or from a stream
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using Nancy; | |
using Nancy.Responses; | |
using Nancy.Responses.Negotiation; | |
namespace ByteArrayDemo | |
{ | |
public class BinaryProcessor : IResponseProcessor | |
{ | |
public static IList<Tuple<string, MediaRange>> Mappings { get; set; } | |
static BinaryProcessor() | |
{ | |
Mappings = new List<Tuple<string, MediaRange>>(); | |
} | |
public IEnumerable<Tuple<string, MediaRange>> ExtensionMappings | |
{ | |
get { return Mappings.ToArray(); } | |
} | |
public ProcessorMatch CanProcess(MediaRange requestedMediaRange, dynamic model, NancyContext context) | |
{ | |
var acceptableType = (model != null && (model.GetType() == typeof(byte[]) || model is Stream)); | |
var modelResult = acceptableType ? MatchResult.ExactMatch : MatchResult.NoMatch; | |
var contentTypeResult = Mappings.Any(map => map.Item2.Matches(requestedMediaRange)) ? MatchResult.ExactMatch : MatchResult.NoMatch; | |
return new ProcessorMatch { ModelResult = modelResult, RequestedContentTypeResult = contentTypeResult }; | |
} | |
public Response Process(MediaRange requestedMediaRange, dynamic model, NancyContext context) | |
{ | |
if (model is Stream) | |
{ | |
return new StreamResponse(() => model, requestedMediaRange); | |
} | |
return new ByteArrayResponse((byte[])model, requestedMediaRange); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Nancy; | |
using Nancy.Responses.Negotiation; | |
namespace ByteArrayDemo | |
{ | |
public class Bootstrapper : DefaultNancyBootstrapper | |
{ | |
protected override void ApplicationStartup(Nancy.TinyIoc.TinyIoCContainer container, Nancy.Bootstrapper.IPipelines pipelines) | |
{ | |
base.ApplicationStartup(container, pipelines); | |
ByteArrayProcessor.Mappings.Add(new Tuple<string, MediaRange>("jpg", "image/jpeg")); | |
ByteArrayProcessor.Mappings.Add(new Tuple<string, MediaRange>("jpeg", "image/jpeg")); | |
ByteArrayProcessor.Mappings.Add(new Tuple<string, MediaRange>("png", "image/png")); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.IO; | |
using Nancy; | |
namespace ByteArrayDemo | |
{ | |
public class ByteArrayResponse : Response | |
{ | |
/// <summary> | |
/// Byte array response | |
/// </summary> | |
/// <param name="body">Byte array to be the body of the response</param> | |
/// <param name="contentType">Content type to use</param> | |
public ByteArrayResponse(byte[] body, string contentType = null) | |
{ | |
this.ContentType = contentType ?? "application/octet-stream"; | |
this.Contents = stream => | |
{ | |
using (var writer = new BinaryWriter(stream)) | |
{ | |
writer.Write(body); | |
} | |
}; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.IO; | |
using Nancy; | |
namespace ByteArrayDemo | |
{ | |
public class MainModule : NancyModule | |
{ | |
public MainModule() | |
{ | |
Get["/"] = _ => Negotiate.WithView("index") | |
.WithMediaRangeModel("image/jpeg", () => File.ReadAllBytes(@"Koala.jpg")) | |
.WithMediaRangeModel("image/png", () => File.OpenRead(@"rowlf.png")); | |
} | |
} | |
} |
I think instead of
ByteArrayProcessor.Mappings.Add(new Tuple<string, MediaRange>("jpg", "image/jpeg"));
should be
BinaryProcessor.Mappings.Add(new Tuple<string, MediaRange>("jpg", "image/jpeg"));
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
can you share working project?