Ejemplo n.º 1
0
 public ImagesController(IImageRepository imageRepository, AuthorizationService authService, ImageBuilderFactory imageBuilderFactory, ImageStorageSettings settings)
 {
     _imageRepository     = imageRepository;
     _authService         = authService;
     _imageBuilderFactory = imageBuilderFactory;
     _settings            = settings;
 }
Ejemplo n.º 2
0
 public IVLConfig()
 {
     UserSettings             = new UserSettings();
     AnnotationColorSelection = new AnnotationColorSelection();
     FirmwareSettings         = new FirmwareSettings();
     PostProcessingSettings   = new PostProcessingSettings();
     CameraSettings           = new CameraSettings();
     PrinterSettings          = new PrinterSettings();
     ImageStorageSettings     = new ImageStorageSettings();
     UISettings = new UISettings();
 }
Ejemplo n.º 3
0
 public AssetsController(
     ILogger <AssetsController> logger,
     IOptions <AppSettings> settings,
     IOptions <ImageStorageSettings> imageStorageSettings,
     IBlogImageStorage imageStorage,
     IBlogConfig blogConfig,
     IWebHostEnvironment env)
 {
     _settings             = settings.Value;
     _blogConfig           = blogConfig;
     _env                  = env;
     _imageStorage         = imageStorage;
     _imageStorageSettings = imageStorageSettings.Value;
     _logger               = logger;
 }
Ejemplo n.º 4
0
 public Settings()
 {
     UserSettings             = new UserSettings();
     AnnotationColorSelection = new AnnotationColorSelection();
     FirmwareSettings         = new FirmwareSettings();
     PostProcessingSettings   = new PostProcessingSettings();
     CameraSettings           = new CameraSettings();
     PrinterSettings          = new PrinterSettings();
     ImageStorageSettings     = new ImageStorageSettings();
     UISettings          = new UISettings();
     ImageNameSettings   = new ImageNameSettings();
     EmailSettings       = new EmailSettings();
     ReportSettings      = new ReportSettings();
     PrinterPPSettings   = new PrinterPPSettings();
     MotorOffSetSettings = new AdvanceSettings.MotorOffSetSettings();
 }
Ejemplo n.º 5
0
 public ImageController(
     IBlogImageStorage imageStorage,
     ILogger <ImageController> logger,
     IBlogConfig blogConfig,
     IMemoryCache cache,
     IFileNameGenerator fileNameGenerator,
     IOptions <AppSettings> settings,
     IOptions <ImageStorageSettings> imageStorageSettings)
 {
     _imageStorage         = imageStorage;
     _logger               = logger;
     _blogConfig           = blogConfig;
     _cache                = cache;
     _fileNameGenerator    = fileNameGenerator;
     _settings             = settings.Value;
     _imageStorageSettings = imageStorageSettings.Value;
 }
Ejemplo n.º 6
0
        private void RegisterCustomDependencies(IServiceCollection services)
        {
            //settings
            var databaseSettings = new DatabaseSettings(Configuration.GetConnectionString("DefaultConnection"));

            services.AddSingleton(databaseSettings);
            var imageStorageSettings = new ImageStorageSettings();

            imageStorageSettings.ImageRepositoryPath = Configuration.GetValue <string>("ImageRepositoryPath");
            services.AddSingleton(imageStorageSettings);
            //repositories
            services.AddScoped <IImageRepository, ImageRepository>();
            services.AddScoped <IAuthorizationRepository, AuthorizationRepository>();
            //other
            services.AddSingleton <ImageBuilderFactory>();
            services.AddScoped <AuthorizationService>();
        }
