Skip to content

Commit

Permalink
Fix code smells
Browse files Browse the repository at this point in the history
  • Loading branch information
jordimontana82 committed Aug 20, 2024
1 parent df0aa00 commit 0a5e2e9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
25 changes: 24 additions & 1 deletion src/FakeXrmEasy.Core/FileStorage/Db/FileAttachment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,38 @@
namespace FakeXrmEasy.Core.FileStorage.Db
{
/// <summary>
/// Contains information about the file contents and file metadata associated to an existing record (Target) and field (AttributeName)
/// Represents a file associated to an existing Dataverse record.
/// </summary>
public class FileAttachment: IFileAttachment
{
/// <summary>
/// The unique identifier of the file to be uploaded(i.e. the Guid of the fileattachment record)
/// </summary>
public string Id { get; set; }

/// <summary>
/// The file name, including the file extension
/// </summary>
public string FileName { get; set; }

/// <summary>
/// The file MIME type
/// </summary>
public string MimeType { get; set; }

/// <summary>
/// The actual file contents as a byte array
/// </summary>
public byte[] Content { get; set; }

/// <summary>
/// The associated record to which this file belongs
/// </summary>
public EntityReference Target { get; set; }

/// <summary>
/// The logical name of the File or Image column / field of the Target EntityReference
/// </summary>
public string AttributeName { get; set; }
}
}
22 changes: 9 additions & 13 deletions src/FakeXrmEasy.Core/FileStorage/Db/InMemoryFileDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ public List<IFileAttachment> GetFilesForTarget(EntityReference target)
(f =>
f.Target.LogicalName.Equals(target.LogicalName) &&
f.Target.Id.Equals(target.Id))
.Select(f => f as IFileAttachment)
.ToList();
}

Expand Down Expand Up @@ -303,22 +302,19 @@ private void ValidateMaxFileSize(FileUploadSession fileUploadSession, FileAttach
return;
}

if (attributeMetadata is FileAttributeMetadata fileAttributeMetadata)
if (attributeMetadata is FileAttributeMetadata fileAttributeMetadata
&& (decimal) fileAttachment.Content.Length / 1024 > fileAttributeMetadata.MaxSizeInKB)
{
if ((decimal) fileAttachment.Content.Length / 1024 > fileAttributeMetadata.MaxSizeInKB)
{
throw new MaxSizeExceededException(tableLogicalName, fileUploadSession.Properties.FileAttributeName,
fileAttributeMetadata.MaxSizeInKB.Value);
}
throw new MaxSizeExceededException(tableLogicalName, fileUploadSession.Properties.FileAttributeName,
fileAttributeMetadata.MaxSizeInKB.Value);
}

if (attributeMetadata is ImageAttributeMetadata imageAttributeMetadata)
if (attributeMetadata is ImageAttributeMetadata imageAttributeMetadata &&
(decimal) fileAttachment.Content.Length / 1024 > imageAttributeMetadata.MaxSizeInKB)
{
if ((decimal) fileAttachment.Content.Length / 1024 > imageAttributeMetadata.MaxSizeInKB)
{
throw new MaxSizeExceededException(tableLogicalName, fileUploadSession.Properties.FileAttributeName,
imageAttributeMetadata.MaxSizeInKB.Value);
}

throw new MaxSizeExceededException(tableLogicalName, fileUploadSession.Properties.FileAttributeName,
imageAttributeMetadata.MaxSizeInKB.Value);
}

}
Expand Down

0 comments on commit 0a5e2e9

Please sign in to comment.