mirror of https://github.com/abpframework/abp.git
9 changed files with 204 additions and 5 deletions
@ -0,0 +1,48 @@ |
|||||
|
using System.Threading; |
||||
|
using System.Threading.Tasks; |
||||
|
|
||||
|
namespace Volo.Abp.BlobStoring |
||||
|
{ |
||||
|
public static class BlobContainerManagerExtensions |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Gets a named container.
|
||||
|
/// </summary>
|
||||
|
/// <param name="blobContainerManager">The blob container manager</param>
|
||||
|
/// <param name="cancellationToken">Cancellation token</param>
|
||||
|
/// <returns>
|
||||
|
/// The container object.
|
||||
|
/// </returns>
|
||||
|
public static Task<IBlobContainer> GetAsync<TContainer>( |
||||
|
this IBlobContainerManager blobContainerManager, |
||||
|
CancellationToken cancellationToken = default |
||||
|
) |
||||
|
{ |
||||
|
return blobContainerManager.GetAsync( |
||||
|
BlobContainerNameAttribute.GetContainerName<TContainer>(), |
||||
|
cancellationToken |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
///
|
||||
|
/// </summary>
|
||||
|
/// <param name="blobContainerManager">The blob container manager</param>
|
||||
|
/// <param name="cancellationToken">Cancellation token</param>
|
||||
|
/// <typeparam name="TContainer">Type of the container</typeparam>
|
||||
|
/// <returns>
|
||||
|
/// Returns true if actually deleted the container.
|
||||
|
/// Returns false if the container with the given <typeparamref name="TContainer"/> type was not exists.
|
||||
|
/// </returns>
|
||||
|
public static Task<bool> DeleteAsync<TContainer>( |
||||
|
this IBlobContainerManager blobContainerManager, |
||||
|
CancellationToken cancellationToken = default |
||||
|
) |
||||
|
{ |
||||
|
return blobContainerManager.DeleteAsync( |
||||
|
BlobContainerNameAttribute.GetContainerName<TContainer>(), |
||||
|
cancellationToken |
||||
|
); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,41 @@ |
|||||
|
using System; |
||||
|
using System.Reflection; |
||||
|
using JetBrains.Annotations; |
||||
|
|
||||
|
namespace Volo.Abp.BlobStoring |
||||
|
{ |
||||
|
public class BlobContainerNameAttribute : Attribute |
||||
|
{ |
||||
|
[NotNull] |
||||
|
public string Name { get; } |
||||
|
|
||||
|
public BlobContainerNameAttribute([NotNull] string name) |
||||
|
{ |
||||
|
Check.NotNullOrWhiteSpace(name, nameof(name)); |
||||
|
|
||||
|
Name = name; |
||||
|
} |
||||
|
|
||||
|
public virtual string GetName(Type type) |
||||
|
{ |
||||
|
return Name; |
||||
|
} |
||||
|
|
||||
|
public static string GetContainerName<T>() |
||||
|
{ |
||||
|
return GetContainerName(typeof(T)); |
||||
|
} |
||||
|
|
||||
|
public static string GetContainerName(Type type) |
||||
|
{ |
||||
|
var nameAttribute = type.GetCustomAttribute<BlobContainerNameAttribute>(); |
||||
|
|
||||
|
if (nameAttribute == null) |
||||
|
{ |
||||
|
return type.FullName; |
||||
|
} |
||||
|
|
||||
|
return nameAttribute.GetName(type); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,35 @@ |
|||||
|
using System.Threading; |
||||
|
using System.Threading.Tasks; |
||||
|
|
||||
|
namespace Volo.Abp.BlobStoring |
||||
|
{ |
||||
|
public interface IBlobContainerManager |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Gets a named container.
|
||||
|
/// </summary>
|
||||
|
/// <param name="name">The name of the container</param>
|
||||
|
/// <param name="cancellationToken">Cancellation token</param>
|
||||
|
/// <returns>
|
||||
|
/// The container object.
|
||||
|
/// </returns>
|
||||
|
Task<IBlobContainer> GetAsync( |
||||
|
string name, |
||||
|
CancellationToken cancellationToken = default |
||||
|
); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Deletes a container.
|
||||
|
/// </summary>
|
||||
|
/// <param name="name">The name of the container</param>
|
||||
|
/// <param name="cancellationToken">Cancellation token</param>
|
||||
|
/// <returns>
|
||||
|
/// Returns true if actually deleted the container.
|
||||
|
/// Returns false if the container with the given <paramref name="name"/> was not exists.
|
||||
|
/// </returns>
|
||||
|
Task<bool> DeleteAsync( |
||||
|
string name, |
||||
|
CancellationToken cancellationToken = default |
||||
|
); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,16 @@ |
|||||
|
<Project Sdk="Microsoft.NET.Sdk"> |
||||
|
|
||||
|
<Import Project="..\..\..\common.test.props" /> |
||||
|
|
||||
|
<PropertyGroup> |
||||
|
<TargetFramework>netcoreapp3.1</TargetFramework> |
||||
|
<RootNamespace /> |
||||
|
</PropertyGroup> |
||||
|
|
||||
|
<ItemGroup> |
||||
|
<ProjectReference Include="..\AbpTestBase\AbpTestBase.csproj" /> |
||||
|
<ProjectReference Include="..\..\src\Volo.Abp.BlobStoring\Volo.Abp.BlobStoring.csproj" /> |
||||
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" /> |
||||
|
</ItemGroup> |
||||
|
|
||||
|
</Project> |
||||
@ -0,0 +1,9 @@ |
|||||
|
using Volo.Abp.Testing; |
||||
|
|
||||
|
namespace Volo.Abp.BlobStoring |
||||
|
{ |
||||
|
public abstract class AbpBlobStoringTestBase : AbpIntegratedTest<AbpBlobStoringTestModule> |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,13 @@ |
|||||
|
using Volo.Abp.Modularity; |
||||
|
|
||||
|
namespace Volo.Abp.BlobStoring |
||||
|
{ |
||||
|
[DependsOn( |
||||
|
typeof(AbpBlobStoringModule), |
||||
|
typeof(AbpTestBaseModule) |
||||
|
)] |
||||
|
public class AbpBlobStoringTestModule : AbpModule |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,35 @@ |
|||||
|
using Shouldly; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Volo.Abp.BlobStoring |
||||
|
{ |
||||
|
public class BlobContainerNameAttribute_Tests |
||||
|
{ |
||||
|
[Fact] |
||||
|
public void Should_Get_Specified_Name() |
||||
|
{ |
||||
|
BlobContainerNameAttribute |
||||
|
.GetContainerName<MyContainerType2>() |
||||
|
.ShouldBe("ContName2"); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_Get_Full_Class_Name_If_Not_Specified() |
||||
|
{ |
||||
|
BlobContainerNameAttribute |
||||
|
.GetContainerName<MyContainerType1>() |
||||
|
.ShouldBe(typeof(MyContainerType1).FullName); |
||||
|
} |
||||
|
|
||||
|
private class MyContainerType1 |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
|
||||
|
[BlobContainerName("ContName2")] |
||||
|
private class MyContainerType2 |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue