public void GetProviderRootThrows(string providerRootPath, string webRootPath, string contentRootPath)
        {
            var providerOptions = new PhysicalFileSystemProviderOptions
            {
                ProviderRootPath = providerRootPath
            };

            Assert.Throws <InvalidOperationException>(() => PhysicalFileSystemProvider.GetProviderRoot(providerOptions, webRootPath, contentRootPath));
        }
        public void GetProviderRoot(string providerRootPath, string webRootPath, string contentRootPath, string expected)
        {
            var providerOptions = new PhysicalFileSystemProviderOptions
            {
                ProviderRootPath = providerRootPath
            };

            string actual = PhysicalFileSystemProvider.GetProviderRoot(providerOptions, webRootPath, contentRootPath);

            Assert.Equal(expected, actual);
        }
Exemple #3
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext <PuckContext>(options =>
                                                options.UseSqlServer(
                                                    Configuration.GetConnectionString("DefaultConnection"))
                                                , optionsLifetime: ServiceLifetime.Transient);
            services.AddDefaultIdentity <PuckUser>(options => { options.SignIn.RequireConfirmedAccount = false; })
            .AddRoles <PuckRole>()
            .AddEntityFrameworkStores <PuckContext>();
            services.AddMemoryCache();
            services.AddResponseCaching();
            services.AddSession();
            services.AddControllersWithViews()
            .AddApplicationPart(typeof(puck.core.Controllers.BaseController).Assembly)
            .AddControllersAsServices()
            .AddRazorRuntimeCompilation()
            .AddJsonOptions(options => options.JsonSerializerOptions.PropertyNamingPolicy = null);
            services.AddRazorPages();
            services.AddAuthentication().AddCookie(puck.core.Constants.Mvc.AuthenticationScheme, options => {
                options.LoginPath           = "/puck/admin/in";
                options.LogoutPath          = "/puck/admin/out";
                options.AccessDeniedPath    = "/puck/admin/in";
                options.ForwardAuthenticate = "Identity.Application";
            });
            services.AddHttpContextAccessor();
            services.AddPuckServices(Env, Configuration);

            PhysicalFileSystemProvider PhysicalProviderFactory(IServiceProvider provider)
            {
                var env = provider.GetRequiredService <Microsoft.AspNetCore.Hosting.IHostingEnvironment>();

                env.WebRootFileProvider = new PhysicalFileProvider(env.WebRootPath);
                var p = new PhysicalFileSystemProvider(
                    env,
                    provider.GetRequiredService <FormatUtilities>())
                {
                    Match = context =>
                    {
                        return(context.Request.Path.StartsWithSegments("/media"));
                    }
                };

                return(p);
            }

            AzureBlobStorageImageProvider AzureProviderFactory(IServiceProvider provider)
            {
                var containerName = provider.GetService <IConfiguration>().GetValue <string>("AzureImageTransformer_ContainerName");
                var p             = new AzureBlobStorageImageProvider(
                    provider.GetRequiredService <IOptions <AzureBlobStorageImageProviderOptions> >(),
                    provider.GetRequiredService <FormatUtilities>())
                {
                    Match = context =>
                    {
                        return(context.Request.Path.StartsWithSegments($"/{containerName}"));
                    }
                };

                return(p);
            }

            services.AddImageSharpCore()
            .SetRequestParser <QueryCollectionRequestParser>()
            .SetCache(provider =>
            {
                return(new PhysicalFileSystemCache(
                           provider.GetRequiredService <IOptions <PhysicalFileSystemCacheOptions> >(),
                           provider.GetRequiredService <Microsoft.AspNetCore.Hosting.IHostingEnvironment>(),
                           provider.GetRequiredService <IOptions <ImageSharpMiddlewareOptions> >(),
                           provider.GetRequiredService <FormatUtilities>()));
            })
            .SetCacheHash <CacheHash>()
            .AddProvider(AzureProviderFactory)
            .Configure <AzureBlobStorageImageProviderOptions>(options =>
            {
                options.ConnectionString = Configuration.GetValue <string>("AzureBlobStorageConnectionString");
                options.ContainerName    = Configuration.GetValue <string>("AzureImageTransformer_ContainerName");
            })
            .AddProvider(PhysicalProviderFactory)
            .AddProcessor <CropWebProcessor>()
            .AddProcessor <ResizeWebProcessor>()
            .AddProcessor <FormatWebProcessor>()
            .AddProcessor <BackgroundColorWebProcessor>();

            services.AddMiniProfiler(options => {
                // (Optional) Path to use for profiler URLs, default is /mini-profiler-resources
                //options.RouteBasePath = "/profiler";

                // (Optional) Control storage
                // (default is 30 minutes in MemoryCacheStorage)
                (options.Storage as MemoryCacheStorage).CacheDuration = TimeSpan.FromMinutes(60);

                // (Optional) Control which SQL formatter to use, InlineFormatter is the default
                options.SqlFormatter = new StackExchange.Profiling.SqlFormatters.InlineFormatter();
            });
        }
