mirror of https://github.com/abpframework/abp.git
16 changed files with 226 additions and 15 deletions
@ -0,0 +1,15 @@ |
|||
using JetBrains.Annotations; |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace Volo.CmsKit.MediaDescriptors |
|||
{ |
|||
public class CmsKitMediaOptions |
|||
{ |
|||
[NotNull] |
|||
public List<MediaDescriptorDefinition> EntityTypes { get; } = new(); |
|||
} |
|||
} |
|||
@ -0,0 +1,43 @@ |
|||
using JetBrains.Annotations; |
|||
using Microsoft.Extensions.Options; |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp; |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.CmsKit.MediaDescriptors |
|||
{ |
|||
public class DefaultMediaDescriptorDefinitionStore : IMediaDescriptorDefinitionStore, ITransientDependency |
|||
{ |
|||
protected CmsKitMediaOptions Options { get; } |
|||
|
|||
public DefaultMediaDescriptorDefinitionStore(IOptions<CmsKitMediaOptions> options) |
|||
{ |
|||
Options = options.Value; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets single <see cref="MediaDescriptorDefinition"/> by entityType.
|
|||
/// </summary>
|
|||
/// <param name="entityType">EntityType to get definition.</param>
|
|||
/// <exception cref="EntityCantHaveMediaException">Thrown when EntityType is not configured as taggable.</exception>
|
|||
/// <exception cref="InvalidOperationException">More than one element satisfies the condition in predicate.</exception>
|
|||
public Task<MediaDescriptorDefinition> GetDefinitionAsync([NotNull] string entityType) |
|||
{ |
|||
Check.NotNullOrWhiteSpace(entityType, nameof(entityType)); |
|||
|
|||
var result = Options.EntityTypes.SingleOrDefault(x => x.EntityType == entityType) ?? |
|||
throw new EntityCantHaveMediaException(entityType); |
|||
|
|||
return Task.FromResult(result); |
|||
} |
|||
|
|||
public Task<bool> IsDefinedAsync([NotNull] string entityType) |
|||
{ |
|||
return Task.Run(() => Options.EntityTypes.Any(a => a.EntityType == entityType)); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,21 @@ |
|||
using System.Runtime.Serialization; |
|||
using Volo.Abp; |
|||
|
|||
namespace Volo.CmsKit.MediaDescriptors |
|||
{ |
|||
public class EntityCantHaveMediaException : BusinessException |
|||
{ |
|||
public EntityCantHaveMediaException(SerializationInfo serializationInfo, StreamingContext context) : base(serializationInfo, context) |
|||
{ |
|||
} |
|||
|
|||
public EntityCantHaveMediaException(string entityType) |
|||
{ |
|||
EntityType = entityType; |
|||
Code = CmsKitErrorCodes.MediaDescriptors.EntityTypeDoesntExist; |
|||
WithData(nameof(entityType), EntityType); |
|||
} |
|||
|
|||
public string EntityType { get; } |
|||
} |
|||
} |
|||
@ -0,0 +1,12 @@ |
|||
using JetBrains.Annotations; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace Volo.CmsKit.MediaDescriptors |
|||
{ |
|||
public interface IMediaDescriptorDefinitionStore |
|||
{ |
|||
Task<bool> IsDefinedAsync([NotNull] string entityType); |
|||
|
|||
Task<MediaDescriptorDefinition> GetDefinitionAsync([NotNull] string entityType); |
|||
} |
|||
} |
|||
@ -0,0 +1,18 @@ |
|||
using JetBrains.Annotations; |
|||
|
|||
namespace Volo.CmsKit.MediaDescriptors |
|||
{ |
|||
public class MediaDescriptorDefinition : PolicySpecifiedDefinition |
|||
{ |
|||
public MediaDescriptorDefinition( |
|||
[NotNull] string entityType, |
|||
[CanBeNull] string createPolicy = null, |
|||
[CanBeNull] string updatePolicy = null, |
|||
[CanBeNull] string deletePolicy = null) : base(entityType, |
|||
createPolicy, |
|||
updatePolicy, |
|||
deletePolicy) |
|||
{ |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,36 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.Domain.Services; |
|||
using Volo.CmsKit.MediaDescriptors; |
|||
|
|||
namespace Volo.CmsKit.MediaDescriptors |
|||
{ |
|||
public class MediaDescriptorManager : DomainService |
|||
{ |
|||
protected IMediaDescriptorDefinitionStore MediaDescriptorDefinitionStore { get; } |
|||
|
|||
public MediaDescriptorManager(IMediaDescriptorDefinitionStore mediaDescriptorDefinitionStore) |
|||
{ |
|||
MediaDescriptorDefinitionStore = mediaDescriptorDefinitionStore; |
|||
} |
|||
|
|||
public virtual async Task<MediaDescriptor> Create(string entityType, string name, string mimeType, long size) |
|||
{ |
|||
if(!await MediaDescriptorDefinitionStore.IsDefinedAsync(entityType)) |
|||
{ |
|||
throw new EntityCantHaveMediaException(entityType); |
|||
} |
|||
|
|||
return new MediaDescriptor( |
|||
GuidGenerator.Create(), |
|||
entityType, |
|||
name, |
|||
mimeType, |
|||
size, |
|||
CurrentTenant.Id); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue