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;
4using System.Threading.Tasks;
5using Waher.Content;
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 Files.ReadAllBytesAsync(this.FileName);
86 using ICryptoTransform Decryptor = Aes.CreateDecryptor(this.Key, this.IV);
87
88 return Decryptor.TransformFinalBlock(Encrypted, 0, Encrypted.Length);
89 }
90
96 internal async Task Return(HttpRequest Request, HttpResponse Response)
97 {
98 if (!(Request.Header.IfNoneMatch is null) && Request.Header.IfNoneMatch.Value == this.ETag)
99 await Response.SendResponse(new NotModifiedException());
100 else
101 {
102 Response.SetHeader("ETag", "\"" + this.ETag + "\"");
103
104 byte[] Decrypted = await this.Load();
105
106 await Response.Return(this.ContentType, true, Decrypted);
107 }
108 }
109
117 internal async Task<ContentResponse> Scale(int Width, int Height)
118 {
119 if (!this.ContentType.StartsWith("image/", StringComparison.OrdinalIgnoreCase))
120 return new ContentResponse(new UnsupportedMediaTypeException("Not an image attachment."));
121
122 byte[] Original = await this.Load();
123
124 ContentResponse Decoded = await InternetContent.DecodeAsync(this.ContentType, Original, null);
125 if (Decoded.HasError)
126 return Decoded;
127
128 if (!(Decoded.Decoded is SKImage Image))
129 return new ContentResponse(new UnsupportedMediaTypeException("Not an image attachment."));
130
131 double ScaleX = ((double)Width) / Image.Width;
132 double ScaleY = ((double)Height) / Image.Height;
133 double Scale = Math.Min(ScaleX, ScaleY);
134 int W = (int)(Image.Width * Scale + 0.5);
135 int H = (int)(Image.Height * Scale + 0.5);
136
137 using SKSurface Surface = SKSurface.Create(new SKImageInfo(W, H, SKImageInfo.PlatformColorType, SKAlphaType.Premul));
138
139 SKCanvas Canvas = Surface.Canvas;
140 SKPaint Paint = new SKPaint()
141 {
142 IsAntialias = true
143 };
144
145 SKSamplingOptions Options = new SKSamplingOptions(SKCubicResampler.Mitchell);
146
147 Canvas.DrawImage(Image, new SKRect(0, 0, W, H), Options, Paint);
148
149 using SKImage ScaledImage = Surface.Snapshot();
150
151 return await InternetContent.EncodeAsync(ScaledImage, null);
152 }
153
154 }
155}
Contains information about a response to a content request.
bool HasError
If an error occurred.
object Decoded
Decoded object.
Static class managing encoding and decoding of internet content.
static Task< ContentResponse > EncodeAsync(object Object, Encoding Encoding, params string[] AcceptedContentTypes)
Encodes an object.
static Task< ContentResponse > DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair< string, string >[] Fields, Uri BaseUri)
Decodes an object.
HttpFieldIfNoneMatch IfNoneMatch
If-None-Match HTTP Field header. (RFC 2616, §14.26)
Represents an HTTP request.
Definition: HttpRequest.cs:22
HttpRequestHeader Header
Request header.
Definition: HttpRequest.cs:182
Represets a response of an HTTP client request.
Definition: HttpResponse.cs:23
async Task SendResponse()
Sends the response back to the client. If the resource is synchronous, there's no need to call this m...
void SetHeader(string FieldName, string Value)
Sets a custom header field value.
Task Return(Exception ex)
Returns an error to the client.
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...
Contains static methods
Definition: Files.cs:14
static async Task< byte[]> ReadAllBytesAsync(string FileName)
Reads a binary file asynchronously.
Definition: Files.cs:20