diff options
author | crupest <crupest@outlook.com> | 2021-02-10 14:31:31 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2021-02-10 14:31:31 +0800 |
commit | 872f0e9f094f37db9ff208d178ad5bea2fafc1a7 (patch) | |
tree | 27291dc4105e64ca3ed6ed43a8822cb911ed49a9 /BackEnd/Timeline/Formatters/BytesInputFormatter.cs | |
parent | 253b06dfaa091d986a8714c081fd1e01679f538a (diff) | |
download | timeline-872f0e9f094f37db9ff208d178ad5bea2fafc1a7.tar.gz timeline-872f0e9f094f37db9ff208d178ad5bea2fafc1a7.tar.bz2 timeline-872f0e9f094f37db9ff208d178ad5bea2fafc1a7.zip |
...
Diffstat (limited to 'BackEnd/Timeline/Formatters/BytesInputFormatter.cs')
-rw-r--r-- | BackEnd/Timeline/Formatters/BytesInputFormatter.cs | 79 |
1 files changed, 0 insertions, 79 deletions
diff --git a/BackEnd/Timeline/Formatters/BytesInputFormatter.cs b/BackEnd/Timeline/Formatters/BytesInputFormatter.cs deleted file mode 100644 index ac6537c9..00000000 --- a/BackEnd/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));
- }
- }
-}
|