Copied to clipboard

Flag this post as spam?

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


  • Joshua Walsh 30 posts 151 karma points
    Aug 13, 2015 @ 05:41
    Joshua Walsh
    0

    Delete Media item without deleting associated files

    Hi,

    I've got an issue where when I delete a media item it removes all Upload properties associated with it. The reason this is an issue for me is that I'm duplicating media items with the following code:

    var newMedia = new Media(newName, newParent, original.ContentType);
    
    foreach(var property in original.Properties)
    {
        newMedia.SetValue(property.Alias, property.Value);
    }
    

    This means that if I delete any duplicate or duplicated media item it will remove Upload files for all the clones, creating lots of broken links. My current strategy is to never delete this clones, instead putting them into an Archive folder. This isn't really ideal though, as they will slowly build up.

    I believe the deletion is caused by DeleteAssociatedMediaFiles() in src/umbraco.cms/businesslogic/Content.cs, but I can't find where this function is called. I'm wondering if it's possible to delete a media file programmatically without this function being called. I'd rather avoid touching the database.

    Thanks,

    Josh

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Aug 13, 2015 @ 06:49
    Dave Woestenborghs
    1

    Why don't you clone the file as well when cloning the media item.

    I actually don't see the need for having multiple media items pointing to the same file. Can you please try to explain why you are doing this ?

    Maybe we can come up with a better solution.

    Dave

  • Joshua Walsh 30 posts 151 karma points
    Aug 13, 2015 @ 07:03
    Joshua Walsh
    0

    I am making quite a few clones, so the disk space usage from that would be noticeable. Additionally the cloning operation is already quite slow (it's recursive and can clone up to ~60 media items at a time) and so making it duplicate files would make the operation run for a fairly long time.

  • Joshua Walsh 30 posts 151 karma points
    Aug 14, 2015 @ 02:30
    Joshua Walsh
    0

    How would I go about cloning the file? I have another situation where I would like to clone Upload files but I'm not sure how I can achieve that.

  • Dave de Moel 122 posts 574 karma points c-trib
    Aug 13, 2015 @ 09:28
    Dave de Moel
    1

    But why do you need to clone at all? Can you eplain that? If we know why you need to clone, we might be able to come up with a (better) alternative, as in normal circumstances cloning is unnecesairy.

  • Joshua Walsh 30 posts 151 karma points
    Aug 14, 2015 @ 02:29
    Joshua Walsh
    100

    The cloning thing is pretty hideous. Basically we have repeating data that we need to attach to content nodes.

    We store this data by creating a container media item with children for each "row". So, for example:

    Container 1:
        Row 1
        Row 2
        Row 3
        Row 4
    Container 2:
        Row 1
        Row 2
    

    Then on the page we use a Media Picker to allow users to choose a container.

    We do this in the media section so that the containers and rows don't get assigned URLs. This is definitely not a good way of storing the data, but we're at the point that there's so much code dependant on this structure that we can't change it.

    The main practical issue with this approach is that media items have no differentiation between Save and Publish, and there is no version history kept for them. This was becoming a fairly major inconvenience, so I put together a pretty dirty hack. If you are easily disturbed you may wish to stop reading here.

    Every time a content node is published, the following happens:

    1. Any repeating sections attached to the previously published version are archived.

    2. Any repeating sections attached to the now-published version are duplicated.

    3. The duplicate sections are saved against the content node as a new draft.

    This way at any given time we have a Published and Draft container item, plus any number of archived ones.

    The duplication process (steps 2 and 3) can't be done in the Published event so they are instead done using a custom Property Editor which calls a WebApi method. The WebApi method checks if the repeating section attached to Draft has the same id as the one attached to Published, and if it is it duplicates a new one for draft. Step 2 takes quite a long time, and if the user is impatient they may set off the duplication more than once. So before we do Step 3 we check that the repeating section attached to Draft is STILL the same as the one attached to Published. If they no longer are, we don't actually want to do Step 3, so we're stuck with a useless media item.

    Currently I have to archive this item, but I'd like to be able to delete it permanently as it's useless.

    EDIT: I just had an idea, what if I empty all the property fields on the media item before I delete them? Then Umbraco wouldn't find the files and wouldn't delete them. I'm going to try it and see if it works.

    EDIT2: Yep, that worked.

Please Sign in or register to post replies

Write your reply to:

Draft