forked from einaregilsson/ChordImageGenerator
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathChordMiddleware.cs
More file actions
59 lines (52 loc) · 2.19 KB
/
ChordMiddleware.cs
File metadata and controls
59 lines (52 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web;
using Microsoft.AspNetCore.Http;
using Microsoft.Net.Http.Headers;
namespace EinarEgilsson.Chords
{
public class ChordMiddleware
{
private readonly RequestDelegate _next;
public ChordMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task InvokeAsync(HttpContext context)
{
if (!context.Request.Path.ToString().ToLower().EndsWith(".png"))
{
await _next(context);
return;
}
var query = context.Request.Query;
var chordName = Regex.Match(context.Request.Path.ToString(), @"^/(.*)\.png$").Groups[1].Value;
// For whatever reason 'context.Request.Path' resolves a '%2B' to a
// '+', but not '%23' to '#'.
// Using HttpUtilty.UrlDecode() breaks this, because it will remove
// the '+'. That is why we require a workaround here.
var split = chordName.Split('+');
chordName = string.Join('+', split.Select(s => HttpUtility.UrlDecode(s)));
var pos = query["pos"].FirstOrDefault() ?? query["p"].FirstOrDefault() ?? "000000";
var fingers = query["fingers"].FirstOrDefault() ?? query["f"].FirstOrDefault() ?? "------";
fingers = fingers.Replace(' ', '+');
var size = query["size"].FirstOrDefault() ?? query["s"].FirstOrDefault() ?? "1";
var fullBarre = query["full_barre"].FirstOrDefault() ?? query["b"].FirstOrDefault() ?? "false";
if (!bool.TryParse(fullBarre, out var drawFullBarre))
{
drawFullBarre = false;
}
context.Response.ContentType = "image/png";
context.Response.GetTypedHeaders().CacheControl =
new CacheControlHeaderValue()
{
Public = true,
MaxAge = TimeSpan.FromDays(7)
};
using var image = new ChordBoxImage(chordName, pos, fingers, size, drawFullBarre);
await image.SaveAsync(context.Response.Body);
}
}
}