aboutsummaryrefslogtreecommitdiff
path: root/Timeline/Formatters
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2020-10-27 19:21:35 +0800
committercrupest <crupest@outlook.com>2020-10-27 19:21:35 +0800
commitac769e656b122ff569c3f1534701b71e00fed586 (patch)
tree72966645ff1e25139d3995262e1c4349f2c14733 /Timeline/Formatters
parent14e5848c23c643cea9b5d709770747d98c3d75e2 (diff)
downloadtimeline-ac769e656b122ff569c3f1534701b71e00fed586.tar.gz
timeline-ac769e656b122ff569c3f1534701b71e00fed586.tar.bz2
timeline-ac769e656b122ff569c3f1534701b71e00fed586.zip
Split front and back end.
Diffstat (limited to 'Timeline/Formatters')
-rw-r--r--Timeline/Formatters/BytesInputFormatter.cs79
-rw-r--r--Timeline/Formatters/StringInputFormatter.cs26
2 files changed, 0 insertions, 105 deletions
diff --git a/Timeline/Formatters/BytesInputFormatter.cs b/Timeline/Formatters/BytesInputFormatter.cs
deleted file mode 100644
index ac6537c9..00000000
--- a/Timeline/Formatters/BytesInputFormatter.cs
+++ /dev/null
@@ -1,79 +0,0 @@
-using Microsoft.AspNetCore.Mvc.Formatters;
-using Microsoft.Extensions.DependencyInjection;
-using Microsoft.Extensions.Logging;
-using Microsoft.Net.Http.Headers;
-using System;
-using System.Threading.Tasks;
-using Timeline.Models;
-
-namespace Timeline.Formatters
-{
- /// <summary>
- /// Formatter that reads body as bytes.
- /// </summary>
- public class BytesInputFormatter : InputFormatter
- {
- /// <summary>
- ///
- /// </summary>
- public BytesInputFormatter()
- {
- SupportedMediaTypes.Add(new MediaTypeHeaderValue("image/png"));
- SupportedMediaTypes.Add(new MediaTypeHeaderValue("image/jpeg"));
- SupportedMediaTypes.Add(new MediaTypeHeaderValue("image/gif"));
- SupportedMediaTypes.Add(new MediaTypeHeaderValue("image/webp"));
- }
-
- /// <inheritdoc/>
- public override bool CanRead(InputFormatterContext context)
- {
- if (context == null) throw new ArgumentNullException(nameof(context));
-
- if (context.ModelType == typeof(ByteData))
- return true;
-
- return false;
- }
-
- /// <inheritdoc/>
- public override async Task<InputFormatterResult> ReadRequestBodyAsync(InputFormatterContext context)
- {
- var request = context.HttpContext.Request;
- var contentLength = request.ContentLength;
-
- var logger = context.HttpContext.RequestServices.GetRequiredService<ILogger<BytesInputFormatter>>();
-
- if (contentLength == null)
- {
- logger.LogInformation("Failed to read body as bytes. Content-Length is not set.");
- return await InputFormatterResult.FailureAsync();
- }
-
- if (contentLength == 0)
- {
- logger.LogInformation("Failed to read body as bytes. Content-Length is 0.");
- return await InputFormatterResult.FailureAsync();
- }
-
- var bodyStream = request.Body;
-
- var data = new byte[contentLength.Value];
- var bytesRead = await bodyStream.ReadAsync(data);
-
- if (bytesRead != contentLength)
- {
- logger.LogInformation("Failed to read body as bytes. Actual length of body is smaller than Content-Length.");
- return await InputFormatterResult.FailureAsync();
- }
-
- var extraByte = new byte[1];
- if (await bodyStream.ReadAsync(extraByte) != 0)
- {
- logger.LogInformation("Failed to read body as bytes. Actual length of body is greater than Content-Length.");
- return await InputFormatterResult.FailureAsync();
- }
-
- return await InputFormatterResult.SuccessAsync(new ByteData(data, request.ContentType));
- }
- }
-}
diff --git a/Timeline/Formatters/StringInputFormatter.cs b/Timeline/Formatters/StringInputFormatter.cs
deleted file mode 100644
index b1924268..00000000
--- a/Timeline/Formatters/StringInputFormatter.cs
+++ /dev/null
@@ -1,26 +0,0 @@
-using Microsoft.AspNetCore.Mvc.Formatters;
-using Microsoft.Net.Http.Headers;
-using System.IO;
-using System.Net.Mime;
-using System.Text;
-using System.Threading.Tasks;
-
-namespace Timeline.Formatters
-{
- public class StringInputFormatter : TextInputFormatter
- {
- public StringInputFormatter()
- {
- SupportedMediaTypes.Add(MediaTypeHeaderValue.Parse(MediaTypeNames.Text.Plain));
- SupportedEncodings.Add(Encoding.UTF8);
- }
-
- public override async Task<InputFormatterResult> ReadRequestBodyAsync(InputFormatterContext context, Encoding effectiveEncoding)
- {
- var request = context.HttpContext.Request;
- using var reader = new StreamReader(request.Body, effectiveEncoding);
- var stringContent = await reader.ReadToEndAsync();
- return await InputFormatterResult.SuccessAsync(stringContent);
- }
- }
-}