Ejemplo n.º 7
0
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddOptions();
            services.AddMemoryCache();
            services.AddRateLimit(_configuration.GetSection("IpRateLimiting"));

            services.Configure <AppSettings>(_appSettingsSection);

            var authentication = new AuthenticationSettings();

            _configuration.Bind(nameof(Authentication), authentication);
            services.Configure <AuthenticationSettings>(_configuration.GetSection(nameof(Authentication)));

            var imageStorage = new ImageStorageSettings();

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

            services.AddSession(options =>
            {
                options.IdleTimeout     = TimeSpan.FromMinutes(20);
                options.Cookie.HttpOnly = true;
            });

            services.AddApplicationInsightsTelemetry();
            services.AddMoongladeAuthenticaton(authentication);
            services.AddMvc(options =>
                            options.Filters.Add(new AutoValidateAntiforgeryTokenAttribute()));

            services.AddAntiforgery(options =>
            {
                const string cookieBaseName = "CSRF-TOKEN-MOONGLADE";
                options.Cookie.Name         = $"X-{cookieBaseName}";
                options.FormFieldName       = $"{cookieBaseName}-FORM";
            });

            services.AddMoongladeImageStorage(imageStorage, _environment.ContentRootPath);
            services.AddScoped(typeof(IRepository <>), typeof(DbContextRepository <>));
            services.TryAddSingleton <IActionContextAccessor, ActionContextAccessor>();
            services.AddSingleton <IBlogConfig, BlogConfig>();
            services.AddScoped <IMoongladeAudit, MoongladeAudit>();
            services.AddScoped <DeleteSubscriptionCache>();
            services.AddScoped <ISiteIconGenerator, FileSystemSiteIconGenerator>();
            services.AddScoped <IDateTimeResolver>(c =>
                                                   new DateTimeResolver(c.GetService <IBlogConfig>().GeneralSettings.TimeZoneUtcOffset));

            services.AddScoped <IExportManager, ExportManager>();
            services.AddScoped <IPingbackSender, PingbackSender>();
            services.AddScoped <IPingbackReceiver, PingbackReceiver>();
            services.AddScoped <IFileSystemOpmlWriter, FileSystemOpmlWriter>();
            services.AddScoped <IFileNameGenerator>(gen => new GuidFileNameGenerator(Guid.NewGuid()));
            services.AddSessionBasedCaptcha();

            var asm = Assembly.GetAssembly(typeof(MoongladeService));

            if (null != asm)
            {
                var types = asm.GetTypes().Where(t => t.IsClass && t.IsPublic && t.Name.EndsWith("Service"));
                foreach (var t in types)
                {
                    services.AddScoped(t, t);
                }
            }

            services.AddHttpClient <IMoongladeNotificationClient, NotificationClient>()
            .AddTransientHttpErrorPolicy(builder =>
                                         builder.WaitAndRetryAsync(3, retryCount =>
                                                                   TimeSpan.FromSeconds(Math.Pow(2, retryCount)),
                                                                   (result, span, retryCount, context) =>
            {
                _logger?.LogWarning($"Request failed with {result.Result.StatusCode}. Waiting {span} before next retry. Retry attempt {retryCount}/3.");
            }));

            services.AddDbContext <MoongladeDbContext>(options =>
                                                       options.UseLazyLoadingProxies()
                                                       .UseSqlServer(_configuration.GetConnectionString(Constants.DbConnectionName), sqlOptions =>
            {
                sqlOptions.EnableRetryOnFailure(
                    3,
                    TimeSpan.FromSeconds(30),
                    null);
            }));
        }
Ejemplo n.º 8
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);
            }
        }
Ejemplo n.º 9
0
        private void AddImageStorage(IServiceCollection services)
        {
            var imageStorage = new ImageStorageSettings();

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

            if (imageStorage.CDNSettings.GetImageByCDNRedirect)
            {
                if (string.IsNullOrWhiteSpace(imageStorage.CDNSettings.CDNEndpoint))
                {
                    throw new ArgumentNullException(nameof(imageStorage.CDNSettings.CDNEndpoint),
                                                    $"{nameof(imageStorage.CDNSettings.CDNEndpoint)} must be specified when {nameof(imageStorage.CDNSettings.GetImageByCDNRedirect)} 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 AzureStorageInfo(conn, container));
                services.AddSingleton <IAsyncImageStorageProvider, AzureStorageImageProvider>();
                break;

            case "filesystem":
                var path = imageStorage.FileSystemSettings.Path;
                try
                {
                    var fullPath = Utils.ResolveImageStoragePath(Environment.ContentRootPath, path);

                    _logger.LogInformation($"Setting {nameof(FileSystemImageProvider)} to use Path: {fullPath}");
                    services.AddSingleton(s => new FileSystemImageProviderInfo(fullPath));
                    services.AddSingleton <IAsyncImageStorageProvider, FileSystemImageProvider>();
                }
                catch (Exception e)
                {
                    _logger.LogCritical(e, $"Error setting path for {nameof(FileSystemImageProvider)}, raw path: {path}");
                    throw;
                }

                break;

            default:
                var msg = $"Provider {imageStorageProvider} is not supported.";
                _logger.LogCritical(msg);
                throw new NotSupportedException(msg);
            }
        }
Ejemplo n.º 10
0
 public ImageHandler(ImageStorageSettings imageStorageSettings)
 {
     _imageStorageSettings = imageStorageSettings;
 }