Avoid Scandinavian Letters in File Names in Optimizely CMS

AI-generated illustration of a frustrated viking coder

Earlier this year, I wrote a blog post about allowing Scandinavian letters in page URLs. A while later, some strange behavior started to occur with certain images uploaded by editors in the CMS.

Some images were uploaded without issues and could be used normally when selected for image properties on pages. However, they worked only inside the CMS. On the public website, the images did not exist. When I looked closer, I noticed that the image file itself was missing — only the thumbnail and a reference to the original file remained.

It turned out the issue was related to that earlier mentioned blog post. I hadn’t considered the impact on file uploads when allowing Scandinavian characters in URLs. While filenames containing such characters are technically valid, they don’t work reliably in Optimizely CMS.

Solution

To fix this, I created an IInitializableModule that automatically renames media files on upload by replacing Scandinavian characters with their plain-English equivalents:

    [InitializableModule]
    [ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
    public class MediaFileNameSanitizer : IInitializableModule
    {
        public void Initialize(InitializationEngine context)
        {
            var contentEvents = context.Locate.ContentEvents();
            contentEvents.CreatingContent += OnCreatingContent;
        }

        private void OnCreatingContent(object sender, ContentEventArgs e)
        {
            if (e.Content is MediaData media and IContent {Name: not null} content)
            {
                var originalName = content.Name;
                var sanitized = SanitizeFileName(originalName);

                if (originalName != sanitized)
                {
                    content.Name = sanitized;
                }
            }
        }

        public void Uninitialize(InitializationEngine context)
        {
            var contentEvents = ServiceLocator.Current.GetInstance<IContentEvents>();
            contentEvents.CreatingContent -= OnCreatingContent;
        }

        private string SanitizeFileName(string fileName)
        {
            fileName = fileName.Replace("æ", "ae").Replace("ø", "o").Replace("å", "a");
            fileName = fileName.Replace("Æ", "Ae").Replace("Ø", "O").Replace("Å", "A");
            fileName = fileName.Replace("ä", "ae").Replace("ö", "o");
            fileName = fileName.Replace("Ä", "Ae").Replace("Ö", "Oe");
            
            //Replace anything that is not a valid letter, number, dot, underscore or hyphen
            return Regex.Replace(fileName, @"[^a-zA-Z0-9_\.-]", "-");
        }
    }

With this approach, pages can still have Scandinavian characters in their URLs, but uploaded media files will be sanitized automatically to avoid issues—ensuring they work reliably across both the CMS and the public site.

AI-generated illustration of a happy viking coder