Пример #1
0
        internal static ScheduledTask NewInstance()
        {
            var  disableImageOptimizationAppSetting = System.Configuration.ConfigurationManager.AppSettings[ImageOptimizationConstants.DisableImageOptimizationAppSettingKey];
            bool disableImageOptimization;

            if (bool.TryParse(disableImageOptimizationAppSetting, out disableImageOptimization) && disableImageOptimization)
            {
                return(null);
            }

            if (ObjectFactory.GetArgsByName(typeof(ImageOptimizationConfig).Name, typeof(ImageOptimizationConfig)) == null)
            {
                return(null);
            }

            ImageOptimizationConfig imageOptimizationConfig = Config.Get <ImageOptimizationConfig>();

            if (!imageOptimizationConfig.EnableImageOptimization)
            {
                return(null);
            }

            if (!ImageOptimizationProcessorsHelper.ValidateImageOptimizationProcessorsConfigurations())
            {
                return(null);
            }

            // Schedule a task only if required IsOptimized field is present
            Type            contentType     = typeof(Image);
            MetadataManager metadataManager = MetadataManager.GetManager();
            MetaType        metaType        = metadataManager.GetMetaType(contentType);

            if (metaType != null)
            {
                MetaField metaField = metaType.Fields.SingleOrDefault(f => string.Compare(f.FieldName, ImageOptimizationConstants.IsOptimizedFieldName, true, CultureInfo.InvariantCulture) == 0);

                if (metaField == null)
                {
                    return(null);
                }
            }

            string crontabConfig = imageOptimizationConfig.ImageOptimizationCronSpec;

            if (string.IsNullOrEmpty(crontabConfig))
            {
                crontabConfig = ImageOptimizationTask.DefaultCronExpression;
            }

            ImageOptimizationTask task = new ImageOptimizationTask()
            {
                Id               = Guid.NewGuid(),
                ExecuteTime      = DateTime.UtcNow,
                ScheduleSpecType = CrontabScheduleCalculator.ScheduleSpecType,
                ScheduleSpec     = crontabConfig
            };

            return(task);
        }
Пример #2
0
        private static void Bootstrapper_Bootstrapped(object sender, EventArgs e)
        {
            Startup.InitializeHelperFields();
            ImageOptimizationTask.RemoveScheduledTasks();

            var  disableImageOptimizationAppSetting = System.Configuration.ConfigurationManager.AppSettings[ImageOptimizationConstants.DisableImageOptimizationAppSettingKey];
            bool disableImageOptimization;

            if (!(bool.TryParse(disableImageOptimizationAppSetting, out disableImageOptimization) && disableImageOptimization))
            {
                IList <IInstallableFileProcessor> imageOptimizationProcessors = new List <IInstallableFileProcessor>()
                {
                    new KrakenImageOptimizationProcessor(),
                    new TinifyImageOptimizationProcessor()
                };

                IList <IInstallableFileProcessor> imageOptimizationProcessorsToRegister = new List <IInstallableFileProcessor>();
                foreach (var imageOptimizationProcessor in imageOptimizationProcessors)
                {
                    if (!ImageOptimizationProcessorsHelper.IsImageOptimizationProcessorRegistered(imageOptimizationProcessor))
                    {
                        imageOptimizationProcessorsToRegister.Add(imageOptimizationProcessor);
                    }
                }

                if (imageOptimizationProcessorsToRegister.Any())
                {
                    ImageOptimizationProcessorsHelper.RegisterImageOptimizationProcessors(imageOptimizationProcessorsToRegister);
                }


                Startup.hassImageOptimizationProcessorEnabled = ImageOptimizationProcessorsHelper.ValidateImageOptimizationProcessorsConfigurations();

                Res.RegisterResource <ImageOptimizationResources>();
                Config.RegisterSection <ImageOptimizationConfig>();

                Startup.RegisterCrontabTasks();
            }
        }