Copied to clipboard

Flag this post as spam?

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


  • Nirmit 24 posts 97 karma points
    Jan 19, 2015 @ 02:20
    Nirmit
    0

    Physical structure of media folder

    Hi,

    I am new to this forum. So apologize in advance if this is a wrong question for this forum.

    I am evaluating Umbraco 7.2 for my company's website. So far it looks good. however I stuck at media organization. I found that Umbraco is keeping the folder structure just in the database. so in "media" folder there are bunch of 'numbered' folders containing images/pdfs. This arrangement raises 2 concerns for me

    1. image urls are always like /media/1000/someimage.jpeg. which is not SEO/User friendly. I want it to be /media/mybrand1/subbrand1/category/subcategory/some_image.jpeg
    2. We have over 10K images (I know its a lot!). So having 10K folders just in "media" folder is not a good idea.

    Is there any way to store/organize images physically in a same way as they are being shown on backoffice media folder?

    Thanks,

    Nirmit

  • Richard Soeteman 4000 posts 12628 karma points MVP
    Jan 19, 2015 @ 06:13
    Richard Soeteman
    0

    Hi Nirmit,

    Good question. Out of the box it is not possible to change the url structure. You could use a custom file system provider to change how you want to store the files. You find one example here https://github.com/idseefeld/UmbracoAzureBlobStorage/ that stores files in Azure

    I am planning to implement an SEO friendly lookup in SEO Checker http://soetemansoftware.nl/seo-checker but that doesn't help you with the 10K folders in media.

    Best,

    Richard

  • Nirmit 24 posts 97 karma points
    Jan 21, 2015 @ 02:02
    Nirmit
    102

    Hi Richard, Thanks for your reply. After doing more research, I found some help on google (I cant track back from which site I got this solution.. extremely sorry for that). I modified their code a bit and managed to change the directory structure within media folder. My update code looks like (I copied most of the part from internet):

    protected override void ApplicationStarted(
        UmbracoApplicationBase umbracoApplication,
        ApplicationContext applicationContext)
        {
            //Create a custom route
            RouteTable.Routes.MapRoute(
                "LOS",
                "LOS/{action}/{id}",
                new
                {
                    controller = "LOS",
                    action = "Index",
                    id = UrlParameter.Optional
                });
            MediaService.Saving += MediaService_Saving;
        }
    
        void MediaService_Saving(IMediaService sender, SaveEventArgs<Umbraco.Core.Models.IMedia> e)
        {
            var fs = FileSystemProviderManager.Current.GetFileSystemProvider<MediaFileSystem>();
            foreach (var media in e.SavedEntities)
            {
                if ("Image".Equals(media.ContentType.Alias))
                {
                    //Assuming Media item only has one umbracoFile property to handle
                    var umbracoFileProperty = media.Properties["umbracoFile"];
                    //Path will have this format /media/1234/koala.jpg
    
                    string newPath = "";
                    //var tree = sender.GetAncestors(media);                
                    //foreach (var parent in tree)
                    //{
                    //    newPath += "\\" + parent.Name;
                    //}
                    IMedia _media = sender.GetParent(media);
                    while (_media != null)
                    {
                        newPath = _media.Name + "\\" + newPath;
                        _media = sender.GetParent(_media);
                    }
    
    
                    var imagePath = umbracoFileProperty.Value.ToString();
                    var fullPath = fs.GetFullPath(imagePath);
                    var relativePath = fs.GetRelativePath(fullPath);
    
                    //Update the path for each thumbnail
                    var thumbnails = fs.GetThumbnails(relativePath);
                    thumbnails.ForEach(thumbnail => SetNewImagePath(thumbnail, newPath, true, fs));
                    //Update the path for the 'main' image
                    var newImagePath = SetNewImagePath(relativePath, newPath, false, fs);
                    //Update property with new image path
                    umbracoFileProperty.Value = newImagePath;
    
                    //Remove old image and thumbnails
                    fs.DeleteFile(relativePath, true);
                }
            }
        }
    
        private string SetNewImagePath(string imagePath, string newImageLocation, bool isThumbnail, MediaFileSystem fs)
        {
            var file = fs.GetFileName(imagePath);
            //Extract name of file
            var fileNameNoExt = file.Substring(0, file.LastIndexOf('.'));
            if (isThumbnail)
                fileNameNoExt = fileNameNoExt.Replace("_thumb", "").Replace("_big-thumb", "");
            //Extract name of directory
            var folderName = imagePath.Substring(0, imagePath.IndexOf('\\'));
            //Assemble new path
            //var newImagePath = imagePath.Replace(fileNameNoExt, folderName);
            var newImagePath = newImageLocation+'\\'+imagePath;
            //Move file to new path, and override any existing image
            using (var fileStream = fs.OpenFile(imagePath))
            {
                fs.AddFile(newImagePath, fileStream, true);
            }
            //Return the full path as that is what we want to store
            return fs.GetUrl(newImagePath);
        }
    
  • jake williamson 203 posts 845 karma points
    Oct 07, 2017 @ 15:40
    jake williamson
    0

    hi there,

    i've inherited a project from another development team that uses this code.

    one big problem i've found with it is that if you resave the media, it will place it another level down...

    e.g. on the 1st save, in a folder called banners the file name looks something like this:

    /media/banners/12345/my-banner.jpg

    however, if you save the image again, it moves it to:

    /media/banners/banners/12345/my-banner.jpg

    also, if you check the 'remove file' checkbox umbraco throws an error with the message:

    The path is not of a legal form.

    i'm guessing you ended up with the same issue in your application and i was wondering if you found a way around it?

    cheers,

    jake

  • Richard Soeteman 4000 posts 12628 karma points MVP
    Jan 21, 2015 @ 07:23
    Richard Soeteman
    0

    Hi,

    No need to feel sorry :) Great find Don't know why you need a custom route for this but great that it works for you.

    Best,

    Richard

Please Sign in or register to post replies

Write your reply to:

Draft