懒得勤快 1 year ago
parent
commit
a18c048f06

+ 12 - 4
Masuit.Tools.Abstractions/Masuit.Tools.Abstractions.csproj

@@ -49,12 +49,10 @@
     <ItemGroup>
         <PackageReference Include="AngleSharp" Version="1.1.2" />
         <PackageReference Include="AngleSharp.Css" Version="1.0.0-beta.139" />
-        <PackageReference Include="Castle.Core" Version="5.1.1" />
         <PackageReference Include="DnsClient" Version="1.8.0" />
         <PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
         <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
         <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
-        <PackageReference Include="System.Collections.Immutable" Version="8.0.0" />
         <PackageReference Include="System.Configuration.ConfigurationManager" Version="8.0.0" />
         <PackageReference Include="System.Management" Version="8.0" />
         <PackageReference Include="System.Reflection.Emit.Lightweight" Version="4.7.0" />
@@ -62,40 +60,50 @@
     </ItemGroup>
 
     <ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0'">
+        <PackageReference Include="Castle.Core" Version="5.1.1" />
         <PackageReference Include="IndexRange" Version="1.0.3" />
-        <PackageReference Include="System.Collections.Immutable" Version="8.0.0" />
         <PackageReference Include="System.Memory" Version="4.5.5" />
         <PackageReference Include="SixLabors.ImageSharp.Drawing" Version="[1.0.0]" />
+        <PackageReference Include="System.Collections.Immutable" Version="8.0.0" />
         <PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
         <PackageReference Include="System.Diagnostics.PerformanceCounter" Version="7.0.0" />
     </ItemGroup>
 
     <ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.1'">
+        <PackageReference Include="Castle.Core" Version="5.1.1" />
         <PackageReference Include="IndexRange" Version="1.0.3" />
-        <PackageReference Include="System.Collections.Immutable" Version="8.0.0" />
         <PackageReference Include="System.Memory" Version="4.5.5" />
         <PackageReference Include="SixLabors.ImageSharp.Drawing" Version="[1.0.0]" />
+        <PackageReference Include="System.Collections.Immutable" Version="8.0.0" />
         <PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
         <PackageReference Include="System.Diagnostics.PerformanceCounter" Version="7.0.0" />
     </ItemGroup>
 
     <ItemGroup Condition=" '$(TargetFramework)' == 'net5'">
+        <PackageReference Include="Castle.Core" Version="5.1.1" />
         <PackageReference Include="SixLabors.ImageSharp.Drawing" Version="[1.0.0]" />
+        <PackageReference Include="System.Collections.Immutable" Version="8.0.0" />
         <PackageReference Include="System.Diagnostics.PerformanceCounter" Version="7.0.0" />
     </ItemGroup>
 
     <ItemGroup Condition=" '$(TargetFramework)' == 'net6'">
+        <PackageReference Include="Castle.Core" Version="5.1.1" />
         <PackageReference Include="System.Diagnostics.PerformanceCounter" Version="7.0.0" />
+        <PackageReference Include="System.Collections.Immutable" Version="8.0.0" />
         <PackageReference Include="SixLabors.ImageSharp.Drawing" Version="2.1.4" />
     </ItemGroup>
 
     <ItemGroup Condition=" '$(TargetFramework)' == 'net7'">
+        <PackageReference Include="Castle.Core" Version="5.1.1" />
         <PackageReference Include="System.Diagnostics.PerformanceCounter" Version="7.0.0" />
+        <PackageReference Include="System.Collections.Immutable" Version="8.0.0" />
         <PackageReference Include="SixLabors.ImageSharp.Drawing" Version="2.1.4" />
     </ItemGroup>
 
     <ItemGroup Condition=" '$(TargetFramework)' == 'net8'">
+        <PackageReference Include="Castle.Core" Version="5.1.1" />
         <PackageReference Include="System.Diagnostics.PerformanceCounter" Version="8.0.0" />
+        <PackageReference Include="System.Collections.Immutable" Version="8.0.0" />
         <PackageReference Include="SixLabors.ImageSharp.Drawing" Version="2.1.4" />
     </ItemGroup>
 

+ 49 - 51
Masuit.Tools.AspNetCore/Extensions/MultipartRequestService.cs

