Skip to content

Commit

Permalink
Upgrade version, added initial work on DynamicsValue/fake-xrm-easy#157
Browse files Browse the repository at this point in the history
  • Loading branch information
jordimontana82 committed Aug 1, 2024
1 parent 5ae9175 commit a434347
Show file tree
Hide file tree
Showing 14 changed files with 10,566 additions and 37 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
## [2.5.2]
## [2.6.0]

### Added

- Added default max file size for file and image uploads - https://github.com/DynamicsValue/fake-xrm-easy/issues/157

### Changed

Expand Down
2 changes: 1 addition & 1 deletion src/FakeXrmEasy.Core/FakeXrmEasy.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<TargetFrameworks Condition="'$(Configuration)'=='FAKE_XRM_EASY_2013'">net452</TargetFrameworks>
<TargetFrameworks Condition="'$(Configuration)'=='FAKE_XRM_EASY'">net452</TargetFrameworks>
<PackageId>FakeXrmEasy.Core</PackageId>
<VersionPrefix>2.5.2</VersionPrefix>
<VersionPrefix>2.6.0</VersionPrefix>
<Authors>Jordi Montaña</Authors>
<Company>Dynamics Value</Company>
<Title>FakeXrmEasy Core</Title>
Expand Down
54 changes: 54 additions & 0 deletions src/FakeXrmEasy.Core/FileStorage/FileStorageSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
namespace FakeXrmEasy.Core.FileStorage
{
public interface IFileStorageSettings

Check warning on line 3 in src/FakeXrmEasy.Core/FileStorage/FileStorageSettings.cs

View workflow job for this annotation

GitHub Actions / build-windows (FAKE_XRM_EASY)

Missing XML comment for publicly visible type or member 'IFileStorageSettings'

Check warning on line 3 in src/FakeXrmEasy.Core/FileStorage/FileStorageSettings.cs

View workflow job for this annotation

GitHub Actions / build-windows (FAKE_XRM_EASY)

Missing XML comment for publicly visible type or member 'IFileStorageSettings'

Check warning on line 3 in src/FakeXrmEasy.Core/FileStorage/FileStorageSettings.cs

View workflow job for this annotation

GitHub Actions / build-windows (FAKE_XRM_EASY_9)

Missing XML comment for publicly visible type or member 'IFileStorageSettings'

Check warning on line 3 in src/FakeXrmEasy.Core/FileStorage/FileStorageSettings.cs

View workflow job for this annotation

GitHub Actions / build-windows (FAKE_XRM_EASY_9)

Missing XML comment for publicly visible type or member 'IFileStorageSettings'
{
/// <summary>
/// Sets or gets the default maximum file size for file uploads
/// </summary>
int MaxSizeInKB { get; set; }

/// <summary>
/// Sets or gets the default maximum file size for image uploads
/// </summary>
int ImageMaxSizeInKB { get; set; }
}
public class FileStorageSettings: IFileStorageSettings

Check warning on line 15 in src/FakeXrmEasy.Core/FileStorage/FileStorageSettings.cs

View workflow job for this annotation

GitHub Actions / build-windows (FAKE_XRM_EASY)

Missing XML comment for publicly visible type or member 'FileStorageSettings'

Check warning on line 15 in src/FakeXrmEasy.Core/FileStorage/FileStorageSettings.cs

View workflow job for this annotation

GitHub Actions / build-windows (FAKE_XRM_EASY)

Missing XML comment for publicly visible type or member 'FileStorageSettings'

Check warning on line 15 in src/FakeXrmEasy.Core/FileStorage/FileStorageSettings.cs

View workflow job for this annotation

GitHub Actions / build-windows (FAKE_XRM_EASY_9)

Missing XML comment for publicly visible type or member 'FileStorageSettings'

Check warning on line 15 in src/FakeXrmEasy.Core/FileStorage/FileStorageSettings.cs

View workflow job for this annotation

GitHub Actions / build-windows (FAKE_XRM_EASY_9)

Missing XML comment for publicly visible type or member 'FileStorageSettings'
{

/// <summary>
/// The default max size (i.e 32 MB)
/// </summary>
public const int DEFAULT_MAX_FILE_SIZE_IN_KB = 32768;

/// <summary>
/// The maximum file size supported by the platform
/// https://learn.microsoft.com/en-us/dotnet/api/microsoft.xrm.sdk.metadata.fileattributemetadata.maxsizeinkb?view=dataverse-sdk-latest#microsoft-xrm-sdk-metadata-fileattributemetadata-maxsizeinkb
/// </summary>
public const int MAX_SUPPORTED_FILE_SIZE_IN_KB = 10485760;

/// <summary>
/// The maximum file size supported for Image column types
/// https://learn.microsoft.com/en-us/power-apps/developer/data-platform/files-images-overview?tabs=sdk
/// </summary>
public const int MAX_SUPPORTED_IMAGE_FILE_SIZE_IN_KB = 30 * 1024;

/// <summary>
/// Sets or gets the current maximum file size for file uploads that use file storage
/// </summary>
public int MaxSizeInKB { get; set; }

/// <summary>
/// Sets or gets the default maximum file size for image uploads
/// </summary>
public int ImageMaxSizeInKB { get; set; }

/// <summary>
/// Default constructor
/// </summary>
public FileStorageSettings()
{
MaxSizeInKB = DEFAULT_MAX_FILE_SIZE_IN_KB;
ImageMaxSizeInKB = MAX_SUPPORTED_IMAGE_FILE_SIZE_IN_KB;
}
}
}
37 changes: 23 additions & 14 deletions src/FakeXrmEasy.Core/Metadata/MetadataGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,35 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using FakeXrmEasy.Abstractions;
using FakeXrmEasy.Core.Extensions;
using FakeXrmEasy.Core.FileStorage;

