Browse Source

Should not pass the tenantId to the blob provider

pull/4161/head
Halil İbrahim Kalkan 6 years ago
parent
commit
bfce4c2752
  1. 12
      framework/src/Volo.Abp.BlobStoring.FileSystem/Volo/Abp/BlobStoring/FileSystem/DefaultBlobFilePathCalculator.cs
  2. 113
      framework/src/Volo.Abp.BlobStoring/Volo/Abp/BlobStoring/BlobContainer.cs
  3. 7
      framework/src/Volo.Abp.BlobStoring/Volo/Abp/BlobStoring/BlobProviderArgs.cs
  4. 5
      framework/src/Volo.Abp.BlobStoring/Volo/Abp/BlobStoring/BlobProviderDeleteArgs.cs
  5. 5
      framework/src/Volo.Abp.BlobStoring/Volo/Abp/BlobStoring/BlobProviderExistsArgs.cs
  6. 5
      framework/src/Volo.Abp.BlobStoring/Volo/Abp/BlobStoring/BlobProviderGetArgs.cs
  7. 5
      framework/src/Volo.Abp.BlobStoring/Volo/Abp/BlobStoring/BlobProviderSaveArgs.cs
  8. 16
      framework/test/Volo.Abp.BlobStoring.FileSystem.Tests/Volo/Abp/BlobStoring/FileSystem/BlogFilePathCalculator_Tests.cs
  9. 26
      modules/blob-storing-database/src/Volo.Abp.BlobStoring.Database.Domain/Volo/Abp/BlobStoring/Database/DatabaseBlobProvider.cs
  10. 2
      modules/blob-storing-database/src/Volo.Abp.BlobStoring.Database.Domain/Volo/Abp/BlobStoring/Database/IDatabaseBlobContainerRepository.cs
  11. 6
      modules/blob-storing-database/src/Volo.Abp.BlobStoring.Database.Domain/Volo/Abp/BlobStoring/Database/IDatabaseBlobRepository.cs
  12. 5
      modules/blob-storing-database/src/Volo.Abp.BlobStoring.Database.EntityFrameworkCore/Volo/Abp/BlobStoring/Database/EntityFrameworkCore/EfCoreDatabaseBlobContainerRepository.cs
  13. 11
      modules/blob-storing-database/src/Volo.Abp.BlobStoring.Database.EntityFrameworkCore/Volo/Abp/BlobStoring/Database/EntityFrameworkCore/EfCoreDatabaseBlobRepository.cs
  14. 4
      modules/blob-storing-database/src/Volo.Abp.BlobStoring.Database.MongoDB/Volo/Abp/BlobStoring/Database/MongoDB/MongoDbDatabaseBlobContainerRepository.cs
  15. 14
      modules/blob-storing-database/src/Volo.Abp.BlobStoring.Database.MongoDB/Volo/Abp/BlobStoring/Database/MongoDB/MongoDbDatabaseBlobRepository.cs

12
framework/src/Volo.Abp.BlobStoring.FileSystem/Volo/Abp/BlobStoring/FileSystem/DefaultBlobFilePathCalculator.cs

