Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
QuickLoginAttachment.cs
1using SkiaSharp;
2using System;
3using System.Security.Cryptography;
4using System.Threading.Tasks;
5using Waher.Content;
8using System.Collections.Generic;
9
11{
15 [CollectionName("QuickLoginAttachments")]
16 [Index("AttachmentId", "Width", "Height")]
18 {
23 {
24 }
25
29 [ObjectId]
30 public string ObjectId { get; set; }
31
35 public string AttachmentId { get; set; }
36
40 public int? Width { get; set; }
41
45 public int? Height { get; set; }
46
50 public string ETag { get; set; }
51
55 public string FileName { get; set; }
56
60 public string ContentType { get; set; }
61
65 public byte[] Key { get; set; }
66
70 public byte[] IV { get; set; }
71
76 internal async Task<byte[]> Load()
77 {
78 Aes Aes = Aes.Create();
79
80 Aes.BlockSize = 128;
81 Aes.KeySize = 256;
82 Aes.Mode = CipherMode.CBC;
83 Aes.Padding = PaddingMode.PKCS7;
84
85 byte[] Encrypted = await Resources.ReadAllBytesAsync(this.FileName);
86
87 using (ICryptoTransform Decryptor = Aes.CreateDecryptor(this.Key, this.IV))
88 {
89 return Decryptor.TransformFinalBlock(Encrypted, 0, Encrypted.Length);
90 }
91 }
92
98 internal async Task Return(HttpRequest Request, HttpResponse Response)
99 {
100 if (!(Request.Header.IfNoneMatch is null) && Request.Header.IfNoneMatch.Value == this.ETag)
101 throw new NotModifiedException();
102
103 Response.SetHeader("ETag", "\"" + this.ETag + "\"");
104
105 byte[] Decrypted = await this.Load();
106
107 await Response.Return(this.ContentType, Decrypted);
108 }
109
117 internal async Task<KeyValuePair<byte[], string>> Scale(int Width, int Height)
118 {
119 if (!this.ContentType.StartsWith("image/", StringComparison.OrdinalIgnoreCase))
120 throw new UnsupportedMediaTypeException("Not an image attachment.");
121
122 byte[] Original = await this.Load();
123
124 if (!(await InternetContent.DecodeAsync(this.ContentType, Original, null) is SKImage Image))
125 throw new UnsupportedMediaTypeException("Not an image attachment.");
126
127 double ScaleX = ((double)Width) / Image.Width;
128 double ScaleY = ((double)Height) / Image.Height;
129 double Scale = Math.Min(ScaleX, ScaleY);
130 int W = (int)(Image.Width * Scale + 0.5);
131 int H = (int)(Image.Height * Scale + 0.5);
132
133 using (SKSurface Surface = SKSurface.Create(new SKImageInfo(W, H, SKImageInfo.PlatformColorType, SKAlphaType.Premul)))
134 {
135 SKCanvas Canvas = Surface.Canvas;
136 SKPaint Paint = new SKPaint()
137 {
138 FilterQuality = SKFilterQuality.High,
139 IsAntialias = true
140 };
141
142 Canvas.DrawImage(Image, new SKRect(0, 0, W, H), Paint);
143
144 using (SKImage ScaledImage = Surface.Snapshot())
145 {
146 return await InternetContent.EncodeAsync(ScaledImage, null);
147 }
148 }
149 }
150
151 }
152}
Static class managing encoding and decoding of internet content.
static Task< KeyValuePair< byte[], string > > EncodeAsync(object Object, Encoding Encoding, params string[] AcceptedContentTypes)
Encodes an object.
static Task< object > DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair< string, string >[] Fields, Uri BaseUri)
Decodes an object.
Static class managing loading of resources stored as embedded resources or in content files.
Definition: Resources.cs:15
static async Task< byte[]> ReadAllBytesAsync(string FileName)
Reads a binary file asynchronously.
Definition: Resources.cs:183
HttpFieldIfNoneMatch IfNoneMatch
If-None-Match HTTP Field header. (RFC 2616, §14.26)
Represents an HTTP request.
Definition: HttpRequest.cs:18
HttpRequestHeader Header
Request header.
Definition: HttpRequest.cs:134
Represets a response of an HTTP client request.
Definition: HttpResponse.cs:21
void SetHeader(string FieldName, string Value)
Sets a custom header field value.
async Task Return(object Object)
Returns an object to the client. This method can only be called once per response,...
If the client has performed a conditional GET request and access is allowed, but the document has not...
The server is refusing to service the request because the entity of the request is in a format not su...