From fd12632ea745b5d974db58a83b8c0f5ee48835dc Mon Sep 17 00:00:00 2001 From: Hesam Asnaashari <46031836+ilhesam@users.noreply.github.com> Date: Sun, 26 Jun 2022 12:33:59 +0430 Subject: [PATCH 01/21] Added FileEntity --- .../uBeac.Core.Common/Entities/FileEntity.cs | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 src/Common/uBeac.Core.Common/Entities/FileEntity.cs diff --git a/src/Common/uBeac.Core.Common/Entities/FileEntity.cs b/src/Common/uBeac.Core.Common/Entities/FileEntity.cs new file mode 100644 index 00000000..94682589 --- /dev/null +++ b/src/Common/uBeac.Core.Common/Entities/FileEntity.cs @@ -0,0 +1,27 @@ +namespace uBeac; + +public interface IFileEntity : IAuditEntity + where TKey : IEquatable +{ + string Name { get; set; } + string Extension { get; set; } + string Provider { get; set; } + string Path { get; set; } +} + +public interface IFileEntity : IFileEntity, IAuditEntity +{ +} + +public class FileEntity : AuditEntity, IFileEntity + where TKey : IEquatable +{ + public string Name { get; set; } + public string Extension { get; set; } + public string Provider { get; set; } + public string Path { get; set; } +} + +public class FileEntity : FileEntity, IFileEntity +{ +} \ No newline at end of file From 117dc3267f95c2df2f184e5b4bd347e790d4ca71 Mon Sep 17 00:00:00 2001 From: Hesam Asnaashari <46031836+ilhesam@users.noreply.github.com> Date: Sun, 26 Jun 2022 12:34:34 +0430 Subject: [PATCH 02/21] Added IFileRepository and MongoFileRepository --- .../IFileRepository.cs | 12 ++++++++++ .../MongoFileRepository.cs | 22 +++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 src/FileManagement/uBeac.Core.Repositories.FileManagement.Abstractions/IFileRepository.cs create mode 100644 src/FileManagement/uBeac.Core.Repositories.FileManagement.MongoDB/MongoFileRepository.cs diff --git a/src/FileManagement/uBeac.Core.Repositories.FileManagement.Abstractions/IFileRepository.cs b/src/FileManagement/uBeac.Core.Repositories.FileManagement.Abstractions/IFileRepository.cs new file mode 100644 index 00000000..b425d5c5 --- /dev/null +++ b/src/FileManagement/uBeac.Core.Repositories.FileManagement.Abstractions/IFileRepository.cs @@ -0,0 +1,12 @@ +namespace uBeac.Repositories.FileManagement; + +public interface IFileRepository : IEntityRepository + where TKey : IEquatable + where TEntity : IFileEntity +{ +} + +public interface IFileRepository : IFileRepository, IEntityRepository + where TEntity : IFileEntity +{ +} \ No newline at end of file diff --git a/src/FileManagement/uBeac.Core.Repositories.FileManagement.MongoDB/MongoFileRepository.cs b/src/FileManagement/uBeac.Core.Repositories.FileManagement.MongoDB/MongoFileRepository.cs new file mode 100644 index 00000000..5dac57de --- /dev/null +++ b/src/FileManagement/uBeac.Core.Repositories.FileManagement.MongoDB/MongoFileRepository.cs @@ -0,0 +1,22 @@ +using uBeac.Repositories.MongoDB; + +namespace uBeac.Repositories.FileManagement.MongoDB; + +public class MongoFileRepository : MongoEntityRepository, IFileRepository + where TKey : IEquatable + where TEntity : IFileEntity + where TContext : IMongoDBContext +{ + public MongoFileRepository(TContext mongoDbContext, IApplicationContext applicationContext) : base(mongoDbContext, applicationContext) + { + } +} + +public class MongoFileRepository : MongoFileRepository, IFileRepository + where TEntity : IFileEntity + where TContext : IMongoDBContext +{ + public MongoFileRepository(TContext mongoDbContext, IApplicationContext applicationContext) : base(mongoDbContext, applicationContext) + { + } +} \ No newline at end of file From a21527b2f57825c6843684401f2c0d5bda6ec9fe Mon Sep 17 00:00:00 2001 From: Hesam Asnaashari <46031836+ilhesam@users.noreply.github.com> Date: Sun, 26 Jun 2022 13:42:18 +0430 Subject: [PATCH 03/21] Added IFileProvider --- .../IFileProvider.cs | 7 +++++++ ...uBeac.Core.Providers.FileManagement.Abstractions.csproj | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 src/FileManagement/uBeac.Core.Providers.FileManagement.Abstractions/IFileProvider.cs diff --git a/src/FileManagement/uBeac.Core.Providers.FileManagement.Abstractions/IFileProvider.cs b/src/FileManagement/uBeac.Core.Providers.FileManagement.Abstractions/IFileProvider.cs new file mode 100644 index 00000000..32326a3f --- /dev/null +++ b/src/FileManagement/uBeac.Core.Providers.FileManagement.Abstractions/IFileProvider.cs @@ -0,0 +1,7 @@ +namespace uBeac.Providers.FileManagement; + +public interface IFileProvider +{ + Task Create(FileStream fileStream, string fileName, CancellationToken cancellationToken = default); + Task Get(string fileName, CancellationToken cancellationToken = default); +} \ No newline at end of file diff --git a/src/FileManagement/uBeac.Core.Providers.FileManagement.Abstractions/uBeac.Core.Providers.FileManagement.Abstractions.csproj b/src/FileManagement/uBeac.Core.Providers.FileManagement.Abstractions/uBeac.Core.Providers.FileManagement.Abstractions.csproj index 132c02c5..141e38ff 100644 --- a/src/FileManagement/uBeac.Core.Providers.FileManagement.Abstractions/uBeac.Core.Providers.FileManagement.Abstractions.csproj +++ b/src/FileManagement/uBeac.Core.Providers.FileManagement.Abstractions/uBeac.Core.Providers.FileManagement.Abstractions.csproj @@ -3,7 +3,7 @@ net6.0 enable - enable + disable From a83096d84d27e2a969471097b4fbdf897aad8ff0 Mon Sep 17 00:00:00 2001 From: Hesam Asnaashari <46031836+ilhesam@users.noreply.github.com> Date: Sun, 26 Jun 2022 13:42:35 +0430 Subject: [PATCH 04/21] Implemented LocalStorageFileProvider --- .../LocalStorageFileProvider.cs | 27 +++++++++++++++++++ .../LocalStorageOptions.cs | 6 +++++ ...oviders.FileManagement.LocalStorage.csproj | 6 ++++- 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 src/FileManagement/uBeac.Core.Providers.FileManagement.LocalStorage/LocalStorageFileProvider.cs create mode 100644 src/FileManagement/uBeac.Core.Providers.FileManagement.LocalStorage/LocalStorageOptions.cs diff --git a/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalStorage/LocalStorageFileProvider.cs b/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalStorage/LocalStorageFileProvider.cs new file mode 100644 index 00000000..4236c73f --- /dev/null +++ b/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalStorage/LocalStorageFileProvider.cs @@ -0,0 +1,27 @@ +namespace uBeac.Providers.FileManagement.LocalStorage; + +public class LocalStorageFileProvider : IFileProvider +{ + protected readonly LocalStorageOptions Options; + + public LocalStorageFileProvider(LocalStorageOptions options) + { + Options = options; + } + + public async Task Create(FileStream fileStream, string fileName, CancellationToken cancellationToken = default) + { + var path = GetFinalPath(fileName); + await using var createStream = new FileStream(path, FileMode.Create, FileAccess.Write); + await fileStream.CopyToAsync(createStream, cancellationToken); + } + + public async Task Get(string fileName, CancellationToken cancellationToken = default) + { + var path = GetFinalPath(fileName); + await using var readStream = new FileStream(path, FileMode.Open, FileAccess.Read); + return readStream; + } + + protected string GetFinalPath(string fileName) => Path.Combine(Options.DirectoryPath, fileName); +} \ No newline at end of file diff --git a/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalStorage/LocalStorageOptions.cs b/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalStorage/LocalStorageOptions.cs new file mode 100644 index 00000000..bbe05f67 --- /dev/null +++ b/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalStorage/LocalStorageOptions.cs @@ -0,0 +1,6 @@ +namespace uBeac.Providers.FileManagement.LocalStorage; + +public class LocalStorageOptions +{ + public string DirectoryPath { get; set; } +} \ No newline at end of file diff --git a/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalStorage/uBeac.Core.Providers.FileManagement.LocalStorage.csproj b/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalStorage/uBeac.Core.Providers.FileManagement.LocalStorage.csproj index 132c02c5..e5678224 100644 --- a/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalStorage/uBeac.Core.Providers.FileManagement.LocalStorage.csproj +++ b/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalStorage/uBeac.Core.Providers.FileManagement.LocalStorage.csproj @@ -3,7 +3,11 @@ net6.0 enable - enable + disable + + + + From 93eff8e4baae704c9ed543b44d4acafa7f3db54c Mon Sep 17 00:00:00 2001 From: Hesam Asnaashari <46031836+ilhesam@users.noreply.github.com> Date: Sun, 26 Jun 2022 14:05:07 +0430 Subject: [PATCH 05/21] Added Name to IFileProvider --- .../IFileProvider.cs | 2 ++ .../LocalStorageFileProvider.cs | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/FileManagement/uBeac.Core.Providers.FileManagement.Abstractions/IFileProvider.cs b/src/FileManagement/uBeac.Core.Providers.FileManagement.Abstractions/IFileProvider.cs index 32326a3f..0b4b3e05 100644 --- a/src/FileManagement/uBeac.Core.Providers.FileManagement.Abstractions/IFileProvider.cs +++ b/src/FileManagement/uBeac.Core.Providers.FileManagement.Abstractions/IFileProvider.cs @@ -2,6 +2,8 @@ public interface IFileProvider { + string Name { get; } + Task Create(FileStream fileStream, string fileName, CancellationToken cancellationToken = default); Task Get(string fileName, CancellationToken cancellationToken = default); } \ No newline at end of file diff --git a/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalStorage/LocalStorageFileProvider.cs b/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalStorage/LocalStorageFileProvider.cs index 4236c73f..825400de 100644 --- a/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalStorage/LocalStorageFileProvider.cs +++ b/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalStorage/LocalStorageFileProvider.cs @@ -9,6 +9,8 @@ public LocalStorageFileProvider(LocalStorageOptions options) Options = options; } + public string Name => nameof(LocalStorageFileProvider); + public async Task Create(FileStream fileStream, string fileName, CancellationToken cancellationToken = default) { var path = GetFinalPath(fileName); From 87680eb9d1784c80d7f34db89aba19a665d70293 Mon Sep 17 00:00:00 2001 From: Hesam Asnaashari <46031836+ilhesam@users.noreply.github.com> Date: Sun, 26 Jun 2022 14:05:20 +0430 Subject: [PATCH 06/21] Added IFileService --- .../IFileService.cs | 13 +++++++++++++ ...Core.Services.FileManagement.Abstractions.csproj | 5 +++++ 2 files changed, 18 insertions(+) create mode 100644 src/FileManagement/uBeac.Core.Services.FileManagement.Abstractions/IFileService.cs diff --git a/src/FileManagement/uBeac.Core.Services.FileManagement.Abstractions/IFileService.cs b/src/FileManagement/uBeac.Core.Services.FileManagement.Abstractions/IFileService.cs new file mode 100644 index 00000000..344e8010 --- /dev/null +++ b/src/FileManagement/uBeac.Core.Services.FileManagement.Abstractions/IFileService.cs @@ -0,0 +1,13 @@ +namespace uBeac.Services.FileManagement; + +public interface IFileService : IService + where TKey : IEquatable + where TEntity : IFileEntity +{ + Task Create(FileStream fileStream, CancellationToken cancellationToken = default); +} + +public interface IFileService : IFileService + where TEntity : IFileEntity +{ +} \ No newline at end of file diff --git a/src/FileManagement/uBeac.Core.Services.FileManagement.Abstractions/uBeac.Core.Services.FileManagement.Abstractions.csproj b/src/FileManagement/uBeac.Core.Services.FileManagement.Abstractions/uBeac.Core.Services.FileManagement.Abstractions.csproj index 132c02c5..72554e93 100644 --- a/src/FileManagement/uBeac.Core.Services.FileManagement.Abstractions/uBeac.Core.Services.FileManagement.Abstractions.csproj +++ b/src/FileManagement/uBeac.Core.Services.FileManagement.Abstractions/uBeac.Core.Services.FileManagement.Abstractions.csproj @@ -6,4 +6,9 @@ enable + + + + + From b09f0440bf953f0ebbb704b6977744069be5c13d Mon Sep 17 00:00:00 2001 From: Hesam Asnaashari <46031836+ilhesam@users.noreply.github.com> Date: Sun, 26 Jun 2022 14:05:29 +0430 Subject: [PATCH 07/21] Implement FileService --- .../FileService.cs | 49 +++++++++++++++++++ .../uBeac.Core.Services.FileManagement.csproj | 6 +++ 2 files changed, 55 insertions(+) create mode 100644 src/FileManagement/uBeac.Core.Services.FileManagement/FileService.cs diff --git a/src/FileManagement/uBeac.Core.Services.FileManagement/FileService.cs b/src/FileManagement/uBeac.Core.Services.FileManagement/FileService.cs new file mode 100644 index 00000000..c2599692 --- /dev/null +++ b/src/FileManagement/uBeac.Core.Services.FileManagement/FileService.cs @@ -0,0 +1,49 @@ +using uBeac.Providers.FileManagement; +using uBeac.Repositories.FileManagement; + +namespace uBeac.Services.FileManagement; + +public class FileService : IFileService + where TKey : IEquatable + where TEntity : IFileEntity +{ + protected readonly IFileRepository Repository; + protected readonly IFileProvider Provider; + + public FileService(IFileRepository repository, IFileProvider provider) + { + Repository = repository; + Provider = provider; + } + + public async Task Create(FileStream fileStream, CancellationToken cancellationToken = default) + { + var fileName = GetRandomFileName(); + var fileExtension = GetFileExtension(fileStream); + var entity = CreateEntity(fileStream, fileName, fileExtension); + + await Provider.Create(fileStream, fileName, cancellationToken); + await Repository.Create(entity, cancellationToken); + } + + protected virtual TEntity CreateEntity(FileStream fileStream, string fileName, string fileExtension) + { + var entity = Activator.CreateInstance(); + entity.Name = fileName; + entity.Extension = fileExtension; + entity.Provider = Provider.Name; + return entity; + } + + protected virtual string GetRandomFileName() => Path.GetRandomFileName(); + + protected virtual string GetFileExtension(FileStream fileStream) => Path.GetExtension(fileStream.Name); +} + +public class FileService : FileService, IFileService + where TEntity : IFileEntity +{ + public FileService(IFileRepository repository, IFileProvider provider) : base(repository, provider) + { + } +} \ No newline at end of file diff --git a/src/FileManagement/uBeac.Core.Services.FileManagement/uBeac.Core.Services.FileManagement.csproj b/src/FileManagement/uBeac.Core.Services.FileManagement/uBeac.Core.Services.FileManagement.csproj index 132c02c5..1d57368b 100644 --- a/src/FileManagement/uBeac.Core.Services.FileManagement/uBeac.Core.Services.FileManagement.csproj +++ b/src/FileManagement/uBeac.Core.Services.FileManagement/uBeac.Core.Services.FileManagement.csproj @@ -6,4 +6,10 @@ enable + + + + + + From 5c0fec72c640b45c3f08482364dca77192c5e474 Mon Sep 17 00:00:00 2001 From: Hesam Asnaashari <46031836+ilhesam@users.noreply.github.com> Date: Mon, 27 Jun 2022 16:20:01 +0430 Subject: [PATCH 08/21] Initialized file management --- .../uBeac.Core.Common/Entities/FileEntity.cs | 2 + .../IFileCategory.cs | 13 +++++ .../IFileManagementBuilder.cs | 8 +++ .../IFileManager.cs | 6 ++ .../IFileProvider.cs | 14 +++++ .../IFileRepository.cs | 4 +- .../IFileService.cs | 19 +++++++ .../IFileServiceBuilder.cs | 12 ++++ .../IFileValidator.cs | 30 ++++++++++ ...ac.Core.FileManagement.Abstractions.csproj | 15 +++++ .../FileManagementBuilder.cs | 52 ++++++++++++++++++ .../uBeac.Core.FileManagement/FileManager.cs | 19 +++++++ .../uBeac.Core.FileManagement/FileService.cs | 55 +++++++++++++++++++ .../FileServiceBuilder.cs | 27 +++++++++ .../Validators/FileExtensionValidator.cs | 17 ++++++ .../FileServiceBuilderExtensions.cs | 15 +++++ .../uBeac.Core.FileManagement.csproj | 17 ++++++ .../IFileProvider.cs | 9 --- .../FileServiceBuilderExtensions.cs | 16 ++++++ .../LocalStorageFileProvider.cs | 9 +-- .../LocalStorageOptions.cs | 4 +- ...leManagement.Providers.LocalStorage.csproj | 17 ++++++ ...oviders.FileManagement.LocalStorage.csproj | 13 ----- .../FileServiceBuilderExtensions.cs | 30 ++++++++++ .../MongoFileRepository.cs | 2 +- ...ileManagement.Repositories.MongoDB.csproj} | 2 +- .../IFileService.cs | 13 ----- .../FileService.cs | 49 ----------------- .../uBeac.Core.FileManagement.Services.csproj | 9 +++ .../uBeac.Core.Services.FileManagement.csproj | 15 ----- src/Identity/Tests/API/API.csproj | 3 + .../Tests/API/Config/file-management.json | 8 +++ .../API/Controllers/AccountsController.cs | 11 +++- src/Identity/Tests/API/Program.cs | 22 +++++++- src/uBeac.Core.sln | 46 +++++++--------- 35 files changed, 463 insertions(+), 140 deletions(-) create mode 100644 src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileCategory.cs create mode 100644 src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileManagementBuilder.cs create mode 100644 src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileManager.cs create mode 100644 src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileProvider.cs rename src/FileManagement/{uBeac.Core.Repositories.FileManagement.Abstractions => uBeac.Core.FileManagement.Abstractions}/IFileRepository.cs (82%) create mode 100644 src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileService.cs create mode 100644 src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileServiceBuilder.cs create mode 100644 src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileValidator.cs create mode 100644 src/FileManagement/uBeac.Core.FileManagement.Abstractions/uBeac.Core.FileManagement.Abstractions.csproj create mode 100644 src/FileManagement/uBeac.Core.FileManagement/FileManagementBuilder.cs create mode 100644 src/FileManagement/uBeac.Core.FileManagement/FileManager.cs create mode 100644 src/FileManagement/uBeac.Core.FileManagement/FileService.cs create mode 100644 src/FileManagement/uBeac.Core.FileManagement/FileServiceBuilder.cs create mode 100644 src/FileManagement/uBeac.Core.FileManagement/Validators/FileExtensionValidator.cs create mode 100644 src/FileManagement/uBeac.Core.FileManagement/Validators/FileServiceBuilderExtensions.cs create mode 100644 src/FileManagement/uBeac.Core.FileManagement/uBeac.Core.FileManagement.csproj delete mode 100644 src/FileManagement/uBeac.Core.Providers.FileManagement.Abstractions/IFileProvider.cs create mode 100644 src/FileManagement/uBeac.Core.Providers.FileManagement.LocalStorage/FileServiceBuilderExtensions.cs create mode 100644 src/FileManagement/uBeac.Core.Providers.FileManagement.LocalStorage/uBeac.Core.FileManagement.Providers.LocalStorage.csproj delete mode 100644 src/FileManagement/uBeac.Core.Providers.FileManagement.LocalStorage/uBeac.Core.Providers.FileManagement.LocalStorage.csproj create mode 100644 src/FileManagement/uBeac.Core.Repositories.FileManagement.MongoDB/FileServiceBuilderExtensions.cs rename src/FileManagement/uBeac.Core.Repositories.FileManagement.MongoDB/{uBeac.Core.Repositories.FileManagement.MongoDB.csproj => uBeac.Core.FileManagement.Repositories.MongoDB.csproj} (70%) delete mode 100644 src/FileManagement/uBeac.Core.Services.FileManagement.Abstractions/IFileService.cs delete mode 100644 src/FileManagement/uBeac.Core.Services.FileManagement/FileService.cs create mode 100644 src/FileManagement/uBeac.Core.Services.FileManagement/uBeac.Core.FileManagement.Services.csproj delete mode 100644 src/FileManagement/uBeac.Core.Services.FileManagement/uBeac.Core.Services.FileManagement.csproj create mode 100644 src/Identity/Tests/API/Config/file-management.json diff --git a/src/Common/uBeac.Core.Common/Entities/FileEntity.cs b/src/Common/uBeac.Core.Common/Entities/FileEntity.cs index 94682589..22bad03c 100644 --- a/src/Common/uBeac.Core.Common/Entities/FileEntity.cs +++ b/src/Common/uBeac.Core.Common/Entities/FileEntity.cs @@ -7,6 +7,7 @@ public interface IFileEntity : IAuditEntity string Extension { get; set; } string Provider { get; set; } string Path { get; set; } + string Category { get; set; } } public interface IFileEntity : IFileEntity, IAuditEntity @@ -20,6 +21,7 @@ public class FileEntity : AuditEntity, IFileEntity public string Extension { get; set; } public string Provider { get; set; } public string Path { get; set; } + public string Category { get; set; } } public class FileEntity : FileEntity, IFileEntity diff --git a/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileCategory.cs b/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileCategory.cs new file mode 100644 index 00000000..6ec4b27d --- /dev/null +++ b/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileCategory.cs @@ -0,0 +1,13 @@ +namespace uBeac.FileManagement; + +public interface IFileCategory +{ + string CategoryName { get; } + IFileService Service { get; } +} + +public class FileCategory : IFileCategory +{ + public string CategoryName { get; set; } + public IFileService Service { get; set; } +} \ No newline at end of file diff --git a/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileManagementBuilder.cs b/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileManagementBuilder.cs new file mode 100644 index 00000000..e86a2357 --- /dev/null +++ b/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileManagementBuilder.cs @@ -0,0 +1,8 @@ +namespace uBeac.FileManagement; + +public interface IFileManagementBuilder + where TKey : IEquatable + where TEntity : IFileEntity +{ + IFileServiceBuilder AddCategory(string categoryName); +} \ No newline at end of file diff --git a/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileManager.cs b/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileManager.cs new file mode 100644 index 00000000..16890e6f --- /dev/null +++ b/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileManager.cs @@ -0,0 +1,6 @@ +namespace uBeac.FileManagement; + +public interface IFileManager +{ + Task Create(FileStream fileStream, string category, CancellationToken cancellationToken = default); +} \ No newline at end of file diff --git a/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileProvider.cs b/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileProvider.cs new file mode 100644 index 00000000..007da93f --- /dev/null +++ b/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileProvider.cs @@ -0,0 +1,14 @@ +namespace uBeac.FileManagement; + +public interface IFileProvider +{ + string Name { get; } + + Task Create(FileStream fileStream, string fileName, CancellationToken cancellationToken = default); + Task Get(string fileName, CancellationToken cancellationToken = default); +} + +public class CreateFileResult +{ + public string FilePath { get; set; } +} \ No newline at end of file diff --git a/src/FileManagement/uBeac.Core.Repositories.FileManagement.Abstractions/IFileRepository.cs b/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileRepository.cs similarity index 82% rename from src/FileManagement/uBeac.Core.Repositories.FileManagement.Abstractions/IFileRepository.cs rename to src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileRepository.cs index b425d5c5..7f712342 100644 --- a/src/FileManagement/uBeac.Core.Repositories.FileManagement.Abstractions/IFileRepository.cs +++ b/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileRepository.cs @@ -1,4 +1,6 @@ -namespace uBeac.Repositories.FileManagement; +using uBeac.Repositories; + +namespace uBeac.FileManagement; public interface IFileRepository : IEntityRepository where TKey : IEquatable diff --git a/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileService.cs b/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileService.cs new file mode 100644 index 00000000..30f6f227 --- /dev/null +++ b/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileService.cs @@ -0,0 +1,19 @@ +using uBeac.Services; + +namespace uBeac.FileManagement; + +public interface IFileService : IService +{ + Task Create(FileStream fileStream, string category, CancellationToken cancellationToken = default); +} + +public interface IFileService : IFileService + where TKey : IEquatable + where TEntity : IFileEntity +{ +} + +public interface IFileService : IFileService + where TEntity : IFileEntity +{ +} \ No newline at end of file diff --git a/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileServiceBuilder.cs b/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileServiceBuilder.cs new file mode 100644 index 00000000..452c744c --- /dev/null +++ b/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileServiceBuilder.cs @@ -0,0 +1,12 @@ +namespace uBeac.FileManagement; + +public interface IFileServiceBuilder + where TKey : IEquatable + where TEntity : IFileEntity +{ + List> Validators { get; } + Func> Repository { get; set; } + Func Provider { get; set; } + + void AddValidator(IFileValidator validator); +} \ No newline at end of file diff --git a/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileValidator.cs b/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileValidator.cs new file mode 100644 index 00000000..e186f5fd --- /dev/null +++ b/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileValidator.cs @@ -0,0 +1,30 @@ +namespace uBeac.FileManagement; + +public interface IFileValidator +{ + IFileValidationResult Validate(FileStream fileStream); +} + +public interface IFileValidationResult +{ + bool Validated { get; set; } + Exception Exception { get; set; } +} + +public class FileValidationResult : IFileValidationResult +{ + public FileValidationResult() + { + Validated = true; + Exception = null; + } + + public FileValidationResult(Exception exception) + { + Validated = false; + Exception = exception; + } + + public bool Validated { get; set; } + public Exception Exception { get; set; } +} \ No newline at end of file diff --git a/src/FileManagement/uBeac.Core.FileManagement.Abstractions/uBeac.Core.FileManagement.Abstractions.csproj b/src/FileManagement/uBeac.Core.FileManagement.Abstractions/uBeac.Core.FileManagement.Abstractions.csproj new file mode 100644 index 00000000..d8fbe4b9 --- /dev/null +++ b/src/FileManagement/uBeac.Core.FileManagement.Abstractions/uBeac.Core.FileManagement.Abstractions.csproj @@ -0,0 +1,15 @@ + + + + net6.0 + enable + disable + + + + + + + + + diff --git a/src/FileManagement/uBeac.Core.FileManagement/FileManagementBuilder.cs b/src/FileManagement/uBeac.Core.FileManagement/FileManagementBuilder.cs new file mode 100644 index 00000000..adb5b630 --- /dev/null +++ b/src/FileManagement/uBeac.Core.FileManagement/FileManagementBuilder.cs @@ -0,0 +1,52 @@ +using Microsoft.Extensions.DependencyInjection.Extensions; +using uBeac; +using uBeac.FileManagement; + +namespace Microsoft.Extensions.DependencyInjection; + +public class FileManagementBuilder : IFileManagementBuilder + where TKey : IEquatable + where TEntity : IFileEntity +{ + protected readonly IServiceCollection Services; + + public FileManagementBuilder(IServiceCollection services) + { + Services = services; + } + + public IFileServiceBuilder AddCategory(string categoryName) + { + var builder = new FileServiceBuilder(); + + Services.AddScoped(serviceProvider => + { + var service = builder.Build(serviceProvider); + return new FileCategory { CategoryName = categoryName, Service = service }; + }); + + return builder; + } +} + +public static class ServiceCollectionExtensions +{ + public static IServiceCollection AddFileManagement(this IServiceCollection services, Action> options) + where TKey : IEquatable + where TEntity : IFileEntity + { + var builder = new FileManagementBuilder(services); + options.Invoke(builder); + + services.TryAddScoped(); + + return services; + } + + public static IServiceCollection AddFileManagement(this IServiceCollection services, Action> options) + where TEntity : IFileEntity + => AddFileManagement(services, options); + + public static IServiceCollection AddFileManagement(this IServiceCollection services, Action> options) + => AddFileManagement(services, options); +} \ No newline at end of file diff --git a/src/FileManagement/uBeac.Core.FileManagement/FileManager.cs b/src/FileManagement/uBeac.Core.FileManagement/FileManager.cs new file mode 100644 index 00000000..1706d901 --- /dev/null +++ b/src/FileManagement/uBeac.Core.FileManagement/FileManager.cs @@ -0,0 +1,19 @@ +namespace uBeac.FileManagement; + +public class FileManager : IFileManager +{ + protected readonly List Categories; + + public FileManager(IEnumerable categories) + { + Categories = categories.ToList(); + } + + public async Task Create(FileStream fileStream, string category, CancellationToken cancellationToken = default) + { + var service = GetService(category); + await service.Create(fileStream, category, cancellationToken); + } + + protected IFileService GetService(string category) => Categories.Single(c => c.CategoryName == category).Service; +} \ No newline at end of file diff --git a/src/FileManagement/uBeac.Core.FileManagement/FileService.cs b/src/FileManagement/uBeac.Core.FileManagement/FileService.cs new file mode 100644 index 00000000..9229de1f --- /dev/null +++ b/src/FileManagement/uBeac.Core.FileManagement/FileService.cs @@ -0,0 +1,55 @@ +namespace uBeac.FileManagement; + +public class FileService : IFileService + where TKey : IEquatable + where TEntity : IFileEntity +{ + protected readonly List Validators; + protected readonly IFileRepository Repository; + protected readonly IFileProvider Provider; + + public FileService(IEnumerable validators, IFileRepository repository, IFileProvider provider) + { + Validators = validators.ToList(); + Repository = repository; + Provider = provider; + } + + public async Task Create(FileStream fileStream, string category, CancellationToken cancellationToken = default) + { + ThrowExceptionIfNotValid(fileStream); + + var fileName = Path.GetRandomFileName(); + var fileExtension = Path.GetExtension(fileStream.Name); + + var createResult = await Provider.Create(fileStream, fileName, cancellationToken); + + var entity = CreateInstance(fileStream, fileName, fileExtension, createResult.FilePath, category); + await Repository.Create(entity, cancellationToken); + } + + protected virtual TEntity CreateInstance(FileStream fileStream, string fileName, string fileExtension, string filePath, string category) + { + var entity = Activator.CreateInstance(); + entity.Name = fileName; + entity.Extension = fileExtension; + entity.Provider = Provider.Name; + entity.Path = filePath; + entity.Category = category; + return entity; + } + + protected void ThrowExceptionIfNotValid(FileStream fileStream) => Validators.ForEach(validator => + { + var result = validator.Validate(fileStream); + if (!result.Validated) throw result.Exception; + }); +} + +public class FileService : FileService, IFileService + where TEntity : IFileEntity +{ + public FileService(IEnumerable validators, IFileRepository repository, IFileProvider provider) : base(validators, repository, provider) + { + } +} \ No newline at end of file diff --git a/src/FileManagement/uBeac.Core.FileManagement/FileServiceBuilder.cs b/src/FileManagement/uBeac.Core.FileManagement/FileServiceBuilder.cs new file mode 100644 index 00000000..ed6b833a --- /dev/null +++ b/src/FileManagement/uBeac.Core.FileManagement/FileServiceBuilder.cs @@ -0,0 +1,27 @@ +using uBeac; +using uBeac.FileManagement; + +namespace Microsoft.Extensions.DependencyInjection; + +public class FileServiceBuilder : IFileServiceBuilder + where TKey : IEquatable + where TEntity : IFileEntity +{ + public List> Validators { get; } = new(); + public Func> Repository { get; set; } + public Func Provider { get; set; } + + public void AddValidator(IFileValidator validator) + { + Validators.Add(_ => validator); + } + + internal IFileService Build(IServiceProvider serviceProvider) + { + var validators = Validators.Select(validator => validator(serviceProvider)); + var repositories = Repository(serviceProvider); + var providers = Provider(serviceProvider); + + return ActivatorUtilities.CreateInstance>(serviceProvider, repositories, providers, validators); + } +} \ No newline at end of file diff --git a/src/FileManagement/uBeac.Core.FileManagement/Validators/FileExtensionValidator.cs b/src/FileManagement/uBeac.Core.FileManagement/Validators/FileExtensionValidator.cs new file mode 100644 index 00000000..4ddfb858 --- /dev/null +++ b/src/FileManagement/uBeac.Core.FileManagement/Validators/FileExtensionValidator.cs @@ -0,0 +1,17 @@ +namespace uBeac.FileManagement; + +public class FileExtensionValidator : IFileValidator +{ + protected readonly string[] ValidExtensions; + + public FileExtensionValidator(IEnumerable validExtensions) + { + ValidExtensions = validExtensions.Select(e => e.ToUpper()).ToArray(); + } + + public IFileValidationResult Validate(FileStream fileStream) + { + var extension = Path.GetExtension(fileStream.Name).ToUpper(); + return ValidExtensions.Contains(extension) ? new FileValidationResult() : new FileValidationResult(new Exception("File extension is not valid!")); + } +} \ No newline at end of file diff --git a/src/FileManagement/uBeac.Core.FileManagement/Validators/FileServiceBuilderExtensions.cs b/src/FileManagement/uBeac.Core.FileManagement/Validators/FileServiceBuilderExtensions.cs new file mode 100644 index 00000000..c00cd2a3 --- /dev/null +++ b/src/FileManagement/uBeac.Core.FileManagement/Validators/FileServiceBuilderExtensions.cs @@ -0,0 +1,15 @@ +using uBeac; +using uBeac.FileManagement; + +namespace Microsoft.Extensions.DependencyInjection; + +public static class FileServiceBuilderExtensions +{ + public static IFileServiceBuilder SetValidExtensions(this IFileServiceBuilder builder, string[] validExtensions) + where TKey : IEquatable + where TEntity : IFileEntity + { + builder.AddValidator(new FileExtensionValidator(validExtensions)); + return builder; + } +} \ No newline at end of file diff --git a/src/FileManagement/uBeac.Core.FileManagement/uBeac.Core.FileManagement.csproj b/src/FileManagement/uBeac.Core.FileManagement/uBeac.Core.FileManagement.csproj new file mode 100644 index 00000000..e66cee41 --- /dev/null +++ b/src/FileManagement/uBeac.Core.FileManagement/uBeac.Core.FileManagement.csproj @@ -0,0 +1,17 @@ + + + + net6.0 + enable + disable + + + + + + + + + + + diff --git a/src/FileManagement/uBeac.Core.Providers.FileManagement.Abstractions/IFileProvider.cs b/src/FileManagement/uBeac.Core.Providers.FileManagement.Abstractions/IFileProvider.cs deleted file mode 100644 index 0b4b3e05..00000000 --- a/src/FileManagement/uBeac.Core.Providers.FileManagement.Abstractions/IFileProvider.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace uBeac.Providers.FileManagement; - -public interface IFileProvider -{ - string Name { get; } - - Task Create(FileStream fileStream, string fileName, CancellationToken cancellationToken = default); - Task Get(string fileName, CancellationToken cancellationToken = default); -} \ No newline at end of file diff --git a/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalStorage/FileServiceBuilderExtensions.cs b/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalStorage/FileServiceBuilderExtensions.cs new file mode 100644 index 00000000..f3ec78b2 --- /dev/null +++ b/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalStorage/FileServiceBuilderExtensions.cs @@ -0,0 +1,16 @@ +using uBeac; +using uBeac.FileManagement; +using uBeac.FileManagement.LocalStorage; + +namespace Microsoft.Extensions.DependencyInjection; + +public static class FileServiceBuilderExtensions +{ + public static IFileServiceBuilder StoreFilesInLocalStorage(this IFileServiceBuilder builder, FileManagementLocalStorageOptions options) + where TKey : IEquatable + where TEntity : IFileEntity + { + builder.Provider = serviceProvider => ActivatorUtilities.CreateInstance(serviceProvider, options); + return builder; + } +} \ No newline at end of file diff --git a/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalStorage/LocalStorageFileProvider.cs b/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalStorage/LocalStorageFileProvider.cs index 825400de..f5e510fb 100644 --- a/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalStorage/LocalStorageFileProvider.cs +++ b/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalStorage/LocalStorageFileProvider.cs @@ -1,21 +1,22 @@ -namespace uBeac.Providers.FileManagement.LocalStorage; +namespace uBeac.FileManagement.LocalStorage; public class LocalStorageFileProvider : IFileProvider { - protected readonly LocalStorageOptions Options; + protected readonly FileManagementLocalStorageOptions Options; - public LocalStorageFileProvider(LocalStorageOptions options) + public LocalStorageFileProvider(FileManagementLocalStorageOptions options) { Options = options; } public string Name => nameof(LocalStorageFileProvider); - public async Task Create(FileStream fileStream, string fileName, CancellationToken cancellationToken = default) + public async Task Create(FileStream fileStream, string fileName, CancellationToken cancellationToken = default) { var path = GetFinalPath(fileName); await using var createStream = new FileStream(path, FileMode.Create, FileAccess.Write); await fileStream.CopyToAsync(createStream, cancellationToken); + return new CreateFileResult { FilePath = path }; } public async Task Get(string fileName, CancellationToken cancellationToken = default) diff --git a/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalStorage/LocalStorageOptions.cs b/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalStorage/LocalStorageOptions.cs index bbe05f67..7b8398d6 100644 --- a/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalStorage/LocalStorageOptions.cs +++ b/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalStorage/LocalStorageOptions.cs @@ -1,6 +1,6 @@ -namespace uBeac.Providers.FileManagement.LocalStorage; +namespace uBeac.FileManagement.LocalStorage; -public class LocalStorageOptions +public class FileManagementLocalStorageOptions { public string DirectoryPath { get; set; } } \ No newline at end of file diff --git a/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalStorage/uBeac.Core.FileManagement.Providers.LocalStorage.csproj b/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalStorage/uBeac.Core.FileManagement.Providers.LocalStorage.csproj new file mode 100644 index 00000000..e66cee41 --- /dev/null +++ b/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalStorage/uBeac.Core.FileManagement.Providers.LocalStorage.csproj @@ -0,0 +1,17 @@ + + + + net6.0 + enable + disable + + + + + + + + + + + diff --git a/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalStorage/uBeac.Core.Providers.FileManagement.LocalStorage.csproj b/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalStorage/uBeac.Core.Providers.FileManagement.LocalStorage.csproj deleted file mode 100644 index e5678224..00000000 --- a/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalStorage/uBeac.Core.Providers.FileManagement.LocalStorage.csproj +++ /dev/null @@ -1,13 +0,0 @@ - - - - net6.0 - enable - disable - - - - - - - diff --git a/src/FileManagement/uBeac.Core.Repositories.FileManagement.MongoDB/FileServiceBuilderExtensions.cs b/src/FileManagement/uBeac.Core.Repositories.FileManagement.MongoDB/FileServiceBuilderExtensions.cs new file mode 100644 index 00000000..8a629682 --- /dev/null +++ b/src/FileManagement/uBeac.Core.Repositories.FileManagement.MongoDB/FileServiceBuilderExtensions.cs @@ -0,0 +1,30 @@ +using uBeac; +using uBeac.FileManagement; +using uBeac.FileManagement.MongoDB; +using uBeac.Repositories.MongoDB; + +namespace Microsoft.Extensions.DependencyInjection; + +public static class FileServiceBuilderExtensions +{ + public static IFileServiceBuilder StoreInfoInMongoDb(this IFileServiceBuilder builder) + where TKey : IEquatable + where TEntity : IFileEntity + where TContext : IMongoDBContext + { + builder.Repository = ActivatorUtilities.GetServiceOrCreateInstance>; + return builder; + } + + public static IFileServiceBuilder StoreInfoInMongoDb(this IFileServiceBuilder builder) + where TEntity : IFileEntity + where TContext : IMongoDBContext + => StoreInfoInMongoDb(builder); + + public static IFileServiceBuilder StoreInfoInMongoDb(this IFileServiceBuilder builder) + where TContext : IMongoDBContext + => StoreInfoInMongoDb(builder); + + public static IFileServiceBuilder StoreInfoInMongoDb(this IFileServiceBuilder builder) + => StoreInfoInMongoDb(builder); +} \ No newline at end of file diff --git a/src/FileManagement/uBeac.Core.Repositories.FileManagement.MongoDB/MongoFileRepository.cs b/src/FileManagement/uBeac.Core.Repositories.FileManagement.MongoDB/MongoFileRepository.cs index 5dac57de..86930c26 100644 --- a/src/FileManagement/uBeac.Core.Repositories.FileManagement.MongoDB/MongoFileRepository.cs +++ b/src/FileManagement/uBeac.Core.Repositories.FileManagement.MongoDB/MongoFileRepository.cs @@ -1,6 +1,6 @@ using uBeac.Repositories.MongoDB; -namespace uBeac.Repositories.FileManagement.MongoDB; +namespace uBeac.FileManagement.MongoDB; public class MongoFileRepository : MongoEntityRepository, IFileRepository where TKey : IEquatable diff --git a/src/FileManagement/uBeac.Core.Repositories.FileManagement.MongoDB/uBeac.Core.Repositories.FileManagement.MongoDB.csproj b/src/FileManagement/uBeac.Core.Repositories.FileManagement.MongoDB/uBeac.Core.FileManagement.Repositories.MongoDB.csproj similarity index 70% rename from src/FileManagement/uBeac.Core.Repositories.FileManagement.MongoDB/uBeac.Core.Repositories.FileManagement.MongoDB.csproj rename to src/FileManagement/uBeac.Core.Repositories.FileManagement.MongoDB/uBeac.Core.FileManagement.Repositories.MongoDB.csproj index 220c9528..39d50f55 100644 --- a/src/FileManagement/uBeac.Core.Repositories.FileManagement.MongoDB/uBeac.Core.Repositories.FileManagement.MongoDB.csproj +++ b/src/FileManagement/uBeac.Core.Repositories.FileManagement.MongoDB/uBeac.Core.FileManagement.Repositories.MongoDB.csproj @@ -8,7 +8,7 @@ - + diff --git a/src/FileManagement/uBeac.Core.Services.FileManagement.Abstractions/IFileService.cs b/src/FileManagement/uBeac.Core.Services.FileManagement.Abstractions/IFileService.cs deleted file mode 100644 index 344e8010..00000000 --- a/src/FileManagement/uBeac.Core.Services.FileManagement.Abstractions/IFileService.cs +++ /dev/null @@ -1,13 +0,0 @@ -namespace uBeac.Services.FileManagement; - -public interface IFileService : IService - where TKey : IEquatable - where TEntity : IFileEntity -{ - Task Create(FileStream fileStream, CancellationToken cancellationToken = default); -} - -public interface IFileService : IFileService - where TEntity : IFileEntity -{ -} \ No newline at end of file diff --git a/src/FileManagement/uBeac.Core.Services.FileManagement/FileService.cs b/src/FileManagement/uBeac.Core.Services.FileManagement/FileService.cs deleted file mode 100644 index c2599692..00000000 --- a/src/FileManagement/uBeac.Core.Services.FileManagement/FileService.cs +++ /dev/null @@ -1,49 +0,0 @@ -using uBeac.Providers.FileManagement; -using uBeac.Repositories.FileManagement; - -namespace uBeac.Services.FileManagement; - -public class FileService : IFileService - where TKey : IEquatable - where TEntity : IFileEntity -{ - protected readonly IFileRepository Repository; - protected readonly IFileProvider Provider; - - public FileService(IFileRepository repository, IFileProvider provider) - { - Repository = repository; - Provider = provider; - } - - public async Task Create(FileStream fileStream, CancellationToken cancellationToken = default) - { - var fileName = GetRandomFileName(); - var fileExtension = GetFileExtension(fileStream); - var entity = CreateEntity(fileStream, fileName, fileExtension); - - await Provider.Create(fileStream, fileName, cancellationToken); - await Repository.Create(entity, cancellationToken); - } - - protected virtual TEntity CreateEntity(FileStream fileStream, string fileName, string fileExtension) - { - var entity = Activator.CreateInstance(); - entity.Name = fileName; - entity.Extension = fileExtension; - entity.Provider = Provider.Name; - return entity; - } - - protected virtual string GetRandomFileName() => Path.GetRandomFileName(); - - protected virtual string GetFileExtension(FileStream fileStream) => Path.GetExtension(fileStream.Name); -} - -public class FileService : FileService, IFileService - where TEntity : IFileEntity -{ - public FileService(IFileRepository repository, IFileProvider provider) : base(repository, provider) - { - } -} \ No newline at end of file diff --git a/src/FileManagement/uBeac.Core.Services.FileManagement/uBeac.Core.FileManagement.Services.csproj b/src/FileManagement/uBeac.Core.Services.FileManagement/uBeac.Core.FileManagement.Services.csproj new file mode 100644 index 00000000..132c02c5 --- /dev/null +++ b/src/FileManagement/uBeac.Core.Services.FileManagement/uBeac.Core.FileManagement.Services.csproj @@ -0,0 +1,9 @@ + + + + net6.0 + enable + enable + + + diff --git a/src/FileManagement/uBeac.Core.Services.FileManagement/uBeac.Core.Services.FileManagement.csproj b/src/FileManagement/uBeac.Core.Services.FileManagement/uBeac.Core.Services.FileManagement.csproj deleted file mode 100644 index 1d57368b..00000000 --- a/src/FileManagement/uBeac.Core.Services.FileManagement/uBeac.Core.Services.FileManagement.csproj +++ /dev/null @@ -1,15 +0,0 @@ - - - - net6.0 - enable - enable - - - - - - - - - diff --git a/src/Identity/Tests/API/API.csproj b/src/Identity/Tests/API/API.csproj index 05fa98f3..9a3941c8 100644 --- a/src/Identity/Tests/API/API.csproj +++ b/src/Identity/Tests/API/API.csproj @@ -13,6 +13,9 @@ + + + diff --git a/src/Identity/Tests/API/Config/file-management.json b/src/Identity/Tests/API/Config/file-management.json new file mode 100644 index 00000000..1b909397 --- /dev/null +++ b/src/Identity/Tests/API/Config/file-management.json @@ -0,0 +1,8 @@ +{ + "Avatars": { + "DirectoryPath": "C:\\Users\\Hesam\\Documents\\uBeac\\Avatars" + }, + "Documents": { + "DirectoryPath": "C:\\Users\\Hesam\\Documents\\uBeac\\Documents" + } +} \ No newline at end of file diff --git a/src/Identity/Tests/API/Controllers/AccountsController.cs b/src/Identity/Tests/API/Controllers/AccountsController.cs index 58da6e8f..f7246383 100644 --- a/src/Identity/Tests/API/Controllers/AccountsController.cs +++ b/src/Identity/Tests/API/Controllers/AccountsController.cs @@ -1,8 +1,15 @@ -namespace API; +using uBeac.FileManagement; + +namespace API; public class AccountsController : AccountsControllerBase { - public AccountsController(IUserService userService) : base(userService) + public AccountsController(IUserService userService, IFileManager fileManager) : base(userService) { + using var avatar = File.OpenRead("C:\\Users\\Hesam\\Pictures\\f518a14c0cad2394cefc700675bb7b4d.jpg"); + fileManager.Create(avatar, "Avatars").Wait(); + + using var document = File.OpenRead("C:\\Users\\Hesam\\Pictures\\ds.png"); + fileManager.Create(document, "Documents").Wait(); } } \ No newline at end of file diff --git a/src/Identity/Tests/API/Program.cs b/src/Identity/Tests/API/Program.cs index 714a6186..2aaf00ec 100644 --- a/src/Identity/Tests/API/Program.cs +++ b/src/Identity/Tests/API/Program.cs @@ -1,5 +1,6 @@ using System.Reflection; using Microsoft.AspNetCore.Mvc; +using uBeac.FileManagement.LocalStorage; using uBeac.Repositories.History.MongoDB; using uBeac.Repositories.MongoDB; using uBeac.Web; @@ -11,9 +12,6 @@ // Adding json config files (IConfiguration) builder.Configuration.AddJsonConfig(builder.Environment); -// Adding bson serializers -//builder.Services.AddDefaultBsonSerializers(); - // Adding http logging builder.Services.AddMongoDbHttpLogging(() => { @@ -26,12 +24,30 @@ builder.Services.AddControllers(); builder.Services.AddAutoMapper(Assembly.GetExecutingAssembly()); +builder.Services.AddFileManagement(options => +{ + options.AddCategory("Avatars") + .SetValidExtensions(new[] { ".png", ".jpg", ".jpeg", ".bmp" }) + .StoreInfoInMongoDb() + .StoreFilesInLocalStorage(builder.Configuration.GetInstance("Avatars")); + + options.AddCategory("Documents") + .SetValidExtensions(new[] { ".docx", ".pdf" }) + .StoreInfoInMongoDb() + .StoreFilesInLocalStorage(builder.Configuration.GetInstance("Documents")); +}); + // Disabling automatic model state validation builder.Services.Configure(options => { options.SuppressModelStateInvalidFilter = true; }); +builder.Services.AddCors(options => +{ + options.AddPolicy("ds", policy => policy.AllowAnyHeader().Build()); +}); + // Adding application context builder.Services.AddApplicationContext(); diff --git a/src/uBeac.Core.sln b/src/uBeac.Core.sln index 6a9dcea0..b9224fe0 100644 --- a/src/uBeac.Core.sln +++ b/src/uBeac.Core.sln @@ -55,17 +55,15 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "uBeac.Core.Web.Logging.Mong EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "FileManagement", "FileManagement", "{0010CB43-FD6D-4C91-91D9-3065ACBAC2E1}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "uBeac.Core.Services.FileManagement", "FileManagement\uBeac.Core.Services.FileManagement\uBeac.Core.Services.FileManagement.csproj", "{9020A29D-E4B8-42AE-9EE3-BC2B001A0CB9}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "uBeac.Core.FileManagement.Repositories.MongoDB", "FileManagement\uBeac.Core.Repositories.FileManagement.MongoDB\uBeac.Core.FileManagement.Repositories.MongoDB.csproj", "{6B9F2368-1F55-43C0-9DDD-1CDE744F3118}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "uBeac.Core.Repositories.FileManagement.MongoDB", "FileManagement\uBeac.Core.Repositories.FileManagement.MongoDB\uBeac.Core.Repositories.FileManagement.MongoDB.csproj", "{6B9F2368-1F55-43C0-9DDD-1CDE744F3118}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "uBeac.Core.FileManagement.Providers.LocalStorage", "FileManagement\uBeac.Core.Providers.FileManagement.LocalStorage\uBeac.Core.FileManagement.Providers.LocalStorage.csproj", "{6780E437-7D94-4B08-9A25-CA6698C5D8DA}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "uBeac.Core.Repositories.FileManagement.Abstractions", "FileManagement\uBeac.Core.Repositories.FileManagement.Abstractions\uBeac.Core.Repositories.FileManagement.Abstractions.csproj", "{6BAB5C0D-2624-43BC-B126-D40601B1E7A9}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "uBeac.Core.FileManagement.Abstractions", "FileManagement\uBeac.Core.FileManagement.Abstractions\uBeac.Core.FileManagement.Abstractions.csproj", "{71C569AC-28C8-4268-81A2-964D7C48EBAC}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "uBeac.Core.Providers.FileManagement.Abstractions", "FileManagement\uBeac.Core.Providers.FileManagement.Abstractions\uBeac.Core.Providers.FileManagement.Abstractions.csproj", "{D3461DFD-840B-4AB9-93C2-7B329FCF8536}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "uBeac.Core.FileManagement", "FileManagement\uBeac.Core.FileManagement\uBeac.Core.FileManagement.csproj", "{2D14DD45-130F-4B6B-B2CD-5F3F7AD28F96}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "uBeac.Core.Providers.FileManagement.LocalStorage", "FileManagement\uBeac.Core.Providers.FileManagement.LocalStorage\uBeac.Core.Providers.FileManagement.LocalStorage.csproj", "{6780E437-7D94-4B08-9A25-CA6698C5D8DA}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "uBeac.Core.Services.FileManagement.Abstractions", "FileManagement\uBeac.Core.Services.FileManagement.Abstractions\uBeac.Core.Services.FileManagement.Abstractions.csproj", "{EA9BA74E-EE81-40E5-976A-EAB7C586F3D8}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "API", "Identity\Tests\API\API.csproj", "{04D4D6DE-2782-4A0D-9B74-1FF5141BE112}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -149,30 +147,26 @@ Global {2D538398-EAC9-4A18-8459-74CA9E499F4E}.Debug|Any CPU.Build.0 = Debug|Any CPU {2D538398-EAC9-4A18-8459-74CA9E499F4E}.Release|Any CPU.ActiveCfg = Release|Any CPU {2D538398-EAC9-4A18-8459-74CA9E499F4E}.Release|Any CPU.Build.0 = Release|Any CPU - {9020A29D-E4B8-42AE-9EE3-BC2B001A0CB9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {9020A29D-E4B8-42AE-9EE3-BC2B001A0CB9}.Debug|Any CPU.Build.0 = Debug|Any CPU - {9020A29D-E4B8-42AE-9EE3-BC2B001A0CB9}.Release|Any CPU.ActiveCfg = Release|Any CPU - {9020A29D-E4B8-42AE-9EE3-BC2B001A0CB9}.Release|Any CPU.Build.0 = Release|Any CPU {6B9F2368-1F55-43C0-9DDD-1CDE744F3118}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {6B9F2368-1F55-43C0-9DDD-1CDE744F3118}.Debug|Any CPU.Build.0 = Debug|Any CPU {6B9F2368-1F55-43C0-9DDD-1CDE744F3118}.Release|Any CPU.ActiveCfg = Release|Any CPU {6B9F2368-1F55-43C0-9DDD-1CDE744F3118}.Release|Any CPU.Build.0 = Release|Any CPU - {6BAB5C0D-2624-43BC-B126-D40601B1E7A9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {6BAB5C0D-2624-43BC-B126-D40601B1E7A9}.Debug|Any CPU.Build.0 = Debug|Any CPU - {6BAB5C0D-2624-43BC-B126-D40601B1E7A9}.Release|Any CPU.ActiveCfg = Release|Any CPU - {6BAB5C0D-2624-43BC-B126-D40601B1E7A9}.Release|Any CPU.Build.0 = Release|Any CPU - {D3461DFD-840B-4AB9-93C2-7B329FCF8536}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {D3461DFD-840B-4AB9-93C2-7B329FCF8536}.Debug|Any CPU.Build.0 = Debug|Any CPU - {D3461DFD-840B-4AB9-93C2-7B329FCF8536}.Release|Any CPU.ActiveCfg = Release|Any CPU - {D3461DFD-840B-4AB9-93C2-7B329FCF8536}.Release|Any CPU.Build.0 = Release|Any CPU {6780E437-7D94-4B08-9A25-CA6698C5D8DA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {6780E437-7D94-4B08-9A25-CA6698C5D8DA}.Debug|Any CPU.Build.0 = Debug|Any CPU {6780E437-7D94-4B08-9A25-CA6698C5D8DA}.Release|Any CPU.ActiveCfg = Release|Any CPU {6780E437-7D94-4B08-9A25-CA6698C5D8DA}.Release|Any CPU.Build.0 = Release|Any CPU - {EA9BA74E-EE81-40E5-976A-EAB7C586F3D8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {EA9BA74E-EE81-40E5-976A-EAB7C586F3D8}.Debug|Any CPU.Build.0 = Debug|Any CPU - {EA9BA74E-EE81-40E5-976A-EAB7C586F3D8}.Release|Any CPU.ActiveCfg = Release|Any CPU - {EA9BA74E-EE81-40E5-976A-EAB7C586F3D8}.Release|Any CPU.Build.0 = Release|Any CPU + {71C569AC-28C8-4268-81A2-964D7C48EBAC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {71C569AC-28C8-4268-81A2-964D7C48EBAC}.Debug|Any CPU.Build.0 = Debug|Any CPU + {71C569AC-28C8-4268-81A2-964D7C48EBAC}.Release|Any CPU.ActiveCfg = Release|Any CPU + {71C569AC-28C8-4268-81A2-964D7C48EBAC}.Release|Any CPU.Build.0 = Release|Any CPU + {2D14DD45-130F-4B6B-B2CD-5F3F7AD28F96}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2D14DD45-130F-4B6B-B2CD-5F3F7AD28F96}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2D14DD45-130F-4B6B-B2CD-5F3F7AD28F96}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2D14DD45-130F-4B6B-B2CD-5F3F7AD28F96}.Release|Any CPU.Build.0 = Release|Any CPU + {04D4D6DE-2782-4A0D-9B74-1FF5141BE112}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {04D4D6DE-2782-4A0D-9B74-1FF5141BE112}.Debug|Any CPU.Build.0 = Debug|Any CPU + {04D4D6DE-2782-4A0D-9B74-1FF5141BE112}.Release|Any CPU.ActiveCfg = Release|Any CPU + {04D4D6DE-2782-4A0D-9B74-1FF5141BE112}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -197,12 +191,10 @@ Global {AAA5B1BE-4216-4ADB-9FB3-B03D35CF8D03} = {2939E5A7-F1C3-4853-9F35-31EB64C08036} {CA77313D-6D97-4F91-9818-B729FA0359D2} = {2939E5A7-F1C3-4853-9F35-31EB64C08036} {2D538398-EAC9-4A18-8459-74CA9E499F4E} = {117FC7BE-0153-4819-9A27-0FDA22132FA1} - {9020A29D-E4B8-42AE-9EE3-BC2B001A0CB9} = {0010CB43-FD6D-4C91-91D9-3065ACBAC2E1} {6B9F2368-1F55-43C0-9DDD-1CDE744F3118} = {0010CB43-FD6D-4C91-91D9-3065ACBAC2E1} - {6BAB5C0D-2624-43BC-B126-D40601B1E7A9} = {0010CB43-FD6D-4C91-91D9-3065ACBAC2E1} - {D3461DFD-840B-4AB9-93C2-7B329FCF8536} = {0010CB43-FD6D-4C91-91D9-3065ACBAC2E1} {6780E437-7D94-4B08-9A25-CA6698C5D8DA} = {0010CB43-FD6D-4C91-91D9-3065ACBAC2E1} - {EA9BA74E-EE81-40E5-976A-EAB7C586F3D8} = {0010CB43-FD6D-4C91-91D9-3065ACBAC2E1} + {71C569AC-28C8-4268-81A2-964D7C48EBAC} = {0010CB43-FD6D-4C91-91D9-3065ACBAC2E1} + {2D14DD45-130F-4B6B-B2CD-5F3F7AD28F96} = {0010CB43-FD6D-4C91-91D9-3065ACBAC2E1} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {247F9BF0-18BB-4A57-BB3E-2870DDAEF0D1} From 3b4d84151a4439a3b58934aade6dfe4d68d0e492 Mon Sep 17 00:00:00 2001 From: Hesam Asnaashari <46031836+ilhesam@users.noreply.github.com> Date: Mon, 27 Jun 2022 16:20:19 +0430 Subject: [PATCH 09/21] Remove unused code --- src/Identity/Tests/API/Program.cs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/Identity/Tests/API/Program.cs b/src/Identity/Tests/API/Program.cs index 2aaf00ec..7ec05401 100644 --- a/src/Identity/Tests/API/Program.cs +++ b/src/Identity/Tests/API/Program.cs @@ -43,11 +43,6 @@ options.SuppressModelStateInvalidFilter = true; }); -builder.Services.AddCors(options => -{ - options.AddPolicy("ds", policy => policy.AllowAnyHeader().Build()); -}); - // Adding application context builder.Services.AddApplicationContext(); From 7480c010021c1a99a8ae8f80b61cbb45e769b352 Mon Sep 17 00:00:00 2001 From: Hesam Asnaashari <46031836+ilhesam@users.noreply.github.com> Date: Mon, 27 Jun 2022 17:14:41 +0430 Subject: [PATCH 10/21] Removed unused code --- src/Identity/Tests/API/Controllers/AccountsController.cs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/Identity/Tests/API/Controllers/AccountsController.cs b/src/Identity/Tests/API/Controllers/AccountsController.cs index f7246383..afca1556 100644 --- a/src/Identity/Tests/API/Controllers/AccountsController.cs +++ b/src/Identity/Tests/API/Controllers/AccountsController.cs @@ -4,12 +4,7 @@ namespace API; public class AccountsController : AccountsControllerBase { - public AccountsController(IUserService userService, IFileManager fileManager) : base(userService) + public AccountsController(IUserService userService) : base(userService) { - using var avatar = File.OpenRead("C:\\Users\\Hesam\\Pictures\\f518a14c0cad2394cefc700675bb7b4d.jpg"); - fileManager.Create(avatar, "Avatars").Wait(); - - using var document = File.OpenRead("C:\\Users\\Hesam\\Pictures\\ds.png"); - fileManager.Create(document, "Documents").Wait(); } } \ No newline at end of file From a14399ca27ccb5e493040a086b0f4ad89172be0e Mon Sep 17 00:00:00 2001 From: Hesam Asnaashari <46031836+ilhesam@users.noreply.github.com> Date: Tue, 28 Jun 2022 16:32:10 +0430 Subject: [PATCH 11/21] File provider based-on relative path not absolute path --- .../uBeac.Core.Common/Entities/FileEntity.cs | 2 -- .../IFileProvider.cs | 7 +------ .../uBeac.Core.FileManagement/FileService.cs | 7 +++---- .../LocalStorageFileProvider.cs | 16 +++++++++++----- .../Tests/API/Config/file-management.json | 4 ++-- 5 files changed, 17 insertions(+), 19 deletions(-) diff --git a/src/Common/uBeac.Core.Common/Entities/FileEntity.cs b/src/Common/uBeac.Core.Common/Entities/FileEntity.cs index 22bad03c..8eb60f1e 100644 --- a/src/Common/uBeac.Core.Common/Entities/FileEntity.cs +++ b/src/Common/uBeac.Core.Common/Entities/FileEntity.cs @@ -6,7 +6,6 @@ public interface IFileEntity : IAuditEntity string Name { get; set; } string Extension { get; set; } string Provider { get; set; } - string Path { get; set; } string Category { get; set; } } @@ -20,7 +19,6 @@ public class FileEntity : AuditEntity, IFileEntity public string Name { get; set; } public string Extension { get; set; } public string Provider { get; set; } - public string Path { get; set; } public string Category { get; set; } } diff --git a/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileProvider.cs b/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileProvider.cs index 007da93f..575d4e51 100644 --- a/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileProvider.cs +++ b/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileProvider.cs @@ -4,11 +4,6 @@ public interface IFileProvider { string Name { get; } - Task Create(FileStream fileStream, string fileName, CancellationToken cancellationToken = default); + Task Create(FileStream fileStream, string fileName, CancellationToken cancellationToken = default); Task Get(string fileName, CancellationToken cancellationToken = default); -} - -public class CreateFileResult -{ - public string FilePath { get; set; } } \ No newline at end of file diff --git a/src/FileManagement/uBeac.Core.FileManagement/FileService.cs b/src/FileManagement/uBeac.Core.FileManagement/FileService.cs index 9229de1f..06c5de37 100644 --- a/src/FileManagement/uBeac.Core.FileManagement/FileService.cs +++ b/src/FileManagement/uBeac.Core.FileManagement/FileService.cs @@ -22,19 +22,18 @@ public async Task Create(FileStream fileStream, string category, CancellationTok var fileName = Path.GetRandomFileName(); var fileExtension = Path.GetExtension(fileStream.Name); - var createResult = await Provider.Create(fileStream, fileName, cancellationToken); + await Provider.Create(fileStream, fileName, cancellationToken); - var entity = CreateInstance(fileStream, fileName, fileExtension, createResult.FilePath, category); + var entity = CreateInstance(fileStream, fileName, fileExtension, category); await Repository.Create(entity, cancellationToken); } - protected virtual TEntity CreateInstance(FileStream fileStream, string fileName, string fileExtension, string filePath, string category) + protected virtual TEntity CreateInstance(FileStream fileStream, string fileName, string fileExtension, string category) { var entity = Activator.CreateInstance(); entity.Name = fileName; entity.Extension = fileExtension; entity.Provider = Provider.Name; - entity.Path = filePath; entity.Category = category; return entity; } diff --git a/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalStorage/LocalStorageFileProvider.cs b/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalStorage/LocalStorageFileProvider.cs index f5e510fb..83a8c0db 100644 --- a/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalStorage/LocalStorageFileProvider.cs +++ b/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalStorage/LocalStorageFileProvider.cs @@ -3,28 +3,34 @@ public class LocalStorageFileProvider : IFileProvider { protected readonly FileManagementLocalStorageOptions Options; + protected readonly string DirPath; public LocalStorageFileProvider(FileManagementLocalStorageOptions options) { Options = options; + + DirPath = Path.Combine(Directory.GetCurrentDirectory(), Options.DirectoryPath); + if (!Directory.Exists(DirPath)) Directory.CreateDirectory(DirPath); } public string Name => nameof(LocalStorageFileProvider); - public async Task Create(FileStream fileStream, string fileName, CancellationToken cancellationToken = default) + public async Task Create(FileStream fileStream, string fileName, CancellationToken cancellationToken = default) { - var path = GetFinalPath(fileName); + var path = GetFilePath(fileName); + await using var createStream = new FileStream(path, FileMode.Create, FileAccess.Write); await fileStream.CopyToAsync(createStream, cancellationToken); - return new CreateFileResult { FilePath = path }; + } public async Task Get(string fileName, CancellationToken cancellationToken = default) { - var path = GetFinalPath(fileName); + var path = GetFilePath(fileName); + await using var readStream = new FileStream(path, FileMode.Open, FileAccess.Read); return readStream; } - protected string GetFinalPath(string fileName) => Path.Combine(Options.DirectoryPath, fileName); + protected string GetFilePath(string fileName) => Path.Combine(DirPath, fileName); } \ No newline at end of file diff --git a/src/Identity/Tests/API/Config/file-management.json b/src/Identity/Tests/API/Config/file-management.json index 1b909397..69f5db3a 100644 --- a/src/Identity/Tests/API/Config/file-management.json +++ b/src/Identity/Tests/API/Config/file-management.json @@ -1,8 +1,8 @@ { "Avatars": { - "DirectoryPath": "C:\\Users\\Hesam\\Documents\\uBeac\\Avatars" + "DirectoryPath": "Avatars" }, "Documents": { - "DirectoryPath": "C:\\Users\\Hesam\\Documents\\uBeac\\Documents" + "DirectoryPath": "Documents" } } \ No newline at end of file From 5345b4b8f1e9c6ed65de9cd9d69ccb615022a246 Mon Sep 17 00:00:00 2001 From: Hesam Asnaashari <46031836+ilhesam@users.noreply.github.com> Date: Tue, 28 Jun 2022 16:35:59 +0430 Subject: [PATCH 12/21] Local Storage -> Local Disk --- .../FileServiceBuilderExtensions.cs | 4 ++-- .../LocalDiskFileProvider.cs} | 8 ++++---- .../LocalDiskOptions.cs} | 2 +- .../uBeac.Core.FileManagement.Providers.LocalDisk.csproj} | 0 src/Identity/Tests/API/API.csproj | 2 +- src/Identity/Tests/API/Program.cs | 4 ++-- src/uBeac.Core.sln | 6 +++--- 7 files changed, 13 insertions(+), 13 deletions(-) rename src/FileManagement/{uBeac.Core.Providers.FileManagement.LocalStorage => uBeac.Core.Providers.FileManagement.LocalDisk}/FileServiceBuilderExtensions.cs (69%) rename src/FileManagement/{uBeac.Core.Providers.FileManagement.LocalStorage/LocalStorageFileProvider.cs => uBeac.Core.Providers.FileManagement.LocalDisk/LocalDiskFileProvider.cs} (79%) rename src/FileManagement/{uBeac.Core.Providers.FileManagement.LocalStorage/LocalStorageOptions.cs => uBeac.Core.Providers.FileManagement.LocalDisk/LocalDiskOptions.cs} (67%) rename src/FileManagement/{uBeac.Core.Providers.FileManagement.LocalStorage/uBeac.Core.FileManagement.Providers.LocalStorage.csproj => uBeac.Core.Providers.FileManagement.LocalDisk/uBeac.Core.FileManagement.Providers.LocalDisk.csproj} (100%) diff --git a/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalStorage/FileServiceBuilderExtensions.cs b/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalDisk/FileServiceBuilderExtensions.cs similarity index 69% rename from src/FileManagement/uBeac.Core.Providers.FileManagement.LocalStorage/FileServiceBuilderExtensions.cs rename to src/FileManagement/uBeac.Core.Providers.FileManagement.LocalDisk/FileServiceBuilderExtensions.cs index f3ec78b2..134c1d58 100644 --- a/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalStorage/FileServiceBuilderExtensions.cs +++ b/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalDisk/FileServiceBuilderExtensions.cs @@ -6,11 +6,11 @@ namespace Microsoft.Extensions.DependencyInjection; public static class FileServiceBuilderExtensions { - public static IFileServiceBuilder StoreFilesInLocalStorage(this IFileServiceBuilder builder, FileManagementLocalStorageOptions options) + public static IFileServiceBuilder StoreFilesInLocalDisk(this IFileServiceBuilder builder, FileManagementLocalDiskOptions options) where TKey : IEquatable where TEntity : IFileEntity { - builder.Provider = serviceProvider => ActivatorUtilities.CreateInstance(serviceProvider, options); + builder.Provider = serviceProvider => ActivatorUtilities.CreateInstance(serviceProvider, options); return builder; } } \ No newline at end of file diff --git a/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalStorage/LocalStorageFileProvider.cs b/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalDisk/LocalDiskFileProvider.cs similarity index 79% rename from src/FileManagement/uBeac.Core.Providers.FileManagement.LocalStorage/LocalStorageFileProvider.cs rename to src/FileManagement/uBeac.Core.Providers.FileManagement.LocalDisk/LocalDiskFileProvider.cs index 83a8c0db..3db22fc7 100644 --- a/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalStorage/LocalStorageFileProvider.cs +++ b/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalDisk/LocalDiskFileProvider.cs @@ -1,11 +1,11 @@ namespace uBeac.FileManagement.LocalStorage; -public class LocalStorageFileProvider : IFileProvider +public class LocalDiskFileProvider : IFileProvider { - protected readonly FileManagementLocalStorageOptions Options; + protected readonly FileManagementLocalDiskOptions Options; protected readonly string DirPath; - public LocalStorageFileProvider(FileManagementLocalStorageOptions options) + public LocalDiskFileProvider(FileManagementLocalDiskOptions options) { Options = options; @@ -13,7 +13,7 @@ public LocalStorageFileProvider(FileManagementLocalStorageOptions options) if (!Directory.Exists(DirPath)) Directory.CreateDirectory(DirPath); } - public string Name => nameof(LocalStorageFileProvider); + public string Name => nameof(LocalDiskFileProvider); public async Task Create(FileStream fileStream, string fileName, CancellationToken cancellationToken = default) { diff --git a/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalStorage/LocalStorageOptions.cs b/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalDisk/LocalDiskOptions.cs similarity index 67% rename from src/FileManagement/uBeac.Core.Providers.FileManagement.LocalStorage/LocalStorageOptions.cs rename to src/FileManagement/uBeac.Core.Providers.FileManagement.LocalDisk/LocalDiskOptions.cs index 7b8398d6..76d32b4b 100644 --- a/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalStorage/LocalStorageOptions.cs +++ b/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalDisk/LocalDiskOptions.cs @@ -1,6 +1,6 @@ namespace uBeac.FileManagement.LocalStorage; -public class FileManagementLocalStorageOptions +public class FileManagementLocalDiskOptions { public string DirectoryPath { get; set; } } \ No newline at end of file diff --git a/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalStorage/uBeac.Core.FileManagement.Providers.LocalStorage.csproj b/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalDisk/uBeac.Core.FileManagement.Providers.LocalDisk.csproj similarity index 100% rename from src/FileManagement/uBeac.Core.Providers.FileManagement.LocalStorage/uBeac.Core.FileManagement.Providers.LocalStorage.csproj rename to src/FileManagement/uBeac.Core.Providers.FileManagement.LocalDisk/uBeac.Core.FileManagement.Providers.LocalDisk.csproj diff --git a/src/Identity/Tests/API/API.csproj b/src/Identity/Tests/API/API.csproj index 9a3941c8..7c029bff 100644 --- a/src/Identity/Tests/API/API.csproj +++ b/src/Identity/Tests/API/API.csproj @@ -14,7 +14,7 @@ - + diff --git a/src/Identity/Tests/API/Program.cs b/src/Identity/Tests/API/Program.cs index 7ec05401..31160b57 100644 --- a/src/Identity/Tests/API/Program.cs +++ b/src/Identity/Tests/API/Program.cs @@ -29,12 +29,12 @@ options.AddCategory("Avatars") .SetValidExtensions(new[] { ".png", ".jpg", ".jpeg", ".bmp" }) .StoreInfoInMongoDb() - .StoreFilesInLocalStorage(builder.Configuration.GetInstance("Avatars")); + .StoreFilesInLocalDisk(builder.Configuration.GetInstance("Avatars")); options.AddCategory("Documents") .SetValidExtensions(new[] { ".docx", ".pdf" }) .StoreInfoInMongoDb() - .StoreFilesInLocalStorage(builder.Configuration.GetInstance("Documents")); + .StoreFilesInLocalDisk(builder.Configuration.GetInstance("Documents")); }); // Disabling automatic model state validation diff --git a/src/uBeac.Core.sln b/src/uBeac.Core.sln index b9224fe0..1ea7e4ef 100644 --- a/src/uBeac.Core.sln +++ b/src/uBeac.Core.sln @@ -57,11 +57,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "FileManagement", "FileManag EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "uBeac.Core.FileManagement.Repositories.MongoDB", "FileManagement\uBeac.Core.Repositories.FileManagement.MongoDB\uBeac.Core.FileManagement.Repositories.MongoDB.csproj", "{6B9F2368-1F55-43C0-9DDD-1CDE744F3118}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "uBeac.Core.FileManagement.Providers.LocalStorage", "FileManagement\uBeac.Core.Providers.FileManagement.LocalStorage\uBeac.Core.FileManagement.Providers.LocalStorage.csproj", "{6780E437-7D94-4B08-9A25-CA6698C5D8DA}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "uBeac.Core.FileManagement.Providers.LocalDisk", "FileManagement\uBeac.Core.Providers.FileManagement.LocalDisk\uBeac.Core.FileManagement.Providers.LocalDisk.csproj", "{6780E437-7D94-4B08-9A25-CA6698C5D8DA}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "uBeac.Core.FileManagement.Abstractions", "FileManagement\uBeac.Core.FileManagement.Abstractions\uBeac.Core.FileManagement.Abstractions.csproj", "{71C569AC-28C8-4268-81A2-964D7C48EBAC}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "uBeac.Core.FileManagement.Abstractions", "FileManagement\uBeac.Core.FileManagement.Abstractions\uBeac.Core.FileManagement.Abstractions.csproj", "{71C569AC-28C8-4268-81A2-964D7C48EBAC}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "uBeac.Core.FileManagement", "FileManagement\uBeac.Core.FileManagement\uBeac.Core.FileManagement.csproj", "{2D14DD45-130F-4B6B-B2CD-5F3F7AD28F96}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "uBeac.Core.FileManagement", "FileManagement\uBeac.Core.FileManagement\uBeac.Core.FileManagement.csproj", "{2D14DD45-130F-4B6B-B2CD-5F3F7AD28F96}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "API", "Identity\Tests\API\API.csproj", "{04D4D6DE-2782-4A0D-9B74-1FF5141BE112}" EndProject From 2cd3eb03339d6c04210fbfe8a6a54fcaca80a6ff Mon Sep 17 00:00:00 2001 From: Hesam Asnaashari <46031836+ilhesam@users.noreply.github.com> Date: Tue, 28 Jun 2022 17:08:28 +0430 Subject: [PATCH 13/21] Fixed Providers.FileManagement.LocalDisk reference in test project --- src/Identity/Tests/API/API.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Identity/Tests/API/API.csproj b/src/Identity/Tests/API/API.csproj index 7c029bff..530ee10a 100644 --- a/src/Identity/Tests/API/API.csproj +++ b/src/Identity/Tests/API/API.csproj @@ -14,7 +14,7 @@ - + From 2267b41fcc1ce16d93414382e8eb684f6569d9c8 Mon Sep 17 00:00:00 2001 From: Hesam Asnaashari <46031836+ilhesam@users.noreply.github.com> Date: Tue, 28 Jun 2022 17:11:39 +0430 Subject: [PATCH 14/21] Extendable file entity --- .../IFileManager.cs | 2 ++ .../IFileService.cs | 1 + .../FileManagementBuilder.cs | 6 ++--- .../uBeac.Core.FileManagement/FileManager.cs | 12 ++++++++++ .../uBeac.Core.FileManagement/FileService.cs | 22 +++++++++---------- .../FileServiceBuilder.cs | 2 +- 6 files changed, 29 insertions(+), 16 deletions(-) diff --git a/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileManager.cs b/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileManager.cs index 16890e6f..b47494c6 100644 --- a/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileManager.cs +++ b/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileManager.cs @@ -3,4 +3,6 @@ public interface IFileManager { Task Create(FileStream fileStream, string category, CancellationToken cancellationToken = default); + Task Create(FileStream fileStream, string category, TEntity entity, CancellationToken cancellationToken = default) where TKey : IEquatable where TEntity : IFileEntity; + Task Create(FileStream fileStream, string category, TEntity entity, CancellationToken cancellationToken = default) where TEntity : IFileEntity; } \ No newline at end of file diff --git a/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileService.cs b/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileService.cs index 30f6f227..d2c71d40 100644 --- a/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileService.cs +++ b/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileService.cs @@ -11,6 +11,7 @@ public interface IFileService : IFileService where TKey : IEquatable where TEntity : IFileEntity { + Task Create(FileStream fileStream, string category, TEntity entity, CancellationToken cancellationToken = default); } public interface IFileService : IFileService diff --git a/src/FileManagement/uBeac.Core.FileManagement/FileManagementBuilder.cs b/src/FileManagement/uBeac.Core.FileManagement/FileManagementBuilder.cs index adb5b630..14c522e4 100644 --- a/src/FileManagement/uBeac.Core.FileManagement/FileManagementBuilder.cs +++ b/src/FileManagement/uBeac.Core.FileManagement/FileManagementBuilder.cs @@ -6,7 +6,7 @@ namespace Microsoft.Extensions.DependencyInjection; public class FileManagementBuilder : IFileManagementBuilder where TKey : IEquatable - where TEntity : IFileEntity + where TEntity : IFileEntity, new() { protected readonly IServiceCollection Services; @@ -33,7 +33,7 @@ public static class ServiceCollectionExtensions { public static IServiceCollection AddFileManagement(this IServiceCollection services, Action> options) where TKey : IEquatable - where TEntity : IFileEntity + where TEntity : IFileEntity, new() { var builder = new FileManagementBuilder(services); options.Invoke(builder); @@ -44,7 +44,7 @@ public static IServiceCollection AddFileManagement(this IServiceC } public static IServiceCollection AddFileManagement(this IServiceCollection services, Action> options) - where TEntity : IFileEntity + where TEntity : IFileEntity, new() => AddFileManagement(services, options); public static IServiceCollection AddFileManagement(this IServiceCollection services, Action> options) diff --git a/src/FileManagement/uBeac.Core.FileManagement/FileManager.cs b/src/FileManagement/uBeac.Core.FileManagement/FileManager.cs index 1706d901..8a326264 100644 --- a/src/FileManagement/uBeac.Core.FileManagement/FileManager.cs +++ b/src/FileManagement/uBeac.Core.FileManagement/FileManager.cs @@ -15,5 +15,17 @@ public async Task Create(FileStream fileStream, string category, CancellationTok await service.Create(fileStream, category, cancellationToken); } + public async Task Create(FileStream fileStream, string category, TEntity entity, CancellationToken cancellationToken = default) where TKey : IEquatable where TEntity : IFileEntity + { + var service = GetService(category); + if (service is not IFileService entityService) throw new Exception("No file service registered for your entity."); + await entityService.Create(fileStream, category, entity, cancellationToken); + } + + public async Task Create(FileStream fileStream, string category, TEntity entity, CancellationToken cancellationToken = default) where TEntity : IFileEntity + { + await Create(fileStream, category, entity, cancellationToken); + } + protected IFileService GetService(string category) => Categories.Single(c => c.CategoryName == category).Service; } \ No newline at end of file diff --git a/src/FileManagement/uBeac.Core.FileManagement/FileService.cs b/src/FileManagement/uBeac.Core.FileManagement/FileService.cs index 06c5de37..c7dc0f79 100644 --- a/src/FileManagement/uBeac.Core.FileManagement/FileService.cs +++ b/src/FileManagement/uBeac.Core.FileManagement/FileService.cs @@ -2,7 +2,7 @@ public class FileService : IFileService where TKey : IEquatable - where TEntity : IFileEntity + where TEntity : IFileEntity, new() { protected readonly List Validators; protected readonly IFileRepository Repository; @@ -15,27 +15,25 @@ public FileService(IEnumerable validators, IFileRepository(); - entity.Name = fileName; - entity.Extension = fileExtension; - entity.Provider = Provider.Name; - entity.Category = category; - return entity; + await Create(fileStream, category, new TEntity(), cancellationToken); } protected void ThrowExceptionIfNotValid(FileStream fileStream) => Validators.ForEach(validator => @@ -46,7 +44,7 @@ protected void ThrowExceptionIfNotValid(FileStream fileStream) => Validators.For } public class FileService : FileService, IFileService - where TEntity : IFileEntity + where TEntity : IFileEntity, new() { public FileService(IEnumerable validators, IFileRepository repository, IFileProvider provider) : base(validators, repository, provider) { diff --git a/src/FileManagement/uBeac.Core.FileManagement/FileServiceBuilder.cs b/src/FileManagement/uBeac.Core.FileManagement/FileServiceBuilder.cs index ed6b833a..3a7da36f 100644 --- a/src/FileManagement/uBeac.Core.FileManagement/FileServiceBuilder.cs +++ b/src/FileManagement/uBeac.Core.FileManagement/FileServiceBuilder.cs @@ -5,7 +5,7 @@ namespace Microsoft.Extensions.DependencyInjection; public class FileServiceBuilder : IFileServiceBuilder where TKey : IEquatable - where TEntity : IFileEntity + where TEntity : IFileEntity, new() { public List> Validators { get; } = new(); public Func> Repository { get; set; } From 6781f9e0dcf8df37690842ea29ce01ba31f19fd0 Mon Sep 17 00:00:00 2001 From: Hesam Asnaashari <46031836+ilhesam@users.noreply.github.com> Date: Thu, 30 Jun 2022 14:17:02 +0430 Subject: [PATCH 15/21] FileManagement - Added CreateFileRequest --- .../IFileManager.cs | 13 ++++++++++--- .../IFileProvider.cs | 2 +- .../IFileService.cs | 4 ++-- .../IFileValidator.cs | 2 +- .../uBeac.Core.FileManagement/FileManager.cs | 16 ++++++++-------- .../uBeac.Core.FileManagement/FileService.cs | 19 +++++++++---------- .../Validators/FileExtensionValidator.cs | 5 ++--- .../LocalDiskFileProvider.cs | 4 ++-- 8 files changed, 35 insertions(+), 30 deletions(-) diff --git a/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileManager.cs b/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileManager.cs index b47494c6..27738e91 100644 --- a/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileManager.cs +++ b/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileManager.cs @@ -2,7 +2,14 @@ public interface IFileManager { - Task Create(FileStream fileStream, string category, CancellationToken cancellationToken = default); - Task Create(FileStream fileStream, string category, TEntity entity, CancellationToken cancellationToken = default) where TKey : IEquatable where TEntity : IFileEntity; - Task Create(FileStream fileStream, string category, TEntity entity, CancellationToken cancellationToken = default) where TEntity : IFileEntity; + Task Create(CreateFileRequest request, CancellationToken cancellationToken = default); + Task Create(CreateFileRequest request, TEntity entity, CancellationToken cancellationToken = default) where TKey : IEquatable where TEntity : IFileEntity; + Task Create(CreateFileRequest request, TEntity entity, CancellationToken cancellationToken = default) where TEntity : IFileEntity; +} + +public class CreateFileRequest +{ + public Stream Stream { get; set; } + public string Extension { get; set; } + public string Category { get; set; } } \ No newline at end of file diff --git a/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileProvider.cs b/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileProvider.cs index 575d4e51..1edb62da 100644 --- a/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileProvider.cs +++ b/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileProvider.cs @@ -4,6 +4,6 @@ public interface IFileProvider { string Name { get; } - Task Create(FileStream fileStream, string fileName, CancellationToken cancellationToken = default); + Task Create(Stream stream, string fileName, CancellationToken cancellationToken = default); Task Get(string fileName, CancellationToken cancellationToken = default); } \ No newline at end of file diff --git a/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileService.cs b/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileService.cs index d2c71d40..94060e9a 100644 --- a/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileService.cs +++ b/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileService.cs @@ -4,14 +4,14 @@ namespace uBeac.FileManagement; public interface IFileService : IService { - Task Create(FileStream fileStream, string category, CancellationToken cancellationToken = default); + Task Create(CreateFileRequest request, CancellationToken cancellationToken = default); } public interface IFileService : IFileService where TKey : IEquatable where TEntity : IFileEntity { - Task Create(FileStream fileStream, string category, TEntity entity, CancellationToken cancellationToken = default); + Task Create(CreateFileRequest request, TEntity entity, CancellationToken cancellationToken = default); } public interface IFileService : IFileService diff --git a/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileValidator.cs b/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileValidator.cs index e186f5fd..12a8cf40 100644 --- a/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileValidator.cs +++ b/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileValidator.cs @@ -2,7 +2,7 @@ public interface IFileValidator { - IFileValidationResult Validate(FileStream fileStream); + IFileValidationResult Validate(CreateFileRequest request); } public interface IFileValidationResult diff --git a/src/FileManagement/uBeac.Core.FileManagement/FileManager.cs b/src/FileManagement/uBeac.Core.FileManagement/FileManager.cs index 8a326264..f757892f 100644 --- a/src/FileManagement/uBeac.Core.FileManagement/FileManager.cs +++ b/src/FileManagement/uBeac.Core.FileManagement/FileManager.cs @@ -9,22 +9,22 @@ public FileManager(IEnumerable categories) Categories = categories.ToList(); } - public async Task Create(FileStream fileStream, string category, CancellationToken cancellationToken = default) + public async Task Create(CreateFileRequest request, CancellationToken cancellationToken = default) { - var service = GetService(category); - await service.Create(fileStream, category, cancellationToken); + var service = GetService(request.Category); + await service.Create(request, cancellationToken); } - public async Task Create(FileStream fileStream, string category, TEntity entity, CancellationToken cancellationToken = default) where TKey : IEquatable where TEntity : IFileEntity + public async Task Create(CreateFileRequest request, TEntity entity, CancellationToken cancellationToken = default) where TKey : IEquatable where TEntity : IFileEntity { - var service = GetService(category); + var service = GetService(request.Category); if (service is not IFileService entityService) throw new Exception("No file service registered for your entity."); - await entityService.Create(fileStream, category, entity, cancellationToken); + await entityService.Create(request, entity, cancellationToken); } - public async Task Create(FileStream fileStream, string category, TEntity entity, CancellationToken cancellationToken = default) where TEntity : IFileEntity + public async Task Create(CreateFileRequest request, TEntity entity, CancellationToken cancellationToken = default) where TEntity : IFileEntity { - await Create(fileStream, category, entity, cancellationToken); + await Create(request, entity, cancellationToken); } protected IFileService GetService(string category) => Categories.Single(c => c.CategoryName == category).Service; diff --git a/src/FileManagement/uBeac.Core.FileManagement/FileService.cs b/src/FileManagement/uBeac.Core.FileManagement/FileService.cs index c7dc0f79..279f4798 100644 --- a/src/FileManagement/uBeac.Core.FileManagement/FileService.cs +++ b/src/FileManagement/uBeac.Core.FileManagement/FileService.cs @@ -15,30 +15,29 @@ public FileService(IEnumerable validators, IFileRepository Validators.ForEach(validator => + protected void ThrowExceptionIfNotValid(CreateFileRequest request) => Validators.ForEach(validator => { - var result = validator.Validate(fileStream); + var result = validator.Validate(request); if (!result.Validated) throw result.Exception; }); } diff --git a/src/FileManagement/uBeac.Core.FileManagement/Validators/FileExtensionValidator.cs b/src/FileManagement/uBeac.Core.FileManagement/Validators/FileExtensionValidator.cs index 4ddfb858..3c9aa220 100644 --- a/src/FileManagement/uBeac.Core.FileManagement/Validators/FileExtensionValidator.cs +++ b/src/FileManagement/uBeac.Core.FileManagement/Validators/FileExtensionValidator.cs @@ -9,9 +9,8 @@ public FileExtensionValidator(IEnumerable validExtensions) ValidExtensions = validExtensions.Select(e => e.ToUpper()).ToArray(); } - public IFileValidationResult Validate(FileStream fileStream) + public IFileValidationResult Validate(CreateFileRequest request) { - var extension = Path.GetExtension(fileStream.Name).ToUpper(); - return ValidExtensions.Contains(extension) ? new FileValidationResult() : new FileValidationResult(new Exception("File extension is not valid!")); + return ValidExtensions.Contains(request.Extension.ToUpper()) ? new FileValidationResult() : new FileValidationResult(new Exception("File extension is not valid!")); } } \ No newline at end of file diff --git a/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalDisk/LocalDiskFileProvider.cs b/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalDisk/LocalDiskFileProvider.cs index 3db22fc7..97e6b4e7 100644 --- a/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalDisk/LocalDiskFileProvider.cs +++ b/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalDisk/LocalDiskFileProvider.cs @@ -15,12 +15,12 @@ public LocalDiskFileProvider(FileManagementLocalDiskOptions options) public string Name => nameof(LocalDiskFileProvider); - public async Task Create(FileStream fileStream, string fileName, CancellationToken cancellationToken = default) + public async Task Create(Stream stream, string fileName, CancellationToken cancellationToken = default) { var path = GetFilePath(fileName); await using var createStream = new FileStream(path, FileMode.Create, FileAccess.Write); - await fileStream.CopyToAsync(createStream, cancellationToken); + await stream.CopyToAsync(createStream, cancellationToken); } From a0bfef9c0c401d6eb5709ded27e676a40dd7b07f Mon Sep 17 00:00:00 2001 From: Hesam Asnaashari <46031836+ilhesam@users.noreply.github.com> Date: Thu, 30 Jun 2022 14:17:51 +0430 Subject: [PATCH 16/21] Test - Added FilesController --- .../Tests/API/Controllers/FilesController.cs | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 src/Identity/Tests/API/Controllers/FilesController.cs diff --git a/src/Identity/Tests/API/Controllers/FilesController.cs b/src/Identity/Tests/API/Controllers/FilesController.cs new file mode 100644 index 00000000..01cee342 --- /dev/null +++ b/src/Identity/Tests/API/Controllers/FilesController.cs @@ -0,0 +1,27 @@ +using Microsoft.AspNetCore.Mvc; +using uBeac.FileManagement; +using uBeac.Web; + +namespace API; + +public class FilesController : BaseController +{ + protected readonly IFileManager FileManager; + + public FilesController(IFileManager fileManager) + { + FileManager = fileManager; + } + + [HttpPost] + public async Task Avatar([FromForm] IFormFile file, CancellationToken cancellationToken = default) + { + await using var stream = file.OpenReadStream(); + await FileManager.Create(new CreateFileRequest + { + Stream = stream, + Category = "Avatars", + Extension = Path.GetExtension(file.FileName) + }, cancellationToken); + } +} \ No newline at end of file From 6b4e0a6c3214c52a053164bce302a05b1ceddbe1 Mon Sep 17 00:00:00 2001 From: Hesam Asnaashari <46031836+ilhesam@users.noreply.github.com> Date: Thu, 30 Jun 2022 14:24:08 +0430 Subject: [PATCH 17/21] Test - Added "Document" to "FilesController" --- .../Tests/API/Controllers/FilesController.cs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/Identity/Tests/API/Controllers/FilesController.cs b/src/Identity/Tests/API/Controllers/FilesController.cs index 01cee342..6f99dc93 100644 --- a/src/Identity/Tests/API/Controllers/FilesController.cs +++ b/src/Identity/Tests/API/Controllers/FilesController.cs @@ -24,4 +24,16 @@ await FileManager.Create(new CreateFileRequest Extension = Path.GetExtension(file.FileName) }, cancellationToken); } + + [HttpPost] + public async Task Document([FromForm] IFormFile file, CancellationToken cancellationToken = default) + { + await using var stream = file.OpenReadStream(); + await FileManager.Create(new CreateFileRequest + { + Stream = stream, + Category = "Documents", + Extension = Path.GetExtension(file.FileName) + }, cancellationToken); + } } \ No newline at end of file From 16d18cd9aacfcc493fa3ed4cd2bcce2f14bcdaeb Mon Sep 17 00:00:00 2001 From: Hesam Asnaashari <46031836+ilhesam@users.noreply.github.com> Date: Thu, 30 Jun 2022 19:07:02 +0430 Subject: [PATCH 18/21] FileManagement - Implemented search files --- .../IFileManager.cs | 4 ++++ .../IFileRepository.cs | 13 +++++++++++++ .../IFileService.cs | 4 ++++ .../uBeac.Core.FileManagement/FileManager.cs | 18 ++++++++++++++++++ .../uBeac.Core.FileManagement/FileService.cs | 6 ++++++ .../MongoFileRepository.cs | 18 +++++++++++++++++- ...FilesController.cs => AvatarsController.cs} | 17 ++++++----------- 7 files changed, 68 insertions(+), 12 deletions(-) rename src/Identity/Tests/API/Controllers/{FilesController.cs => AvatarsController.cs} (50%) diff --git a/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileManager.cs b/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileManager.cs index 27738e91..f2216fd4 100644 --- a/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileManager.cs +++ b/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileManager.cs @@ -2,6 +2,10 @@ public interface IFileManager { + Task> Search(SearchFileRequest request, CancellationToken cancellationToken = default); + Task> Search(SearchFileRequest request, CancellationToken cancellationToken = default) where TKey : IEquatable where TEntity : IFileEntity; + Task> Search(SearchFileRequest request, CancellationToken cancellationToken = default) where TEntity : IFileEntity; + Task Create(CreateFileRequest request, CancellationToken cancellationToken = default); Task Create(CreateFileRequest request, TEntity entity, CancellationToken cancellationToken = default) where TKey : IEquatable where TEntity : IFileEntity; Task Create(CreateFileRequest request, TEntity entity, CancellationToken cancellationToken = default) where TEntity : IFileEntity; diff --git a/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileRepository.cs b/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileRepository.cs index 7f712342..8ce30fe7 100644 --- a/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileRepository.cs +++ b/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileRepository.cs @@ -6,9 +6,22 @@ public interface IFileRepository : IEntityRepository where TEntity : IFileEntity { + Task> Search(SearchFileRequest request, CancellationToken cancellationToken = default); } public interface IFileRepository : IFileRepository, IEntityRepository where TEntity : IFileEntity { +} + +public class SearchFileRequest where TKey : IEquatable +{ + public string Category { get; set; } + public IEnumerable Ids { get; set; } + public IEnumerable Extensions { get; set; } + public IEnumerable Providers { get; set; } +} + +public class SearchFileRequest : SearchFileRequest +{ } \ No newline at end of file diff --git a/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileService.cs b/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileService.cs index 94060e9a..d6dd498d 100644 --- a/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileService.cs +++ b/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileService.cs @@ -4,6 +4,8 @@ namespace uBeac.FileManagement; public interface IFileService : IService { + Task> Search(SearchFileRequest request, CancellationToken cancellationToken = default); + Task Create(CreateFileRequest request, CancellationToken cancellationToken = default); } @@ -11,6 +13,8 @@ public interface IFileService : IFileService where TKey : IEquatable where TEntity : IFileEntity { + Task> Search(SearchFileRequest request, CancellationToken cancellationToken = default); + Task Create(CreateFileRequest request, TEntity entity, CancellationToken cancellationToken = default); } diff --git a/src/FileManagement/uBeac.Core.FileManagement/FileManager.cs b/src/FileManagement/uBeac.Core.FileManagement/FileManager.cs index f757892f..38e6d476 100644 --- a/src/FileManagement/uBeac.Core.FileManagement/FileManager.cs +++ b/src/FileManagement/uBeac.Core.FileManagement/FileManager.cs @@ -9,6 +9,24 @@ public FileManager(IEnumerable categories) Categories = categories.ToList(); } + public async Task> Search(SearchFileRequest request, CancellationToken cancellationToken = default) + { + var service = GetService(request.Category); + return await service.Search(request, cancellationToken); + } + + public async Task> Search(SearchFileRequest request, CancellationToken cancellationToken = default) where TKey : IEquatable where TEntity : IFileEntity + { + var service = GetService(request.Category); + if (service is not IFileService entityService) throw new Exception("No file service registered for your entity."); + return await entityService.Search(request, cancellationToken); + } + + public async Task> Search(SearchFileRequest request, CancellationToken cancellationToken = default) where TEntity : IFileEntity + { + return await Search(request, cancellationToken); + } + public async Task Create(CreateFileRequest request, CancellationToken cancellationToken = default) { var service = GetService(request.Category); diff --git a/src/FileManagement/uBeac.Core.FileManagement/FileService.cs b/src/FileManagement/uBeac.Core.FileManagement/FileService.cs index 279f4798..0452f099 100644 --- a/src/FileManagement/uBeac.Core.FileManagement/FileService.cs +++ b/src/FileManagement/uBeac.Core.FileManagement/FileService.cs @@ -15,6 +15,12 @@ public FileService(IEnumerable validators, IFileRepository> Search(SearchFileRequest request, CancellationToken cancellationToken = default) + => await Repository.Search(request, cancellationToken); + + public async Task> Search(SearchFileRequest request, CancellationToken cancellationToken = default) + => (IEnumerable) await Search(request as SearchFileRequest, cancellationToken); + public async Task Create(CreateFileRequest request, TEntity entity, CancellationToken cancellationToken = default) { ThrowExceptionIfNotValid(request); diff --git a/src/FileManagement/uBeac.Core.Repositories.FileManagement.MongoDB/MongoFileRepository.cs b/src/FileManagement/uBeac.Core.Repositories.FileManagement.MongoDB/MongoFileRepository.cs index 86930c26..3bafb249 100644 --- a/src/FileManagement/uBeac.Core.Repositories.FileManagement.MongoDB/MongoFileRepository.cs +++ b/src/FileManagement/uBeac.Core.Repositories.FileManagement.MongoDB/MongoFileRepository.cs @@ -1,4 +1,5 @@ -using uBeac.Repositories.MongoDB; +using MongoDB.Driver; +using uBeac.Repositories.MongoDB; namespace uBeac.FileManagement.MongoDB; @@ -10,6 +11,21 @@ public class MongoFileRepository : MongoEntityRepositor public MongoFileRepository(TContext mongoDbContext, IApplicationContext applicationContext) : base(mongoDbContext, applicationContext) { } + + public async Task> Search(SearchFileRequest request, CancellationToken cancellationToken = default) + { + cancellationToken.ThrowIfCancellationRequested(); + + var filterBuilder = Builders.Filter; + var filters = FilterDefinition.Empty; + if (!string.IsNullOrWhiteSpace(request.Category)) filters &= filterBuilder.Eq(file => file.Category, request.Category); + if (request.Ids?.Any() == true) filters &= filterBuilder.In(file => file.Id, request.Ids); + if (request.Extensions?.Any() == true) filters &= filterBuilder.In(file => file.Extension, request.Extensions); + if (request.Providers?.Any() == true) filters &= filterBuilder.In(file => file.Provider, request.Providers); + + var result = await Collection.FindAsync(filters, new FindOptions(), cancellationToken); + return result.ToEnumerable(cancellationToken); + } } public class MongoFileRepository : MongoFileRepository, IFileRepository diff --git a/src/Identity/Tests/API/Controllers/FilesController.cs b/src/Identity/Tests/API/Controllers/AvatarsController.cs similarity index 50% rename from src/Identity/Tests/API/Controllers/FilesController.cs rename to src/Identity/Tests/API/Controllers/AvatarsController.cs index 6f99dc93..5cdb55f7 100644 --- a/src/Identity/Tests/API/Controllers/FilesController.cs +++ b/src/Identity/Tests/API/Controllers/AvatarsController.cs @@ -4,17 +4,17 @@ namespace API; -public class FilesController : BaseController +public class AvatarsController : BaseController { protected readonly IFileManager FileManager; - public FilesController(IFileManager fileManager) + public AvatarsController(IFileManager fileManager) { FileManager = fileManager; } [HttpPost] - public async Task Avatar([FromForm] IFormFile file, CancellationToken cancellationToken = default) + public async Task Upload([FromForm] IFormFile file, CancellationToken cancellationToken = default) { await using var stream = file.OpenReadStream(); await FileManager.Create(new CreateFileRequest @@ -26,14 +26,9 @@ await FileManager.Create(new CreateFileRequest } [HttpPost] - public async Task Document([FromForm] IFormFile file, CancellationToken cancellationToken = default) + public async Task> Search([FromBody] SearchFileRequest request, CancellationToken cancellationToken = default) { - await using var stream = file.OpenReadStream(); - await FileManager.Create(new CreateFileRequest - { - Stream = stream, - Category = "Documents", - Extension = Path.GetExtension(file.FileName) - }, cancellationToken); + request.Category = "Avatars"; + return await FileManager.Search(request, cancellationToken); } } \ No newline at end of file From 7057dcc9ae650cf1f2851dd4d5e8a3e50c305dc3 Mon Sep 17 00:00:00 2001 From: Hesam Asnaashari <46031836+ilhesam@users.noreply.github.com> Date: Thu, 30 Jun 2022 19:48:06 +0430 Subject: [PATCH 19/21] FileManagement - Implemented download file --- .../IFileManager.cs | 20 ++++++++--- .../IFileRepository.cs | 1 + .../IFileService.cs | 6 ++-- .../IFileValidator.cs | 2 +- .../uBeac.Core.FileManagement/FileManager.cs | 22 +++++++----- .../uBeac.Core.FileManagement/FileService.cs | 34 ++++++++++++++----- .../Validators/FileExtensionValidator.cs | 4 +-- .../LocalDiskFileProvider.cs | 4 +-- .../API/Controllers/AvatarsController.cs | 9 ++++- 9 files changed, 72 insertions(+), 30 deletions(-) diff --git a/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileManager.cs b/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileManager.cs index f2216fd4..c42c8f19 100644 --- a/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileManager.cs +++ b/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileManager.cs @@ -6,14 +6,26 @@ public interface IFileManager Task> Search(SearchFileRequest request, CancellationToken cancellationToken = default) where TKey : IEquatable where TEntity : IFileEntity; Task> Search(SearchFileRequest request, CancellationToken cancellationToken = default) where TEntity : IFileEntity; - Task Create(CreateFileRequest request, CancellationToken cancellationToken = default); - Task Create(CreateFileRequest request, TEntity entity, CancellationToken cancellationToken = default) where TKey : IEquatable where TEntity : IFileEntity; - Task Create(CreateFileRequest request, TEntity entity, CancellationToken cancellationToken = default) where TEntity : IFileEntity; + Task Create(FileModel model, CancellationToken cancellationToken = default); + Task Create(FileModel model, TEntity entity, CancellationToken cancellationToken = default) where TKey : IEquatable where TEntity : IFileEntity; + Task Create(FileModel model, TEntity entity, CancellationToken cancellationToken = default) where TEntity : IFileEntity; + + Task Get(GetFileRequest request, CancellationToken cancellationToken = default); } -public class CreateFileRequest +public class GetFileRequest +{ + public string Name { get; set; } + public string Category { get; set; } +} + +public class FileModel : IDisposable, IAsyncDisposable { public Stream Stream { get; set; } public string Extension { get; set; } public string Category { get; set; } + + public void Dispose() => Stream.Dispose(); + + public async ValueTask DisposeAsync() => await Stream.DisposeAsync(); } \ No newline at end of file diff --git a/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileRepository.cs b/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileRepository.cs index 8ce30fe7..17f00ed7 100644 --- a/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileRepository.cs +++ b/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileRepository.cs @@ -18,6 +18,7 @@ public class SearchFileRequest where TKey : IEquatable { public string Category { get; set; } public IEnumerable Ids { get; set; } + public IEnumerable Names { get; set; } public IEnumerable Extensions { get; set; } public IEnumerable Providers { get; set; } } diff --git a/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileService.cs b/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileService.cs index d6dd498d..09da07d4 100644 --- a/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileService.cs +++ b/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileService.cs @@ -6,7 +6,9 @@ public interface IFileService : IService { Task> Search(SearchFileRequest request, CancellationToken cancellationToken = default); - Task Create(CreateFileRequest request, CancellationToken cancellationToken = default); + Task Get(GetFileRequest request, CancellationToken cancellationToken = default); + + Task Create(FileModel model, CancellationToken cancellationToken = default); } public interface IFileService : IFileService @@ -15,7 +17,7 @@ public interface IFileService : IFileService { Task> Search(SearchFileRequest request, CancellationToken cancellationToken = default); - Task Create(CreateFileRequest request, TEntity entity, CancellationToken cancellationToken = default); + Task Create(FileModel model, TEntity entity, CancellationToken cancellationToken = default); } public interface IFileService : IFileService diff --git a/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileValidator.cs b/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileValidator.cs index 12a8cf40..4092891a 100644 --- a/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileValidator.cs +++ b/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileValidator.cs @@ -2,7 +2,7 @@ public interface IFileValidator { - IFileValidationResult Validate(CreateFileRequest request); + IFileValidationResult Validate(FileModel model); } public interface IFileValidationResult diff --git a/src/FileManagement/uBeac.Core.FileManagement/FileManager.cs b/src/FileManagement/uBeac.Core.FileManagement/FileManager.cs index 38e6d476..6a4c0644 100644 --- a/src/FileManagement/uBeac.Core.FileManagement/FileManager.cs +++ b/src/FileManagement/uBeac.Core.FileManagement/FileManager.cs @@ -27,22 +27,28 @@ public async Task> Search(SearchFileRequest reques return await Search(request, cancellationToken); } - public async Task Create(CreateFileRequest request, CancellationToken cancellationToken = default) + public async Task Create(FileModel model, CancellationToken cancellationToken = default) { - var service = GetService(request.Category); - await service.Create(request, cancellationToken); + var service = GetService(model.Category); + await service.Create(model, cancellationToken); } - public async Task Create(CreateFileRequest request, TEntity entity, CancellationToken cancellationToken = default) where TKey : IEquatable where TEntity : IFileEntity + public async Task Create(FileModel model, TEntity entity, CancellationToken cancellationToken = default) where TKey : IEquatable where TEntity : IFileEntity { - var service = GetService(request.Category); + var service = GetService(model.Category); if (service is not IFileService entityService) throw new Exception("No file service registered for your entity."); - await entityService.Create(request, entity, cancellationToken); + await entityService.Create(model, entity, cancellationToken); + } + + public async Task Create(FileModel model, TEntity entity, CancellationToken cancellationToken = default) where TEntity : IFileEntity + { + await Create(model, entity, cancellationToken); } - public async Task Create(CreateFileRequest request, TEntity entity, CancellationToken cancellationToken = default) where TEntity : IFileEntity + public async Task Get(GetFileRequest request, CancellationToken cancellationToken = default) { - await Create(request, entity, cancellationToken); + var service = GetService(request.Category); + return await service.Get(request, cancellationToken); } protected IFileService GetService(string category) => Categories.Single(c => c.CategoryName == category).Service; diff --git a/src/FileManagement/uBeac.Core.FileManagement/FileService.cs b/src/FileManagement/uBeac.Core.FileManagement/FileService.cs index 0452f099..63f984bf 100644 --- a/src/FileManagement/uBeac.Core.FileManagement/FileService.cs +++ b/src/FileManagement/uBeac.Core.FileManagement/FileService.cs @@ -21,29 +21,45 @@ public async Task> Search(SearchFileRequest request, public async Task> Search(SearchFileRequest request, CancellationToken cancellationToken = default) => (IEnumerable) await Search(request as SearchFileRequest, cancellationToken); - public async Task Create(CreateFileRequest request, TEntity entity, CancellationToken cancellationToken = default) + public async Task Get(GetFileRequest request, CancellationToken cancellationToken = default) { - ThrowExceptionIfNotValid(request); + var entity = (await Search(new SearchFileRequest + { + Category = request.Category, + Names = new[] { request.Name } + }, cancellationToken)).First(); + + return new FileModel + { + Stream = await Provider.Get(entity.Name, cancellationToken), + Category = request.Category, + Extension = entity.Extension + }; + } + + public async Task Create(FileModel model, TEntity entity, CancellationToken cancellationToken = default) + { + ThrowExceptionIfNotValid(model); var fileName = Path.GetRandomFileName(); entity.Name = fileName; - entity.Extension = request.Extension; + entity.Extension = model.Extension; entity.Provider = Provider.Name; - entity.Category = request.Category; + entity.Category = model.Category; - await Provider.Create(request.Stream, fileName, cancellationToken); + await Provider.Create(model.Stream, fileName, cancellationToken); await Repository.Create(entity, cancellationToken); } - public async Task Create(CreateFileRequest request, CancellationToken cancellationToken = default) + public async Task Create(FileModel model, CancellationToken cancellationToken = default) { - await Create(request, new TEntity(), cancellationToken); + await Create(model, new TEntity(), cancellationToken); } - protected void ThrowExceptionIfNotValid(CreateFileRequest request) => Validators.ForEach(validator => + protected void ThrowExceptionIfNotValid(FileModel model) => Validators.ForEach(validator => { - var result = validator.Validate(request); + var result = validator.Validate(model); if (!result.Validated) throw result.Exception; }); } diff --git a/src/FileManagement/uBeac.Core.FileManagement/Validators/FileExtensionValidator.cs b/src/FileManagement/uBeac.Core.FileManagement/Validators/FileExtensionValidator.cs index 3c9aa220..cbec3df7 100644 --- a/src/FileManagement/uBeac.Core.FileManagement/Validators/FileExtensionValidator.cs +++ b/src/FileManagement/uBeac.Core.FileManagement/Validators/FileExtensionValidator.cs @@ -9,8 +9,8 @@ public FileExtensionValidator(IEnumerable validExtensions) ValidExtensions = validExtensions.Select(e => e.ToUpper()).ToArray(); } - public IFileValidationResult Validate(CreateFileRequest request) + public IFileValidationResult Validate(FileModel model) { - return ValidExtensions.Contains(request.Extension.ToUpper()) ? new FileValidationResult() : new FileValidationResult(new Exception("File extension is not valid!")); + return ValidExtensions.Contains(model.Extension.ToUpper()) ? new FileValidationResult() : new FileValidationResult(new Exception("File extension is not valid!")); } } \ No newline at end of file diff --git a/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalDisk/LocalDiskFileProvider.cs b/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalDisk/LocalDiskFileProvider.cs index 97e6b4e7..dfbec00e 100644 --- a/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalDisk/LocalDiskFileProvider.cs +++ b/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalDisk/LocalDiskFileProvider.cs @@ -27,9 +27,7 @@ public async Task Create(Stream stream, string fileName, CancellationToken cance public async Task Get(string fileName, CancellationToken cancellationToken = default) { var path = GetFilePath(fileName); - - await using var readStream = new FileStream(path, FileMode.Open, FileAccess.Read); - return readStream; + return new FileStream(path, FileMode.Open, FileAccess.Read); ; } protected string GetFilePath(string fileName) => Path.Combine(DirPath, fileName); diff --git a/src/Identity/Tests/API/Controllers/AvatarsController.cs b/src/Identity/Tests/API/Controllers/AvatarsController.cs index 5cdb55f7..4d03f6e1 100644 --- a/src/Identity/Tests/API/Controllers/AvatarsController.cs +++ b/src/Identity/Tests/API/Controllers/AvatarsController.cs @@ -17,7 +17,7 @@ public AvatarsController(IFileManager fileManager) public async Task Upload([FromForm] IFormFile file, CancellationToken cancellationToken = default) { await using var stream = file.OpenReadStream(); - await FileManager.Create(new CreateFileRequest + await FileManager.Create(new FileModel { Stream = stream, Category = "Avatars", @@ -25,6 +25,13 @@ await FileManager.Create(new CreateFileRequest }, cancellationToken); } + [HttpPost] + public async Task Download([FromBody] GetFileRequest request, CancellationToken cancellationToken = default) + { + var response = await FileManager.Get(request, cancellationToken); + return new FileStreamResult(response.Stream, "application/octet-stream") { FileDownloadName = $"{request.Name}.{response.Extension}" }; + } + [HttpPost] public async Task> Search([FromBody] SearchFileRequest request, CancellationToken cancellationToken = default) { From 9e8d18b2f7ab56bef46eee1b6d061a4792023f63 Mon Sep 17 00:00:00 2001 From: Hesam Asnaashari <46031836+ilhesam@users.noreply.github.com> Date: Fri, 1 Jul 2022 17:06:31 +0430 Subject: [PATCH 20/21] FileManagement - Refactor -Dependency inversion --- .../IFileCategory.cs | 13 ---- .../IFileManagementBuilder.cs | 8 --- .../IFileManager.cs | 31 --------- .../IFileRepository.cs | 13 ---- .../IFileService.cs | 7 +- .../IFileServiceBuilder.cs | 12 ---- ...ileProvider.cs => IFileStorageProvider.cs} | 2 +- .../Models}/FileEntity.cs | 0 .../Models/FileModel.cs | 12 ++++ .../Models/GetFileRequest.cs | 7 ++ .../Models/SearchFileRequest.cs | 15 ++++ .../FileService.cs | 14 ++-- .../uBeac.Core.FileManagement.Services.csproj | 13 ++++ .../Builders/FileManagementBuilder.cs | 27 ++++++++ .../Builders/FileServiceBuilder.cs | 64 ++++++++++++++++++ .../Interfaces/IFileManagementBuilder.cs | 6 ++ .../Interfaces/IFileServiceBuilder.cs | 16 +++++ .../ServiceCollectionExtensions.cs} | 27 -------- .../FileServiceBuilder.cs | 27 -------- .../Models/FileCategory.cs | 7 ++ .../{ => Services}/FileManager.cs | 16 ++--- .../Services/Interfaces/IFileManager.cs | 14 ++++ .../uBeac.Core.FileManagement.csproj | 2 +- .../FileServiceBuilderExtensions.cs | 16 ----- .../LocalDiskFileProvider.cs | 2 +- .../FileServiceBuilderExtensions.cs | 30 -------- .../MongoFileRepository.cs | 7 ++ ...FileManagement.Repositories.MongoDB.csproj | 2 +- src/Identity/Tests/API/Avatars/t1zq2ubf.ncm | Bin 0 -> 14552 bytes .../API/Controllers/AvatarsController.cs | 4 +- src/Identity/Tests/API/Program.cs | 14 ++-- src/uBeac.Core.sln | 7 ++ 32 files changed, 229 insertions(+), 206 deletions(-) delete mode 100644 src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileCategory.cs delete mode 100644 src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileManagementBuilder.cs delete mode 100644 src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileManager.cs delete mode 100644 src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileServiceBuilder.cs rename src/FileManagement/uBeac.Core.FileManagement.Abstractions/{IFileProvider.cs => IFileStorageProvider.cs} (86%) rename src/{Common/uBeac.Core.Common/Entities => FileManagement/uBeac.Core.FileManagement.Abstractions/Models}/FileEntity.cs (100%) create mode 100644 src/FileManagement/uBeac.Core.FileManagement.Abstractions/Models/FileModel.cs create mode 100644 src/FileManagement/uBeac.Core.FileManagement.Abstractions/Models/GetFileRequest.cs create mode 100644 src/FileManagement/uBeac.Core.FileManagement.Abstractions/Models/SearchFileRequest.cs rename src/FileManagement/{uBeac.Core.FileManagement => uBeac.Core.FileManagement.Infrastructure}/FileService.cs (80%) create mode 100644 src/FileManagement/uBeac.Core.FileManagement.Infrastructure/uBeac.Core.FileManagement.Services.csproj create mode 100644 src/FileManagement/uBeac.Core.FileManagement/Builders/FileManagementBuilder.cs create mode 100644 src/FileManagement/uBeac.Core.FileManagement/Builders/FileServiceBuilder.cs create mode 100644 src/FileManagement/uBeac.Core.FileManagement/Builders/Interfaces/IFileManagementBuilder.cs create mode 100644 src/FileManagement/uBeac.Core.FileManagement/Builders/Interfaces/IFileServiceBuilder.cs rename src/FileManagement/uBeac.Core.FileManagement/{FileManagementBuilder.cs => Extensions/ServiceCollectionExtensions.cs} (60%) delete mode 100644 src/FileManagement/uBeac.Core.FileManagement/FileServiceBuilder.cs create mode 100644 src/FileManagement/uBeac.Core.FileManagement/Models/FileCategory.cs rename src/FileManagement/uBeac.Core.FileManagement/{ => Services}/FileManager.cs (68%) create mode 100644 src/FileManagement/uBeac.Core.FileManagement/Services/Interfaces/IFileManager.cs delete mode 100644 src/FileManagement/uBeac.Core.Providers.FileManagement.LocalDisk/FileServiceBuilderExtensions.cs delete mode 100644 src/FileManagement/uBeac.Core.Repositories.FileManagement.MongoDB/FileServiceBuilderExtensions.cs create mode 100644 src/Identity/Tests/API/Avatars/t1zq2ubf.ncm diff --git a/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileCategory.cs b/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileCategory.cs deleted file mode 100644 index 6ec4b27d..00000000 --- a/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileCategory.cs +++ /dev/null @@ -1,13 +0,0 @@ -namespace uBeac.FileManagement; - -public interface IFileCategory -{ - string CategoryName { get; } - IFileService Service { get; } -} - -public class FileCategory : IFileCategory -{ - public string CategoryName { get; set; } - public IFileService Service { get; set; } -} \ No newline at end of file diff --git a/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileManagementBuilder.cs b/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileManagementBuilder.cs deleted file mode 100644 index e86a2357..00000000 --- a/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileManagementBuilder.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace uBeac.FileManagement; - -public interface IFileManagementBuilder - where TKey : IEquatable - where TEntity : IFileEntity -{ - IFileServiceBuilder AddCategory(string categoryName); -} \ No newline at end of file diff --git a/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileManager.cs b/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileManager.cs deleted file mode 100644 index c42c8f19..00000000 --- a/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileManager.cs +++ /dev/null @@ -1,31 +0,0 @@ -namespace uBeac.FileManagement; - -public interface IFileManager -{ - Task> Search(SearchFileRequest request, CancellationToken cancellationToken = default); - Task> Search(SearchFileRequest request, CancellationToken cancellationToken = default) where TKey : IEquatable where TEntity : IFileEntity; - Task> Search(SearchFileRequest request, CancellationToken cancellationToken = default) where TEntity : IFileEntity; - - Task Create(FileModel model, CancellationToken cancellationToken = default); - Task Create(FileModel model, TEntity entity, CancellationToken cancellationToken = default) where TKey : IEquatable where TEntity : IFileEntity; - Task Create(FileModel model, TEntity entity, CancellationToken cancellationToken = default) where TEntity : IFileEntity; - - Task Get(GetFileRequest request, CancellationToken cancellationToken = default); -} - -public class GetFileRequest -{ - public string Name { get; set; } - public string Category { get; set; } -} - -public class FileModel : IDisposable, IAsyncDisposable -{ - public Stream Stream { get; set; } - public string Extension { get; set; } - public string Category { get; set; } - - public void Dispose() => Stream.Dispose(); - - public async ValueTask DisposeAsync() => await Stream.DisposeAsync(); -} \ No newline at end of file diff --git a/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileRepository.cs b/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileRepository.cs index 17f00ed7..35b3861e 100644 --- a/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileRepository.cs +++ b/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileRepository.cs @@ -12,17 +12,4 @@ public interface IFileRepository : IEntityRepository : IFileRepository, IEntityRepository where TEntity : IFileEntity { -} - -public class SearchFileRequest where TKey : IEquatable -{ - public string Category { get; set; } - public IEnumerable Ids { get; set; } - public IEnumerable Names { get; set; } - public IEnumerable Extensions { get; set; } - public IEnumerable Providers { get; set; } -} - -public class SearchFileRequest : SearchFileRequest -{ } \ No newline at end of file diff --git a/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileService.cs b/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileService.cs index 09da07d4..7ee32fd9 100644 --- a/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileService.cs +++ b/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileService.cs @@ -5,10 +5,8 @@ namespace uBeac.FileManagement; public interface IFileService : IService { Task> Search(SearchFileRequest request, CancellationToken cancellationToken = default); - Task Get(GetFileRequest request, CancellationToken cancellationToken = default); - - Task Create(FileModel model, CancellationToken cancellationToken = default); + Task Create(FileModel model, CancellationToken cancellationToken = default); } public interface IFileService : IFileService @@ -16,8 +14,7 @@ public interface IFileService : IFileService where TEntity : IFileEntity { Task> Search(SearchFileRequest request, CancellationToken cancellationToken = default); - - Task Create(FileModel model, TEntity entity, CancellationToken cancellationToken = default); + Task Create(FileModel model, TEntity entity, CancellationToken cancellationToken = default); } public interface IFileService : IFileService diff --git a/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileServiceBuilder.cs b/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileServiceBuilder.cs deleted file mode 100644 index 452c744c..00000000 --- a/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileServiceBuilder.cs +++ /dev/null @@ -1,12 +0,0 @@ -namespace uBeac.FileManagement; - -public interface IFileServiceBuilder - where TKey : IEquatable - where TEntity : IFileEntity -{ - List> Validators { get; } - Func> Repository { get; set; } - Func Provider { get; set; } - - void AddValidator(IFileValidator validator); -} \ No newline at end of file diff --git a/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileProvider.cs b/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileStorageProvider.cs similarity index 86% rename from src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileProvider.cs rename to src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileStorageProvider.cs index 1edb62da..72899f83 100644 --- a/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileProvider.cs +++ b/src/FileManagement/uBeac.Core.FileManagement.Abstractions/IFileStorageProvider.cs @@ -1,6 +1,6 @@ namespace uBeac.FileManagement; -public interface IFileProvider +public interface IFileStorageProvider { string Name { get; } diff --git a/src/Common/uBeac.Core.Common/Entities/FileEntity.cs b/src/FileManagement/uBeac.Core.FileManagement.Abstractions/Models/FileEntity.cs similarity index 100% rename from src/Common/uBeac.Core.Common/Entities/FileEntity.cs rename to src/FileManagement/uBeac.Core.FileManagement.Abstractions/Models/FileEntity.cs diff --git a/src/FileManagement/uBeac.Core.FileManagement.Abstractions/Models/FileModel.cs b/src/FileManagement/uBeac.Core.FileManagement.Abstractions/Models/FileModel.cs new file mode 100644 index 00000000..dd5ccd4e --- /dev/null +++ b/src/FileManagement/uBeac.Core.FileManagement.Abstractions/Models/FileModel.cs @@ -0,0 +1,12 @@ +namespace uBeac.FileManagement; + +public class FileModel : IDisposable, IAsyncDisposable +{ + public Stream Stream { get; set; } + public string Extension { get; set; } + public string Category { get; set; } + + public void Dispose() => Stream.Dispose(); + + public async ValueTask DisposeAsync() => await Stream.DisposeAsync(); +} \ No newline at end of file diff --git a/src/FileManagement/uBeac.Core.FileManagement.Abstractions/Models/GetFileRequest.cs b/src/FileManagement/uBeac.Core.FileManagement.Abstractions/Models/GetFileRequest.cs new file mode 100644 index 00000000..b34c9fca --- /dev/null +++ b/src/FileManagement/uBeac.Core.FileManagement.Abstractions/Models/GetFileRequest.cs @@ -0,0 +1,7 @@ +namespace uBeac.FileManagement; + +public class GetFileRequest +{ + public string Name { get; set; } + public string Category { get; set; } +} \ No newline at end of file diff --git a/src/FileManagement/uBeac.Core.FileManagement.Abstractions/Models/SearchFileRequest.cs b/src/FileManagement/uBeac.Core.FileManagement.Abstractions/Models/SearchFileRequest.cs new file mode 100644 index 00000000..fd687a2d --- /dev/null +++ b/src/FileManagement/uBeac.Core.FileManagement.Abstractions/Models/SearchFileRequest.cs @@ -0,0 +1,15 @@ +namespace uBeac.FileManagement; + + +public class SearchFileRequest where TKey : IEquatable +{ + public string Category { get; set; } + public IEnumerable Ids { get; set; } + public IEnumerable Names { get; set; } + public IEnumerable Extensions { get; set; } + public IEnumerable Providers { get; set; } +} + +public class SearchFileRequest : SearchFileRequest +{ +} \ No newline at end of file diff --git a/src/FileManagement/uBeac.Core.FileManagement/FileService.cs b/src/FileManagement/uBeac.Core.FileManagement.Infrastructure/FileService.cs similarity index 80% rename from src/FileManagement/uBeac.Core.FileManagement/FileService.cs rename to src/FileManagement/uBeac.Core.FileManagement.Infrastructure/FileService.cs index 63f984bf..813bdd94 100644 --- a/src/FileManagement/uBeac.Core.FileManagement/FileService.cs +++ b/src/FileManagement/uBeac.Core.FileManagement.Infrastructure/FileService.cs @@ -6,9 +6,9 @@ public class FileService : IFileService { protected readonly List Validators; protected readonly IFileRepository Repository; - protected readonly IFileProvider Provider; + protected readonly IFileStorageProvider Provider; - public FileService(IEnumerable validators, IFileRepository repository, IFileProvider provider) + public FileService(IEnumerable validators, IFileRepository repository, IFileStorageProvider provider) { Validators = validators.ToList(); Repository = repository; @@ -37,7 +37,7 @@ public async Task Get(GetFileRequest request, CancellationToken cance }; } - public async Task Create(FileModel model, TEntity entity, CancellationToken cancellationToken = default) + public async Task Create(FileModel model, TEntity entity, CancellationToken cancellationToken = default) { ThrowExceptionIfNotValid(model); @@ -50,11 +50,13 @@ public async Task Create(FileModel model, TEntity entity, CancellationToken canc await Provider.Create(model.Stream, fileName, cancellationToken); await Repository.Create(entity, cancellationToken); + + return entity; } - public async Task Create(FileModel model, CancellationToken cancellationToken = default) + public async Task Create(FileModel model, CancellationToken cancellationToken = default) { - await Create(model, new TEntity(), cancellationToken); + return (IFileEntity) await Create(model, new TEntity(), cancellationToken); } protected void ThrowExceptionIfNotValid(FileModel model) => Validators.ForEach(validator => @@ -67,7 +69,7 @@ protected void ThrowExceptionIfNotValid(FileModel model) => Validators.ForEach(v public class FileService : FileService, IFileService where TEntity : IFileEntity, new() { - public FileService(IEnumerable validators, IFileRepository repository, IFileProvider provider) : base(validators, repository, provider) + public FileService(IEnumerable validators, IFileRepository repository, IFileStorageProvider provider) : base(validators, repository, provider) { } } \ No newline at end of file diff --git a/src/FileManagement/uBeac.Core.FileManagement.Infrastructure/uBeac.Core.FileManagement.Services.csproj b/src/FileManagement/uBeac.Core.FileManagement.Infrastructure/uBeac.Core.FileManagement.Services.csproj new file mode 100644 index 00000000..d48374b0 --- /dev/null +++ b/src/FileManagement/uBeac.Core.FileManagement.Infrastructure/uBeac.Core.FileManagement.Services.csproj @@ -0,0 +1,13 @@ + + + + net6.0 + enable + disable + + + + + + + diff --git a/src/FileManagement/uBeac.Core.FileManagement/Builders/FileManagementBuilder.cs b/src/FileManagement/uBeac.Core.FileManagement/Builders/FileManagementBuilder.cs new file mode 100644 index 00000000..75cac2e2 --- /dev/null +++ b/src/FileManagement/uBeac.Core.FileManagement/Builders/FileManagementBuilder.cs @@ -0,0 +1,27 @@ +using uBeac; +using uBeac.FileManagement; + +namespace Microsoft.Extensions.DependencyInjection; + +public class FileManagementBuilder : IFileManagementBuilder where TKey : IEquatable where TEntity : IFileEntity, new() +{ + protected readonly IServiceCollection Services; + + public FileManagementBuilder(IServiceCollection services) + { + Services = services; + } + + public IFileServiceBuilder AddCategory(string categoryName) + { + var builder = new FileServiceBuilder(); + + Services.AddScoped(serviceProvider => + { + var service = builder.Build(serviceProvider); + return new FileCategory { CategoryName = categoryName, Service = service }; + }); + + return builder; + } +} \ No newline at end of file diff --git a/src/FileManagement/uBeac.Core.FileManagement/Builders/FileServiceBuilder.cs b/src/FileManagement/uBeac.Core.FileManagement/Builders/FileServiceBuilder.cs new file mode 100644 index 00000000..318c3dc6 --- /dev/null +++ b/src/FileManagement/uBeac.Core.FileManagement/Builders/FileServiceBuilder.cs @@ -0,0 +1,64 @@ +using uBeac; +using uBeac.FileManagement; + +namespace Microsoft.Extensions.DependencyInjection; + +public class FileServiceBuilder : IFileServiceBuilder + where TKey : IEquatable + where TEntity : IFileEntity, new() +{ + public List> Validators { get; } = new(); + public Func> Repository { get; set; } + public Func Provider { get; set; } + + public IFileServiceBuilder AddValidator(IFileValidator validator) + { + Validators.Add(_ => validator); + return this; + } + + public IFileServiceBuilder StoreInfoIn() where TRepository : IFileRepository + { + Repository = serviceProvider => serviceProvider.GetRequiredService(); + return this; + } + + public IFileServiceBuilder StoreInfoIn(TRepository repository) where TRepository : IFileRepository + { + Repository = _ => repository; + return this; + } + + public IFileServiceBuilder StoreInfoIn(Func> builder) + { + Repository = builder; + return this; + } + + public IFileServiceBuilder StoreFilesIn() where TStorageProvider : IFileStorageProvider + { + Provider = serviceProvider => serviceProvider.GetRequiredService(); + return this; + } + + public IFileServiceBuilder StoreFilesIn(TStorageProvider provider) where TStorageProvider : IFileStorageProvider + { + Provider = _ => provider; + return this; + } + + public IFileServiceBuilder StoreFilesIn(Func builder) + { + Provider = builder; + return this; + } + + internal IFileService Build(IServiceProvider serviceProvider) + { + var validators = Validators.Select(validator => validator(serviceProvider)); + var repository = Repository(serviceProvider); + var provider = Provider(serviceProvider); + + return new FileService(validators, repository, provider); + } +} \ No newline at end of file diff --git a/src/FileManagement/uBeac.Core.FileManagement/Builders/Interfaces/IFileManagementBuilder.cs b/src/FileManagement/uBeac.Core.FileManagement/Builders/Interfaces/IFileManagementBuilder.cs new file mode 100644 index 00000000..e5b5a3b2 --- /dev/null +++ b/src/FileManagement/uBeac.Core.FileManagement/Builders/Interfaces/IFileManagementBuilder.cs @@ -0,0 +1,6 @@ +namespace uBeac.FileManagement; + +public interface IFileManagementBuilder where TKey : IEquatable where TEntity : IFileEntity +{ + IFileServiceBuilder AddCategory(string categoryName); +} \ No newline at end of file diff --git a/src/FileManagement/uBeac.Core.FileManagement/Builders/Interfaces/IFileServiceBuilder.cs b/src/FileManagement/uBeac.Core.FileManagement/Builders/Interfaces/IFileServiceBuilder.cs new file mode 100644 index 00000000..58645d51 --- /dev/null +++ b/src/FileManagement/uBeac.Core.FileManagement/Builders/Interfaces/IFileServiceBuilder.cs @@ -0,0 +1,16 @@ +namespace uBeac.FileManagement; + +public interface IFileServiceBuilder + where TKey : IEquatable + where TEntity : IFileEntity +{ + IFileServiceBuilder AddValidator(IFileValidator validator); + + IFileServiceBuilder StoreInfoIn() where TRepository : IFileRepository; + IFileServiceBuilder StoreInfoIn(TRepository repository) where TRepository : IFileRepository; + IFileServiceBuilder StoreInfoIn(Func> builder); + + IFileServiceBuilder StoreFilesIn() where TStorageProvider : IFileStorageProvider; + IFileServiceBuilder StoreFilesIn(TStorageProvider provider) where TStorageProvider : IFileStorageProvider; + IFileServiceBuilder StoreFilesIn(Func builder); +} \ No newline at end of file diff --git a/src/FileManagement/uBeac.Core.FileManagement/FileManagementBuilder.cs b/src/FileManagement/uBeac.Core.FileManagement/Extensions/ServiceCollectionExtensions.cs similarity index 60% rename from src/FileManagement/uBeac.Core.FileManagement/FileManagementBuilder.cs rename to src/FileManagement/uBeac.Core.FileManagement/Extensions/ServiceCollectionExtensions.cs index 14c522e4..57fc24af 100644 --- a/src/FileManagement/uBeac.Core.FileManagement/FileManagementBuilder.cs +++ b/src/FileManagement/uBeac.Core.FileManagement/Extensions/ServiceCollectionExtensions.cs @@ -4,31 +4,6 @@ namespace Microsoft.Extensions.DependencyInjection; -public class FileManagementBuilder : IFileManagementBuilder - where TKey : IEquatable - where TEntity : IFileEntity, new() -{ - protected readonly IServiceCollection Services; - - public FileManagementBuilder(IServiceCollection services) - { - Services = services; - } - - public IFileServiceBuilder AddCategory(string categoryName) - { - var builder = new FileServiceBuilder(); - - Services.AddScoped(serviceProvider => - { - var service = builder.Build(serviceProvider); - return new FileCategory { CategoryName = categoryName, Service = service }; - }); - - return builder; - } -} - public static class ServiceCollectionExtensions { public static IServiceCollection AddFileManagement(this IServiceCollection services, Action> options) @@ -37,9 +12,7 @@ public static IServiceCollection AddFileManagement(this IServiceC { var builder = new FileManagementBuilder(services); options.Invoke(builder); - services.TryAddScoped(); - return services; } diff --git a/src/FileManagement/uBeac.Core.FileManagement/FileServiceBuilder.cs b/src/FileManagement/uBeac.Core.FileManagement/FileServiceBuilder.cs deleted file mode 100644 index 3a7da36f..00000000 --- a/src/FileManagement/uBeac.Core.FileManagement/FileServiceBuilder.cs +++ /dev/null @@ -1,27 +0,0 @@ -using uBeac; -using uBeac.FileManagement; - -namespace Microsoft.Extensions.DependencyInjection; - -public class FileServiceBuilder : IFileServiceBuilder - where TKey : IEquatable - where TEntity : IFileEntity, new() -{ - public List> Validators { get; } = new(); - public Func> Repository { get; set; } - public Func Provider { get; set; } - - public void AddValidator(IFileValidator validator) - { - Validators.Add(_ => validator); - } - - internal IFileService Build(IServiceProvider serviceProvider) - { - var validators = Validators.Select(validator => validator(serviceProvider)); - var repositories = Repository(serviceProvider); - var providers = Provider(serviceProvider); - - return ActivatorUtilities.CreateInstance>(serviceProvider, repositories, providers, validators); - } -} \ No newline at end of file diff --git a/src/FileManagement/uBeac.Core.FileManagement/Models/FileCategory.cs b/src/FileManagement/uBeac.Core.FileManagement/Models/FileCategory.cs new file mode 100644 index 00000000..221335f0 --- /dev/null +++ b/src/FileManagement/uBeac.Core.FileManagement/Models/FileCategory.cs @@ -0,0 +1,7 @@ +namespace uBeac.FileManagement; + +public class FileCategory +{ + public string CategoryName { get; set; } + public IFileService Service { get; set; } +} \ No newline at end of file diff --git a/src/FileManagement/uBeac.Core.FileManagement/FileManager.cs b/src/FileManagement/uBeac.Core.FileManagement/Services/FileManager.cs similarity index 68% rename from src/FileManagement/uBeac.Core.FileManagement/FileManager.cs rename to src/FileManagement/uBeac.Core.FileManagement/Services/FileManager.cs index 6a4c0644..20f3a96c 100644 --- a/src/FileManagement/uBeac.Core.FileManagement/FileManager.cs +++ b/src/FileManagement/uBeac.Core.FileManagement/Services/FileManager.cs @@ -2,9 +2,9 @@ public class FileManager : IFileManager { - protected readonly List Categories; + protected readonly List Categories; - public FileManager(IEnumerable categories) + public FileManager(IEnumerable categories) { Categories = categories.ToList(); } @@ -27,22 +27,22 @@ public async Task> Search(SearchFileRequest reques return await Search(request, cancellationToken); } - public async Task Create(FileModel model, CancellationToken cancellationToken = default) + public async Task Create(FileModel model, CancellationToken cancellationToken = default) { var service = GetService(model.Category); - await service.Create(model, cancellationToken); + return await service.Create(model, cancellationToken); } - public async Task Create(FileModel model, TEntity entity, CancellationToken cancellationToken = default) where TKey : IEquatable where TEntity : IFileEntity + public async Task Create(FileModel model, TEntity entity, CancellationToken cancellationToken = default) where TKey : IEquatable where TEntity : IFileEntity { var service = GetService(model.Category); if (service is not IFileService entityService) throw new Exception("No file service registered for your entity."); - await entityService.Create(model, entity, cancellationToken); + return await entityService.Create(model, entity, cancellationToken); } - public async Task Create(FileModel model, TEntity entity, CancellationToken cancellationToken = default) where TEntity : IFileEntity + public async Task Create(FileModel model, TEntity entity, CancellationToken cancellationToken = default) where TEntity : IFileEntity { - await Create(model, entity, cancellationToken); + return await Create(model, entity, cancellationToken); } public async Task Get(GetFileRequest request, CancellationToken cancellationToken = default) diff --git a/src/FileManagement/uBeac.Core.FileManagement/Services/Interfaces/IFileManager.cs b/src/FileManagement/uBeac.Core.FileManagement/Services/Interfaces/IFileManager.cs new file mode 100644 index 00000000..d02305d9 --- /dev/null +++ b/src/FileManagement/uBeac.Core.FileManagement/Services/Interfaces/IFileManager.cs @@ -0,0 +1,14 @@ +namespace uBeac.FileManagement; + +public interface IFileManager +{ + Task> Search(SearchFileRequest request, CancellationToken cancellationToken = default); + Task> Search(SearchFileRequest request, CancellationToken cancellationToken = default) where TKey : IEquatable where TEntity : IFileEntity; + Task> Search(SearchFileRequest request, CancellationToken cancellationToken = default) where TEntity : IFileEntity; + + Task Create(FileModel model, CancellationToken cancellationToken = default); + Task Create(FileModel model, TEntity entity, CancellationToken cancellationToken = default) where TKey : IEquatable where TEntity : IFileEntity; + Task Create(FileModel model, TEntity entity, CancellationToken cancellationToken = default) where TEntity : IFileEntity; + + Task Get(GetFileRequest request, CancellationToken cancellationToken = default); +} \ No newline at end of file diff --git a/src/FileManagement/uBeac.Core.FileManagement/uBeac.Core.FileManagement.csproj b/src/FileManagement/uBeac.Core.FileManagement/uBeac.Core.FileManagement.csproj index e66cee41..dc7b4be6 100644 --- a/src/FileManagement/uBeac.Core.FileManagement/uBeac.Core.FileManagement.csproj +++ b/src/FileManagement/uBeac.Core.FileManagement/uBeac.Core.FileManagement.csproj @@ -11,7 +11,7 @@ - + diff --git a/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalDisk/FileServiceBuilderExtensions.cs b/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalDisk/FileServiceBuilderExtensions.cs deleted file mode 100644 index 134c1d58..00000000 --- a/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalDisk/FileServiceBuilderExtensions.cs +++ /dev/null @@ -1,16 +0,0 @@ -using uBeac; -using uBeac.FileManagement; -using uBeac.FileManagement.LocalStorage; - -namespace Microsoft.Extensions.DependencyInjection; - -public static class FileServiceBuilderExtensions -{ - public static IFileServiceBuilder StoreFilesInLocalDisk(this IFileServiceBuilder builder, FileManagementLocalDiskOptions options) - where TKey : IEquatable - where TEntity : IFileEntity - { - builder.Provider = serviceProvider => ActivatorUtilities.CreateInstance(serviceProvider, options); - return builder; - } -} \ No newline at end of file diff --git a/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalDisk/LocalDiskFileProvider.cs b/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalDisk/LocalDiskFileProvider.cs index dfbec00e..769f7b37 100644 --- a/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalDisk/LocalDiskFileProvider.cs +++ b/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalDisk/LocalDiskFileProvider.cs @@ -1,6 +1,6 @@ namespace uBeac.FileManagement.LocalStorage; -public class LocalDiskFileProvider : IFileProvider +public class LocalDiskFileProvider : IFileStorageProvider { protected readonly FileManagementLocalDiskOptions Options; protected readonly string DirPath; diff --git a/src/FileManagement/uBeac.Core.Repositories.FileManagement.MongoDB/FileServiceBuilderExtensions.cs b/src/FileManagement/uBeac.Core.Repositories.FileManagement.MongoDB/FileServiceBuilderExtensions.cs deleted file mode 100644 index 8a629682..00000000 --- a/src/FileManagement/uBeac.Core.Repositories.FileManagement.MongoDB/FileServiceBuilderExtensions.cs +++ /dev/null @@ -1,30 +0,0 @@ -using uBeac; -using uBeac.FileManagement; -using uBeac.FileManagement.MongoDB; -using uBeac.Repositories.MongoDB; - -namespace Microsoft.Extensions.DependencyInjection; - -public static class FileServiceBuilderExtensions -{ - public static IFileServiceBuilder StoreInfoInMongoDb(this IFileServiceBuilder builder) - where TKey : IEquatable - where TEntity : IFileEntity - where TContext : IMongoDBContext - { - builder.Repository = ActivatorUtilities.GetServiceOrCreateInstance>; - return builder; - } - - public static IFileServiceBuilder StoreInfoInMongoDb(this IFileServiceBuilder builder) - where TEntity : IFileEntity - where TContext : IMongoDBContext - => StoreInfoInMongoDb(builder); - - public static IFileServiceBuilder StoreInfoInMongoDb(this IFileServiceBuilder builder) - where TContext : IMongoDBContext - => StoreInfoInMongoDb(builder); - - public static IFileServiceBuilder StoreInfoInMongoDb(this IFileServiceBuilder builder) - => StoreInfoInMongoDb(builder); -} \ No newline at end of file diff --git a/src/FileManagement/uBeac.Core.Repositories.FileManagement.MongoDB/MongoFileRepository.cs b/src/FileManagement/uBeac.Core.Repositories.FileManagement.MongoDB/MongoFileRepository.cs index 3bafb249..dd2bf15c 100644 --- a/src/FileManagement/uBeac.Core.Repositories.FileManagement.MongoDB/MongoFileRepository.cs +++ b/src/FileManagement/uBeac.Core.Repositories.FileManagement.MongoDB/MongoFileRepository.cs @@ -35,4 +35,11 @@ public class MongoFileRepository : MongoFileRepository +{ + public MongoFileRepository(MongoDBContext mongoDbContext, IApplicationContext applicationContext) : base(mongoDbContext, applicationContext) + { + } } \ No newline at end of file diff --git a/src/FileManagement/uBeac.Core.Repositories.FileManagement.MongoDB/uBeac.Core.FileManagement.Repositories.MongoDB.csproj b/src/FileManagement/uBeac.Core.Repositories.FileManagement.MongoDB/uBeac.Core.FileManagement.Repositories.MongoDB.csproj index 39d50f55..8ae57f21 100644 --- a/src/FileManagement/uBeac.Core.Repositories.FileManagement.MongoDB/uBeac.Core.FileManagement.Repositories.MongoDB.csproj +++ b/src/FileManagement/uBeac.Core.Repositories.FileManagement.MongoDB/uBeac.Core.FileManagement.Repositories.MongoDB.csproj @@ -3,7 +3,7 @@ net6.0 enable - enable + disable diff --git a/src/Identity/Tests/API/Avatars/t1zq2ubf.ncm b/src/Identity/Tests/API/Avatars/t1zq2ubf.ncm new file mode 100644 index 0000000000000000000000000000000000000000..8f12a008ed10b7f9291b23e8af6795ff4378265f GIT binary patch literal 14552 zcmb8VWl)_#w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DW5kZ_$?!yZG6K z5n7Z~IK4rL$U-80X=CMThcEY+F?n~noSr^B-a=Ab*Z6@mL^LtF)aBRrl-4m1&Y_ri&2}4Q%iu^En->lT55mT8dvn^Dc`C|?1`kA15 zSw&-T1HrMTYSK`VZ{l4D!xD`t{+CCwnF+-<9KIt14G-M(5~UMt_t@vCSt-g-IA9@h zV4WLPS6OH&@^K!ua?u!V<;Z>fS{^Fs@|AKi?ST(uM4c~saMxfn_z343?Kr%c?&^HgTTIN*Tw05))^%)P*#V3Dq;s?Ij*$U666eNB_CO zV1<`_FC8Y*x{oML>DJX;FC==Qt#5o>TuTW^cmp>L?(08dX=Ore6c8*m5T&rxvstjP zVPi)fb@d6Xb>%G4Cf3-EPCMRzIpB5|oY-?ZAhzpf;djf&Zt zwI*Z(4K8Tp9cI7*qg6X}=d)Ov=|;Xfdc%KZp=JmkWQ54icK9T`LC5a+M_r8%LNcja zVVz`+@MUO!Dfh!L|HS7m-F6y=Y{Pi3^Dimoaomla{j@*&Jp3@~+mxHeM&kNMU5!Fwsy69(x; z1nYr3Nnq-PVtvw$ lGNr(njE^A1x$6sm>Q&%ud9S6ksgw&BRf|1KeeU1Y{|`dyUoijx literal 0 HcmV?d00001 diff --git a/src/Identity/Tests/API/Controllers/AvatarsController.cs b/src/Identity/Tests/API/Controllers/AvatarsController.cs index 4d03f6e1..66ab22d0 100644 --- a/src/Identity/Tests/API/Controllers/AvatarsController.cs +++ b/src/Identity/Tests/API/Controllers/AvatarsController.cs @@ -14,10 +14,10 @@ public AvatarsController(IFileManager fileManager) } [HttpPost] - public async Task Upload([FromForm] IFormFile file, CancellationToken cancellationToken = default) + public async Task Upload([FromForm] IFormFile file, CancellationToken cancellationToken = default) { await using var stream = file.OpenReadStream(); - await FileManager.Create(new FileModel + return await FileManager.Create(new FileModel { Stream = stream, Category = "Avatars", diff --git a/src/Identity/Tests/API/Program.cs b/src/Identity/Tests/API/Program.cs index 31160b57..dcb10af6 100644 --- a/src/Identity/Tests/API/Program.cs +++ b/src/Identity/Tests/API/Program.cs @@ -1,6 +1,7 @@ using System.Reflection; using Microsoft.AspNetCore.Mvc; using uBeac.FileManagement.LocalStorage; +using uBeac.FileManagement.MongoDB; using uBeac.Repositories.History.MongoDB; using uBeac.Repositories.MongoDB; using uBeac.Web; @@ -24,17 +25,22 @@ builder.Services.AddControllers(); builder.Services.AddAutoMapper(Assembly.GetExecutingAssembly()); +builder.Services.AddScoped(); + builder.Services.AddFileManagement(options => { + var avatarsLocalDiskOptions = builder.Configuration.GetInstance("Avatars"); + var documentsLocalDiskOptions = builder.Configuration.GetInstance("Documents"); + options.AddCategory("Avatars") .SetValidExtensions(new[] { ".png", ".jpg", ".jpeg", ".bmp" }) - .StoreInfoInMongoDb() - .StoreFilesInLocalDisk(builder.Configuration.GetInstance("Avatars")); + .StoreInfoIn() + .StoreFilesIn(new LocalDiskFileProvider(avatarsLocalDiskOptions)); options.AddCategory("Documents") .SetValidExtensions(new[] { ".docx", ".pdf" }) - .StoreInfoInMongoDb() - .StoreFilesInLocalDisk(builder.Configuration.GetInstance("Documents")); + .StoreInfoIn() + .StoreFilesIn(new LocalDiskFileProvider(documentsLocalDiskOptions)); }); // Disabling automatic model state validation diff --git a/src/uBeac.Core.sln b/src/uBeac.Core.sln index 1ea7e4ef..a41dcd38 100644 --- a/src/uBeac.Core.sln +++ b/src/uBeac.Core.sln @@ -65,6 +65,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "uBeac.Core.FileManagement", EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "API", "Identity\Tests\API\API.csproj", "{04D4D6DE-2782-4A0D-9B74-1FF5141BE112}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "uBeac.Core.FileManagement.Services", "FileManagement\uBeac.Core.FileManagement.Infrastructure\uBeac.Core.FileManagement.Services.csproj", "{50BE9416-E281-4FF1-BE56-B7CCC4A70869}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -167,6 +169,10 @@ Global {04D4D6DE-2782-4A0D-9B74-1FF5141BE112}.Debug|Any CPU.Build.0 = Debug|Any CPU {04D4D6DE-2782-4A0D-9B74-1FF5141BE112}.Release|Any CPU.ActiveCfg = Release|Any CPU {04D4D6DE-2782-4A0D-9B74-1FF5141BE112}.Release|Any CPU.Build.0 = Release|Any CPU + {50BE9416-E281-4FF1-BE56-B7CCC4A70869}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {50BE9416-E281-4FF1-BE56-B7CCC4A70869}.Debug|Any CPU.Build.0 = Debug|Any CPU + {50BE9416-E281-4FF1-BE56-B7CCC4A70869}.Release|Any CPU.ActiveCfg = Release|Any CPU + {50BE9416-E281-4FF1-BE56-B7CCC4A70869}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -195,6 +201,7 @@ Global {6780E437-7D94-4B08-9A25-CA6698C5D8DA} = {0010CB43-FD6D-4C91-91D9-3065ACBAC2E1} {71C569AC-28C8-4268-81A2-964D7C48EBAC} = {0010CB43-FD6D-4C91-91D9-3065ACBAC2E1} {2D14DD45-130F-4B6B-B2CD-5F3F7AD28F96} = {0010CB43-FD6D-4C91-91D9-3065ACBAC2E1} + {50BE9416-E281-4FF1-BE56-B7CCC4A70869} = {0010CB43-FD6D-4C91-91D9-3065ACBAC2E1} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {247F9BF0-18BB-4A57-BB3E-2870DDAEF0D1} From 3444aba986e1b2bd0324221f8cb0ec7976079d17 Mon Sep 17 00:00:00 2001 From: Hesam Asnaashari <46031836+ilhesam@users.noreply.github.com> Date: Fri, 1 Jul 2022 17:14:38 +0430 Subject: [PATCH 21/21] FileManagement - Modify properties of packages --- ...ac.Core.FileManagement.Abstractions.csproj | 25 +++++++++------- .../uBeac.Core.FileManagement.Services.csproj | 23 +++++++++------ .../uBeac.Core.FileManagement.csproj | 27 ++++++++++------- ....FileManagement.Providers.LocalDisk.csproj | 29 +++++++++++-------- ...FileManagement.Repositories.MongoDB.csproj | 25 +++++++++------- 5 files changed, 77 insertions(+), 52 deletions(-) diff --git a/src/FileManagement/uBeac.Core.FileManagement.Abstractions/uBeac.Core.FileManagement.Abstractions.csproj b/src/FileManagement/uBeac.Core.FileManagement.Abstractions/uBeac.Core.FileManagement.Abstractions.csproj index d8fbe4b9..878147e0 100644 --- a/src/FileManagement/uBeac.Core.FileManagement.Abstractions/uBeac.Core.FileManagement.Abstractions.csproj +++ b/src/FileManagement/uBeac.Core.FileManagement.Abstractions/uBeac.Core.FileManagement.Abstractions.csproj @@ -1,15 +1,20 @@  - - net6.0 - enable - disable - + + uBeac.FileManagement.Abstractions + uBeac + net6.0 + enable + disable + Contains abstractions of file management. + ubeac-logo.jpg + https://github.com/ubeac/ubeac-api + - - - - - + + + + + diff --git a/src/FileManagement/uBeac.Core.FileManagement.Infrastructure/uBeac.Core.FileManagement.Services.csproj b/src/FileManagement/uBeac.Core.FileManagement.Infrastructure/uBeac.Core.FileManagement.Services.csproj index d48374b0..66d7b2b8 100644 --- a/src/FileManagement/uBeac.Core.FileManagement.Infrastructure/uBeac.Core.FileManagement.Services.csproj +++ b/src/FileManagement/uBeac.Core.FileManagement.Infrastructure/uBeac.Core.FileManagement.Services.csproj @@ -1,13 +1,18 @@ - + - - net6.0 - enable - disable - + + uBeac.FileManagement.Services + uBeac + net6.0 + enable + disable + Implements file management service. + ubeac-logo.jpg + https://github.com/ubeac/ubeac-api + - - - + + + diff --git a/src/FileManagement/uBeac.Core.FileManagement/uBeac.Core.FileManagement.csproj b/src/FileManagement/uBeac.Core.FileManagement/uBeac.Core.FileManagement.csproj index dc7b4be6..33bdcc4a 100644 --- a/src/FileManagement/uBeac.Core.FileManagement/uBeac.Core.FileManagement.csproj +++ b/src/FileManagement/uBeac.Core.FileManagement/uBeac.Core.FileManagement.csproj @@ -1,17 +1,22 @@ - - net6.0 - enable - disable - + + uBeac.FileManagement + uBeac + net6.0 + enable + disable + Easily implement file management in your .NET projects! + ubeac-logo.jpg + https://github.com/ubeac/ubeac-api + - - - + + + - - - + + + diff --git a/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalDisk/uBeac.Core.FileManagement.Providers.LocalDisk.csproj b/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalDisk/uBeac.Core.FileManagement.Providers.LocalDisk.csproj index e66cee41..c4cd1891 100644 --- a/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalDisk/uBeac.Core.FileManagement.Providers.LocalDisk.csproj +++ b/src/FileManagement/uBeac.Core.Providers.FileManagement.LocalDisk/uBeac.Core.FileManagement.Providers.LocalDisk.csproj @@ -1,17 +1,22 @@ - + - - net6.0 - enable - disable - + + uBeac.FileManagement.StorageProviders.LocalDisk + uBeac + net6.0 + enable + disable + Used to implement file management with local disk storage provider. + ubeac-logo.jpg + https://github.com/ubeac/ubeac-api + - - - + + + - - - + + + diff --git a/src/FileManagement/uBeac.Core.Repositories.FileManagement.MongoDB/uBeac.Core.FileManagement.Repositories.MongoDB.csproj b/src/FileManagement/uBeac.Core.Repositories.FileManagement.MongoDB/uBeac.Core.FileManagement.Repositories.MongoDB.csproj index 8ae57f21..587c5055 100644 --- a/src/FileManagement/uBeac.Core.Repositories.FileManagement.MongoDB/uBeac.Core.FileManagement.Repositories.MongoDB.csproj +++ b/src/FileManagement/uBeac.Core.Repositories.FileManagement.MongoDB/uBeac.Core.FileManagement.Repositories.MongoDB.csproj @@ -1,14 +1,19 @@ - + - - net6.0 - enable - disable - + + uBeac.FileManagement.Repositories.MongoDB + uBeac + net6.0 + enable + disable + Used to implement file management with MongoDB repository. + ubeac-logo.jpg + https://github.com/ubeac/ubeac-api + - - - - + + + +