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;
}
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.
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.
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..
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.
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.
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?
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
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
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
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
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
Sounds very nice - got to look more into this when I get the time :-)
/Rune
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
:)
is working on a reply...