From 3a7a3fea009141ad1314b6921acb738572da7bbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Tue, 26 May 2020 12:39:12 +0300 Subject: [PATCH] Added tests. --- .../DefaultBlogFilePathCalculator.cs | 32 ++++++++++ .../FileSystem/FileSystemBlobProvider.cs | 39 ++++-------- .../FileSystemBlobProviderConfiguration.cs | 2 +- .../FileSystem/IBlogFilePathCalculator.cs | 7 +++ .../BlogFilePathCalculator_Tests.cs | 60 +++++++++++++++++++ 5 files changed, 111 insertions(+), 29 deletions(-) create mode 100644 framework/src/Volo.Abp.BlobStoring.FileSystem/Volo/Abp/BlobStoring/FileSystem/DefaultBlogFilePathCalculator.cs create mode 100644 framework/src/Volo.Abp.BlobStoring.FileSystem/Volo/Abp/BlobStoring/FileSystem/IBlogFilePathCalculator.cs create mode 100644 framework/test/Volo.Abp.BlobStoring.FileSystem.Tests/Volo/Abp/BlobStoring/FileSystem/BlogFilePathCalculator_Tests.cs diff --git a/framework/src/Volo.Abp.BlobStoring.FileSystem/Volo/Abp/BlobStoring/FileSystem/DefaultBlogFilePathCalculator.cs b/framework/src/Volo.Abp.BlobStoring.FileSystem/Volo/Abp/BlobStoring/FileSystem/DefaultBlogFilePathCalculator.cs new file mode 100644 index 0000000000..62c2585375 --- /dev/null +++ b/framework/src/Volo.Abp.BlobStoring.FileSystem/Volo/Abp/BlobStoring/FileSystem/DefaultBlogFilePathCalculator.cs @@ -0,0 +1,32 @@ +using System.IO; +using Volo.Abp.DependencyInjection; + +namespace Volo.Abp.BlobStoring.FileSystem +{ + public class DefaultBlogFilePathCalculator : IBlogFilePathCalculator, ITransientDependency + { + public virtual string Calculate(BlobProviderArgs args) + { + var fileSystemConfiguration = args.Configuration.GetFileSystemConfiguration(); + var blobPath = fileSystemConfiguration.BasePath; + + if (args.TenantId == null) + { + blobPath = Path.Combine(blobPath, "host"); + } + else + { + blobPath = Path.Combine(blobPath, "tenants", args.TenantId.Value.ToString("D")); + } + + if (fileSystemConfiguration.AppendContainerNameToBasePath) + { + blobPath = Path.Combine(blobPath, args.ContainerName); + } + + blobPath = Path.Combine(blobPath, args.BlobName); + + return blobPath; + } + } +} \ No newline at end of file diff --git a/framework/src/Volo.Abp.BlobStoring.FileSystem/Volo/Abp/BlobStoring/FileSystem/FileSystemBlobProvider.cs b/framework/src/Volo.Abp.BlobStoring.FileSystem/Volo/Abp/BlobStoring/FileSystem/FileSystemBlobProvider.cs index 4de039982f..b927bdbf47 100644 --- a/framework/src/Volo.Abp.BlobStoring.FileSystem/Volo/Abp/BlobStoring/FileSystem/FileSystemBlobProvider.cs +++ b/framework/src/Volo.Abp.BlobStoring.FileSystem/Volo/Abp/BlobStoring/FileSystem/FileSystemBlobProvider.cs @@ -9,9 +9,16 @@ namespace Volo.Abp.BlobStoring.FileSystem public class FileSystemBlobProvider : BlobProviderBase, ITransientDependency { + protected IBlogFilePathCalculator FilePathCalculator { get; } + + public FileSystemBlobProvider(IBlogFilePathCalculator filePathCalculator) + { + FilePathCalculator = filePathCalculator; + } + public override async Task SaveAsync(BlobProviderSaveArgs args) { - var filePath = CalculateBlobFilePath(args); + var filePath = FilePathCalculator.Calculate(args); DirectoryHelper.CreateIfNotExists(Path.GetDirectoryName(filePath)); @@ -35,21 +42,21 @@ namespace Volo.Abp.BlobStoring.FileSystem public override Task DeleteAsync(BlobProviderDeleteArgs args) { - var filePath = CalculateBlobFilePath(args); + var filePath = FilePathCalculator.Calculate(args); return Task.FromResult(FileHelper.DeleteIfExists(filePath)); } public override Task ExistsAsync(BlobProviderExistsArgs args) { - var filePath = CalculateBlobFilePath(args); + var filePath = FilePathCalculator.Calculate(args); return Task.FromResult(File.Exists(filePath)); } public override Task GetOrNullAsync(BlobProviderGetArgs args) { - var filePath = CalculateBlobFilePath(args); + var filePath = FilePathCalculator.Calculate(args); if (!File.Exists(filePath)) { @@ -58,29 +65,5 @@ namespace Volo.Abp.BlobStoring.FileSystem return Task.FromResult(File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)); } - - protected virtual string CalculateBlobFilePath(BlobProviderArgs args) - { - var fileSystemConfiguration = args.Configuration.GetFileSystemConfiguration(); - var blobPath = fileSystemConfiguration.BasePath; - - if (args.TenantId == null) - { - blobPath = Path.Combine(blobPath, "host"); - } - else - { - blobPath = Path.Combine(blobPath, "tenants", args.TenantId.Value.ToString("D")); - } - - if (fileSystemConfiguration.AppendContainerNameToBasePath) - { - blobPath = Path.Combine(blobPath, args.ContainerName); - } - - blobPath = Path.Combine(blobPath, args.BlobName); - - return blobPath; - } } } \ No newline at end of file diff --git a/framework/src/Volo.Abp.BlobStoring.FileSystem/Volo/Abp/BlobStoring/FileSystem/FileSystemBlobProviderConfiguration.cs b/framework/src/Volo.Abp.BlobStoring.FileSystem/Volo/Abp/BlobStoring/FileSystem/FileSystemBlobProviderConfiguration.cs index 442744c4f3..4d4a786d16 100644 --- a/framework/src/Volo.Abp.BlobStoring.FileSystem/Volo/Abp/BlobStoring/FileSystem/FileSystemBlobProviderConfiguration.cs +++ b/framework/src/Volo.Abp.BlobStoring.FileSystem/Volo/Abp/BlobStoring/FileSystem/FileSystemBlobProviderConfiguration.cs @@ -15,7 +15,7 @@ public bool AppendContainerNameToBasePath { get => _containerConfiguration.GetConfigurationOrDefault(FileSystemBlobProviderConfigurationNames.AppendContainerNameToBasePath, true); - set => _containerConfiguration.SetConfiguration(FileSystemBlobProviderConfigurationNames.BasePath, value); + set => _containerConfiguration.SetConfiguration(FileSystemBlobProviderConfigurationNames.AppendContainerNameToBasePath, value); } private readonly BlobContainerConfiguration _containerConfiguration; diff --git a/framework/src/Volo.Abp.BlobStoring.FileSystem/Volo/Abp/BlobStoring/FileSystem/IBlogFilePathCalculator.cs b/framework/src/Volo.Abp.BlobStoring.FileSystem/Volo/Abp/BlobStoring/FileSystem/IBlogFilePathCalculator.cs new file mode 100644 index 0000000000..9e4ec4713a --- /dev/null +++ b/framework/src/Volo.Abp.BlobStoring.FileSystem/Volo/Abp/BlobStoring/FileSystem/IBlogFilePathCalculator.cs @@ -0,0 +1,7 @@ +namespace Volo.Abp.BlobStoring.FileSystem +{ + public interface IBlogFilePathCalculator + { + string Calculate(BlobProviderArgs args); + } +} \ No newline at end of file diff --git a/framework/test/Volo.Abp.BlobStoring.FileSystem.Tests/Volo/Abp/BlobStoring/FileSystem/BlogFilePathCalculator_Tests.cs b/framework/test/Volo.Abp.BlobStoring.FileSystem.Tests/Volo/Abp/BlobStoring/FileSystem/BlogFilePathCalculator_Tests.cs new file mode 100644 index 0000000000..a8a02fb7af --- /dev/null +++ b/framework/test/Volo.Abp.BlobStoring.FileSystem.Tests/Volo/Abp/BlobStoring/FileSystem/BlogFilePathCalculator_Tests.cs @@ -0,0 +1,60 @@ +using System; +using System.IO; +using Shouldly; +using Xunit; + +namespace Volo.Abp.BlobStoring.FileSystem +{ + public class BlogFilePathCalculator_Tests : AbpBlobStoringFileSystemTestBase + { + private readonly IBlogFilePathCalculator _calculator; + + public BlogFilePathCalculator_Tests() + { + _calculator = GetRequiredService(); + } + + [Fact] + public void Default_Settings() + { + var separator = Path.DirectorySeparatorChar; + + _calculator.Calculate( + GetArgs($"C:{separator}my-files","my-container","my-blob") + ).ShouldBe($"C:{separator}my-files{separator}host{separator}my-container{separator}my-blob"); + } + + [Fact] + public void AppendContainerNameToBasePath_Set_To_False() + { + var separator = Path.DirectorySeparatorChar; + + _calculator.Calculate( + GetArgs($"C:{separator}my-files","my-container","my-blob", appendContainerNameToBasePath: false) + ).ShouldBe($"C:{separator}my-files{separator}host{separator}my-blob"); + } + + private BlobProviderArgs GetArgs( + string basePath, + string containerName, + string blobName, + Guid? tenantId = null, + bool? appendContainerNameToBasePath = null) + { + return new BlobProviderGetArgs( + containerName, + new BlobContainerConfiguration() + .UseFileSystem(fs => + { + fs.BasePath = basePath; + if (appendContainerNameToBasePath.HasValue) + { + fs.AppendContainerNameToBasePath = appendContainerNameToBasePath.Value; + } + }), + blobName, + tenantId + ); + } + } +} \ No newline at end of file