Exemplo n.º 1
0
        public void TestResolveImageStoragePath_Invalid_Char()
        {
            var contentRootPath = @"C:\Moonglade";
            var path            = @"${basedir}\Uploads<>|foo";

            Assert.Catch <InvalidOperationException>(() => { FileSystemImageStorage.ResolveImageStoragePath(contentRootPath, path); });
        }
Exemplo n.º 2
0
        public void TestResolveImageStoragePath_Invalid_Relative()
        {
            var contentRootPath = @"C:\Moonglade";
            var path            = @"..\${basedir}\Uploads";

            Assert.Catch <NotSupportedException>(() => { FileSystemImageStorage.ResolveImageStoragePath(contentRootPath, path); });
        }
Exemplo n.º 3
0
        public void ResolveImageStoragePath_Valid_Relative()
        {
            var contentRootPath = @"C:\Moonglade";
            var path            = @"${basedir}\Uploads";

            var finalPath = FileSystemImageStorage.ResolveImageStoragePath(contentRootPath, path);

            Assert.IsTrue(finalPath == @"C:\Moonglade\Uploads");
        }
Exemplo n.º 4
0
        public void ResolveImageStoragePath_Valid_Absolute()
        {
            var contentRootPath = @"C:\Moonglade";
            var path            = @"C:\MoongladeData\Uploads";

            var finalPath = FileSystemImageStorage.ResolveImageStoragePath(contentRootPath, path);

            Assert.IsTrue(finalPath == @"C:\MoongladeData\Uploads");
        }
Exemplo n.º 5
0
        public void TestResolveImageStoragePath_EmptyParameter(string path)
        {
            var contentRootPath = @"C:\Moonglade";

            Assert.Catch <ArgumentNullException>(() => { FileSystemImageStorage.ResolveImageStoragePath(contentRootPath, path); });
        }
Exemplo n.º 6
0
 public void TestResolveImageStoragePath_EmptyParameter(string path)
 {
     Assert.Catch<ArgumentNullException>(() => { FileSystemImageStorage.ResolveImageStoragePath(path); });
 }
Exemplo n.º 7
0
        public static void AddImageStorage(
            this IServiceCollection services, IConfiguration configuration, IWebHostEnvironment environment)
        {
            var imageStorage = new ImageStorageSettings();

            configuration.Bind(nameof(ImageStorage), imageStorage);
            services.Configure <ImageStorageSettings>(configuration.GetSection(nameof(ImageStorage)));

            services.AddScoped <IFileNameGenerator>(gen => new GuidFileNameGenerator(Guid.NewGuid()));

            if (imageStorage.CDNSettings.EnableCDNRedirect)
            {
                if (string.IsNullOrWhiteSpace(imageStorage.CDNSettings.CDNEndpoint))
                {
                    throw new ArgumentNullException(nameof(imageStorage.CDNSettings.CDNEndpoint),
                                                    $"{nameof(imageStorage.CDNSettings.CDNEndpoint)} must be specified when {nameof(imageStorage.CDNSettings.EnableCDNRedirect)} is set to 'true'.");
                }

                // _logger.LogWarning("Images are configured to use CDN, the endpoint is out of control, use it on your own risk.");

                // Validate endpoint Url to avoid security risks
                // But it still has risks:
                // e.g. If the endpoint is compromised, the attacker could return any kind of response from a image with a big f**k to a script that can attack users.

                var endpoint        = imageStorage.CDNSettings.CDNEndpoint;
                var isValidEndpoint = endpoint.IsValidUrl(Utils.UrlScheme.Https);
                if (!isValidEndpoint)
                {
                    throw new UriFormatException("CDN Endpoint is not a valid HTTPS Url.");
                }
            }

            if (null == imageStorage.Provider)
            {
                throw new ArgumentNullException("Provider", "Provider can not be null.");
            }

            var imageStorageProvider = imageStorage.Provider.ToLower();

            if (string.IsNullOrWhiteSpace(imageStorageProvider))
            {
                throw new ArgumentNullException("Provider", "Provider can not be empty.");
            }

            switch (imageStorageProvider)
            {
            case "azurestorage":
                var conn      = imageStorage.AzureStorageSettings.ConnectionString;
                var container = imageStorage.AzureStorageSettings.ContainerName;
                services.AddSingleton(s => new AzureBlobConfiguration(conn, container));
                services.AddSingleton <IBlogImageStorage, AzureBlobImageStorage>();
                break;

            case "filesystem":
                var path     = imageStorage.FileSystemSettings.Path;
                var fullPath = FileSystemImageStorage.ResolveImageStoragePath(environment.ContentRootPath, path);
                services.AddSingleton(s => new FileSystemImageConfiguration(fullPath));
                services.AddSingleton <IBlogImageStorage, FileSystemImageStorage>();
                break;

            default:
                var msg = $"Provider {imageStorageProvider} is not supported.";
                throw new NotSupportedException(msg);
            }
        }