Skip to content

Commit

Permalink
Rearrange files and add InitializeDownload logic and tests DynamicsVa…
Browse files Browse the repository at this point in the history
  • Loading branch information
jordimontana82 committed Aug 9, 2024
1 parent 214400e commit 0e8bbcd
Show file tree
Hide file tree
Showing 15 changed files with 297 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace FakeXrmEasy.Core.FileStorage.Db.Exceptions
{
public class CouldNotAddFileException: Exception

Check warning on line 5 in src/FakeXrmEasy.Core/FileStorage/Db/Exceptions/CouldNotAddFileException.cs

View workflow job for this annotation

GitHub Actions / build-windows (FAKE_XRM_EASY_2016)

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

Check warning on line 5 in src/FakeXrmEasy.Core/FileStorage/Db/Exceptions/CouldNotAddFileException.cs

View workflow job for this annotation

GitHub Actions / build-windows (FAKE_XRM_EASY_9)

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

Check warning on line 5 in src/FakeXrmEasy.Core/FileStorage/Db/Exceptions/CouldNotAddFileException.cs

View workflow job for this annotation

GitHub Actions / build-windows (FAKE_XRM_EASY_2015)

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

Check warning on line 5 in src/FakeXrmEasy.Core/FileStorage/Db/Exceptions/CouldNotAddFileException.cs

View workflow job for this annotation

GitHub Actions / build-windows (FAKE_XRM_EASY_365)

Missing XML comment for publicly visible type or member 'CouldNotAddFileException'
{
public CouldNotAddFileException(): base("A file could not be added")

Check warning on line 7 in src/FakeXrmEasy.Core/FileStorage/Db/Exceptions/CouldNotAddFileException.cs

View workflow job for this annotation

GitHub Actions / build-windows (FAKE_XRM_EASY_2016)

Missing XML comment for publicly visible type or member 'CouldNotAddFileException.CouldNotAddFileException()'

Check warning on line 7 in src/FakeXrmEasy.Core/FileStorage/Db/Exceptions/CouldNotAddFileException.cs

View workflow job for this annotation

GitHub Actions / build-windows (FAKE_XRM_EASY_9)

Missing XML comment for publicly visible type or member 'CouldNotAddFileException.CouldNotAddFileException()'

Check warning on line 7 in src/FakeXrmEasy.Core/FileStorage/Db/Exceptions/CouldNotAddFileException.cs

View workflow job for this annotation

GitHub Actions / build-windows (FAKE_XRM_EASY_2015)

Missing XML comment for publicly visible type or member 'CouldNotAddFileException.CouldNotAddFileException()'

Check warning on line 7 in src/FakeXrmEasy.Core/FileStorage/Db/Exceptions/CouldNotAddFileException.cs

View workflow job for this annotation

GitHub Actions / build-windows (FAKE_XRM_EASY_365)

Missing XML comment for publicly visible type or member 'CouldNotAddFileException.CouldNotAddFileException()'
{

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using Microsoft.Xrm.Sdk;

namespace FakeXrmEasy.Core.FileStorage.Db.Exceptions
{
/// <summary>
/// Exception raised when downloading a file block for a database record and column that doesn't actually have a file against it
/// </summary>
public class FileToDownloadNotFoundException: Exception
{
/// <summary>
/// Default constructor
/// </summary>
/// <param name="entityReference">An entity reference of the record</param>
/// <param name="fileAttributeName">The column where a file was not found</param>
public FileToDownloadNotFoundException(EntityReference entityReference, string fileAttributeName)
: base($"A file was not found for record with logical name '{entityReference.LogicalName}' and Id '{entityReference.Id.ToString()}' in column '{fileAttributeName}'")
{

}
}
}
11 changes: 11 additions & 0 deletions src/FakeXrmEasy.Core/FileStorage/Db/FileDownloadSession.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using FakeXrmEasy.Core.FileStorage.Download;

namespace FakeXrmEasy.Core.FileStorage.Db
{
internal class FileDownloadSession
{
internal string FileDownloadSessionId { get; set; }
internal FileDownloadProperties Properties { get; set; }
internal FileAttachment File { get; set; }
}
}
1 change: 1 addition & 0 deletions src/FakeXrmEasy.Core/FileStorage/Db/FileUploadSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using FakeXrmEasy.Core.FileStorage.Db.Exceptions;
using FakeXrmEasy.Core.FileStorage.Upload;

namespace FakeXrmEasy.Core.FileStorage.Db
{
Expand Down
16 changes: 16 additions & 0 deletions src/FakeXrmEasy.Core/FileStorage/Db/IInMemoryFileDbDownloader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Collections.Generic;
using FakeXrmEasy.Core.FileStorage.Download;

namespace FakeXrmEasy.Core.FileStorage.Db
{
internal interface IInMemoryFileDbDownloader
{
string InitFileDownloadSession(FileDownloadProperties fileDownloadProperties);

FileDownloadSession GetFileDownloadSession(string fileDownloadSessionId);

List<FileDownloadSession> GetAllFileDownloadSessions();

byte[] DownloadFileBlock(DownloadBlockProperties uploadBlockProperties);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using System.Collections.Generic;
using FakeXrmEasy.Core.FileStorage.Upload;

namespace FakeXrmEasy.Core.FileStorage.Db
{
internal interface IInMemoryFileDbInternal
internal interface IInMemoryFileDbUploader
{
/// <summary>
/// Inits a file upload session against this InMemoryFileDb
Expand Down
90 changes: 84 additions & 6 deletions src/FakeXrmEasy.Core/FileStorage/Db/InMemoryFileDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@
using System.Linq;
using FakeXrmEasy.Core.Db;
using FakeXrmEasy.Core.FileStorage.Db.Exceptions;
using FakeXrmEasy.Core.FileStorage.Download;
using FakeXrmEasy.Core.FileStorage.Upload;
using Microsoft.Xrm.Sdk;
#if FAKE_XRM_EASY_9
using Microsoft.Xrm.Sdk.Metadata;
#endif

namespace FakeXrmEasy.Core.FileStorage.Db
{
internal class InMemoryFileDb : IInMemoryFileDbInternal
internal class InMemoryFileDb : IInMemoryFileDbUploader, IInMemoryFileDbDownloader
{
private readonly ConcurrentDictionary<string, FileUploadSession> _uncommittedFileUploads;
private readonly ConcurrentDictionary<string, FileAttachment> _files;
private readonly ConcurrentDictionary<string, FileDownloadSession> _fileDownloadSessions;
private readonly InMemoryDb _db;

internal const string FILE_ATTACHMENT_TABLE_NAME = "fileattachment";
Expand All @@ -22,11 +27,12 @@ internal InMemoryFileDb(InMemoryDb db)
_db = db;
_uncommittedFileUploads = new ConcurrentDictionary<string, FileUploadSession>();
_files = new ConcurrentDictionary<string, FileAttachment>();
_fileDownloadSessions = new ConcurrentDictionary<string, FileDownloadSession>();
}

public string InitFileUploadSession(FileUploadProperties fileUploadProperties)
{
ValidateEntityReference(fileUploadProperties);
ValidateEntityReference(fileUploadProperties.Target);

string fileContinuationToken = Guid.NewGuid().ToString();
_uncommittedFileUploads.GetOrAdd(fileContinuationToken, new FileUploadSession()
Expand All @@ -37,11 +43,11 @@ public string InitFileUploadSession(FileUploadProperties fileUploadProperties)
return fileContinuationToken;
}

private void ValidateEntityReference(FileUploadProperties fileUploadProperties)
private void ValidateEntityReference(EntityReference entityReference)
{
if (!_db.ContainsEntityRecord(fileUploadProperties.Target.LogicalName, fileUploadProperties.Target.Id))
if (!_db.ContainsEntityRecord(entityReference.LogicalName, entityReference.Id))
{
throw new RecordNotFoundException(fileUploadProperties.Target);
throw new RecordNotFoundException(entityReference);
}
}

Expand All @@ -62,11 +68,22 @@ public List<FileUploadSession> GetAllFileUploadSessions()
return _uncommittedFileUploads.Values.ToList();
}

public List<FileAttachment> GetAllFiles()
#region Internal File Manipulation
internal List<FileAttachment> GetAllFiles()
{
return _files.Values.ToList();
}

internal void AddFile(FileAttachment fileAttachment)
{
var wasAdded = _files.TryAdd(fileAttachment.Id, fileAttachment);
if (!wasAdded)
{
throw new CouldNotAddFileException();
}
}
#endregion

/// <summary>
///
/// </summary>
Expand All @@ -76,6 +93,7 @@ public IQueryable<FileAttachment> CreateQuery()
return _files.Values.AsQueryable();
}

#region Upload
public string CommitFileUploadSession(CommitFileUploadSessionProperties commitProperties)
{
FileUploadSession fileUploadSession;
Expand Down Expand Up @@ -166,5 +184,65 @@ private void SaveFileAttachment(FileUploadSession fileUploadSession, FileAttachm
record[fileUploadSession.Properties.FileAttributeName] = fileAttachment.Id;
record[$"{fileUploadSession.Properties.FileAttributeName}_name"] = fileAttachment.FileName;
}

#endregion

#region Download
public string InitFileDownloadSession(FileDownloadProperties fileDownloadProperties)
{
ValidateEntityReference(fileDownloadProperties.Target);
var file = CheckFileExists(fileDownloadProperties);

string fileContinuationToken = Guid.NewGuid().ToString();
_fileDownloadSessions.GetOrAdd(fileContinuationToken, new FileDownloadSession()
{
FileDownloadSessionId = fileContinuationToken,
Properties = new FileDownloadProperties(fileDownloadProperties),
File = file
});
return fileContinuationToken;
}

private FileAttachment CheckFileExists(FileDownloadProperties fileDownloadProperties)
{
var entityReference = fileDownloadProperties.Target;
var record = _db.GetTable(entityReference.LogicalName).GetById(entityReference.Id);
if (!record.Attributes.ContainsKey(fileDownloadProperties.FileAttributeName))
{
throw new FileToDownloadNotFoundException(entityReference, fileDownloadProperties.FileAttributeName);
}

var fileId = (string)record[fileDownloadProperties.FileAttributeName];

var exists = _files.TryGetValue(fileId, out var file);
if (!exists)
{
throw new FileToDownloadNotFoundException(entityReference, fileDownloadProperties.FileAttributeName);
}
return file;
}

public FileDownloadSession GetFileDownloadSession(string fileDownloadSessionId)
{
FileDownloadSession session = null;
var exists = _fileDownloadSessions.TryGetValue(fileDownloadSessionId, out session);
if (exists)
{
return session;
}

return null;
}

public List<FileDownloadSession> GetAllFileDownloadSessions()
{
return _fileDownloadSessions.Values.ToList();
}

public byte[] DownloadFileBlock(DownloadBlockProperties uploadBlockProperties)
{
throw new NotImplementedException();
}
#endregion
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace FakeXrmEasy.Core.FileStorage.Download
{
internal class DownloadBlockProperties
{
internal string FileDownloadSessionId { get; set; }
internal long BlockLength { get; set; }
internal long Offset { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Microsoft.Xrm.Sdk;

namespace FakeXrmEasy.Core.FileStorage.Download
{
internal class FileDownloadProperties
{
internal EntityReference Target { get; set; }
internal string FileAttributeName { get; set; }

internal FileDownloadProperties()
{

}

internal FileDownloadProperties(FileDownloadProperties other)
{
if (other.Target != null)
{
Target = new EntityReference(other.Target.LogicalName)

Check failure on line 19 in src/FakeXrmEasy.Core/FileStorage/Download/FileDownloadProperties.cs

View workflow job for this annotation

GitHub Actions / build-windows (FAKE_XRM_EASY)

There is no argument given that corresponds to the required formal parameter 'id' of 'EntityReference.EntityReference(string, Guid)'

Check failure on line 19 in src/FakeXrmEasy.Core/FileStorage/Download/FileDownloadProperties.cs

View workflow job for this annotation

GitHub Actions / build-windows (FAKE_XRM_EASY)

There is no argument given that corresponds to the required formal parameter 'id' of 'EntityReference.EntityReference(string, Guid)'

Check failure on line 19 in src/FakeXrmEasy.Core/FileStorage/Download/FileDownloadProperties.cs

View workflow job for this annotation

GitHub Actions / build-windows (FAKE_XRM_EASY_2013)

There is no argument given that corresponds to the required formal parameter 'id' of 'EntityReference.EntityReference(string, Guid)'

Check failure on line 19 in src/FakeXrmEasy.Core/FileStorage/Download/FileDownloadProperties.cs

View workflow job for this annotation

GitHub Actions / build-windows (FAKE_XRM_EASY_2013)

There is no argument given that corresponds to the required formal parameter 'id' of 'EntityReference.EntityReference(string, Guid)'
{
Id = other.Target.Id
};
}

FileAttributeName = other.FileAttributeName;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace FakeXrmEasy.Core.FileStorage
namespace FakeXrmEasy.Core.FileStorage.Upload
{
internal class CommitFileUploadSessionProperties
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using FakeXrmEasy.Core.FileStorage.Db;
using Microsoft.Xrm.Sdk;

namespace FakeXrmEasy.Core.FileStorage
namespace FakeXrmEasy.Core.FileStorage.Upload
{
/// <summary>
/// Properties needed to initialize a file upload
Expand All @@ -12,7 +11,7 @@ internal class FileUploadProperties
public string FileAttributeName { get; set; }
public string FileName { get; set; }

public FileUploadProperties()
internal FileUploadProperties()
{

}
Expand All @@ -21,7 +20,7 @@ public FileUploadProperties()
/// Copy constructor
/// </summary>
/// <param name="other"></param>
public FileUploadProperties(FileUploadProperties other)
internal FileUploadProperties(FileUploadProperties other)
{
if (other.Target != null)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace FakeXrmEasy.Core.FileStorage
namespace FakeXrmEasy.Core.FileStorage.Upload
{
internal interface IUploadBlockProperties
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using FakeXrmEasy.Core.FileStorage;
using FakeXrmEasy.Core.FileStorage.Db;
using FakeXrmEasy.Core.FileStorage.Db.Exceptions;
using FakeXrmEasy.Core.FileStorage.Upload;
using Xunit;

namespace FakeXrmEasy.Core.Tests.FileStorage.Db
Expand Down
Loading

0 comments on commit 0e8bbcd

Please sign in to comment.