@@ -11,62 +11,60 @@ namespace Masuit.Tools.AspNetCore.Extensions;
 [ServiceInject(ServiceLifetime.Scoped)]
 public class MultipartRequestService : IMultipartRequestService
 {
-    public async Task<(Dictionary<string, StringValues>, byte[])> GetDataFromMultiPart(MultipartReader reader, CancellationToken cancellationToken)
-    {
-        var formAccumulator = new KeyValueAccumulator();
-        var file = Array.Empty<byte>();
+	public async Task<(Dictionary<string, StringValues>, byte[])> GetDataFromMultiPart(MultipartReader reader, CancellationToken cancellationToken)
+	{
+		var formAccumulator = new KeyValueAccumulator();
+		var file = Array.Empty<byte>();
 
-        while (await reader.ReadNextSectionAsync(cancellationToken) is { } section)
-        {
-            if (!ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out var contentDisposition))
-            {
-                continue;
-            }
+		while (await reader.ReadNextSectionAsync(cancellationToken) is { } section)
+		{
+			if (!ContentDispositionHeaderValue.TryParse(section.ContentDisposition, out var contentDisposition))
+			{
+				continue;
+			}
 
-            if (contentDisposition.IsFormDisposition())
-            {
-                formAccumulator = await AccumulateForm(formAccumulator, section, contentDisposition);
-            }
-            else if (contentDisposition.IsFileDisposition())
-            {
-                await using var memoryStream = new PooledMemoryStream();
-                await section.Body.CopyToAsync(memoryStream, cancellationToken);
-                file = memoryStream.ToArray();
-            }
-        }
+			if (contentDisposition.IsFormDisposition())
+			{
+				formAccumulator = await AccumulateForm(formAccumulator, section, contentDisposition);
+			}
+			else if (contentDisposition.IsFileDisposition())
+			{
+				await using var memoryStream = new PooledMemoryStream();
+				await section.Body.CopyToAsync(memoryStream, cancellationToken);
+				file = memoryStream.ToArray();
+			}
+		}
 
-        return (formAccumulator.GetResults(), file);
-    }
+		return (formAccumulator.GetResults(), file);
+	}
 
-    private Encoding GetEncoding(MultipartSection section)
-    {
-        var hasMediaTypeHeader = MediaTypeHeaderValue.TryParse(section.ContentType, out var mediaType);
-        if (!hasMediaTypeHeader || Encoding.UTF7.Equals(mediaType.Encoding))
-        {
-            return Encoding.UTF8;
-        }
+	private Encoding GetEncoding(MultipartSection section)
+	{
+		var hasMediaTypeHeader = MediaTypeHeaderValue.TryParse(section.ContentType, out var mediaType);
+		if (!hasMediaTypeHeader || Encoding.UTF7.Equals(mediaType.Encoding))
+		{
+			return Encoding.UTF8;
+		}
 
-        return mediaType.Encoding;
-    }
+		return mediaType.Encoding;
+	}
 
-    private async Task<KeyValueAccumulator> AccumulateForm(KeyValueAccumulator formAccumulator, MultipartSection section, ContentDispositionHeaderValue contentDisposition)
-    {
-        var key = HeaderUtilities.RemoveQuotes(contentDisposition.Name).Value;
-        using var streamReader = new StreamReader(section.Body, GetEncoding(section), true, 1024, true);
-        {
-            var value = await streamReader.ReadToEndAsync();
-            if (string.Equals(value, "undefined", StringComparison.OrdinalIgnoreCase))
-            {
-                value = string.Empty;
-            }
-            formAccumulator.Append(key, value);
+	private async Task<KeyValueAccumulator> AccumulateForm(KeyValueAccumulator formAccumulator, MultipartSection section, ContentDispositionHeaderValue contentDisposition)
+	{
+		var key = HeaderUtilities.RemoveQuotes(contentDisposition.Name).Value;
+		using var streamReader = new StreamReader(section.Body, GetEncoding(section), true, 1024, true);
+		var value = await streamReader.ReadToEndAsync();
+		if (string.Equals(value, "undefined", StringComparison.OrdinalIgnoreCase))
+		{
+			value = string.Empty;
+		}
 
-            if (formAccumulator.ValueCount > FormReader.DefaultValueCountLimit)
-            {
-                throw new InvalidDataException($"Form key count limit {FormReader.DefaultValueCountLimit} exceeded.");
-            }
-        }
+		formAccumulator.Append(key, value);
+		if (formAccumulator.ValueCount > FormReader.DefaultValueCountLimit)
+		{
+			throw new InvalidDataException($"Form key count limit {FormReader.DefaultValueCountLimit} exceeded.");
+		}
 
-        return formAccumulator;
-    }
-}
+		return formAccumulator;
+	}
+}