Files
Umbraco-CMS/src/Umbraco.Core/Composing/CompositionExtensions/FileSystems.cs

99 lines
3.9 KiB
C#
Raw Normal View History

2019-02-14 09:15:47 +01:00
using Umbraco.Core.Compose;
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
2019-02-14 09:15:47 +01:00
namespace Umbraco.Core.Composing.CompositionExtensions
2018-07-20 09:49:05 +02:00
{
2019-02-14 09:15:47 +01:00
public static class FileSystems
2018-07-20 09:49:05 +02:00
{
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:
*
2019-01-09 17:39:32 +01:00
* composition.RegisterUniqueFor<IFileSystem, IMediaFileSystem>(...);
2018-10-26 15:06:53 +02:00
*
2019-01-09 17:39:32 +01:00
* and register 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)
* { }
* }
*
2019-01-09 17:39:32 +01:00
* The ctor can have more parameters, that will be resolved by the container.
2018-11-24 15:38:00 +01:00
*
* Register your filesystem, in a component:
*
2019-01-09 17:39:32 +01:00
* composition.RegisterFileSystem<MyFileSystem>();
*
* Register the underlying filesystem:
*
* composition.RegisterUniqueFor<IFileSystem, MyFileSystem>(...);
2018-11-24 15:38:00 +01:00
*
* 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
*
2019-01-09 17:39:32 +01:00
* composition.RegisterFileSystem<IMyFileSystem, MyFileSystem>();
* composition.RegisterUniqueFor<IFileSystem, IMyFileSystem>(...);
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.
2019-02-14 09:15:47 +01:00
composition.RegisterUnique(factory => factory.CreateInstance<IO.FileSystems>(factory));
2018-07-20 09:49:05 +02:00
// register IFileSystems, which gives access too all filesystems
2019-02-14 09:15:47 +01:00
composition.RegisterUnique<IFileSystems>(factory => factory.GetInstance<IO.FileSystems>());
2018-07-20 09:49:05 +02:00
2018-11-24 15:38:00 +01:00
// register the scheme for media paths
2019-02-21 11:28:51 +01:00
composition.RegisterUnique<IMediaPathScheme, CombinedGuidsMediaPathScheme>();
2018-10-26 15:06:53 +02:00
2019-01-09 17:39:32 +01:00
// register the IMediaFileSystem implementation
composition.RegisterFileSystem<IMediaFileSystem, MediaFileSystem>();
// register the supporting filesystems provider
composition.Register(factory => new SupportingFileSystems(factory), Lifetime.Singleton);
// register the IFileSystem supporting the IMediaFileSystem
2019-01-14 15:55:18 +01:00
// THIS IS THE ONLY THING THAT NEEDS TO CHANGE, IN ORDER TO REPLACE THE UNDERLYING FILESYSTEM
2019-01-09 17:39:32 +01:00
// and, SupportingFileSystem.For<IMediaFileSystem>() returns the underlying filesystem
2019-01-14 15:55:18 +01:00
composition.SetMediaFileSystem(() => 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
}
}
}