Created
January 3, 2022 12:45
-
-
Save tslazarov/9a89a3540f1ca80c84e7b157210998e5 to your computer and use it in GitHub Desktop.
Sitefinity Image Optimization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.ComponentModel; | |
using System.Diagnostics.CodeAnalysis; | |
using System.Linq; | |
using Progress.Sitefinity.ImageOptimization.Configuration; | |
using Progress.Sitefinity.ImageOptimization.Utils; | |
using Telerik.Sitefinity.Abstractions; | |
using Telerik.Sitefinity.Configuration; | |
using Telerik.Sitefinity.Data; | |
using Telerik.Sitefinity.Data.Events; | |
using Telerik.Sitefinity.GenericContent.Model; | |
using Telerik.Sitefinity.Libraries.Model; | |
using Telerik.Sitefinity.Model; | |
using Telerik.Sitefinity.Modules.Libraries; | |
using Telerik.Sitefinity.Services; | |
namespace Progress.Sitefinity.ImageOptimization | |
{ | |
/// <summary> | |
/// Contains the application startup event handlers registering the required components for the packaging module of Sitefinity. | |
/// </summary> | |
[ExcludeFromCodeCoverage] | |
public static class Startup | |
{ | |
/// <summary> | |
/// Called before the Asp.Net application is started. Subscribes for the logging and exception handling configuration related events. | |
/// </summary> | |
[EditorBrowsable(EditorBrowsableState.Never)] | |
public static void OnPreApplicationStart() | |
{ | |
Bootstrapper.Initialized -= Bootstrapper_Initialized; | |
Bootstrapper.Initialized += Bootstrapper_Initialized; | |
} | |
private static void Bootstrapper_Initialized(object sender, ExecutedEventArgs e) | |
{ | |
if (e.CommandName == "Bootstrapped") | |
{ | |
EventHub.Subscribe<IDataEvent>(Content_Action); | |
} | |
} | |
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); | |
} | |
} | |
private static bool ValidateEventType(IDataEvent @event) | |
{ | |
string action = @event.Action; | |
Type contentType = @event.ItemType; | |
if (action != "Updated" || contentType != typeof(Image)) | |
{ | |
return false; | |
} | |
var propertyChangeDataEvent = @event as IPropertyChangeDataEvent; | |
if (propertyChangeDataEvent == null || (!propertyChangeDataEvent.ChangedProperties.Any(p => p.Key == "Thumbnails") && !propertyChangeDataEvent.ChangedProperties.Any(p => p.Key == "IsOptimized"))) | |
{ | |
return false; | |
} | |
if(propertyChangeDataEvent.ChangedProperties.Any(p => p.Key == "IsOptimized")) | |
{ | |
var changedIsOptimized = propertyChangeDataEvent.ChangedProperties.FirstOrDefault(p => p.Key == "IsOptimized"); | |
if ((bool)changedIsOptimized.Value.NewValue == Startup.hassImageOptimizationProcessorEnabled) | |
{ | |
return false; | |
} | |
} | |
return true; | |
} | |
private static bool hassImageOptimizationProcessorEnabled; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment