Copied to clipboard

Flag this post as spam?

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


  • Mikael Mørup 297 posts 326 karma points
    Sep 23, 2009 @ 14:39
    Mikael Mørup
    0

    getting media Id from path

    if i have a media (file) witha path like:

        http://somehost/media/7632/1267_139.pdf

    is theer a way that i with the API can get the media ID of the file ?

    Mikael

  • Ismail Mayat 4511 posts 10092 karma points MVP 2x admin c-trib
    Sep 23, 2009 @ 16:07
    Ismail Mayat
    2

    Mikael,

    Can you explain a bit more about what you are trying to do? Are you working on a datatype / macro ? I had to get id for file path that was in an httpmodule i wrote to secure pdfs.  Before serving the pdf i would do some security checks then if all was good serve the document, however i had to get the id of the item from the requested url this is how i did it:

     

    private int getMediaIdFromUrl(string url, HttpContext context){

     

    Regex reg = new Regex("/media.*");

    int id;

    try

    {

    SqlParameter[] sqParams = {new SqlParameter("@url", reg.Match(url).Value)};

    string sql = "select contentNodeId from cmsPropertyData where dataNvarchar = @url";

    id= (int)SqlHelper.ExecuteScalar(umbraco.GlobalSettings.DbDSN,CommandType.Text,sql,sqParams);

    return id;

    }

    catch(Exception ex){

     umbraco.BusinessLogic.Log.Add(umbraco.BusinessLogic.LogTypes.Error,new umbraco.BusinessLogic.User(0),0,"Error from download security handler ->" + ex.Message.ToString());

    }

    return 0;

     

    }

     

     

    Regards

    Ismail

  • Mikael Mørup 297 posts 326 karma points
    Sep 23, 2009 @ 16:18
    Mikael Mørup
    0

    My problem is basically the same as yours. I was thinking of coding an XSLT extension to do it. Right now i don't know if i have to do it or not. :-)

    I can see that you just look it up in the DB, so i assume that there is no API to do it. But a DB lookup is ok for me too.

    Thanks

    Mikael

     

     

  • Ismail Mayat 4511 posts 10092 karma points MVP 2x admin c-trib
    Sep 24, 2009 @ 08:23
    Ismail Mayat
    0

    if it works dont forget to mark as solution :-}

     

    Ismail

  • Marco Lusini 176 posts 1370 karma points
    Sep 25, 2009 @ 10:11
    Marco Lusini
    0

    Ismail's solution works fine, but is a bit hard on the DB: cmsPropertyData does not have an index for dataNvarchar, so the query must do a scan on the table, which can become slow if you have a lots of properties/versions/hits.

    You'd better extract the propertyID from the path (is the number after /media, 7632 in your example) and use that to retrieve contentNodeID (I didn't look, but there should be an API to retrieve property's data from its ID).

    Marco

     

  • Steven Wilber 103 posts 98 karma points
    Feb 24, 2010 @ 13:00
    Steven Wilber
    0

    Hi,

    If you extract the id from the path, this is actually the id from the cmsPropertyData table. This will make for a more efficient SQL query than selecting the dataNvarchar field.

    Cheers

    Steve

Please Sign in or register to post replies

Write your reply to:

Draft