Skip to content

Commit

Permalink
将文件存储接口移动到Util.FileStorage.Abstractions类库
Browse files Browse the repository at this point in the history
  • Loading branch information
UtilCore committed Jan 7, 2024
1 parent efd3ca9 commit d9b8fee
Show file tree
Hide file tree
Showing 42 changed files with 189 additions and 57 deletions.
15 changes: 11 additions & 4 deletions Util.sln
Original file line number Diff line number Diff line change
Expand Up @@ -267,11 +267,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Util.Generators.Tests", "te
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "08-FileStorage", "08-FileStorage", "{E069CEB0-8092-4907-BC9D-C6B8BDE20AD2}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "01-Util.FileStorage", "src\Util.FileStorage\01-Util.FileStorage.csproj", "{6FC29D15-7581-4E29-9897-76DC5AEF2042}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "02-Util.FileStorage", "src\Util.FileStorage\02-Util.FileStorage.csproj", "{6FC29D15-7581-4E29-9897-76DC5AEF2042}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "02-Util.FileStorage.Minio", "src\Util.FileStorage.Minio\02-Util.FileStorage.Minio.csproj", "{3E81F8D0-06C7-4FD4-A5B0-E3D96E909646}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "03-Util.FileStorage.Minio", "src\Util.FileStorage.Minio\03-Util.FileStorage.Minio.csproj", "{3E81F8D0-06C7-4FD4-A5B0-E3D96E909646}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "03-Util.FileStorage.Aliyun", "src\Util.FileStorage.Aliyun\03-Util.FileStorage.Aliyun.csproj", "{C233FF83-E15D-4A17-8FF1-BBFF9E9DE210}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "04-Util.FileStorage.Aliyun", "src\Util.FileStorage.Aliyun\04-Util.FileStorage.Aliyun.csproj", "{C233FF83-E15D-4A17-8FF1-BBFF9E9DE210}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "08-FileStorage", "08-FileStorage", "{16175228-03CA-414A-8786-E5BA844110C4}"
EndProject
Expand Down Expand Up @@ -339,10 +339,12 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "07-Util.Http", "src\Util.Ht
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Util.FileStorage.Tests.Integration", "test\Util.FileStorage.Tests.Integration\Util.FileStorage.Tests.Integration.csproj", "{A23BA1A3-8EC7-4937-B0AA-69CCEE40453C}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "04-Util.FileStorage.All", "src\Util.FileStorage.All\04-Util.FileStorage.All.csproj", "{4938D59D-CF50-4090-B936-36DB29EBE344}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "05-Util.FileStorage.All", "src\Util.FileStorage.All\05-Util.FileStorage.All.csproj", "{4938D59D-CF50-4090-B936-36DB29EBE344}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Util.FileStorage.Aliyun.Tests.Integration", "test\Util.FileStorage.Aliyun.Tests.Integration\Util.FileStorage.Aliyun.Tests.Integration.csproj", "{58C01936-5D8F-4077-8663-EE0E35776D65}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "01-Util.FileStorage.Abstractions", "src\Util.FileStorage.Abstractions\01-Util.FileStorage.Abstractions.csproj", "{814E42E9-508E-487F-BCD3-0F07AE7D1492}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -857,6 +859,10 @@ Global
{58C01936-5D8F-4077-8663-EE0E35776D65}.Debug|Any CPU.Build.0 = Debug|Any CPU
{58C01936-5D8F-4077-8663-EE0E35776D65}.Release|Any CPU.ActiveCfg = Release|Any CPU
{58C01936-5D8F-4077-8663-EE0E35776D65}.Release|Any CPU.Build.0 = Release|Any CPU
{814E42E9-508E-487F-BCD3-0F07AE7D1492}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{814E42E9-508E-487F-BCD3-0F07AE7D1492}.Debug|Any CPU.Build.0 = Debug|Any CPU
{814E42E9-508E-487F-BCD3-0F07AE7D1492}.Release|Any CPU.ActiveCfg = Release|Any CPU
{814E42E9-508E-487F-BCD3-0F07AE7D1492}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -1027,6 +1033,7 @@ Global
{A23BA1A3-8EC7-4937-B0AA-69CCEE40453C} = {16175228-03CA-414A-8786-E5BA844110C4}
{4938D59D-CF50-4090-B936-36DB29EBE344} = {E069CEB0-8092-4907-BC9D-C6B8BDE20AD2}
{58C01936-5D8F-4077-8663-EE0E35776D65} = {16175228-03CA-414A-8786-E5BA844110C4}
{814E42E9-508E-487F-BCD3-0F07AE7D1492} = {E069CEB0-8092-4907-BC9D-C6B8BDE20AD2}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {94347832-A36D-4C42-9C4D-B848BD4F5DA9}
Expand Down
2 changes: 1 addition & 1 deletion build/version.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<VersionMajor>7</VersionMajor>
<VersionMinor>1</VersionMinor>
<VersionPatch>123</VersionPatch>
<VersionPatch>125</VersionPatch>
<VersionPrefix>$(VersionMajor).$(VersionMinor).$(VersionPatch)</VersionPrefix>
<VersionSuffix></VersionSuffix>
</PropertyGroup>
Expand Down
15 changes: 15 additions & 0 deletions src/Util.Aop.AspectCore/AopOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Util.Aop;

