Last active
December 21, 2017 12:53
Revisions
-
grumpydev revised this gist
Jan 23, 2013 . 3 changed files with 15 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,16 +1,18 @@ 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>>(); } @@ -22,14 +24,21 @@ public IEnumerable<Tuple<string, MediaRange>> ExtensionMappings 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 hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -12,6 +12,7 @@ protected override void ApplicationStartup(Nancy.TinyIoc.TinyIoCContainer contai 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 hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -8,7 +8,8 @@ public class MainModule : NancyModule public MainModule() { Get["/"] = _ => Negotiate.WithView("index") .WithMediaRangeModel("image/jpeg", () => File.ReadAllBytes(@"Koala.jpg")) .WithMediaRangeModel("image/png", () => File.OpenRead(@"rowlf.png")); } } } -
grumpydev created this gist
Jan 23, 2013 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,17 @@ 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")); } } } This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,36 @@ using System; using System.Collections.Generic; using System.Linq; using Nancy; using Nancy.Responses.Negotiation; namespace ByteArrayDemo { public class ByteArrayProcessor : IResponseProcessor { public static IList<Tuple<string, MediaRange>> Mappings { get; set; } static ByteArrayProcessor() { 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 modelResult = (model != null && model.GetType() == typeof (byte[])) ? 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) { return new ByteArrayResponse((byte[])model, requestedMediaRange); } } } This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,26 @@ 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 hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,14 @@ using System.IO; using Nancy; namespace ByteArrayDemo { public class MainModule : NancyModule { public MainModule() { Get["/"] = _ => Negotiate.WithView("index") .WithMediaRangeModel("image/jpeg", () => File.ReadAllBytes(@"Koala.jpg")); } } }