Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Media Cache Azure WebApps
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ClearMediaCache.aspx.cs" Inherits="SitecoreProject.sitecore.admin.MediaCache" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server" />
</div>
<br />
<br />
<div>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Get Files List" />
<br />
<br />
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Clear Media Cache" />
<br />
<br />
</div>
<div>
<asp:Label ID="Label1" runat="server" />
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Sitecore.Data.Items;
using Sitecore.Resources.Media;
using Sitecore.Sites;
namespace SitecoreProject.sitecore.admin
{
public partial class MediaCache : System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
}
protected void Button1_Click (object sender, EventArgs e)
{
Label1.Text = "";
StringBuilder sb = new StringBuilder ();
string id = TextBox1.Text;
var files = GetFilesList (id);
foreach (var file in files) {
var parentDirectory = Directory.GetParent (file).FullName;
sb.AppendFormat ("Filename is {0}", file);
sb.Append ("</br>");
sb.AppendFormat ("Parent Directory is {0}", parentDirectory);
sb.Append ("</br>");
sb.Append ("</br>");
}
Label1.Text = sb.ToString ();
}
protected void Button2_Click (object sender, EventArgs e)
{
Label1.Text = "";
StringBuilder sb = new StringBuilder ();
string id = TextBox1.Text;
var files = GetFilesList (id);
foreach (var file in files) {
sb.AppendFormat ("Filename is {0}", file);
sb.Append ("</br>");
var parentDirectory = Directory.GetParent (file).FullName;
sb.AppendFormat ("Deleting Parent Directory");
Directory.Delete (parentDirectory, true);
}
Label1.Text = sb.ToString ();
}
private List<string> GetFilesList (string id)
{
var database = Sitecore.Data.Database.GetDatabase ("master");
var mediaItem = new MediaItem (database.GetItem (new Sitecore.Data.ID (id)));
var media = Sitecore.Resources.Media.MediaManager.GetMedia (mediaItem);
var mediaId = media.MediaData.MediaId;
var mediaCachePath = Sitecore.Configuration.Settings.GetSetting ("Media.CacheFolder");
var finalPath = Sitecore.IO.FileUtil.MapPath (mediaCachePath);
var files = Directory.EnumerateFiles (finalPath, "*.*", SearchOption.AllDirectories).Where (s => s.Contains (mediaId + ".ini"));
return files.ToList ();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.