Copied to clipboard

Flag this post as spam?

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


  • Anthony Dang 1404 posts 2558 karma points MVP 3x c-trib
    Dec 09, 2010 @ 12:56
    Anthony Dang
    0

    Get media from path

    I've written a neat little method to get media based on it's path. However it is very very slow.

    I'm just wondering if anyone has a faster way of doing this? Without querying the db directly?

     

            public static Media GetMediaFromPath(string mediaPath, string alias)
            {
                // get the folder 
                int folder = int.Parse(mediaPath.Split("/".ToCharArray())[2]);
    
                var media = Media.GetMediaOfMediaType(MediaType.GetByAlias(alias).Id)
                                .Where(x => (int)x.getProperty("umbracoFile").Id == folder)
                                .FirstOrDefault();
                return media;
            }
    

     

     

     

     

  • RuneO 36 posts 116 karma points
    Dec 09, 2010 @ 13:54
    RuneO
    0

    Haven't tried it - but what about splitting the path and traversing the archive tree for the correct media item?

    It will not be as nice code as in your example but you can skip using GetMediaOfMediaType and therefore not fetching all media items of the desired type from the database.

    Maybe I'm wrong, I have never used GetMediaOfMediaType myself - but it seems unnecessary to fetch "a lot" to find only one item. E.g. if the media is of type "image" it may have to fetch hundreds or thousands of media items.

    /Rune

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Dec 09, 2010 @ 14:11
    Jeroen Breuer
    0

    I think the Media.GetMediaOfMediaType is a heavy method which will do a lot of database calls. Also if you do x.getProperty("umbracoFile") that is another call to the database and since it happens in a loop that's heavy! Look at the Umbraco source code if you're interested to see how it works exactly. The fastest way to do this is probably write a custom query which will return all the required data. You'll need a couple of inner joins, but it should be possible.

    Jeroen

  • Hendy Racher 863 posts 3849 karma points MVP 2x admin c-trib
    Dec 09, 2010 @ 14:30
    Hendy Racher
    3

    Hi,

    How about getting media via an XPath expression ? (uComponents v2 has a helper method for this), this queries the cmsContentXml table with 1 db hit, and returns xml with attributes for the url Name, full path etc..

    Hendy

  • RuneO 36 posts 116 karma points
    Dec 09, 2010 @ 14:51
    RuneO
    0

    Of course Hendy's UmbracoHelper class has a solution for it - borrowed a bit from this class once in a while myself (thanks to Hendy) :-)

    /Rune

  • Hendy Racher 863 posts 3849 karma points MVP 2x admin c-trib
    Dec 09, 2010 @ 15:30
    Hendy Racher
    0

    That's very kind of you to say so RuneO :)

    Sorry to hijack this thread, but thought I'd mention that the version in uComponents has been updated to take advantage .net 3.5 (since this is now a requirement for Umbraco) using extension methods on the Node, Media and Member objects (thanks Stefan), and also offers a variety of XPath axis type methods to traverse the object hierarchies so as to make elegant LINQ syntax for small sets of data (as based on the helper methods on the Wiki by Murray). There's also a few other new additions, like being able to chain property setters, and other extension methods on the RelationType object...

    Would be good to get any feedback (via codeplex) on the API before uComponents v2 is released into the wild.

    Cheers,

    Hendy

  • RuneO 36 posts 116 karma points
    Dec 09, 2010 @ 15:36
    RuneO
    0

    Sounds very nice - got to look more into this when I get the time :-)

    /Rune

  • Anthony Dang 1404 posts 2558 karma points MVP 3x c-trib
    Dec 09, 2010 @ 15:41
    Anthony Dang
    1

    Speaking of libraries Hendy...

    Not to compete with uComponents or anything, but I decided to share my personal collection of extensions etc. It's a tiny library but it does everything I normally need.

    It's aimed at c# developers (not XSLT developers), so uComponents will still be more helpful to XSLT dudes. 

    http://uhelpsy.codeplex.com

    :)

     

Please Sign in or register to post replies

Write your reply to:

Draft