@ -1,22 +1,30 @@
using System.IO;
using Volo.Abp.DependencyInjection;
using Volo.Abp.MultiTenancy;
namespace Volo.Abp.BlobStoring.FileSystem
{
public class DefaultBlobFilePathCalculator : IBlobFilePathCalculator, ITransientDependency
{
protected ICurrentTenant CurrentTenant { get; }
public DefaultBlobFilePathCalculator(ICurrentTenant currentTenant)
{
CurrentTenant = currentTenant;
}
public virtual string Calculate(BlobProviderArgs args)
{
var fileSystemConfiguration = args.Configuration.GetFileSystemConfiguration();
var blobPath = fileSystemConfiguration.BasePath;
if (args.TenantId == null)
if (CurrentTenant.Id == null)
{
blobPath = Path.Combine(blobPath, "host");
}
else
{
blobPath = Path.Combine(blobPath, "tenants", args.TenantId.Value.ToString("D"));
blobPath = Path.Combine(blobPath, "tenants", CurrentTenant.Id.Value.ToString("D"));
}
if (fileSystemConfiguration.AppendContainerNameToBasePath)

113
framework/src/Volo.Abp.BlobStoring/Volo/Abp/BlobStoring/BlobContainer.cs

@ -79,9 +79,9 @@ namespace Volo.Abp.BlobStoring
protected BlobContainerConfiguration Configuration { get; }
protected IBlobProvider Provider { get; }
protected ICurrentTenant CurrentTenant { get; }
protected ICancellationTokenProvider CancellationTokenProvider { get; }
public BlobContainer(
@ -98,100 +98,101 @@ namespace Volo.Abp.BlobStoring
CancellationTokenProvider = cancellationTokenProvider;
}
public virtual Task SaveAsync(
public virtual async Task SaveAsync(
string name,
Stream stream,
bool overrideExisting = false,
CancellationToken cancellationToken = default)
{
return Provider.SaveAsync(
new BlobProviderSaveArgs(
ContainerName,
Configuration,
name,
stream,
overrideExisting,
GetTenantIdOrNull(),
CancellationTokenProvider.FallbackToProvider(cancellationToken)
)
);
using (CurrentTenant.Change(GetTenantIdOrNull()))
{
await Provider.SaveAsync(
new BlobProviderSaveArgs(
ContainerName,
Configuration,
name,
stream,
overrideExisting,
CancellationTokenProvider.FallbackToProvider(cancellationToken)
)
);
}
}
public virtual Task<bool> DeleteAsync(
public virtual async Task<bool> DeleteAsync(
string name,
CancellationToken cancellationToken = default)
{
return Provider.DeleteAsync(
new BlobProviderDeleteArgs(
ContainerName,
Configuration,
name,
GetTenantIdOrNull(),
CancellationTokenProvider.FallbackToProvider(cancellationToken)
)
);
using (CurrentTenant.Change(GetTenantIdOrNull()))
{
return await Provider.DeleteAsync(
new BlobProviderDeleteArgs(
ContainerName,
Configuration,
name,
CancellationTokenProvider.FallbackToProvider(cancellationToken)
)
);
}
}
public virtual Task<bool> ExistsAsync(
public virtual async Task<bool> ExistsAsync(
string name,
CancellationToken cancellationToken = default)
{
return Provider.ExistsAsync(
new BlobProviderExistsArgs(
ContainerName,
Configuration,
name,
GetTenantIdOrNull(),
CancellationTokenProvider.FallbackToProvider(cancellationToken)
)
);
using (CurrentTenant.Change(GetTenantIdOrNull()))
{
return await Provider.ExistsAsync(
new BlobProviderExistsArgs(
ContainerName,
Configuration,
name,
CancellationTokenProvider.FallbackToProvider(cancellationToken)
)
);
}
}
public virtual async Task<Stream> GetAsync(
string name,
CancellationToken cancellationToken = default)
{
var stream = await Provider.GetOrNullAsync(
new BlobProviderGetArgs(
ContainerName,
Configuration,
name,
GetTenantIdOrNull(),
CancellationTokenProvider.FallbackToProvider(cancellationToken)
)
);
var stream = await GetOrNullAsync(name, cancellationToken);
if (stream == null)
{
//TODO: Consider to throw some type of "not found" exception and handle on the HTTP status side
throw new AbpException($"Could not found the requested BLOB '{name}' in the container '{ContainerName}'!");
throw new AbpException(
$"Could not found the requested BLOB '{name}' in the container '{ContainerName}'!");
}
return stream;
}
public virtual Task<Stream> GetOrNullAsync(
public virtual async Task<Stream> GetOrNullAsync(
string name,
CancellationToken cancellationToken = default)
{
return Provider.GetOrNullAsync(
new BlobProviderGetArgs(
ContainerName,
Configuration,
name,
GetTenantIdOrNull(),
CancellationTokenProvider.FallbackToProvider(cancellationToken)
)
);
using (CurrentTenant.Change(GetTenantIdOrNull()))
{
return await Provider.GetOrNullAsync(
new BlobProviderGetArgs(
ContainerName,
Configuration,
name,
CancellationTokenProvider.FallbackToProvider(cancellationToken)
)
);
}
}
protected virtual Guid? GetTenantIdOrNull()
{
if (!Configuration.IsMultiTenant)
{
return null;
}
return CurrentTenant.Id;
}
}

7
framework/src/Volo.Abp.BlobStoring/Volo/Abp/BlobStoring/BlobProviderArgs.cs

@ -1,5 +1,4 @@
using System;
using System.Threading;
using System.Threading;
using JetBrains.Annotations;
namespace Volo.Abp.BlobStoring
@ -16,20 +15,16 @@ namespace Volo.Abp.BlobStoring
public string BlobName { get; }
public CancellationToken CancellationToken { get; }
public Guid? TenantId { get; }
protected BlobProviderArgs(
[NotNull] string containerName,
[NotNull] BlobContainerConfiguration configuration,
[NotNull] string blobName,
[CanBeNull] Guid? tenantId = null,
CancellationToken cancellationToken = default)
{
ContainerName = Check.NotNullOrWhiteSpace(containerName, nameof(containerName));
Configuration = Check.NotNull(configuration, nameof(configuration));
BlobName = Check.NotNullOrWhiteSpace(blobName, nameof(blobName));
TenantId = tenantId;
CancellationToken = cancellationToken;
}
}

5
framework/src/Volo.Abp.BlobStoring/Volo/Abp/BlobStoring/BlobProviderDeleteArgs.cs

@ -1,5 +1,4 @@
using System;
using System.Threading;
using System.Threading;
using JetBrains.Annotations;
namespace Volo.Abp.BlobStoring
@ -10,13 +9,11 @@ namespace Volo.Abp.BlobStoring
[NotNull] string containerName,
[NotNull] BlobContainerConfiguration configuration,
[NotNull] string blobName,
[CanBeNull] Guid? tenantId = null,
CancellationToken cancellationToken = default)
: base(
containerName,
configuration,
blobName,
tenantId,
cancellationToken)
{
}

5
framework/src/Volo.Abp.BlobStoring/Volo/Abp/BlobStoring/BlobProviderExistsArgs.cs

@ -1,5 +1,4 @@
using System;
using System.Threading;
using System.Threading;
using JetBrains.Annotations;
namespace Volo.Abp.BlobStoring
@ -10,13 +9,11 @@ namespace Volo.Abp.BlobStoring
[NotNull] string containerName,
[NotNull] BlobContainerConfiguration configuration,
[NotNull] string blobName,
[CanBeNull] Guid? tenantId = null,
CancellationToken cancellationToken = default)
: base(
containerName,
configuration,
blobName,
tenantId,
cancellationToken)
{
}

5
framework/src/Volo.Abp.BlobStoring/Volo/Abp/BlobStoring/BlobProviderGetArgs.cs

@ -1,5 +1,4 @@
using System;
using System.Threading;
using System.Threading;
using JetBrains.Annotations;
namespace Volo.Abp.BlobStoring
@ -10,13 +9,11 @@ namespace Volo.Abp.BlobStoring
[NotNull] string containerName,
[NotNull] BlobContainerConfiguration configuration,
[NotNull] string blobName,
[CanBeNull] Guid? tenantId = null,
CancellationToken cancellationToken = default)
: base(
containerName,
configuration,
blobName,
tenantId,
cancellationToken)
{
}

5
framework/src/Volo.Abp.BlobStoring/Volo/Abp/BlobStoring/BlobProviderSaveArgs.cs

@ -1,5 +1,4 @@
using System;
using System.IO;
using System.IO;
using System.Threading;
using JetBrains.Annotations;
@ -18,13 +17,11 @@ namespace Volo.Abp.BlobStoring
[NotNull] string blobName,
[NotNull] Stream blobStream,
bool overrideExisting = false,
[CanBeNull] Guid? tenantId = null,
CancellationToken cancellationToken = default)
: base(
containerName,
configuration,
blobName,
tenantId,
cancellationToken)
{
BlobStream = Check.NotNull(blobStream, nameof(blobStream));

16
framework/test/Volo.Abp.BlobStoring.FileSystem.Tests/Volo/Abp/BlobStoring/FileSystem/BlogFilePathCalculator_Tests.cs

@ -1,6 +1,7 @@
using System;
using System.IO;
using Shouldly;
using Volo.Abp.MultiTenancy;
using Xunit;
namespace Volo.Abp.BlobStoring.FileSystem
@ -8,10 +9,12 @@ namespace Volo.Abp.BlobStoring.FileSystem
public class BlogFilePathCalculator_Tests : AbpBlobStoringFileSystemTestBase
{
private readonly IBlobFilePathCalculator _calculator;
private readonly ICurrentTenant _currentTenant;
public BlogFilePathCalculator_Tests()
{
_calculator = GetRequiredService<IBlobFilePathCalculator>();
_currentTenant = GetRequiredService<ICurrentTenant>();
}
[Fact]
@ -30,9 +33,12 @@ namespace Volo.Abp.BlobStoring.FileSystem
var separator = Path.DirectorySeparatorChar;
var tenantId = Guid.NewGuid();
_calculator.Calculate(
GetArgs($"C:{separator}my-files", "my-container", "my-blob", tenantId: tenantId)
).ShouldBe($"C:{separator}my-files{separator}tenants{separator}{tenantId:D}{separator}my-container{separator}my-blob");
using (_currentTenant.Change(tenantId))
{
_calculator.Calculate(
GetArgs($"C:{separator}my-files", "my-container", "my-blob")
).ShouldBe($"C:{separator}my-files{separator}tenants{separator}{tenantId:D}{separator}my-container{separator}my-blob");
}
}
[Fact]
@ -49,7 +55,6 @@ namespace Volo.Abp.BlobStoring.FileSystem
string basePath,
string containerName,
string blobName,
Guid? tenantId = null,
bool? appendContainerNameToBasePath = null)
{
return new BlobProviderGetArgs(
@ -63,8 +68,7 @@ namespace Volo.Abp.BlobStoring.FileSystem
fs.AppendContainerNameToBasePath = appendContainerNameToBasePath.Value;
}
}),
blobName,
tenantId
blobName
);
}
}