Exemple #4
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMemoryCache(x => x.SizeLimit = null);

            services.AddResponseCaching();
            services.AddSession();
            services.AddControllersWithViews()
            .AddApplicationPart(typeof(puck.core.Controllers.BaseController).Assembly)
            .AddControllersAsServices()
            .AddRazorRuntimeCompilation()
            .AddJsonOptions(options => options.JsonSerializerOptions.PropertyNamingPolicy = null);
            services.AddRazorPages();
            services.AddHttpContextAccessor();

            if (Configuration.GetValue <bool?>("UseSQLServer") ?? false)
            {
                services.AddPuckServices <User, Role, DbContextSQLServer>(Env, Configuration, ServiceLifetime.Scoped);
            }
            else if (Configuration.GetValue <bool?>("UsePostgreSQL") ?? false)
            {
                services.AddPuckServices <User, Role, DbContextPostgreSQL>(Env, Configuration, ServiceLifetime.Scoped);
            }
            else if (Configuration.GetValue <bool?>("UseMySQL") ?? false)
            {
                services.AddPuckServices <User, Role, DbContextMySQL>(Env, Configuration, ServiceLifetime.Scoped);
            }
            else if (Configuration.GetValue <bool?>("UseSQLite") ?? false)
            {
                services.AddPuckServices <User, Role, DbContextSQLite>(Env, Configuration, ServiceLifetime.Scoped);
            }

            PhysicalFileSystemProvider PhysicalProviderFactory(IServiceProvider provider)
            {
                var env = provider.GetRequiredService <Microsoft.AspNetCore.Hosting.IWebHostEnvironment>();

                env.WebRootFileProvider = new PhysicalFileProvider(env.WebRootPath);
                var p = new PhysicalFileSystemProvider(
                    env,
                    provider.GetRequiredService <FormatUtilities>())
                {
                    Match = context =>
                    {
                        return(context.Request.Path.StartsWithSegments("/media"));
                    }
                };

                return(p);
            }

            AzureBlobStorageImageProvider AzureProviderFactory(IServiceProvider provider)
            {
                var containerName = provider.GetService <IConfiguration>().GetValue <string>("AzureImageTransformer_ContainerName");
                var p             = new AzureBlobStorageImageProvider(
                    provider.GetRequiredService <IOptions <AzureBlobStorageImageProviderOptions> >(),
                    provider.GetRequiredService <FormatUtilities>())
                {
                    Match = context =>
                    {
                        return(context.Request.Path.StartsWithSegments($"/{containerName}"));
                    }
                };

                return(p);
            }

            if (!string.IsNullOrEmpty(Configuration.GetValue <string>("AzureBlobStorageConnectionString")) &&
                !string.IsNullOrEmpty(Configuration.GetValue <string>("AzureImageTransformer_ContainerName")))
            {
                services.AddImageSharpCore()
                .SetRequestParser <QueryCollectionRequestParser>()
                .SetCache(provider =>
                {
                    return(new PhysicalFileSystemCache(
                               provider.GetRequiredService <IOptions <PhysicalFileSystemCacheOptions> >(),
                               provider.GetRequiredService <Microsoft.AspNetCore.Hosting.IWebHostEnvironment>(),
                               provider.GetRequiredService <IOptions <ImageSharpMiddlewareOptions> >(),
                               provider.GetRequiredService <FormatUtilities>()));
                })
                .SetCacheHash <CacheHash>()
                .AddProvider(AzureProviderFactory)
                .Configure <AzureBlobStorageImageProviderOptions>(options =>
                {
                    options.BlobContainers.Add(new AzureBlobContainerClientOptions
                    {
                        ConnectionString = Configuration.GetValue <string>("AzureBlobStorageConnectionString"),
                        ContainerName    = Configuration.GetValue <string>("AzureImageTransformer_ContainerName")
                    });
                })
                .AddProvider(PhysicalProviderFactory)
                .AddProcessor <ResizeWebProcessor>()
                .AddProcessor <CropWebProcessor>()
                .AddProcessor <FormatWebProcessor>()
                .AddProcessor <BackgroundColorWebProcessor>();
            }
            else
            {
                services.AddImageSharpCore()
                .SetRequestParser <QueryCollectionRequestParser>()
                .SetCache(provider =>
                {
                    return(new PhysicalFileSystemCache(
                               provider.GetRequiredService <IOptions <PhysicalFileSystemCacheOptions> >(),
                               provider.GetRequiredService <Microsoft.AspNetCore.Hosting.IWebHostEnvironment>(),
                               provider.GetRequiredService <IOptions <ImageSharpMiddlewareOptions> >(),
                               provider.GetRequiredService <FormatUtilities>()));
                })
                .SetCacheHash <CacheHash>()
                .AddProvider(PhysicalProviderFactory)
                .AddProcessor <ResizeWebProcessor>()
                .AddProcessor <CropWebProcessor>()
                .AddProcessor <FormatWebProcessor>()
                .AddProcessor <BackgroundColorWebProcessor>();
            }
            services.AddMiniProfiler(options => {
                // (Optional) Path to use for profiler URLs, default is /mini-profiler-resources
                options.RouteBasePath = "/profiler";

                // (Optional) Control storage
                // (default is 30 minutes in MemoryCacheStorage)
                (options.Storage as MemoryCacheStorage).CacheDuration = TimeSpan.FromMinutes(60);

                // (Optional) Control which SQL formatter to use, InlineFormatter is the default
                options.SqlFormatter = new StackExchange.Profiling.SqlFormatters.InlineFormatter();

                options.EnableServerTimingHeader = true;

                options.IgnoredPaths.Add("/lib");
                options.IgnoredPaths.Add("/css");
                options.IgnoredPaths.Add("/js");
            });
        }