mirror of https://github.com/abpframework/abp.git
5 changed files with 155 additions and 0 deletions
@ -0,0 +1,26 @@ |
|||
using System; |
|||
using Volo.Abp.Data; |
|||
using Volo.Abp.Domain.Entities; |
|||
using Volo.Abp.MultiTenancy; |
|||
|
|||
namespace Volo.Abp.BlobStoring.Database |
|||
{ |
|||
public class Blob : AggregateRoot<Guid>, IMultiTenant |
|||
{ |
|||
public Guid ContainerId { get; set; } |
|||
|
|||
public Guid? TenantId { get; } |
|||
|
|||
public string Name { get; set; } |
|||
|
|||
public byte[] Content { get; set; } |
|||
|
|||
public Blob(Guid id, Guid containerId, string name, byte[] content, Guid? tenantId) : base(id) |
|||
{ |
|||
ContainerId = containerId; |
|||
Name = name; |
|||
Content = content; |
|||
TenantId = tenantId; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,11 @@ |
|||
using System; |
|||
using Volo.Abp.Domain.Entities; |
|||
using Volo.Abp.MultiTenancy; |
|||
|
|||
namespace Volo.Abp.BlobStoring.Database |
|||
{ |
|||
public class Container : AggregateRoot<Guid>, IMultiTenant |
|||
{ |
|||
public Guid? TenantId { get; } |
|||
} |
|||
} |
|||
@ -0,0 +1,87 @@ |
|||
using System; |
|||
using System.IO; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.BlobStoring.Database |
|||
{ |
|||
public class DatabaseBlobProvider : BlobProviderBase, ITransientDependency |
|||
{ |
|||
protected IBlobRepository BlobRepository { get; } |
|||
protected IContainerRepository ContainerRepository { get; } |
|||
|
|||
public DatabaseBlobProvider(IBlobRepository blobRepository, IContainerRepository containerRepository) |
|||
{ |
|||
BlobRepository = blobRepository; |
|||
ContainerRepository = containerRepository; |
|||
} |
|||
|
|||
public override async Task SaveAsync(BlobProviderSaveArgs args) |
|||
{ |
|||
var container = await ContainerRepository.GetContainerAsync(args.ContainerName); |
|||
|
|||
var blob = await BlobRepository.FindAsync(container.Id, args.BlobName, args.TenantId, args.CancellationToken); |
|||
|
|||
var content = await args.BlobStream.GetAllBytesAsync(args.CancellationToken); |
|||
|
|||
if (blob != null) |
|||
{ |
|||
if (!args.OverrideExisting) |
|||
{ |
|||
throw new BlobAlreadyExistsException($"Saving BLOB '{args.BlobName}' does already exists in the container '{args.ContainerName}'! Set {nameof(args.OverrideExisting)} if it should be overwritten."); |
|||
} |
|||
|
|||
blob.Content = content; |
|||
await BlobRepository.UpdateAsync(blob); |
|||
} |
|||
else |
|||
{ |
|||
blob = new Blob(Guid.NewGuid(), container.Id, args.BlobName, content, args.TenantId); |
|||
await BlobRepository.InsertAsync(blob); |
|||
} |
|||
} |
|||
|
|||
public override async Task<bool> DeleteAsync(BlobProviderDeleteArgs args) |
|||
{ |
|||
var container = await ContainerRepository.FindContainerAsync(args.ContainerName); |
|||
|
|||
if (container == null) |
|||
{ |
|||
return false; |
|||
} |
|||
|
|||
return await BlobRepository.DeleteAsync(container.Id, args.BlobName, args.TenantId, args.CancellationToken); |
|||
} |
|||
|
|||
public override async Task<bool> ExistsAsync(BlobProviderExistsArgs args) |
|||
{ |
|||
var container = await ContainerRepository.FindContainerAsync(args.ContainerName); |
|||
|
|||
if (container == null) |
|||
{ |
|||
return false; |
|||
} |
|||
|
|||
return await BlobRepository.ExistsAsync(container.Id, args.BlobName, args.TenantId, args.CancellationToken); |
|||
} |
|||
|
|||
public override async Task<Stream> GetOrNullAsync(BlobProviderGetArgs args) |
|||
{ |
|||
var container = await ContainerRepository.FindContainerAsync(args.ContainerName); |
|||
|
|||
if (container == null) |
|||
{ |
|||
return null; |
|||
} |
|||
|
|||
var blob = await BlobRepository.FindAsync(container.Id, args.BlobName, args.TenantId, args.CancellationToken); |
|||
|
|||
if (blob == null) |
|||
{ |
|||
return null; |
|||
} |
|||
|
|||
return new MemoryStream(blob.Content); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,17 @@ |
|||
using System; |
|||
using System.Threading; |
|||
using System.Threading.Tasks; |
|||
using JetBrains.Annotations; |
|||
using Volo.Abp.Domain.Repositories; |
|||
|
|||
namespace Volo.Abp.BlobStoring.Database |
|||
{ |
|||
public interface IBlobRepository : IBasicRepository<Blob, Guid> |
|||
{ |
|||
Task<Blob> FindAsync(Guid containerId, [NotNull] string blobName, Guid? tenantId = null, CancellationToken cancellationToken = default); |
|||
|
|||
Task<bool> ExistsAsync(Guid containerId, [NotNull] string blobName, Guid? tenantId = null, CancellationToken cancellationToken = default); |
|||
|
|||
Task<bool> DeleteAsync(Guid containerId, [NotNull] string blobName, Guid? tenantId = null, CancellationToken cancellationToken = default); |
|||
} |
|||
} |
|||
@ -0,0 +1,14 @@ |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using JetBrains.Annotations; |
|||
using Volo.Abp.Domain.Repositories; |
|||
|
|||
namespace Volo.Abp.BlobStoring.Database |
|||
{ |
|||
public interface IContainerRepository : IBasicRepository<Container, Guid> |
|||
{ |
|||
Task<Container> GetContainerAsync([NotNull] string name); |
|||
|
|||
Task<Container> FindContainerAsync([NotNull] string name); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue