Skip to content

Instantly share code, notes, and snippets.

@grumpydev
Last active December 21, 2017 12:53

Revisions

  1. grumpydev revised this gist Jan 23, 2013. 3 changed files with 15 additions and 4 deletions.
    15 changes: 12 additions & 3 deletions ByteArrayProcessor.cs → BinaryProcessor.cs
    Original 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 ByteArrayProcessor : IResponseProcessor
    public class BinaryProcessor : IResponseProcessor
    {
    public static IList<Tuple<string, MediaRange>> Mappings { get; set; }

    static ByteArrayProcessor()
    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 modelResult = (model != null && model.GetType() == typeof (byte[])) ? MatchResult.ExactMatch : MatchResult.NoMatch;
    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);
    }
    }
    1 change: 1 addition & 0 deletions Bootstrapper.cs
    Original 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"));
    }
    }
    }
    3 changes: 2 additions & 1 deletion MainModule.cs
    Original 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/jpeg", () => File.ReadAllBytes(@"Koala.jpg"))
    .WithMediaRangeModel("image/png", () => File.OpenRead(@"rowlf.png"));
    }
    }
    }
  2. grumpydev created this gist Jan 23, 2013.
    17 changes: 17 additions & 0 deletions Bootstrapper.cs
    Original 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"));
    }
    }
    }
    36 changes: 36 additions & 0 deletions ByteArrayProcessor.cs
    Original 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);
    }
    }
    }
    26 changes: 26 additions & 0 deletions ByteArrayResponse.cs
    Original 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);
    }
    };
    }
    }
    }
    14 changes: 14 additions & 0 deletions MainModule.cs
    Original 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"));
    }
    }
    }