Copied to clipboard

Flag this post as spam?

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


  • Chris Mahoney 235 posts 447 karma points
    May 03, 2018 @ 04:14
    Chris Mahoney
    0

    Block access to media in Recycle Bin

    We've had a longstanding issue with media in the Recycle Bin and I feel like I must be missing something.

    When a file is deleted from the Media section, it's moved into the Recycle Bin. However, if a customer browses directly to the file's URL then it still opens. How can I prevent this? We often "delete" old media files but it's easy for editors to forget to empty the Recycle Bin, and then customers end up looking at outdated information (usually when the media has been indexed directly by Google).

    I can't figure out how to block customer access to media files that are in the Recycle Bin. How can I do this?

    Thanks :)

  • Chris Mahoney 235 posts 447 karma points
    May 11, 2018 @ 04:31
    Chris Mahoney
    0

    In an attempt to fix this, I'm trying to hook an event up to the Trashed event that'll rename the file to something like "blah.jpg-deleted" so that it'll 404 if anyone accesses it with an old link.

    However, I can't figure out how to get the path to the file. MediaService.Trashed provides a MoveEventArgs<IMedia> called e and I've tried variations of:

    foreach (var item in e.MoveInfoCollection)
    {
      var path = Server.MapPath(item.Entity.Path);
    }
    

    However, this doesn't work as Path contains the Umbraco path (eg. -1,-21,1234) and not the actual file path. How do I get the file path?

  • Mike Delamater 5 posts 75 karma points
    Aug 21, 2019 @ 18:07
    Mike Delamater
    0

    Can we just get rid of the recycle bin? Or have a config option that lets us bypass it? I don't understand why it's there in the first place.

  • BG Techie 1 post 71 karma points
    Dec 10, 2019 @ 10:32
    BG Techie
    0

    Just write an event handler that extends MediaService.Trashed

    using System.Web;
    using Umbraco.Core;
    using Umbraco.Core.Events;
    using Umbraco.Core.Models;
    using Umbraco.Core.Services;
    
    namespace MyUmbraco.Site.EventHandlers
    {
        public class MediaEventHandler : ApplicationEventHandler
        {
            protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                MediaService.Trashed += MediaServiceTrashed;
            }
    
            private void MediaServiceTrashed(IMediaService sender, MoveEventArgs<IMedia> e)
            {
                foreach (var mediaItem in e.MoveInfoCollection)
                {
                    if (mediaItem.Entity != null && mediaItem.Entity.ContentType.Alias == "File")
                    {
                        if (mediaItem.Entity.Properties["umbracoFile"] != null)
                        {
                            var mediaItemPath = mediaItem.Entity.Properties["umbracoFile"].Value.ToString();
    
                            var filePath = HttpContext.Current.Server.MapPath(mediaItemPath);
                            if (System.IO.File.Exists(filePath))
                                System.IO.File.Delete(filePath);
    
                            sender.Delete(mediaItem.Entity);
                        }
                    }
                }
            }
    
        }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft