Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • dominik 711 posts 733 karma points
    Dec 03, 2012 @ 13:41
    dominik
    0

    Delete unused media items

    Hello,

    As we are currently creating our new website there are about 1400 folders inside the media folder.

    Is there any possibility to delete all of the not used folders and items? 

    Otherwise the file system will get bigger and bigger.

    Thanks

  • Fernando Camillo 41 posts 82 karma points
    Jun 18, 2013 @ 19:59
    Fernando Camillo
    0

    Hello.

    You probably can delete files with are not registered in Media section.

    I've made a check script in a Visual Studio project for Umbraco 4.5. First I listed all files within media folder. After, I checked all registered media and marked the files as "used" or "not used" (it means "registered"). Here goes the code. It only lists unregistered files and empty directories, no removal is made. You must log as admin in the backend, and access your page. I hope it helps. Feel free to change.

    References:

    • umbraco.dll
    • businesslogic.dll
    • cms.dll

    ASPX page (section):

            <asp:Panel ID="panCheck" runat="server" Visible="false">
    <p>
    <asp:Button ID="btnDoCheck" runat="server" Text="Do check"
    onclick="btnDoCheck_Click" />
    </p>
    <div>
    <p>Files not in media (unregistered files):</p>
    <asp:GridView ID="gvFiles" runat="server"></asp:GridView>
    </div>
    <div>
    <p>Empty directories:</p>
    <asp:GridView ID="gvDirectories" runat="server"></asp:GridView>
    </div>
    </asp:Panel>
    <asp:Panel ID="panDenied" runat="server" Visible="false">
    <p>You must be logged as admin to use this function.</p>
    </asp:Panel>

     

    Code behind:

    /* Page to check for unused files (unregistered) in Media section of Umbraco 4.5
    * Developed by Fernando Camillo ([email protected]) (I'm not a C# master... sorry!)
    * Please test it before use! And feel free to change it. */

    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;

    using umbraco.BusinessLogic;
    using umbraco.cms.businesslogic.media;
    using System.IO;

    namespace CheckForUnusedMedia
    {
    public partial class CheckForUnusedMedia : System.Web.UI.Page
    {
    //Deny use of this tool if not admin
    protected void Page_Load(object sender, EventArgs e)
    {
    if (!(Page.IsPostBack))
    {
    if (isUserLoggedAndAdmin())
    {
    panCheck.Visible = true;
    panDenied.Visible = false;
    }
    else
    {
    panCheck.Visible = false;
    panDenied.Visible = true;
    }
    }
    }


    //Do check button
    protected void btnDoCheck_Click(object sender, EventArgs e)
    {
    // Part 1 - Unused files in media
    DataTable dtFiles = new DataTable("files");
    dtFiles.Columns.Add(new DataColumn("file", typeof(System.String)));
    dtFiles.Columns.Add(new DataColumn("exists_in_media", typeof(System.String)));
    dtFiles.Columns.Add(new DataColumn("media_id", typeof(System.Int32)));
    dtFiles.Columns.Add(new DataColumn("file_size", typeof(System.Int64)));

    loadFilesFromADirectory(dtFiles, Server.MapPath(@"/media/"));
    loadFilesFromUmbracoMedia(dtFiles, null);

    //filter, order and show
    DataView dvUnusedFiles = new DataView(dtFiles,
    @"exists_in_media = 'NO'",
    "file ASC",
    DataViewRowState.CurrentRows);
    gvFiles.DataSource = dvUnusedFiles;
    gvFiles.DataBind();

    // ===================

    // Part 2 - Empty directories
    DataTable dtEmptyDirectories = new DataTable("directories");
    dtEmptyDirectories.Columns.Add(new DataColumn("directory", typeof(System.String)));

    loadEmptyDirectories(dtEmptyDirectories, Server.MapPath(@"/media/"));

    //filter, order and show
    DataView dvEmptyDir = new DataView(dtEmptyDirectories,
    string.Empty,
    "directory ASC",
    DataViewRowState.CurrentRows);
    gvDirectories.DataSource = dvEmptyDir;
    gvDirectories.DataBind();
    }


    //Fills the datatable with files in the directory and its subdirectories
    private void loadFilesFromADirectory(DataTable dt, string path)
    {
    DirectoryInfo myDir = new DirectoryInfo(path);
    FileInfo[] fileInfos = myDir.GetFiles();
    DataRow dr;

    foreach (FileInfo fi in fileInfos)
    {
    dr = dt.NewRow();
    dr["file"] = fi.FullName.ToLower().Trim(); // uses lowercase for easy compare
    dr["exists_in_media"] = "NO";
    dr["media_id"] = -1;
    dr["file_size"] = fi.Length;
    dt.Rows.Add(dr);
    }

    DirectoryInfo[] dirInfos = myDir.GetDirectories();
    foreach (DirectoryInfo di in dirInfos)
    {
    loadFilesFromADirectory(dt, di.FullName);
    }
    }


    //Fills the datatable with files in Umbraco Media
    private void loadFilesFromUmbracoMedia(DataTable dt, umbraco.cms.businesslogic.media.Media mediaNode)
    {
    string fileName;

    if (mediaNode == null)
    {
    foreach (umbraco.cms.businesslogic.media.Media m in umbraco.cms.businesslogic.media.Media.GetRootMedias())
    {
    try
    {
    fileName = m.getProperty("umbracoFile").Value.ToString().Trim();
    if (fileName != string.Empty)
    {
    fileName = Server.MapPath(fileName).ToLower().Trim(); // uses lowercase for easy compare
    markThatFileExistsInUmbracoMedia(dt, fileName, m.Id);
    }
    }
    catch (Exception)
    {
    //do nothing
    }
    loadFilesFromUmbracoMedia(dt, m);
    }
    }
    else
    {
    foreach (umbraco.cms.businesslogic.media.Media m in mediaNode.Children)
    {
    try
    {
    fileName = m.getProperty("umbracoFile").Value.ToString().Trim();
    if (fileName != string.Empty)
    {
    fileName = Server.MapPath(fileName).ToLower().Trim(); // uses lowercase for easy compare
    markThatFileExistsInUmbracoMedia(dt, fileName, m.Id);
    }
    }
    catch (Exception)
    {
    //do nothing
    }
    loadFilesFromUmbracoMedia(dt, m);
    }
    }
    }


    //Mark that a file exists in Media section of Umbraco
    private void markThatFileExistsInUmbracoMedia(DataTable dt, string fileName, int mediaId)
    {
    foreach (DataRow dr in dt.Rows)
    {
    if (Convert.ToString(dr["file"]) == fileName)
    {
    dr["exists_in_media"] = "YES";
    dr["media_id"] = mediaId;
    }
    }

    //check also for the thumbnail version
    foreach (DataRow dr in dt.Rows)
    {
    if (Convert.ToString(dr["file"]) == fileName.Replace(".", "_thumb."))
    {
    dr["exists_in_media"] = "YES";
    dr["media_id"] = mediaId;
    return;
    }
    }
    }


    //Fills the datatable with empty directories
    private void loadEmptyDirectories(DataTable dt, string path)
    {
    DirectoryInfo myDir = new DirectoryInfo(path);
    DataRow dr;

    FileInfo[] fileInfos = myDir.GetFiles();
    DirectoryInfo[] dirInfos = myDir.GetDirectories();

    if ((fileInfos.Count() == 0) && (dirInfos.Count() == 0))
    {
    dr = dt.NewRow();
    dr["directory"] = myDir.FullName.ToLower().Trim();
    dt.Rows.Add(dr);
    }
    else
    {
    if (dirInfos.Count() > 0)
    {
    foreach (DirectoryInfo di in dirInfos)
    {
    loadEmptyDirectories(dt, di.FullName);
    }
    }
    }
    }


    //Check if a user is logged in Umbraco backend, and if its an administrator
    private bool isUserLoggedAndAdmin()
    {
    umbraco.BusinessLogic.User user;
    try
    {
    user = umbraco.helper.GetCurrentUmbracoUser();
    if (user == null)
    {
    return false;
    }
    else
    {
    if (user.UserType.Alias.ToLower() == "admin")
    {
    return true;
    }
    else
    {
    return false;
    }
    }
    }
    catch (Exception)
    {
    return false;
    }
    }

    }
    }
  • ismini 14 posts 135 karma points
    Sep 01, 2017 @ 11:13
    ismini
    0

    Can you tell me how can I put this in my Umbraco ( i use the latest version 7.6.5 ) ?

    I've made a plugin for Umbraco but i used angularJS, json and some HTML. If I understand correct your ASPX is as my HTML and your C# is as my AngularJS.

    My plugin I putted it in Plugin folder and i found it as DataType. I can't understand how can I put it in my Umbraco your code.

    Thanks in advance.

  • Andrew Asche 1 post 21 karma points
    Jul 17, 2015 @ 01:45
    Andrew Asche
    0

    can you give some help to put this in my umbraco, I'm new to umbraco.

Please Sign in or register to post replies

Write your reply to:

Draft