/// <summary>
/// Aop配置
/// </summary>
public class AopOptions {
/// <summary>
/// 是否启用IAopProxy接口标记
/// </summary>
public bool IsEnableIAopProxy { get; set; }
/// <summary>
/// 是否启用参数拦截器,默认值: true
/// </summary>
public bool IsEnableParameterAspect { get; set; } = true;
}
23 changes: 17 additions & 6 deletions src/Util.Aop.AspectCore/AppBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,22 @@ public static IAppBuilder AddAop( this IAppBuilder builder, Action<IAspectConfig
/// <param name="setupAction">AspectCore拦截器配置操作</param>
/// <param name="isEnableIAopProxy">是否启用IAopProxy接口标记</param>
private static IAppBuilder AddAop( this IAppBuilder builder, Action<IAspectConfiguration> setupAction, bool isEnableIAopProxy ) {
return builder.AddAop( setupAction, t => t.IsEnableIAopProxy = isEnableIAopProxy );
}

/// <summary>
/// 启用AspectCore拦截器
/// </summary>
/// <param name="builder">应用生成器</param>
/// <param name="setupAction">AspectCore拦截器配置操作</param>
/// <param name="action">配置操作</param>
private static IAppBuilder AddAop( this IAppBuilder builder, Action<IAspectConfiguration> setupAction, Action<AopOptions> action ) {
builder.CheckNull( nameof( builder ) );
builder.Host.UseServiceProviderFactory( new DynamicProxyServiceProviderFactory() );
builder.Host.ConfigureServices( ( context, services ) => {
ConfigureDynamicProxy( services, setupAction, isEnableIAopProxy );
var options = new AopOptions();
action?.Invoke( options );
ConfigureDynamicProxy( services, setupAction, options.IsEnableParameterAspect,options.IsEnableIAopProxy );
RegisterAspectScoped( services );
} );
return builder;
Expand All @@ -51,11 +63,12 @@ private static IAppBuilder AddAop( this IAppBuilder builder, Action<IAspectConfi
/// <summary>
/// 配置拦截器
/// </summary>
private static void ConfigureDynamicProxy( IServiceCollection services, Action<IAspectConfiguration> setupAction, bool isEnableIAopProxy ) {
private static void ConfigureDynamicProxy( IServiceCollection services, Action<IAspectConfiguration> setupAction,bool isEnableParameterAspect, bool isEnableIAopProxy ) {
services.ConfigureDynamicProxy( config => {
if ( setupAction == null ) {
config.NonAspectPredicates.Add( t => !IsProxy( t.DeclaringType, isEnableIAopProxy ) );
config.EnableParameterAspect();
if( isEnableParameterAspect )
config.EnableParameterAspect();
return;
}
setupAction.Invoke( config );
Expand All @@ -69,9 +82,7 @@ private static bool IsProxy( Type type, bool isEnableIAopProxy ) {
if ( type == null )
return false;
if ( isEnableIAopProxy == false ) {
if ( type.SafeString().Contains( "Xunit.DependencyInjection.ITestOutputHelperAccessor" ) )
return false;
return true;
return type.SafeString().Contains( "Xunit.DependencyInjection.ITestOutputHelperAccessor" ) == false;
}
var interfaces = type.GetInterfaces();
if ( interfaces == null || interfaces.Length == 0 )
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>$(NetTargetFramework)</TargetFramework>
<PackageIcon>icon.jpg</PackageIcon>
<AssemblyName>Util.FileStorage.Abstractions</AssemblyName>
<RootNamespace>Util.FileStorage</RootNamespace>
<Description>Util.FileStorage.Abstractions是Util应用框架文件存储操作接口定义类库</Description>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<OutputPath></OutputPath>
<DocumentationFile>.\obj\Debug\$(NetTargetFramework)\Util.FileStorage.Abstractions.xml</DocumentationFile>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<OutputPath></OutputPath>
<DocumentationFile>.\obj\Release\$(NetTargetFramework)\Util.FileStorage.Abstractions.xml</DocumentationFile>
</PropertyGroup>

<ItemGroup>
<None Include="..\..\build\icon.jpg">
<Pack>True</Pack>
<Visible>False</Visible>
<PackagePath></PackagePath>
</None>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Util.Core\01-Util.Core.csproj" />
</ItemGroup>

</Project>
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ public FileSize( long size, FileSizeUnit unit = FileSizeUnit.Byte ) {
_size = GetSize( size, unit );
}

/// <summary>
/// 初始化文件大小
/// </summary>
/// <param name="size">文件大小</param>
/// <param name="unit">文件大小单位</param>
public FileSize( double size, FileSizeUnit unit = FileSizeUnit.Byte ) {
_size = Util.Helpers.Convert.ToLong( GetSize( size, unit ) );
}

/// <summary>
/// 获取文件大小
/// </summary>
Expand All @@ -34,6 +43,22 @@ private static long GetSize( long size, FileSizeUnit unit ) {
}
}

/// <summary>
/// 获取文件大小
/// </summary>
private static double GetSize( double size, FileSizeUnit unit ) {
switch( unit ) {
case FileSizeUnit.K:
return size * 1024;
case FileSizeUnit.M:
return size * 1024 * 1024;
case FileSizeUnit.G:
return size * 1024 * 1024 * 1024;
default:
return size;
}
}

/// <summary>
/// 文件字节长度
/// </summary>
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,9 @@ public void AddHeader( string key,string value ) {
public IDictionary<string, string> GetHeaders() {
return _headers;
}

/// <summary>
/// 文件大小限制,单位:字节
/// </summary>
public long SizeLimit { get; set; }
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
11 changes: 11 additions & 0 deletions src/Util.FileStorage.Abstractions/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
global using System;
global using System.Threading.Tasks;
global using System.Collections.Generic;
global using System.Threading;
global using System.IO;
global using System.ComponentModel;
global using System.Linq;
global using Microsoft.Extensions.Options;
global using Microsoft.Extensions.DependencyInjection;
global using Microsoft.Extensions.DependencyInjection.Extensions;
global using Util.Dependency;
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Util.FileStorage\01-Util.FileStorage.csproj" />
<ProjectReference Include="..\Util.FileStorage\02-Util.FileStorage.csproj" />
</ItemGroup>

</Project>
13 changes: 7 additions & 6 deletions src/Util.FileStorage.Aliyun/AliyunFileStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -442,16 +442,16 @@ protected async Task<ProcessedName> ProcessBucketName( FileStorageArgs args ) {
args.CheckNull( nameof( args ) );
var processedFileName = ProcessFileName( args );
var processedBucketName = await ProcessBucketName( args );
return await GenerateUploadUrlAsync( processedFileName, processedBucketName, cancellationToken );
return await GenerateUploadUrlAsync( processedFileName, processedBucketName, args.SizeLimit );
}

/// <summary>
/// 生成直传Url
/// </summary>
protected async Task<DirectUploadParam> GenerateUploadUrlAsync( ProcessedName fileName, ProcessedName bucketName, CancellationToken cancellationToken ) {
protected async Task<DirectUploadParam> GenerateUploadUrlAsync( ProcessedName fileName, ProcessedName bucketName, long sizeLimit ) {
await InitConfig();
var url = CreateGenerateUploadHost( bucketName.Name );
var data = await CreateGenerateUploadData( fileName, bucketName, cancellationToken );
var data = await CreateGenerateUploadData( fileName, bucketName, sizeLimit );
return new DirectUploadParam( fileName.Name, url, data, fileName.OriginalName, bucketName.Name );
}

Expand All @@ -472,20 +472,21 @@ protected string CreateGenerateUploadHost( string bucketName ) {
/// <summary>
/// 创建直传数据
/// </summary>
protected async Task<DirectUploadData> CreateGenerateUploadData( ProcessedName fileName, ProcessedName bucketName, CancellationToken cancellationToken ) {
var policy = await GetPostPolicy( bucketName );
protected async Task<DirectUploadData> CreateGenerateUploadData( ProcessedName fileName, ProcessedName bucketName, long sizeLimit ) {
var policy = await GetPostPolicy( bucketName, sizeLimit );
var signature = ComputeSignature( _config.AccessKeySecret, policy );
return new DirectUploadData( fileName.Name, policy, _config.AccessKeyId, signature );
}

/// <summary>
/// 获取Post策略
/// </summary>
protected virtual async Task<string> GetPostPolicy( ProcessedName bucketName ) {
protected virtual async Task<string> GetPostPolicy( ProcessedName bucketName,long sizeLimit ) {
var client = await GetClient();
var expiration = DateTime.Now.AddSeconds( _config.UploadUrlExpiration );
var policy = new PolicyConditions();
policy.AddConditionItem( "bucket", bucketName.Name );
policy.AddConditionItem( "content-length-range", 1,sizeLimit );
var postPolicy = client.GeneratePostPolicy( expiration, policy );
return Util.Helpers.Convert.ToBase64( postPolicy );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Util.FileStorage.Aliyun\03-Util.FileStorage.Aliyun.csproj" />
<ProjectReference Include="..\Util.FileStorage.Minio\02-Util.FileStorage.Minio.csproj" />
<ProjectReference Include="..\Util.FileStorage.Aliyun\04-Util.FileStorage.Aliyun.csproj" />
<ProjectReference Include="..\Util.FileStorage.Minio\03-Util.FileStorage.Minio.csproj" />
</ItemGroup>

</Project>
10 changes: 2 additions & 8 deletions src/Util.FileStorage.All/FileStoreFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ public class FileStoreFactory : IFileStoreFactory {
/// </summary>
private readonly IBucketNameProcessorFactory _bucketNameProcessorFactory;
/// <summary>
/// 文件扩展名检查器
/// </summary>
private readonly IFileExtensionInspector _inspector;
/// <summary>
/// Http操作
/// </summary>
private readonly IHttpClient _httpClient;
Expand All @@ -32,19 +28,17 @@ public class FileStoreFactory : IFileStoreFactory {
/// <param name="bucketNameProcessorFactory">存储桶名称处理器工厂</param>
/// <param name="httpClientFactory">Http客户端工厂</param>
/// <param name="httpClient">Http操作</param>
/// <param name="inspector">文件扩展名检查器</param>
public FileStoreFactory( IFileNameProcessorFactory fileNameProcessorFactory, IBucketNameProcessorFactory bucketNameProcessorFactory,
IHttpClientFactory httpClientFactory, IHttpClient httpClient, IFileExtensionInspector inspector ) {
IHttpClientFactory httpClientFactory, IHttpClient httpClient ) {
_fileNameProcessorFactory = fileNameProcessorFactory ?? throw new ArgumentNullException( nameof( fileNameProcessorFactory ) );
_bucketNameProcessorFactory = bucketNameProcessorFactory ?? throw new ArgumentNullException( nameof( bucketNameProcessorFactory ) );
_httpClientFactory = httpClientFactory ?? throw new ArgumentNullException( nameof( httpClientFactory ) );
_httpClient = httpClient ?? throw new ArgumentNullException( nameof( httpClient ) );
_inspector = inspector ?? throw new ArgumentNullException( nameof( inspector ) );
}

/// <inheritdoc />
public IFileStore Create( LocalStoreOptions options ) {
return new LocalFileStore( new LocalStoreConfigProvider( options ), _fileNameProcessorFactory, _inspector, _httpClient );
return new LocalFileStore( new LocalStoreConfigProvider( options ), _fileNameProcessorFactory, _httpClient );
}

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Util.FileStorage\01-Util.FileStorage.csproj" />
<ProjectReference Include="..\Util.FileStorage\02-Util.FileStorage.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

<ItemGroup>
<ProjectReference Include="..\Util.AspNetCore\08-Util.AspNetCore.csproj" />
<ProjectReference Include="..\Util.FileStorage.Abstractions\01-Util.FileStorage.Abstractions.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/// <summary>
/// 文件存储服务操作扩展
/// </summary>
public static class IFileStoreExtensions {
public static class FileStoreExtensions {
/// <summary>
/// 保存文件
/// </summary>
Expand Down
23 changes: 23 additions & 0 deletions src/Util.FileStorage/FileValidation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace Util.FileStorage;

/// <summary>
/// 文件验证操作
/// </summary>
public static class FileValidation {
/// <summary>
/// 扩展名是否有效
/// </summary>
/// <param name="fileName">文件名,范例: a.jpg</param>
/// <param name="accepts">接受的扩展名列表,以逗号分隔,范例: .jpg,.png,.gif</param>
public static bool IsValidExtension( string fileName, string accepts ) {
if( fileName.IsEmpty() )
return false;
if( accepts.IsEmpty() )
return true;
var extension = Path.GetExtension( fileName );
var list = accepts.Split( ',' ).Where( t => t.IsEmpty() == false ).ToList();
if ( list.Count == 0 )
return true;
return list.Any( type => type.TrimStart( '.' ) == extension.TrimStart( '.' ) );
}
}
Loading

0 comments on commit d9b8fee

Please sign in to comment.