Clearing ImageProcessor Cache in Azure
- Page Owner: Not Set
- Last Reviewed: 2021-03-04
How do you clear ImageProcessor thumbnails in Azure ImageProcessor Cache
Answer
Here is the code that is a service that I call on IContentPublishing Event
public void PublishingContent(object sender, ContentEventArgs e)
{
if (ConfigurationManager.AppSettings.Get("episerver:EnvironmentName").HasValue())
{
var enviroment = ServiceLocator.Current.GetInstance<DxcApplicationContext>();
if (enviroment.CurrentEnvironment == DxcEnvironment.Integration || enviroment.CurrentEnvironment == DxcEnvironment.Preproduction || enviroment.CurrentEnvironment == DxcEnvironment.Production)
{
if (e.Content is ImageFile imageFile)
{
var provider = new AzureThumbnailCleanerService();
provider.DeleteThumbnails(imageFile);
}
}
}
}
This is the service that handles the deleting of thumbnails
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
[ServiceConfiguration(FactoryType = typeof(AzureThumbnailCleanerService), Lifecycle = ServiceInstanceScope.Singleton)]
public class AzureThumbnailCleanerService
{
private static CloudBlobContainer rootContainer;
public AzureThumbnailCleanerService()
{
string providerName = EPiServerFrameworkSection.Instance.Blob.DefaultProvider;
if (rootContainer == null)
{
// Get the name of the connection string from there
string connectionStringName = EPiServerFrameworkSection.Instance.Blob.Providers[providerName].Parameters["connectionStringName"];
// Get storage account from connection string.
CloudStorageAccount cloudCachedStorageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString);
// Create the blob client.
CloudBlobClient cloudBlobClient = cloudCachedStorageAccount.CreateCloudBlobClient();
// Retrieve reference to the container. Container is already created as part of the initialization process for the BlobProvider
rootContainer = cloudBlobClient.GetContainerReference(EPiServerFrameworkSection.Instance.Blob.Providers[providerName].Parameters["container"]);
}
}
public void DeleteThumbnails(ImageFile media)
{
string containerName = media?.BinaryDataContainer?.Segments[1];
CloudBlockBlob directory = rootContainer.GetBlockBlobReference(containerName);
// Get list of directories based on media data
var items = rootContainer.ListBlobs(directory.Name);
if (items.HasValue())
{
// Loop the blobs and see if it is a cloud directory.
foreach (var item in items)
{
if (item is CloudBlobDirectory cloudDirectory)
{
// YES SIR!!!!
// get blobs
var dir = cloudDirectory.ListBlobs(true);
var blobs = dir.OfType<CloudBlob>();
foreach (var blob in blobs)
{
// Check for names
var paths = blob.Name.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
if (paths.LastOrDefault().Contains("_Thumbnail.jpg") ||
paths.LastOrDefault().Contains("_Thumb900x450.jpg") ||
paths.LastOrDefault().Contains("_thumb350x175.jpg") ||
paths.LastOrDefault().Contains("_Thumb530x450.jpg") ||
paths.LastOrDefault().Contains("_thumb2x1.jpg") ||
paths.LastOrDefault().Contains("_ThumbMax560.png") ||
paths.LastOrDefault().Contains("_Thumb720x360.jpg") ||
paths.LastOrDefault().Contains("_thumb767.jpg") ||
paths.LastOrDefault().StartsWith("3p!_")) // The lovely file for Imageprocessor
{
// DEUCES!!!
blob.DeleteIfExists();
continue;
}
}
}
}
}
}
}
Here is the code for DXPContext Enviroment
[ServiceConfiguration(Lifecycle = ServiceInstanceScope.Singleton)]
public class DxcApplicationContext
{
private DxcEnvironment environment;
public DxcApplicationContext()
{
var setting = ConfigurationManager.AppSettings.Get("episerver:EnvironmentName");
if (!Enum.TryParse(setting, true, out environment))
{
//Default environment is integration;
environment = DxcEnvironment.Integration;
}
}
public DxcEnvironment CurrentEnvironment => environment;
}
public enum DxcEnvironment
{
Local,
Integration,
Preproduction,
Production
}