Search code examples
c1-cms

Retrieve external URL of media file


I have a page data folder with a property "Image", which is a reference to a Media file in the Composite C1 backend.

I now want to retrieve the data in my asp.net user control, but all I get when I access the "Image" Property of my PageDataFolder Type, is a string with the following format:

mediaarchive:b5354eba-3f69-4885-9eba-74576dff372d

I am not sure how to get the external image url from that. Is there an API function I can use?


Solution

  • Build a url like

    ~/media({MediaKey})

    example: "~/media(mediaarchive:b5354eba-3f69-4885-9eba-74576dff372d)"

    Once C1 page is rendered, it will be replaced with the following SEO friendly url:

    /media/{Path to your image in media archive})

    example: "/media/5611182f-6462-4b80-a051-3c3b9bb3276d/References/Screenshots/Olympiacos/1_png"

    Note that you can specify image resing/cropping options via query string. http://docs.composite.net/Getting-started/Configuration/Resizing-Images

    If you, for some reason cannot rely on the C1 page rendering logic, you can build a public media url with the following code:

    protected string GetMediaUrl(string mediaPath)
    {
        string[] parts = mediaPath.Split(new[] { ':' });
    
        string mediaStore = parts[0];
        Guid mediaId = new Guid(parts[1]);
    
        string mediaUrl = MediaUrls.BuildUrl(new MediaUrlData { MediaStore = mediaStore, MediaId = mediaId, QueryParameters = new NameValueCollection() },
                                             UrlKind.Public);
    
        // Temporary fix, allows media player to receive a nice url with an extension
        return mediaUrl.Replace("_jpg", ".jpg").Replace("_mov", ".mov").Replace("_m4v", ".m4v").Replace("_swf", ".swf");
    }