Copied to clipboard

Flag this post as spam?

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


  • Stewart Ellis 25 posts 64 karma points
    Mar 05, 2015 @ 19:24
    Stewart Ellis
    1

    Always overwrite media

    Now that V7 has a nice drag and drop UI for uploading media, life is better and I was hoping to leave behind the Desktop Media Uploader once and for all...  however I don't think I can.

    The scenario:

    • A folder in the media section contains a large amount of PDFs
    • The PDFs are updated every month and a strict naming convention is maintained
    • There are links to the various PDFs scattered throughout the site
    • Links to the PDFs are progarmatically created and also created in the RTE

    The issue:

    Historyically (V4.xx) the task of updating the PDFs was a monthly drag and drop of all the files into the Desktop Media Uploader - ***with the: "Overwrite existing files with the same filename?" checkbox checked. This works fine - assuming you check the box. Otherwise you have to log into the media section and torch all the duplicate files and do it all over again.

    In V7

    When you click on a folder in the media section, the UI allows you to drag and drop files for uploading - which is great for new files. The problem is that it automatically makes a choice to not overwrite existing files and instead it dynamically appends a number to the end of the file name to make it unique and issues a new media node Id. It doesn't ever ask if that's what I wanted to do. 

    Why is this the default behaviour?
    Why is this the only behaviour?

    I question the assumption that this is the correct approach. It certainly seems counter-intuitive as I believe overwriting files is a standard thing everybody does - if you upload a file wth the same file name - what do you expect?

    Can this behaviour be changed?

    Is there (or can there be) a configuration toggle that says ALWAYS OVERWRITE MEDIA: TRUE/FALSE?

    Is there some other solution to this issue?

    Everytime I launch the Desktop Media Uploader I'm prompted by Adobe to download some patch or something. It's time to put that tired soldier to bed and keep everything in the browser.

    What says you HQ?

    thanks in advance...

  • rob 75 posts 170 karma points
    Sep 08, 2015 @ 13:14
    rob
    0

    Just been bitten by this.

    Client has a high ranking PDF in Google search results as a media URL.

    Tried to update it only to find the link in the results goes to the old document URL which is no longer valid.

    It was totally non-obvious to them that choosing a new file after selecting the old media file media and uploading/saving would not overwrite the existing PDF. And that it would then delete the old one when creating the new media ID.

    Surely if uploading a new file over an existing one Umbraco should either:

    Overwrite the old one updating the size/thumbnail but not the name/ID (my preferred option) or It should create a new media item with all the new details and change all the media item links to point to the new id. Preserving the old one. Which is almost what happens currently.

    In any case the user should be warned.

  • Mark Drake 134 posts 458 karma points c-trib
    Mar 30, 2016 @ 14:46
    Mark Drake
    0

    This definitely needs to be updated. Perform the same operation that would occur on any OS -> prompt for confirmation to replace or keep.

  • Stewart Ellis 25 posts 64 karma points
    Jun 16, 2016 @ 16:20
    Stewart Ellis
    0

    Bumpity bump bump with extra bump on top

    CodeGarden 2016 is on as I write this. Will there be a grand reveal of a rethink of media in umbraco that will fix this issue. One can only hope.

    Please give this topic some consideration HQ.

    Many thanks

  • Jamie Attwood 208 posts 504 karma points c-trib
    Jun 16, 2016 @ 17:55
    Jamie Attwood
    1

    This is a source of many calls and confusion. The media section, while so much better in 7+ still needs some some attention to detail IMO. I would love to see:

    • Overwrite promps - if you are inside a media item to begin with, why would you want anything else than overwrite existing? If you wanted to delete the item, then delete the media node...

    • Get rid of "Remove files." Totally confusing. In my mind, it should be: send a media node to the recyle bin to pull it out of circulation, then clear the recycle bin and PERMANENTLY delete the node and delete the media folder off of the filesystem - no orphans! I have a client with 8 gigs of orphaned media and we are all too scared to delete them via FALM.

      • inside media item - choose files should upload media into that media id regardless of whether or not it has the same file name, if we wanted to upload media with a different id, it follows that you would just create a new media item through the regular channels.

      • Use an id to reference media instead of a file path within the RTE in the same way that you do with files to prevent the possibility of orphaning...

    At the very least some better text/instructions within.

    Thank-you Umbraco team and keep up the amazing work.

    Jamie

  • Morgan 1 post 71 karma points
    Jul 19, 2018 @ 15:50
    Morgan
    0

    It is now 2018 and I still think these issues remain.

    :(

    Does anyone have any solution to bulk updating of files?

  • Thomas Beckert 193 posts 469 karma points
    Oct 21, 2018 @ 14:22
    Thomas Beckert
    0

    What about this solution? I just hooked into the saving-event of a media file, checking if the file in this folder already exists and if so, just replace it instead of counting it up. (with this file, you always overwrite an existing file with a new one - there will be no choice!)

    Just copy this code into a file e.g. overwriteMediaFiles.cs and place it in your App_Code Folder or include it in your own binary-file of you web-project:

    using MySql.Data.MySqlClient;
    using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Web;
    using Umbraco.Core;
    using Umbraco.Core.Events;
    using Umbraco.Core.Models;
    using Umbraco.Core.Models.EntityBase;
    using Umbraco.Core.Services;
    
    namespace umbHbiG.Klassen
    {
        public class OverrideExistingMediaFile : IApplicationEventHandler
        {
            public void OnApplicationInitialized(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                MediaService.Saving += MediaService_Saving;
            }
    
            public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
    
            }
    
            public void OnApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
    
            }
    
            private void MediaService_Saving(IMediaService sender, Umbraco.Core.Events.SaveEventArgs<Umbraco.Core.Models.IMedia> e)
            {
                foreach (IMedia node in e.SavedEntities)
                {
                    if (node.ContentType.Alias != "Folder") // IF FILE OR IMAGE
                    {
                        if (node.Id == 0) // IF NEW
                        {
                            IMediaService ms = ApplicationContext.Current.Services.MediaService;
                            List<IMedia> media = getMediaByFileName(node.Name); // GET ALLE MEDIA FILES, INCLUDING THE FILENAME UPLOADED
    
                            if (media.Count > 0)
                            {
                                bool overwriteFound = false;
                                foreach (IMedia m in media)
                                {
                                    // OVERWRITE FILE IF PARANT ID IS THE SAME
                                    if (m.ParentId == node.ParentId)
                                    {
                                        m.SetValue(Constants.Conventions.Media.File, node.GetValue(Constants.Conventions.Media.File));
                                        ms.Save(m, 0, false);
                                        overwriteFound = true;
                                    }
                                }
    
                                if (overwriteFound)
                                {
                                    e.Cancel = true; // CANCEL THE COMMON UPLOAD EVENT
                                    EventMessage cancelMessage = new EventMessage("Media Upload", "File has been overridden successfully", EventMessageType.Success);
                                    e.CancelOperation(cancelMessage);
                                }
                            }
                        }
                    }
                }
            }
    
            // SELECT MEDIA ENTRIES THAT ENDS WITH THE GIVEN FILENAME
            private List<IMedia> getMediaByFileName(string filename)
            {
                List<IMedia> media = new List<IMedia>();
    
                // CAN BE MADE SAVER AGAINST SQL INJECTIONS
                string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["umbracoDbDSN"].ConnectionString;
                string sqlSelect = "SELECT nodeId FROM cmsMedia WHERE mediaPath LIKE \'%" + filename + "\'";
    
                using (SqlConnection connection = new SqlConnection(connectionString))
                {
                    connection.Open();
                    //
                    using (SqlCommand command = new SqlCommand(
                        sqlSelect,
                        connection))
                    {
    
                        using (SqlDataReader reader = command.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                string strNodeId = reader.GetValue(0).ToString();
                                int nodeId = Convert.ToInt32(strNodeId);
                                IMediaService ms = ApplicationContext.Current.Services.MediaService;
                                IMedia m = ms.GetById(nodeId);
                                media.Add(m);
                            }
                        }
                    }
                }
    
                return media;
            }
        }
    }
    
  • Jamie Attwood 208 posts 504 karma points c-trib
    Oct 22, 2018 @ 13:34
    Jamie Attwood
    0

    Thomas - thanks for this great bit of code. I will test implement and let you know how it works out in my scenario. Thanks for the lead into hooking into the events. I might write up some delete file behavior for when a media item is removed from the recycle bin. I Just had another client last week using 7.6.4 confused as hell about how to physically remove a file of the filesystem after they thought they were permanently removing media items. I have yet to test if these functionality now exists in later versions.

    Cheers!

    Jamie

  • Thomas Beckert 193 posts 469 karma points
    Oct 22, 2018 @ 13:46
    Thomas Beckert
    0

    Hi, Jamie,

    you are welcome. Hope it helps with your scenario.

    I just had a delete-issue with media files today. We removed an image, also deleted the physical media-path. BUT: be aware of the imageProcessor-Cache.

    So, we had the case, that the image cannot be called with the normal URL:

    e.g. "/media/12345/picname.jpg".

    BUT still be reachable, if you use it with an imageprocessor-Querystring:

    "/media/12345/picname.jpg?autorotate=true".

    This is a serious problem, cause we had an copy-right issue and was forced by a laywer to remove the image. We removed it in the believe, it is really removed, even checked the url (but without thinking of the image processor). And this get us in kind of trouble, as you can imagine.

    So if you want to delete an image, be aware of the image-processor caching. :)

Please Sign in or register to post replies

Write your reply to:

Draft