Created
September 5, 2017 20:39
-
-
Save PNergard/952e4225563a27a79b4671ad4dee0412 to your computer and use it in GitHub Desktop.
Assets cleaner. A simple plugin that lets you delete all or selected unreferenced assets
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
<%@ Page Language="c#" CodeBehind="Cleaner.aspx.cs" AutoEventWireup="False" Inherits="LatestEpi.modules.CleanAssetsPlugin.Cleaner" Title="" %> | |
<%@ Import Namespace="EPiServer.Web.Mvc.Html" %> | |
<asp:content contentplaceholderid="MainRegion" runat="server"> | |
<asp:HiddenField runat="server" id="SelectedMedia" ClientIDMode="static" /> | |
<script type="text/javascript"> | |
function SetSelected(getAll) { | |
var checkboxValues = []; | |
if (getAll) { | |
$('input[type="checkbox"]').map(function () { | |
checkboxValues.push($(this).val()); | |
}); | |
} | |
else | |
{ | |
$('input:checked').map(function () { | |
checkboxValues.push($(this).val()); | |
}); | |
} | |
$('#SelectedMedia').val(checkboxValues.join(',')); | |
}; | |
</script> | |
<div class="epi-formArea epi-paddingHorizontal"> | |
<fieldset> | |
<legend>Summary</legend> | |
<dl> | |
<dt>All media:</dt><dd><asp:Literal runat="server" id="allMediaCount" /></dd> | |
<dt>Unreferenced media</dt><dd><asp:Literal runat="server" id="unreferencedMediaCount" /></dd> | |
</dl> | |
</fieldset> | |
<div class="epi-buttonContainer"> | |
<span class="epi-cmsButton"> | |
<asp:Button OnClientClick="SetSelected(1)" ClientIDMode="Static" Text="Delete all" CssClass="epi-cmsButton-text epi-cmsButton-tools epi-cmsButton-Save" id="savereset" onmouseover="EPi.ToolButton.MouseDownHandler(this)" onmouseout="EPi.ToolButton.ResetMouseDownHandler(this)" runat="server" /> | |
</span> | |
<span class="epi-cmsButton"> | |
<asp:Button OnClientClick="SetSelected()" Text="Delete selected" CssClass="epi-cmsButton-text epi-cmsButton-tools epi-cmsButton-Save" id="save" onmouseover="EPi.ToolButton.MouseDownHandler(this)" onmouseout="EPi.ToolButton.ResetMouseDownHandler(this)" runat="server" /> | |
</span> | |
</div> | |
<div class="epi-buttonContainer"></div> | |
<fieldset> | |
<legend>Unreferenced media</legend> | |
<dl> | |
<dd> | |
<asp:Repeater runat="server" id="rptUnreferencedMedia"> | |
<ItemTemplate> | |
<div class="epi-size25"> | |
<input type="checkbox" value="<%#MD.ContentLink %>" /> <label><a href="<%#Url.ContentUrl(MD.ContentLink) %>" target="_blank"><%# BreadCrumb(MD)%></a></label> | |
</div> | |
</ItemTemplate> | |
</asp:Repeater> | |
<asp:Literal runat="server" id="Literal4" /> | |
</dd> | |
</dl> | |
</fieldset> | |
</div> | |
</asp:content> |
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.Collections.Generic; | |
using System.Web.Security; | |
using System.Web.UI.WebControls; | |
using EPiServer.Personalization; | |
using EPiServer.PlugIn; | |
using EPiServer.Security; | |
using EPiServer.Util.PlugIns; | |
using System.Web.UI; | |
using EPiServer.Shell.WebForms; | |
using EPiServer; | |
using EPiServer.Core; | |
using EPiServer.ServiceLocation; | |
using System.Linq; | |
using System.Web.Mvc; | |
namespace LatestEpi.modules.CleanAssetsPlugin | |
{ | |
[GuiPlugIn(DisplayName = "Assets cleaner", Description = "", Area = PlugInArea.AdminMenu, Url = "~/modules/CleanAssetsPlugin/Cleaner.aspx")] | |
public partial class Cleaner : WebFormsBase | |
{ | |
private IContentRepository contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>(); | |
protected UrlHelper Url = ServiceLocator.Current.GetInstance<UrlHelper>(); | |
protected MediaData MD { get { return Page.GetDataItem() as MediaData; } } | |
protected override void OnPreInit(EventArgs e) | |
{ | |
base.OnPreInit(e); | |
MasterPageFile = UriSupport.ResolveUrlFromUIBySettings("MasterPages/EPiServerUI.master"); | |
SystemMessageContainer.Heading = "Assets cleaner"; | |
SystemMessageContainer.Description = "A plugin that simply removes all or selected unreferenced media in the assets folder."; | |
} | |
protected override void OnLoad(EventArgs e) | |
{ | |
if (IsPostBack) | |
{ | |
var mediaContentLinks = SelectedMedia.Value.Split(",".ToCharArray()); | |
foreach(var mediaLink in mediaContentLinks) | |
{ | |
contentRepository.Delete(ContentReference.Parse(mediaLink), false, AccessLevel.NoAccess); | |
} | |
} | |
List<MediaData> unReferencedList = new List<MediaData>(); | |
var allDescendantsInAssetsFolder = contentRepository.GetDescendents(ContentReference.GlobalBlockFolder).Select(contentRepository.Get<IContent>).ToList(); | |
var allMediaInAssetsFolder = allDescendantsInAssetsFolder.OfType<MediaData>().ToList(); | |
foreach (var media in allMediaInAssetsFolder) | |
{ | |
var refs = contentRepository.GetReferencesToContent(media.ContentLink, false); | |
if (refs.Count() == 0) | |
{ | |
unReferencedList.Add(media); | |
} | |
} | |
allMediaCount.Text = allMediaInAssetsFolder.Count().ToString(); | |
unreferencedMediaCount.Text = unReferencedList.Count().ToString(); | |
rptUnreferencedMedia.DataSource = unReferencedList; | |
rptUnreferencedMedia.DataBind(); | |
} | |
protected string BreadCrumb(IContent content) | |
{ | |
var ancestor = contentRepository.GetAncestors(content.ContentLink).ToList(); | |
string path = ""; | |
for (int x = ancestor.Count() - 1; x >= 0; x--) | |
{ | |
path += "\\" + ancestor[x].Name; | |
} | |
return (path + "\\" + content.Name).Trim("\\".ToCharArray()); | |
} | |
protected string UiUrl() | |
{ | |
return EPiServer.Configuration.Settings.Instance.UIUrl.ToString().Trim('~'); | |
} | |
} | |
} |
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
//------------------------------------------------------------------------------ | |
// <auto-generated> | |
// This code was generated by a tool. | |
// | |
// Changes to this file may cause incorrect behavior and will be lost if | |
// the code is regenerated. | |
// </auto-generated> | |
//------------------------------------------------------------------------------ | |
namespace LatestEpi.modules.CleanAssetsPlugin { | |
public partial class Cleaner { | |
/// <summary> | |
/// SelectedMedia control. | |
/// </summary> | |
/// <remarks> | |
/// Auto-generated field. | |
/// To modify move field declaration from designer file to code-behind file. | |
/// </remarks> | |
protected global::System.Web.UI.WebControls.HiddenField SelectedMedia; | |
/// <summary> | |
/// allMediaCount control. | |
/// </summary> | |
/// <remarks> | |
/// Auto-generated field. | |
/// To modify move field declaration from designer file to code-behind file. | |
/// </remarks> | |
protected global::System.Web.UI.WebControls.Literal allMediaCount; | |
/// <summary> | |
/// unreferencedMediaCount control. | |
/// </summary> | |
/// <remarks> | |
/// Auto-generated field. | |
/// To modify move field declaration from designer file to code-behind file. | |
/// </remarks> | |
protected global::System.Web.UI.WebControls.Literal unreferencedMediaCount; | |
/// <summary> | |
/// savereset control. | |
/// </summary> | |
/// <remarks> | |
/// Auto-generated field. | |
/// To modify move field declaration from designer file to code-behind file. | |
/// </remarks> | |
protected global::System.Web.UI.WebControls.Button savereset; | |
/// <summary> | |
/// save control. | |
/// </summary> | |
/// <remarks> | |
/// Auto-generated field. | |
/// To modify move field declaration from designer file to code-behind file. | |
/// </remarks> | |
protected global::System.Web.UI.WebControls.Button save; | |
/// <summary> | |
/// rptUnreferencedMedia control. | |
/// </summary> | |
/// <remarks> | |
/// Auto-generated field. | |
/// To modify move field declaration from designer file to code-behind file. | |
/// </remarks> | |
protected global::System.Web.UI.WebControls.Repeater rptUnreferencedMedia; | |
/// <summary> | |
/// Literal4 control. | |
/// </summary> | |
/// <remarks> | |
/// Auto-generated field. | |
/// To modify move field declaration from designer file to code-behind file. | |
/// </remarks> | |
protected global::System.Web.UI.WebControls.Literal Literal4; | |
} | |
} |
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
<%@ Page Language="c#" CodeBehind="Cleaner.aspx.cs" AutoEventWireup="False" Inherits="LatestEpi.modules.CleanAssetsPlugin.Cleaner" Title="" %> | |
<%@ Import Namespace="EPiServer.Web.Mvc.Html" %> | |
<asp:content contentplaceholderid="MainRegion" runat="server"> | |
<asp:HiddenField runat="server" id="SelectedMedia" ClientIDMode="static" /> | |
<script type="text/javascript"> | |
function SetSelected(getAll) { | |
var checkboxValues = []; | |
if (getAll) { | |
$('input[type="checkbox"]').map(function () { | |
checkboxValues.push($(this).val()); | |
}); | |
} | |
else | |
{ | |
$('input:checked').map(function () { | |
checkboxValues.push($(this).val()); | |
}); | |
} | |
$('#SelectedMedia').val(checkboxValues.join(',')); | |
}; | |
</script> | |
<div class="epi-formArea epi-paddingHorizontal"> | |
<fieldset> | |
<legend>Summary</legend> | |
<dl> | |
<dt>All media:</dt><dd><asp:Literal runat="server" id="allMediaCount" /></dd> | |
<dt>Unreferenced media</dt><dd><asp:Literal runat="server" id="unreferencedMediaCount" /></dd> | |
</dl> | |
</fieldset> | |
<div class="epi-buttonContainer"> | |
<span class="epi-cmsButton"> | |
<asp:Button OnClientClick="SetSelected(1)" ClientIDMode="Static" Text="Delete all" CssClass="epi-cmsButton-text epi-cmsButton-tools epi-cmsButton-Save" id="savereset" onmouseover="EPi.ToolButton.MouseDownHandler(this)" onmouseout="EPi.ToolButton.ResetMouseDownHandler(this)" runat="server" /> | |
</span> | |
<span class="epi-cmsButton"> | |
<asp:Button OnClientClick="SetSelected()" Text="Delete selected" CssClass="epi-cmsButton-text epi-cmsButton-tools epi-cmsButton-Save" id="save" onmouseover="EPi.ToolButton.MouseDownHandler(this)" onmouseout="EPi.ToolButton.ResetMouseDownHandler(this)" runat="server" /> | |
</span> | |
</div> | |
<div class="epi-buttonContainer"></div> | |
<fieldset> | |
<legend>Unreferenced media</legend> | |
<dl> | |
<dd> | |
<asp:Repeater runat="server" id="rptUnreferencedMedia"> | |
<ItemTemplate> | |
<div class="epi-size25"> | |
<input type="checkbox" value="<%#MD.ContentLink %>" /> <label><a href="<%#Url.ContentUrl(MD.ContentLink) %>" target="_blank"><%# BreadCrumb(MD)%></a></label> | |
</div> | |
</ItemTemplate> | |
</asp:Repeater> | |
<asp:Literal runat="server" id="Literal4" /> | |
</dd> | |
</dl> | |
</fieldset> | |
</div> | |
</asp:content> | |
using System; | |
using System.Collections.Generic; | |
using System.Web.Security; | |
using System.Web.UI.WebControls; | |
using EPiServer.Personalization; | |
using EPiServer.PlugIn; | |
using EPiServer.Security; | |
using EPiServer.Util.PlugIns; | |
using System.Web.UI; | |
using EPiServer.Shell.WebForms; | |
using EPiServer; | |
using EPiServer.Core; | |
using EPiServer.ServiceLocation; | |
using System.Linq; | |
using System.Web.Mvc; | |
namespace LatestEpi.modules.CleanAssetsPlugin | |
{ | |
[GuiPlugIn(DisplayName = "Assets cleaner", Description = "", Area = PlugInArea.AdminMenu, Url = "~/modules/CleanAssetsPlugin/Cleaner.aspx")] | |
public partial class Cleaner : WebFormsBase | |
{ | |
private IContentRepository contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>(); | |
protected UrlHelper Url = ServiceLocator.Current.GetInstance<UrlHelper>(); | |
protected MediaData MD { get { return Page.GetDataItem() as MediaData; } } | |
protected override void OnPreInit(EventArgs e) | |
{ | |
base.OnPreInit(e); | |
MasterPageFile = UriSupport.ResolveUrlFromUIBySettings("MasterPages/EPiServerUI.master"); | |
SystemMessageContainer.Heading = "Assets cleaner"; | |
SystemMessageContainer.Description = "A plugin that simply removes all or selected unreferenced media in the assets folder."; | |
} | |
protected override void OnLoad(EventArgs e) | |
{ | |
if (IsPostBack) | |
{ | |
var mediaContentLinks = SelectedMedia.Value.Split(",".ToCharArray()); | |
foreach(var mediaLink in mediaContentLinks) | |
{ | |
contentRepository.Delete(ContentReference.Parse(mediaLink), false, AccessLevel.NoAccess); | |
} | |
} | |
List<MediaData> unReferencedList = new List<MediaData>(); | |
var allDescendantsInAssetsFolder = contentRepository.GetDescendents(ContentReference.GlobalBlockFolder).Select(contentRepository.Get<IContent>).ToList(); | |
var allMediaInAssetsFolder = allDescendantsInAssetsFolder.OfType<MediaData>().ToList(); | |
foreach (var media in allMediaInAssetsFolder) | |
{ | |
var refs = contentRepository.GetReferencesToContent(media.ContentLink, false); | |
if (refs.Count() == 0) | |
{ | |
unReferencedList.Add(media); | |
} | |
} | |
allMediaCount.Text = allMediaInAssetsFolder.Count().ToString(); | |
unreferencedMediaCount.Text = unReferencedList.Count().ToString(); | |
rptUnreferencedMedia.DataSource = unReferencedList; | |
rptUnreferencedMedia.DataBind(); | |
} | |
protected string BreadCrumb(IContent content) | |
{ | |
var ancestor = contentRepository.GetAncestors(content.ContentLink).ToList(); | |
string path = ""; | |
for (int x = ancestor.Count() - 1; x >= 0; x--) | |
{ | |
path += "\\" + ancestor[x].Name; | |
} | |
return (path + "\\" + content.Name).Trim("\\".ToCharArray()); | |
} | |
protected string UiUrl() | |
{ | |
return EPiServer.Configuration.Settings.Instance.UIUrl.ToString().Trim('~'); | |
} | |
} | |
} | |
//------------------------------------------------------------------------------ | |
// <auto-generated> | |
// This code was generated by a tool. | |
// | |
// Changes to this file may cause incorrect behavior and will be lost if | |
// the code is regenerated. | |
// </auto-generated> | |
//------------------------------------------------------------------------------ | |
namespace LatestEpi.modules.CleanAssetsPlugin { | |
public partial class Cleaner { | |
/// <summary> | |
/// SelectedMedia control. | |
/// </summary> | |
/// <remarks> | |
/// Auto-generated field. | |
/// To modify move field declaration from designer file to code-behind file. | |
/// </remarks> | |
protected global::System.Web.UI.WebControls.HiddenField SelectedMedia; | |
/// <summary> | |
/// allMediaCount control. | |
/// </summary> | |
/// <remarks> | |
/// Auto-generated field. | |
/// To modify move field declaration from designer file to code-behind file. | |
/// </remarks> | |
protected global::System.Web.UI.WebControls.Literal allMediaCount; | |
/// <summary> | |
/// unreferencedMediaCount control. | |
/// </summary> | |
/// <remarks> | |
/// Auto-generated field. | |
/// To modify move field declaration from designer file to code-behind file. | |
/// </remarks> | |
protected global::System.Web.UI.WebControls.Literal unreferencedMediaCount; | |
/// <summary> | |
/// savereset control. | |
/// </summary> | |
/// <remarks> | |
/// Auto-generated field. | |
/// To modify move field declaration from designer file to code-behind file. | |
/// </remarks> | |
protected global::System.Web.UI.WebControls.Button savereset; | |
/// <summary> | |
/// save control. | |
/// </summary> | |
/// <remarks> | |
/// Auto-generated field. | |
/// To modify move field declaration from designer file to code-behind file. | |
/// </remarks> | |
protected global::System.Web.UI.WebControls.Button save; | |
/// <summary> | |
/// rptUnreferencedMedia control. | |
/// </summary> | |
/// <remarks> | |
/// Auto-generated field. | |
/// To modify move field declaration from designer file to code-behind file. | |
/// </remarks> | |
protected global::System.Web.UI.WebControls.Repeater rptUnreferencedMedia; | |
/// <summary> | |
/// Literal4 control. | |
/// </summary> | |
/// <remarks> | |
/// Auto-generated field. | |
/// To modify move field declaration from designer file to code-behind file. | |
/// </remarks> | |
protected global::System.Web.UI.WebControls.Literal Literal4; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment