This repository has been archived by the owner on May 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQrController.cs
102 lines (81 loc) · 3.9 KB
/
QrController.cs
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using QRCoder;
namespace QRocket
{
[Route("/api/[controller]")]
[ApiController]
public class QrController : ControllerBase
{
private readonly dynamic _barkleyBrand = new
{
DarkColor = "#131313",
LightColor = "#E6E7E8",
RedColor = "#EB052C",
Icon = "Barkley_Logo_64.png"
};
private readonly IWebHostEnvironment _env;
private readonly dynamic _tacoJohnBrand = new
{
DarkColor = "#76232F",
LightColor = "#E5DFDC",
Icon = "TacoJohns_small.png"
};
public QrController(IWebHostEnvironment env) => _env = env;
[HttpGet("gen-url")]
public ActionResult GenUrl([FromQuery] string val)
{
Bitmap icon = null;
var www = _env.WebRootPath;
var model = Decode(val);
if (string.IsNullOrEmpty(model.DarkColor)) model.DarkColor = _barkleyBrand.DarkColor;
if (string.IsNullOrEmpty(model.LightColor)) model.LightColor = _barkleyBrand.LightColor;
model.Icon = null; //_tacoJohnBrand.Icon;
var dark = ColorTranslator.FromHtml(model.DarkColor);
var light = ColorTranslator.FromHtml(model.LightColor);
if (!string.IsNullOrEmpty(model.Icon)) icon = (Bitmap) Image.FromFile(Path.Combine(www, "media", model.Icon));
var generator = new QRCodeGenerator();
var data = generator.CreateQrCode(new PayloadGenerator.Url(model.Url), QRCodeGenerator.ECCLevel.M);
var qr = new QRCode(data);
using var graphic = qr.GetGraphic(20, dark, light, icon);
using var stream = new MemoryStream();
graphic.Save(stream, ImageFormat.Png);
var result = stream.ToArray();
return File(result, "image/png");
}
[HttpGet("gen-ics")]
public ActionResult GenIcs([FromQuery] string val)
{
Bitmap icon = null;
var www = _env.WebRootPath;
var model = Decode(val);
if (string.IsNullOrEmpty(model.DarkColor)) model.DarkColor = _barkleyBrand.DarkColor;
if (string.IsNullOrEmpty(model.LightColor)) model.LightColor = _barkleyBrand.LightColor;
model.Icon = null; //_tacoJohnBrand.Icon;
var dark = ColorTranslator.FromHtml(model.DarkColor);
var light = ColorTranslator.FromHtml(model.LightColor);
if (!string.IsNullOrEmpty(model.Icon)) icon = (Bitmap) Image.FromFile(Path.Combine(www, "media", model.Icon));
var allDay = model.EndDate is null || model.EndDate.Value == model.StartDate.Value;
var icsPayload = new PayloadGenerator.CalendarEvent(model.Title, model.Notes, model.Location, model.StartDate.Value, model.EndDate.Value, allDay, PayloadGenerator.CalendarEvent.EventEncoding.iCalComplete);
var generator = new QRCodeGenerator();
var data = generator.CreateQrCode(icsPayload, QRCodeGenerator.ECCLevel.M);
var qr = new QRCode(data);
using var graphic = qr.GetGraphic(20, dark, light, icon);
using var stream = new MemoryStream();
graphic.Save(stream, ImageFormat.Png);
var result = stream.ToArray();
return File(result, "image/png");
}
private static PayloadModel Decode(string base64Json)
{
var json = Encoding.UTF8.GetString(Convert.FromBase64String(base64Json));
return JsonConvert.DeserializeObject<PayloadModel>(json);
}
}
}