Files
Umbraco-CMS/src/Umbraco.Core/Composing/Composers/FileSystemsComposer.cs

90 lines
3.5 KiB
C#
Raw Normal View History

2018-11-27 10:37:33 +01:00
using Umbraco.Core.Components;
2018-10-26 15:06:53 +02:00
using Umbraco.Core.IO;
2018-11-24 15:38:00 +01:00
using Umbraco.Core.IO.MediaPathSchemes;
2018-07-20 09:49:05 +02:00
namespace Umbraco.Core.Composing.Composers
{
public static class FileSystemsComposer
{
2018-10-26 15:06:53 +02:00
/*
* HOW TO REPLACE THE MEDIA UNDERLYING FILESYSTEM
* ----------------------------------------------
*
* Create a component and use it to modify the composition by adding something like:
*
2018-11-24 15:38:00 +01:00
* composition.Container.RegisterFileSystem<IMediaFileSystem, MediaFileSystem>(
* factory => new PhysicalFileSystem("~/somewhere"));
2018-10-26 15:06:53 +02:00
*
2018-11-24 15:38:00 +01:00
* return whatever supporting filesystem you like.
2018-10-26 15:06:53 +02:00
*
*
* HOW TO IMPLEMENT MY OWN FILESYSTEM
* ----------------------------------
*
* Create your filesystem class:
*
2018-11-24 15:38:00 +01:00
* public class MyFileSystem : FileSystemWrapper
2018-10-26 15:06:53 +02:00
* {
2018-11-24 15:38:00 +01:00
* public MyFileSystem(IFileSystem innerFileSystem)
2018-10-26 15:06:53 +02:00
* : base(innerFileSystem)
* { }
* }
*
2018-11-24 15:38:00 +01:00
* The ctor can have more parameters that will be resolved by the container.
*
* Register your filesystem, in a component:
*
* composition.Container.RegisterFileSystem<MyFileSystem>(
* factory => new PhysicalFileSystem("~/my"));
*
* And that's it, you can inject MyFileSystem wherever it's needed.
*
*
* You can also declare a filesystem interface:
*
* public interface IMyFileSystem : IFileSystem
* { }
*
* Make the class implement the interface, then
* register your filesystem, in a component:
2018-10-26 15:06:53 +02:00
*
2018-11-24 15:38:00 +01:00
* composition.Container.RegisterFileSystem<IMyFileSystem, MyFileSystem>(
* factory => new PhysicalFileSystem("~/my"));
2018-10-26 15:06:53 +02:00
*
* And that's it, you can inject IMyFileSystem wherever it's needed.
*
*
* WHAT IS SHADOWING
* -----------------
*
* Shadowing is the technology used for Deploy to implement some sort of
* transaction-management on top of filesystems. The plumbing explained above,
* compared to creating your own physical filesystem, ensures that your filesystem
* would participate into such transactions.
*
*
*/
2018-11-27 10:37:33 +01:00
public static Composition ComposeFileSystems(this Composition composition)
2018-07-20 09:49:05 +02:00
{
// register FileSystems, which manages all filesystems
2018-10-26 15:06:53 +02:00
// it needs to be registered (not only the interface) because it provides additional
// functionality eg for scoping, and is injected in the scope provider - whereas the
// interface is really for end-users to get access to filesystems.
2018-11-29 10:35:16 +01:00
composition.RegisterUnique(factory => factory.CreateInstance<FileSystems>(factory));
2018-07-20 09:49:05 +02:00
// register IFileSystems, which gives access too all filesystems
2018-11-29 10:35:16 +01:00
composition.RegisterUnique<IFileSystems>(factory => factory.GetInstance<FileSystems>());
2018-07-20 09:49:05 +02:00
2018-11-24 15:38:00 +01:00
// register the scheme for media paths
2018-11-29 10:35:16 +01:00
composition.RegisterUnique<IMediaPathScheme, TwoGuidsMediaPathScheme>();
2018-10-26 15:06:53 +02:00
2018-11-24 15:38:00 +01:00
// register the IMediaFileSystem implementation with a supporting filesystem
2018-11-28 11:05:41 +01:00
composition.RegisterFileSystem<IMediaFileSystem, MediaFileSystem>(
2018-11-24 15:38:00 +01:00
factory => new PhysicalFileSystem("~/media"));
2018-10-26 15:06:53 +02:00
2018-11-27 10:37:33 +01:00
return composition;
2018-07-20 09:49:05 +02:00
}
}
}