26
modules/blob-storing-database/src/Volo.Abp.BlobStoring.Database.Domain/Volo/Abp/BlobStoring/Database/DatabaseBlobProvider.cs

@ -1,5 +1,4 @@
using System;
using System.IO;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
@ -25,9 +24,9 @@ namespace Volo.Abp.BlobStoring.Database
public override async Task SaveAsync(BlobProviderSaveArgs args)
{
var container = await GetOrCreateContainerAsync(args.ContainerName, args.TenantId, args.CancellationToken);
var container = await GetOrCreateContainerAsync(args.ContainerName, args.CancellationToken);
var blob = await DatabaseBlobRepository.FindAsync(container.Id, args.BlobName, args.TenantId,
var blob = await DatabaseBlobRepository.FindAsync(container.Id, args.BlobName,
args.CancellationToken);
var content = await args.BlobStream.GetAllBytesAsync(args.CancellationToken);
@ -45,7 +44,7 @@ namespace Volo.Abp.BlobStoring.Database
}
else
{
blob = new DatabaseBlob(GuidGenerator.Create(), container.Id, args.BlobName, content, args.TenantId);
blob = new DatabaseBlob(GuidGenerator.Create(), container.Id, args.BlobName, content);
await DatabaseBlobRepository.InsertAsync(blob);
}
}
@ -53,7 +52,7 @@ namespace Volo.Abp.BlobStoring.Database
public override async Task<bool> DeleteAsync(BlobProviderDeleteArgs args)
{
var container =
await DatabaseBlobContainerRepository.FindAsync(args.ContainerName, args.TenantId,
await DatabaseBlobContainerRepository.FindAsync(args.ContainerName,
args.CancellationToken);
if (container == null)
@ -61,14 +60,14 @@ namespace Volo.Abp.BlobStoring.Database
return false;
}
return await DatabaseBlobRepository.DeleteAsync(container.Id, args.BlobName, args.TenantId,
return await DatabaseBlobRepository.DeleteAsync(container.Id, args.BlobName,
args.CancellationToken);
}
public override async Task<bool> ExistsAsync(BlobProviderExistsArgs args)
{
var container =
await DatabaseBlobContainerRepository.FindAsync(args.ContainerName, args.TenantId,
await DatabaseBlobContainerRepository.FindAsync(args.ContainerName,
args.CancellationToken);
if (container == null)
@ -76,14 +75,14 @@ namespace Volo.Abp.BlobStoring.Database
return false;
}
return await DatabaseBlobRepository.ExistsAsync(container.Id, args.BlobName, args.TenantId,
return await DatabaseBlobRepository.ExistsAsync(container.Id, args.BlobName,
args.CancellationToken);
}
public override async Task<Stream> GetOrNullAsync(BlobProviderGetArgs args)
{
var container =
await DatabaseBlobContainerRepository.FindAsync(args.ContainerName, args.TenantId,
await DatabaseBlobContainerRepository.FindAsync(args.ContainerName,
args.CancellationToken);
if (container == null)
@ -91,7 +90,7 @@ namespace Volo.Abp.BlobStoring.Database
return null;
}
var blob = await DatabaseBlobRepository.FindAsync(container.Id, args.BlobName, args.TenantId,
var blob = await DatabaseBlobRepository.FindAsync(container.Id, args.BlobName,
args.CancellationToken);
if (blob == null)
@ -104,16 +103,15 @@ namespace Volo.Abp.BlobStoring.Database
protected virtual async Task<DatabaseBlobContainer> GetOrCreateContainerAsync(
string name,
Guid? tenantId = null,
CancellationToken cancellationToken = default)
{
var container = await DatabaseBlobContainerRepository.FindAsync(name, tenantId, cancellationToken);
var container = await DatabaseBlobContainerRepository.FindAsync(name, cancellationToken);
if (container != null)
{
return container;
}
container = new DatabaseBlobContainer(GuidGenerator.Create(), name, tenantId);
container = new DatabaseBlobContainer(GuidGenerator.Create(), name);
await DatabaseBlobContainerRepository.InsertAsync(container, cancellationToken: cancellationToken);
return container;

2
modules/blob-storing-database/src/Volo.Abp.BlobStoring.Database.Domain/Volo/Abp/BlobStoring/Database/IDatabaseBlobContainerRepository.cs

@ -8,6 +8,6 @@ namespace Volo.Abp.BlobStoring.Database
{
public interface IDatabaseBlobContainerRepository : IBasicRepository<DatabaseBlobContainer, Guid>
{
Task<DatabaseBlobContainer> FindAsync([NotNull] string name, Guid? tenantId = null, CancellationToken cancellationToken = default);
Task<DatabaseBlobContainer> FindAsync([NotNull] string name, CancellationToken cancellationToken = default);
}
}

6
modules/blob-storing-database/src/Volo.Abp.BlobStoring.Database.Domain/Volo/Abp/BlobStoring/Database/IDatabaseBlobRepository.cs

@ -8,10 +8,10 @@ namespace Volo.Abp.BlobStoring.Database
{
public interface IDatabaseBlobRepository : IBasicRepository<DatabaseBlob, Guid>
{
Task<DatabaseBlob> FindAsync(Guid containerId, [NotNull] string name, Guid? tenantId = null, CancellationToken cancellationToken = default);
Task<DatabaseBlob> FindAsync(Guid containerId, [NotNull] string name, CancellationToken cancellationToken = default);
Task<bool> ExistsAsync(Guid containerId, [NotNull] string name, Guid? tenantId = null, CancellationToken cancellationToken = default);
Task<bool> ExistsAsync(Guid containerId, [NotNull] string name, CancellationToken cancellationToken = default);
Task<bool> DeleteAsync(Guid containerId, [NotNull] string name, Guid? tenantId = null, CancellationToken cancellationToken = default);
Task<bool> DeleteAsync(Guid containerId, [NotNull] string name, CancellationToken cancellationToken = default);
}
}

5
modules/blob-storing-database/src/Volo.Abp.BlobStoring.Database.EntityFrameworkCore/Volo/Abp/BlobStoring/Database/EntityFrameworkCore/EfCoreDatabaseBlobContainerRepository.cs

@ -15,10 +15,9 @@ namespace Volo.Abp.BlobStoring.Database.EntityFrameworkCore
{
}
public virtual async Task<DatabaseBlobContainer> FindAsync(string name, Guid? tenantId = null, CancellationToken cancellationToken = default)
public virtual async Task<DatabaseBlobContainer> FindAsync(string name, CancellationToken cancellationToken = default)
{
return await DbSet.WhereIf(tenantId != null, x => x.TenantId == tenantId)
.FirstOrDefaultAsync(x => x.Name == name, GetCancellationToken(cancellationToken));
return await DbSet.FirstOrDefaultAsync(x => x.Name == name, GetCancellationToken(cancellationToken));
}
}
}

11
modules/blob-storing-database/src/Volo.Abp.BlobStoring.Database.EntityFrameworkCore/Volo/Abp/BlobStoring/Database/EntityFrameworkCore/EfCoreDatabaseBlobRepository.cs

@ -17,11 +17,10 @@ namespace Volo.Abp.BlobStoring.Database.EntityFrameworkCore
public virtual async Task<DatabaseBlob> FindAsync(
Guid containerId,
string name,
Guid? tenantId = null,
CancellationToken cancellationToken = default)
{
return await DbSet.FirstOrDefaultAsync(
x => x.ContainerId == containerId && x.Name == name && x.TenantId == tenantId,
x => x.ContainerId == containerId && x.Name == name,
GetCancellationToken(cancellationToken)
);
}
@ -29,23 +28,19 @@ namespace Volo.Abp.BlobStoring.Database.EntityFrameworkCore
public virtual async Task<bool> ExistsAsync(
Guid containerId,
string name,
Guid? tenantId = null,
CancellationToken cancellationToken = default)
{
return await DbSet.AnyAsync(
x => x.ContainerId == containerId &&
x.Name == name &&
x.TenantId == tenantId,
x => x.ContainerId == containerId && x.Name == name,
GetCancellationToken(cancellationToken));
}
public virtual async Task<bool> DeleteAsync(
Guid containerId,
string name,
Guid? tenantId = null,
CancellationToken cancellationToken = default)
{
var blob = await FindAsync(containerId, name, tenantId, cancellationToken);
var blob = await FindAsync(containerId, name, cancellationToken);
if (blob == null)
{
return false;

4
modules/blob-storing-database/src/Volo.Abp.BlobStoring.Database.MongoDB/Volo/Abp/BlobStoring/Database/MongoDB/MongoDbDatabaseBlobContainerRepository.cs

@ -13,9 +13,9 @@ namespace Volo.Abp.BlobStoring.Database.MongoDB
{
}
public virtual async Task<DatabaseBlobContainer> FindAsync(string name, Guid? tenantId = null, CancellationToken cancellationToken = default)
public virtual async Task<DatabaseBlobContainer> FindAsync(string name, CancellationToken cancellationToken = default)
{
return await base.FindAsync(x => x.Name == name && x.TenantId == tenantId, cancellationToken: GetCancellationToken(cancellationToken));
return await base.FindAsync(x => x.Name == name, cancellationToken: GetCancellationToken(cancellationToken));
}
}
}

14
modules/blob-storing-database/src/Volo.Abp.BlobStoring.Database.MongoDB/Volo/Abp/BlobStoring/Database/MongoDB/MongoDbDatabaseBlobRepository.cs

@ -13,27 +13,25 @@ namespace Volo.Abp.BlobStoring.Database.MongoDB
{
}
public virtual async Task<DatabaseBlob> FindAsync(Guid containerId, string name, Guid? tenantId = null, CancellationToken cancellationToken = default)
public virtual async Task<DatabaseBlob> FindAsync(Guid containerId, string name, CancellationToken cancellationToken = default)
{
return await GetMongoQueryable().FirstOrDefaultAsync(
x => x.ContainerId == containerId &&
x.Name == name &&
x.TenantId == tenantId,
x.Name == name,
GetCancellationToken(cancellationToken));
}
public virtual async Task<bool> ExistsAsync(Guid containerId, string name, Guid? tenantId = null, CancellationToken cancellationToken = default)
public virtual async Task<bool> ExistsAsync(Guid containerId, string name, CancellationToken cancellationToken = default)
{
return await GetMongoQueryable().AnyAsync(
x => x.ContainerId == containerId &&
x.Name == name &&
x.TenantId == tenantId,
x.Name == name,
GetCancellationToken(cancellationToken));
}
public virtual async Task<bool> DeleteAsync(Guid containerId, string name, Guid? tenantId = null, CancellationToken cancellationToken = default)
public virtual async Task<bool> DeleteAsync(Guid containerId, string name, CancellationToken cancellationToken = default)
{
var blob = await FindAsync(containerId, name, tenantId, cancellationToken);
var blob = await FindAsync(containerId, name, cancellationToken);
if (blob == null)
{

Loading…
Cancel
Save