Copied to clipboard

Flag this post as spam?

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


  • Mads Sørensen 188 posts 433 karma points
    May 02, 2018 @ 08:50
    Mads Sørensen
    0

    Hi guys

    I'm trying to convert a string(URL) to a normal Umbraco Media item/ID.

    I have no idea if this the smartest way to do it but this is how far I'm right now:

    var imageUrl = "/media/1103/test-product.png";
    
            if (imageUrl.StartsWith("/media/"))
            {
    
                String imageUrlString = imageUrl;
    
                String[] words = imageUrlString.Split('/');
    
                var imageId = 0;
    
                foreach (string word in words.Skip(2).Take(1))
                {
                    imageId = Int32.Parse(word);
                }
    
                if(imageId > 0) 
                {
                    //Check ID
                    <h1>@imageId</h1>
    
                    // convert to Umbraco Media 
                    var image = Umbraco.Media(imageId);
    
                   <h1>Test: @image.Name</h1>
                }  
            }
        }
    

    So my problem is to convert it to an media it so I can call @image.GetCropUrl() @image.Name etc.

    The ImageURL is generated from the uCommerce API.

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    May 02, 2018 @ 09:06
    Jeavon Leopold
    102

    The folder name "1103" is not the media item id which is why it doesn't work.

    There is one method to do this on the MediaService but as with all services this shouldn't be utilised on the front end of the website as management services are not designed for this purpose and therefore not cached.

    However if you have no other option then it is done like this

    var imageUrl = "/media/1103/test-product.png";
    var ms = ApplicationContext.Services.MediaService;
    var mediaId = ms.GetMediaByPath(imageUrl).Id;
    

    You should implement some caching if you were to use this on the front end of the website.

    Jeavon

  • Mads Sørensen 188 posts 433 karma points
    May 02, 2018 @ 10:52
    Mads Sørensen
    0

    Hi Jeavon

    That's perfect. Well, and a bit stupid of me :D

    The imageURL is a media picker from uCommerce so i'm not woried about the caching. Or am I missing something? :)

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    May 02, 2018 @ 11:05
    Jeavon Leopold
    0

    The issue is that this

    var ms = ApplicationContext.Services.MediaService;
    var mediaId = ms.GetMediaByPath(imageUrl).Id;
    

    shouldn't be used to render a page. The services will make database queries, they are not designed to be used to render pages but for management operations in the back end of Umbraco.

    If you absolutely have to use them, then you should ensure that you are somehow caching the result so that the database queries aren't executing on every request. In it's simplest form you could cache the result in a cached partial view. Another approach might be to use Output caching such as MVCDonutCaching or to utilise RuntimeCache (you would need an event to clear this on media save also).

    Is there absolutely no way that uCommerce can give you a id/udi rather than only a url? I would recommend you explore this before committing to using the media service.

  • Mads Sørensen 188 posts 433 karma points
    May 02, 2018 @ 11:42
    Mads Sørensen
    0

    Ohh, that sucks :/

    Right now i can get the image like this:

    var image = Model.ProductViewModel.ThumbnailImageUrl;
    <img src="@image" />
    

    But that only returns the url.

    I've contacted the uCommerce Team.

    Is this also a direct call to the database?

    var currentPage = SiteContext.Current.CatalogContext.CurrentProduct;
    
  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    May 02, 2018 @ 13:56
    Jeavon Leopold
    0

    I don't know I'm afraid, that's a uCommerce service.

    It would be much better if you could have

    Model.ProductViewModel.ThumbnailImageId or Model.ProductViewModel.ThumbnailImage.Id

    Hopefully they can get you a id/udi

  • Mads Sørensen 188 posts 433 karma points
    May 02, 2018 @ 14:05
    Mads Sørensen
    0

    Agree :D

    Thanks Jeavon :D

Please Sign in or register to post replies

Write your reply to:

Draft