Copied to clipboard

Flag this post as spam?

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


  • Evan Jardine 108 posts 168 karma points
    May 19, 2010 @ 03:22
    Evan Jardine
    0

    Ampersand (&) in media filename

    Hi

    I'm having a problem with a number of users uploading pdf files into the media library with & in the filename then creating a link from the richtext editor that of course doesn't work.

    Apart from educating them... would it be better to check with a Regex on the Media type or deal with it by using events?

    Cheers

    Evan

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    May 19, 2010 @ 07:36
    Dirk De Grave
    4

    Yup, I'd use events to handle this and use store what characters should be replaced by what other character (similar to what is now being used for constructing urls)

     

    Cheers,

    /Dirk

  • Evan Jardine 108 posts 168 karma points
    May 19, 2010 @ 07:43
    Evan Jardine
    1

    Thanks Dirk

    I have used a Regex validation on the 'File' Media Type as a quick solution.

    ^[0-9a-zA-Z-_]+.(pdf|PDF)$

    To only allow alphanumeric PDF files with - or _ in the name.

    I think I'll wait until tomorrow to look at the Events option now.

    Cheers

    Evan

  • Evan Jardine 108 posts 168 karma points
    May 20, 2010 @ 01:32
    Evan Jardine
    0

    Here is a slightly cut down version of the event code I ended up using to catch this problem and rename the file and notify the user.

    using System;
    using System.Web;
    using System.IO;

    using umbraco.BusinessLogic;
    using umbraco.cms.businesslogic;
    using umbraco.cms.businesslogic.web;
    using umbraco.cms.businesslogic.media;
    using umbraco.cms.businesslogic.property;
    using umbraco.BasePages;

    namespace ESCUmbracoEvents.media
    {
        public class filenames : ApplicationBase
        {
            public filenames()
            {
                Media.AfterSave += new Media.SaveEventHandler(Media_AfterSave);
            }

            void Media_AfterSave(Media sender, SaveEventArgs e)
            {
                if (sender.ContentType.Alias == "File")
                {
                    Property fileprop = sender.getProperty("umbracoFile");
                    Property extensionprop = sender.getProperty("umbracoExtension");
                   
                    //check to see if there is a file
                    if (!string.IsNullOrEmpty(fileprop.Value.ToString()))
                    {
                        string path = fileprop.Value.ToString();

                        if(path.Contains("&"))
                        {
                        
                            string safePath = path.Replace("&", "");

                            //copy the old file and rename
                            System.IO.File.Copy(HttpContext.Current.Server.MapPath(fileprop.Value.ToString()), HttpContext.Current.Server.MapPath(safePath), true);

                            //delete the old file
                            System.IO.File.Delete(HttpContext.Current.Server.MapPath(fileprop.Value.ToString()));

                            //set the file property to the new path
                            fileprop.Value = safePath;

                            ((BasePage)HttpContext.Current.Handler).speechBubble(BasePage.speechBubbleIcon.error, "Illegal Characters", "Illegal characters have been removed from the filename. Please reload this node to see the changes.");                          
                        }
                    }
                }
            }
        }

    Pretty simple and I hope it helps someone else out.

    Cheers

    Evan

     

Please Sign in or register to post replies

Write your reply to:

Draft