Skip to content

Instantly share code, notes, and snippets.

@grumpydev
Last active December 21, 2017 12:53
Show Gist options
  • Save grumpydev/4605541 to your computer and use it in GitHub Desktop.
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
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);
}
}
}
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"));
}
}
}
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);
}
};
}
}
}
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"));
}
}
}
@Radzhab
Copy link

Radzhab commented Nov 18, 2015

can you share working project?

@pawelsawicz
Copy link

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