namespace FakeXrmEasy.Metadata
{
internal static class MetadataGenerator
{
public static IEnumerable<EntityMetadata> FromEarlyBoundEntities(Assembly earlyBoundEntitiesAssembly)
public static IEnumerable<EntityMetadata> FromEarlyBoundEntities(Assembly earlyBoundEntitiesAssembly, IXrmFakedContext context)
{
var types = earlyBoundEntitiesAssembly.GetTypes();
return FromTypes(types);
return FromTypes(types, context);
}

internal static IEnumerable<EntityMetadata> FromTypes(Type[] types)
internal static IEnumerable<EntityMetadata> FromTypes(Type[] types, IXrmFakedContext context)
{
var entityMetadatas = new List<EntityMetadata>();

foreach (var possibleEarlyBoundEntity in types)
{
var metadata = FromType(possibleEarlyBoundEntity);
var metadata = FromType(possibleEarlyBoundEntity, context);
if(metadata != null)
entityMetadatas.Add(metadata);
}

return entityMetadatas;
}

internal static EntityMetadata FromType(Type possibleEarlyBoundEntity)
internal static EntityMetadata FromType(Type possibleEarlyBoundEntity, IXrmFakedContext context)
{
EntityLogicalNameAttribute entityLogicalNameAttribute = GetCustomAttribute<EntityLogicalNameAttribute>(possibleEarlyBoundEntity);
if (entityLogicalNameAttribute == null) return null;
Expand Down Expand Up @@ -60,7 +62,7 @@ internal static EntityMetadata FromType(Type possibleEarlyBoundEntity)
var relationshipMetadataProperties = possibleEarlyBoundEntity.GetProperties(BindingFlags.Instance | BindingFlags.Public)
.Where(x => Attribute.IsDefined(x, typeof(RelationshipSchemaNameAttribute)));

var attributeMetadatas = PopulateAttributeProperties(metadata, attributeMetadataProperties);
var attributeMetadatas = PopulateAttributeProperties(metadata, attributeMetadataProperties, context);
if (attributeMetadatas.Any())
{
metadata.SetSealedPropertyValue("Attributes", attributeMetadatas.ToArray());
Expand All @@ -83,13 +85,13 @@ internal static EntityMetadata FromType(Type possibleEarlyBoundEntity)
return metadata;
}

private static List<AttributeMetadata> PopulateAttributeProperties(EntityMetadata metadata, IEnumerable<PropertyInfo> properties)
private static List<AttributeMetadata> PopulateAttributeProperties(EntityMetadata metadata, IEnumerable<PropertyInfo> properties, IXrmFakedContext context)
{
List<AttributeMetadata> attributeMetadatas = new List<AttributeMetadata>();

foreach (var property in properties)
{
var attributeMetadata = PopulateAttributeProperty(metadata, property);
var attributeMetadata = PopulateAttributeProperty(metadata, property, context);
if (attributeMetadata != null)
{
attributeMetadatas.Add(attributeMetadata);
Expand Down Expand Up @@ -127,7 +129,7 @@ private static AllRelationShips PopulateRelationshipProperties(EntityMetadata en
return allRelationships;
}

private static AttributeMetadata PopulateAttributeProperty(EntityMetadata metadata, PropertyInfo property)
private static AttributeMetadata PopulateAttributeProperty(EntityMetadata metadata, PropertyInfo property, IXrmFakedContext context)
{
var attributeLogicalNameAttribute = GetCustomAttribute<AttributeLogicalNameAttribute>(property);
#if !FAKE_XRM_EASY
Expand All @@ -152,7 +154,7 @@ private static AttributeMetadata PopulateAttributeProperty(EntityMetadata metada
}
else
{
attributeMetadata = CreateAttributeMetadata(property.PropertyType);
attributeMetadata = CreateAttributeMetadata(property.PropertyType, context);
}

attributeMetadata.SetFieldValue("_entityLogicalName", metadata.LogicalName);
Expand Down Expand Up @@ -207,8 +209,10 @@ private static T GetCustomAttribute<T>(MemberInfo member) where T : Attribute
return (T)Attribute.GetCustomAttribute(member, typeof(T));
}

internal static AttributeMetadata CreateAttributeMetadata(Type propertyType)
internal static AttributeMetadata CreateAttributeMetadata(Type propertyType, IXrmFakedContext context)
{
var fileStorageSettings = context.GetProperty<IFileStorageSettings>();

if (typeof(string) == propertyType)
{
return new StringAttributeMetadata();
Expand Down Expand Up @@ -244,8 +248,10 @@ internal static AttributeMetadata CreateAttributeMetadata(Type propertyType)
#if !FAKE_XRM_EASY
else if (typeof(byte[]) == propertyType)
{

return new ImageAttributeMetadata();
return new ImageAttributeMetadata()
{
MaxSizeInKB = fileStorageSettings.ImageMaxSizeInKB

Check failure on line 253 in src/FakeXrmEasy.Core/Metadata/MetadataGenerator.cs

View workflow job for this annotation

GitHub Actions / build-windows (FAKE_XRM_EASY_2015)

'ImageAttributeMetadata' does not contain a definition for 'MaxSizeInKB'

Check failure on line 253 in src/FakeXrmEasy.Core/Metadata/MetadataGenerator.cs

View workflow job for this annotation

GitHub Actions / build-windows (FAKE_XRM_EASY_2015)

'ImageAttributeMetadata' does not contain a definition for 'MaxSizeInKB'

Check failure on line 253 in src/FakeXrmEasy.Core/Metadata/MetadataGenerator.cs

View workflow job for this annotation

GitHub Actions / build-windows (FAKE_XRM_EASY_2016)

'ImageAttributeMetadata' does not contain a definition for 'MaxSizeInKB'

Check failure on line 253 in src/FakeXrmEasy.Core/Metadata/MetadataGenerator.cs

View workflow job for this annotation

GitHub Actions / build-windows (FAKE_XRM_EASY_2016)

'ImageAttributeMetadata' does not contain a definition for 'MaxSizeInKB'

Check failure on line 253 in src/FakeXrmEasy.Core/Metadata/MetadataGenerator.cs

View workflow job for this annotation

GitHub Actions / build-windows (FAKE_XRM_EASY_2013)

'ImageAttributeMetadata' does not contain a definition for 'MaxSizeInKB'

Check failure on line 253 in src/FakeXrmEasy.Core/Metadata/MetadataGenerator.cs

View workflow job for this annotation

GitHub Actions / build-windows (FAKE_XRM_EASY_2013)

'ImageAttributeMetadata' does not contain a definition for 'MaxSizeInKB'

Check failure on line 253 in src/FakeXrmEasy.Core/Metadata/MetadataGenerator.cs

View workflow job for this annotation

GitHub Actions / build-windows (FAKE_XRM_EASY_365)

'ImageAttributeMetadata' does not contain a definition for 'MaxSizeInKB'

Check failure on line 253 in src/FakeXrmEasy.Core/Metadata/MetadataGenerator.cs

View workflow job for this annotation

GitHub Actions / build-windows (FAKE_XRM_EASY_365)

'ImageAttributeMetadata' does not contain a definition for 'MaxSizeInKB'

Check failure on line 253 in src/FakeXrmEasy.Core/Metadata/MetadataGenerator.cs

View workflow job for this annotation

GitHub Actions / build-windows (FAKE_XRM_EASY_365)

'ImageAttributeMetadata' does not contain a definition for 'MaxSizeInKB'

Check failure on line 253 in src/FakeXrmEasy.Core/Metadata/MetadataGenerator.cs

View workflow job for this annotation

GitHub Actions / build-windows (FAKE_XRM_EASY_365)

'ImageAttributeMetadata' does not contain a definition for 'MaxSizeInKB'
};
}
#endif
#if FAKE_XRM_EASY_9
Expand All @@ -255,7 +261,10 @@ internal static AttributeMetadata CreateAttributeMetadata(Type propertyType)
}
else if (typeof(object) == propertyType)
{
return new FileAttributeMetadata();
return new FileAttributeMetadata()
{
MaxSizeInKB = fileStorageSettings.MaxSizeInKB
};
}
#endif
else
Expand Down
2 changes: 2 additions & 0 deletions src/FakeXrmEasy.Core/Middleware/MiddlewareBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using FakeXrmEasy.Abstractions.Exceptions;
using FakeXrmEasy.Core.CommercialLicense;
using FakeXrmEasy.Core.Exceptions;
using FakeXrmEasy.Core.FileStorage;

namespace FakeXrmEasy.Middleware
{
Expand Down Expand Up @@ -64,6 +65,7 @@ public IMiddlewareBuilder Add(Action<IXrmFakedContext> addToContextAction)
private void AddDefaults()
{
_context.SetProperty<IIntegrityOptions>(new IntegrityOptions() { ValidateEntityReferences = false });
_context.SetProperty<IFileStorageSettings>(new FileStorageSettings());
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/FakeXrmEasy.Core/XrmFakedContext.Metadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void InitializeMetadata(EntityMetadata entityMetadata)
/// <param name="earlyBoundEntitiesAssembly"></param>
public void InitializeMetadata(Assembly earlyBoundEntitiesAssembly)
{
IEnumerable<EntityMetadata> entityMetadatas = MetadataGenerator.FromEarlyBoundEntities(earlyBoundEntitiesAssembly);
IEnumerable<EntityMetadata> entityMetadatas = MetadataGenerator.FromEarlyBoundEntities(earlyBoundEntitiesAssembly, this);
if (entityMetadatas.Any())
{
this.InitializeMetadata(entityMetadatas);
Expand Down
Loading

0 comments on commit a434347

Please sign in to comment.