Exemplo n.º 1
0
        private static void Content_Action(IDataEvent @event)
        {
            try
            {
                Type   contentType  = @event.ItemType;
                Guid   itemId       = @event.ItemId;
                string providerName = @event.ProviderName;
                string language     = @event.GetLanguage();

                if (!ValidateEventType(@event))
                {
                    return;
                }

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

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

                LibrariesManager manager = ManagerBase.GetMappedManager(contentType, providerName) as LibrariesManager;

                if (manager == null)
                {
                    return;
                }

                var   item  = manager.GetItemOrDefault(contentType, itemId);
                Image image = item as Image;

                if (image.Status == ContentLifecycleStatus.Master)
                {
                    var imageTemp = manager.Lifecycle.CheckOut(image) as Image;
                    imageTemp.SetValue(ImageOptimizationConstants.IsOptimizedFieldName, Startup.hassImageOptimizationProcessorEnabled);
                    manager.Lifecycle.CheckIn(imageTemp);
                }
                else if (image.Status == ContentLifecycleStatus.Temp)
                {
                    image.SetValue(ImageOptimizationConstants.IsOptimizedFieldName, Startup.hassImageOptimizationProcessorEnabled);
                    Image master = manager.Lifecycle.GetMaster(image) as Image;
                    master.SetValue(ImageOptimizationConstants.IsOptimizedFieldName, Startup.hassImageOptimizationProcessorEnabled);
                }

                manager.SaveChanges();
            }
            catch (Exception ex)
            {
                Log.Write(string.Format("Error occurred while setting image optimization field value: {0}", ex.Message), ConfigurationPolicy.ErrorLog);
            }
        }