diff --git a/docs/Index.md b/docs/Index.md index fdf43d4a6f..0fd752b9af 100644 --- a/docs/Index.md +++ b/docs/Index.md @@ -7,7 +7,7 @@ * With ASP.NET Core Web Application * Fundamentals * Dependency Injection - * Virtual File System + * [Virtual File System](Virtual-File-System.md) * Localization * Exception Handling * [Multi Tenancy](Multi-Tenancy.md) diff --git a/docs/Virtual-File-System.md b/docs/Virtual-File-System.md index aa284ee050..4c3ac5bbd8 100644 --- a/docs/Virtual-File-System.md +++ b/docs/Virtual-File-System.md @@ -30,9 +30,21 @@ namespace MyCompany.MyProject #### Registering Embedded Files +A file should be first marked as embedded resource to embed the file into the assembly. The easiest way to do it is to select the file from the **Solution Explorer** and set **Build Action** to **Embedded Resource** from the **Properties** window. Example: +![build-action-embedded-resource-sample](images/build-action-embedded-resource-sample.png) -A file should be first registered in order to use it in the application. Example: +If you want to add multiple files, this can be tedious. Alternatively, you can directly edit your **.csproj** file: + +````C# + + + +```` + +This configuration recursively adds all files under the **MyResources** folder of the project (including the files you will add in the future). + +Then the module should configure `VirtualFileSystemOptions` to register embedded files to the virtual file system. Example: ````C# using Microsoft.Extensions.DependencyInjection; @@ -58,6 +70,38 @@ namespace MyCompany.MyProject } ```` -AddEmbedded extension method takes a +`AddEmbedded` extension method takes a class, finds all embedded files from the assembly of the given class and register to the virtual file system. It's a shortcut and could be written as shown below: + +````C# +options.FileSets.Add(new EmbeddedFileSet(typeof(MyModule).Assembly)); +```` + +#### Getting Virtual Files: IVirtualFileProvider + +After embedding a file into an assembly and registering to the virtual file system, `IVirtualFileProvider` can be used to get files or directory contents: + +````C# +public class MyService +{ + private readonly IVirtualFileProvider _virtualFileProvider; + + public MyService(IVirtualFileProvider virtualFileProvider) + { + _virtualFileProvider = virtualFileProvider; + } + + public void Foo() + { + //Getting a single file + var file = _virtualFileProvider.GetFileInfo("/MyResources/js/test.js"); + var fileContent = file.ReadAsString(); //ReadAsString is an extension method of ABP + + //Getting all files/directories under a directory + var directoryContents = _virtualFileProvider.GetDirectoryContents("/MyResources/js"); + } +} +```` + + s \ No newline at end of file diff --git a/docs/images/build-action-embedded-resource-sample.png b/docs/images/build-action-embedded-resource-sample.png new file mode 100644 index 0000000000..d001c5d19f Binary files /dev/null and b/docs/images/build-action-embedded-resource-sample.png differ diff --git a/src/Volo.Abp.VirtualFileSystem/Microsoft/Extensions/FileProviders/AbpFileInfoExtensions.cs b/src/Volo.Abp.VirtualFileSystem/Microsoft/Extensions/FileProviders/AbpFileInfoExtensions.cs new file mode 100644 index 0000000000..25c16b5b65 --- /dev/null +++ b/src/Volo.Abp.VirtualFileSystem/Microsoft/Extensions/FileProviders/AbpFileInfoExtensions.cs @@ -0,0 +1,31 @@ +using System.IO; +using System.Text; +using JetBrains.Annotations; +using Volo.Abp; + +namespace Microsoft.Extensions.FileProviders +{ + public static class AbpFileInfoExtensions + { + /// + /// Reads file content as string using encoding. + /// + public static string ReadAsString([NotNull] this IFileInfo fileInfo) + { + return fileInfo.ReadAsString(Encoding.UTF8); + } + + /// + /// Reads file content as string using the given . + /// + public static string ReadAsString([NotNull] this IFileInfo fileInfo, Encoding encoding) + { + Check.NotNull(fileInfo, nameof(fileInfo)); + + using (var stream = fileInfo.CreateReadStream()) + { + return encoding.GetString(stream.GetAllBytes()); + } + } + } +} diff --git a/src/Volo.Abp.VirtualFileSystem/Volo/Abp/VirtualFileSystem/Embedded/EmbeddedFileSet.cs b/src/Volo.Abp.VirtualFileSystem/Volo/Abp/VirtualFileSystem/Embedded/EmbeddedFileSet.cs index c846bb9c41..d2a0556527 100644 --- a/src/Volo.Abp.VirtualFileSystem/Volo/Abp/VirtualFileSystem/Embedded/EmbeddedFileSet.cs +++ b/src/Volo.Abp.VirtualFileSystem/Volo/Abp/VirtualFileSystem/Embedded/EmbeddedFileSet.cs @@ -21,7 +21,7 @@ namespace Volo.Abp.VirtualFileSystem.Embedded public EmbeddedFileSet( [NotNull] Assembly assembly, - [CanBeNull] string baseNamespace, + [CanBeNull] string baseNamespace = null, [CanBeNull] string baseFolderInProject = null) { Check.NotNull(assembly, nameof(assembly)); diff --git a/test/Volo.Abp.VirtualFileSystem.Tests/Volo.Abp.VirtualFileSystem.Tests.csproj b/test/Volo.Abp.VirtualFileSystem.Tests/Volo.Abp.VirtualFileSystem.Tests.csproj index 1db4b6e8c4..59b82ae994 100644 --- a/test/Volo.Abp.VirtualFileSystem.Tests/Volo.Abp.VirtualFileSystem.Tests.csproj +++ b/test/Volo.Abp.VirtualFileSystem.Tests/Volo.Abp.VirtualFileSystem.Tests.csproj @@ -12,7 +12,7 @@ - + diff --git a/test/Volo.Abp.VirtualFileSystem.Tests/Volo/Abp/VirtualFileSystem/MyResources/js/test.js b/test/Volo.Abp.VirtualFileSystem.Tests/Volo/Abp/VirtualFileSystem/MyResources/js/test.js new file mode 100644 index 0000000000..07547e22b5 --- /dev/null +++ b/test/Volo.Abp.VirtualFileSystem.Tests/Volo/Abp/VirtualFileSystem/MyResources/js/test.js @@ -0,0 +1 @@ +//test.js-contents \ No newline at end of file diff --git a/test/Volo.Abp.VirtualFileSystem.Tests/Volo/Abp/VirtualFileSystem/VirtualFileProvider_Tests.cs b/test/Volo.Abp.VirtualFileSystem.Tests/Volo/Abp/VirtualFileSystem/VirtualFileProvider_Tests.cs index 94679766c5..941ebb0c13 100644 --- a/test/Volo.Abp.VirtualFileSystem.Tests/Volo/Abp/VirtualFileSystem/VirtualFileProvider_Tests.cs +++ b/test/Volo.Abp.VirtualFileSystem.Tests/Volo/Abp/VirtualFileSystem/VirtualFileProvider_Tests.cs @@ -3,24 +3,25 @@ using System.Text; using Microsoft.Extensions.DependencyInjection; using Shouldly; using Volo.Abp.Modularity; +using Volo.Abp.VirtualFileSystem.Embedded; using Xunit; namespace Volo.Abp.VirtualFileSystem { public class VirtualFileProvider_Tests : AbpIntegratedTest { - private readonly IVirtualFileProvider _embeddedFileManager; + private readonly IVirtualFileProvider _virtualFileProvider; public VirtualFileProvider_Tests() { - _embeddedFileManager = ServiceProvider.GetRequiredService(); + _virtualFileProvider = ServiceProvider.GetRequiredService(); } [Fact] public void Should_Define_And_Get_Embedded_Resources() { //Act - var resource = _embeddedFileManager.GetFileInfo("/js/jquery-3-1-1-min.js"); + var resource = _virtualFileProvider.GetFileInfo("/js/jquery-3-1-1-min.js"); //Assert resource.ShouldNotBeNull();