using System; using System.IO; using System.Reflection; using EmbedIO.Files; using EmbedIO.Utilities; namespace EmbedIO { partial class WebModuleContainerExtensions { /// /// Creates an instance of , uses it to initialize /// a , and adds the latter to a module container. /// /// The type of the module container. /// The on which this method is called. /// The base route of the module. /// The path of the directory to serve. /// if files and directories in /// are not expected to change during a web server's /// lifetime; otherwise. /// A callback used to configure the module. /// with a added. /// is . /// is . /// is not a valid local path. /// /// /// /// public static TContainer WithStaticFolder( this TContainer @this, string baseRoute, string fileSystemPath, bool isImmutable, Action? configure = null) where TContainer : class, IWebModuleContainer => WithStaticFolder(@this, null, baseRoute, fileSystemPath, isImmutable, configure); /// /// Creates an instance of , uses it to initialize /// a , and adds the latter to a module container, /// giving it the specified if not . /// /// /// OSX doesn't support , the parameter will be always . /// /// The type of the module container. /// The on which this method is called. /// The name. /// The base route of the module. /// The path of the directory to serve. /// if files and directories in /// are not expected to change during a web server's /// lifetime; otherwise. /// A callback used to configure the module. /// with a added. /// is . /// is . /// is not a valid local path. /// /// /// /// public static TContainer WithStaticFolder( this TContainer @this, string? name, string baseRoute, string fileSystemPath, bool isImmutable, Action? configure = null) where TContainer : class, IWebModuleContainer { #pragma warning disable CA2000 // Call Dispose on disposable - Ownership of provider is transferred to module var provider = new FileSystemProvider(fileSystemPath, isImmutable); #pragma warning restore CA2000 try { var module = new FileModule(baseRoute, provider); return WithModule(@this, name, module, configure); } catch { provider.Dispose(); throw; } } /// /// Creates an instance of , uses it to initialize /// a , and adds the latter to a module container. /// /// The type of the module container. /// The on which this method is called. /// The base route of the module. /// The assembly where served files are contained as embedded resources. /// A string to prepend to provider-specific paths /// to form the name of a manifest resource in . /// A callback used to configure the module. /// with a added. /// is . /// is . /// /// /// /// public static TContainer WithEmbeddedResources( this TContainer @this, string baseRoute, Assembly assembly, string pathPrefix, Action? configure = null) where TContainer : class, IWebModuleContainer => WithEmbeddedResources(@this, null, baseRoute, assembly, pathPrefix, configure); /// /// Creates an instance of , uses it to initialize /// a , and adds the latter to a module container, /// giving it the specified if not . /// /// The type of the module container. /// The on which this method is called. /// The name. /// The base route of the module. /// The assembly where served files are contained as embedded resources. /// A string to prepend to provider-specific paths /// to form the name of a manifest resource in . /// A callback used to configure the module. /// with a added. /// is . /// is . /// /// /// /// public static TContainer WithEmbeddedResources( this TContainer @this, string? name, string baseRoute, Assembly assembly, string pathPrefix, Action? configure = null) where TContainer : class, IWebModuleContainer { var module = new FileModule(baseRoute, new ResourceFileProvider(assembly, pathPrefix)); return WithModule(@this, name, module, configure); } /// /// Creates an instance of using a file-system path, uses it to initialize /// a , and adds the latter to a module container. /// /// The type of the module container. /// The on which this method is called. /// The base route of the module. /// The local path of the Zip file. /// A callback used to configure the module. /// with a added. /// is . /// /// /// /// public static TContainer WithZipFile( this TContainer @this, string baseRoute, string zipFilePath, Action? configure = null) where TContainer : class, IWebModuleContainer => WithZipFile(@this, null, baseRoute, zipFilePath, configure); /// /// Creates an instance of using a file-system path, uses it to initialize /// a , and adds the latter to a module container, /// giving it the specified if not . /// /// The type of the module container. /// The on which this method is called. /// The name. /// The base route of the module. /// The zip file-system path. /// A callback used to configure the module. /// with a added. /// is . /// /// /// /// public static TContainer WithZipFile( this TContainer @this, string? name, string baseRoute, string zipFilePath, Action? configure = null) where TContainer : class, IWebModuleContainer { #pragma warning disable CA2000 // Call Dispose on disposable - Ownership of provider is transferred to module var provider = new ZipFileProvider(zipFilePath); #pragma warning restore CA2000 try { var module = new FileModule(baseRoute, provider); return WithModule(@this, name, module, configure); } catch { provider.Dispose(); throw; } } /// /// Creates an instance of using a zip file as stream, uses it to initialize /// a , and adds the latter to a module container. /// /// The type of the module container. /// The on which this method is called. /// The base route of the module. /// The zip file as stream. /// A callback used to configure the module. /// with a added. /// is . /// /// /// /// public static TContainer WithZipFileStream( this TContainer @this, string baseRoute, Stream zipFileStream, Action? configure = null) where TContainer : class, IWebModuleContainer => WithZipFileStream(@this, null, baseRoute, zipFileStream, configure); /// /// Creates an instance of using a zip file as stream, uses it to initialize /// a , and adds the latter to a module container, /// giving it the specified if not . /// /// The type of the module container. /// The on which this method is called. /// The name. /// The base route of the module. /// The zip file as stream. /// A callback used to configure the module. /// with a added. /// is . /// /// /// /// public static TContainer WithZipFileStream( this TContainer @this, string? name, string baseRoute, Stream zipFileStream, Action? configure = null) where TContainer : class, IWebModuleContainer { #pragma warning disable CA2000 // Call Dispose on disposable - Ownership of provider is transferred to module var provider = new ZipFileProvider(zipFileStream); #pragma warning restore CA2000 try { var module = new FileModule(baseRoute, provider); return WithModule(@this, name, module, configure); } catch { provider.Dispose(); throw; } } } }