Created
May 5, 2023 10:31
-
-
Save hawjeh/46775333e616621cf8a4bba2692f9c01 to your computer and use it in GitHub Desktop.
SparksCloudFront
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
namespace SparksCloudFront | |
{ | |
public class SparksAppSetting | |
{ | |
public CloudFront CloudFront { get; set; } | |
} | |
public class CloudFront | |
{ | |
public string Profile { get; set; } | |
public string DistributionId { get; set; } | |
public string AccessKeyId { get; set; } | |
public string AccessKey { get; set; } | |
} | |
} |
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 Amazon; | |
using Amazon.CloudFront; | |
using Amazon.CloudFront.Model; | |
using Newtonsoft.Json; | |
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using Telerik.Sitefinity.Abstractions; | |
using Telerik.Sitefinity.Data; | |
using Telerik.Sitefinity.Data.Events; | |
using Telerik.Sitefinity.Modules.Pages; | |
using Telerik.Sitefinity.Pages.Model; | |
using Telerik.Sitefinity.Publishing; | |
using Telerik.Sitefinity.Services; | |
namespace SparksCloudFront | |
{ | |
public class SparksEventSubscriber | |
{ | |
public static void Start() | |
{ | |
Bootstrapper.Bootstrapped += Bootstrapper_Bootstrapped; | |
} | |
private static void Bootstrapper_Bootstrapped(object sender, EventArgs e) | |
{ | |
EventHub.Subscribe<IDataEvent>(evt => DataEventHandler(evt)); | |
} | |
// https://community.progress.com/s/article/How-to-catch-the-Publish-Unpublish-and-Save-as-draft-events-using-API | |
private static void DataEventHandler(IDataEvent eventInfo) | |
{ | |
var action = eventInfo.Action; | |
var contentType = eventInfo.ItemType; | |
if (contentType != typeof(PageNode)) | |
{ | |
return; | |
} | |
if (action == "Updated" && eventInfo.GetPropertyValue<bool>("HasPageDataChanged") == true && eventInfo.GetPropertyValue<string>("ApprovalWorkflowState") == "Published") | |
{ | |
var urls = new List<string>(); | |
var itemId = eventInfo.ItemId; | |
var providerName = eventInfo.ProviderName; | |
var manager = ManagerBase.GetMappedManager(contentType, providerName); | |
var item = (PageNode)manager.GetItemOrDefault(contentType, itemId); | |
urls.Add(item.GetUrl().Replace("~", "")); | |
if (item.Urls.Count > 0) | |
{ | |
urls.AddRange(item.Urls.Select(x => x.Url.Replace("~", "")).ToList()); | |
} | |
var homePageId = SystemManager.CurrentContext.CurrentSite.HomePageId; | |
if (homePageId != null || homePageId != Guid.Empty && item.Id == homePageId) | |
{ | |
urls.Add("/"); | |
} | |
ClearCloudFrontCache(urls); | |
} | |
} | |
private static void ClearCloudFrontCache(List<string> urls) | |
{ | |
using (var reader = new StreamReader(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin\\sparksSetting.json"))) | |
{ | |
var settings = JsonConvert.DeserializeObject<SparksAppSetting>(reader.ReadToEnd()); | |
var client = new AmazonCloudFrontClient(settings.CloudFront.AccessKeyId, settings.CloudFront.AccessKey, RegionEndpoint.APSoutheast1); | |
var batch = new InvalidationBatch() | |
{ | |
CallerReference = DateTime.Now.ToString("ddMMyyyyHHmmss"), | |
Paths = new Paths() { Items = urls, Quantity = urls.Count }, | |
}; | |
var request = new CreateInvalidationRequest(settings.CloudFront.DistributionId, batch); | |
client.CreateInvalidation(request); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment