+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Serving Library Files in Original Extension

Serving Library Files in Original Extension

15 posts, 0 answered
  1. Jeff
    Jeff avatar
    124 posts
    Registered:
    05 Aug 2007
    01 Feb 2009
    Link to this post
    Hello,

    I'd like to keep the original extension of picture and audio files during URL re-rewriting and I can't seem to get it working following the KB article on the subject.

    I created the class, put in in App_Code, added the following line to web.config:

          <add verb="GET" path="*.gif" type="CustomCmsContentHandler, App_Code" />

    I set up IIS 6.0 to handle the .gif extension and unchecked "Check that file exists."

    When I put a break point in the new Http Handler, it is never called.

    Also, I do not see a line like this anywhere in my web.config:

    <add name="mp3handler" path="*.mp3" verb="*" modules="IsapiModule" scriptProcessor="C:\Windows\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll" resourceType="Unspecified" preCondition="classicMode,runtimeVersionv2.0,bitness32" />


    Either I'm missing something or the KB article is missing a step or two. Either way, can you tell me what else to look for?


    Thanks,
    Jeff
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    02 Feb 2009
    Link to this post
    Hi Jeff,

    Could you sent us the modified CustomCmsContentHandler from your App_Code and website's web.config file. What kind of audio extensions you would like to have ? The example uses .mp3, so the line from the web.config file is result of adding ISAPI filter for .mp3 extension to the IIS configuration.

    Best wishes,
    Ivan Dimitrov
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. Jeff
    Jeff avatar
    124 posts
    Registered:
    05 Aug 2007
    03 Feb 2009
    Link to this post
    I am going for images, not audio. Other than that, it's straight out of the aforementioned KB article.

    Right now I'm just trying to get .gif working but I'd like to serve all the common formats.

    App_Code\CustomCmsContentHandler.cs


    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using Telerik.Cms.Engine;
    using System.Web.Hosting;


    /// <summary>
    /// Summary description for CustomCmsContentHandler
    /// </summary>

    public class CustomCmsContentHandler : ContentHttpHandler
    {
        protected override void ProcessContentItem(IContent content)
        {
            string extension = VirtualPathUtility.GetExtension(HttpContext.Current.Request.Url.AbsolutePath);
            if (extension.Equals(".gif", StringComparison.OrdinalIgnoreCase)
                 && !content.MimeType.StartsWith("image/"))
            {
                throw new HttpException(404, "Not found");
                return;
            }
            base.ProcessContentItem(content);
        }
    }


    web.config:

            <httpHandlers>
          <add verb="GET" path="*.gif" type="CustomCmsContentHandler, App_Code" />
    ...

    I've also mapped .gif to the .NET Framework .dll for all verbs and not verifying the file exists.

    I have the breakpoint set in ProcessContentItem() and it does not get called.

    The request for the .gif file returns a 404. If I change the extension to .sflb, the image is served fine.

    I've found that if I change the URL rewrite format by altering the following line under CMSEngine > Providers, I can serve the file as a .gif but my http handler is still never called.

                    <add name="Libraries" urlRewriteFormat="~/{Provider}/{LibraryName}/[Name].gif" urlDateTimeFormat="yyyy-MM-dd" urlWhitespaceChar="_" visible="False" defaultMetaField="Name" applicationName="/Libraries" allowVersioning="True" allowLocalization="False" localizationProviderName="" allowWorkflow="False" securityProviderName="" versioningProviderName="" connectionStringName="GenericContentConnection" type="Telerik.Libraries.Data.DefaultProvider, Telerik.Libraries.Data" tagEditorTemplate="~/Sitefinity/Admin/ControlTemplates/Libraries/BatchTagsEditor.ascx"/>


    Changing the above line does allow me to serve a database picture as a .gif. However, it implies that I need to create a separate library for .jpg and another for .png, etc. That would not be convenient.

    Thanks,
    Jeff
  4. Jeff
    Jeff avatar
    124 posts
    Registered:
    05 Aug 2007
    03 Feb 2009
    Link to this post
    Well this is strange.

    By changing the urlRewrite parameter in the Library provider back to .sflb, .gif files are now servable with a .gif extension or an .sflb extension.

    I even get the http handler firing intermittently. But it doesn't fire all the time.

    I'm wondering if the Sitefinity cache is at play here. That is, would the custom http handler be called if Sitefinity is serving the picture from the cache?

    In any case, I'm serving up .gifs as .gifs and .jpgs as .jpgs, so I'm 90% there.

    I just know that custom http handler is going to come in handy, so I'd like to get that working too.

    Thanks,
    Jeff
  5. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    03 Feb 2009
    Link to this post
    Hi Jeff,

    It is not necessary to change the urlRewriteFormat.
    You should create the handler and then have the following settings in the web.config file.

    <handlers> 
     <add name="gifHandler" path="*.gif" verb="*" modules="IsapiModule" scriptProcessor="C:\Windows\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll" resourceType="Unspecified" preCondition="classicMode,runtimeVersionv2.0,bitness32" /> 
    </handlers> 

    and
    <httpHandlers> 
          <add verb="GET" path="*.gif" type="CustomCmsContentHandler, App_Code" /> 
    </httpHandlers> 
     
    In this case all .gif-s can be called as Image.gif and Image.sflb.ashx

    Kind regards,
    Ivan Dimitrov
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  6. Jeff
    Jeff avatar
    124 posts
    Registered:
    05 Aug 2007
    04 Feb 2009
    Link to this post
    Thanks Ivan.

    I realize changing the rewriting format was unnecessary. I played with it out of desperation more than anything else. :)

    I think restarting IIS finally got it working but I can't be sure.

    At some point I am going to want to revisit the intermittent firing of the custom http handler since the application I'm migrating from uses custom handlers relies on them. For now, I'm just happy that my initial problem is solved.

    Thanks again.
  7. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    05 Feb 2009
    Link to this post
    Hi Jeff,

    I am glad that everything is fine now. Feel free to contact us if you need further assistance.

    Kind regards,
    Ivan Dimitrov
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  8. Daniel Plomp
    Daniel Plomp avatar
    952 posts
    Registered:
    18 Feb 2004
    08 Feb 2011
    Link to this post
    Hi,

    I tried to implement also a HttpHandler for .jpg files.
    I'm having a couple of problems:

    - When debugging, the HttpHandler doesn't seem to fire.
    - Only images that are not stored in Libraries are not visible anymore. All other images are still visible.

    This is the code of my HttpHandler:

    /// <summary>
    /// Summary description for CustomCmsImageHandler
    /// </summary>
    public class CustomCmsImageHandler : ContentHttpHandler
    {
        protected override object ProcessContentItem(IContent content, HttpRequest request)
        {
            string extension = VirtualPathUtility.GetExtension(HttpContext.Current.Request.Url.AbsolutePath);
            if (extension.Equals("*.jpg", StringComparison.OrdinalIgnoreCase) && !content.MimeType.StartsWith("image/"))
            {
                throw new HttpException(404, "Not found");
            }
      
            return base.ProcessContentItem(content, HttpContext.Current.Request);
        }
    }

    And this is how I registered it inside my web.config:

    <add verb="GET" path="*.jpg" type="CustomCmsImageHandler, App_Code" />

    Since I'm running the site on IIS7, .NET4.0, I also added a managed handler inside IIS7. This results in this change in my web.config:

    <add name="jpgHandler" path="*.jpg" verb="*" type="CustomCmsImageHandler, App_Code" resourceType="Unspecified" preCondition="integratedMode" />

    I have exactly the same approach for .mp3 files, which works.
    In the end I want to have some control on the images.

    I'd like to create a URL like this:

    Please, I really wants some help in this.

    Thanks!
    Daniel

  9. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    08 Feb 2011
    Link to this post
    Hello Daniel,

    Can you try to register the handler in both <configuration><system.webServer><handlers> and
    <configuration><system.web> <httpHandlers> then runt the website in debug mode and see whether the handler will be called.

    Regards,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  10. Daniel Plomp
    Daniel Plomp avatar
    952 posts
    Registered:
    18 Feb 2004
    08 Feb 2011
    Link to this post
    Hi Ivan,

    That is exactly what I have done.

    Below <configuration><system.web><httpHandlers> I have this line:
    <add verb="GET" path="*.jpg" type="CustomCmsImageHandler, App_Code" />

    And below <configuration><system.webServer><handlers> I have this line:
    <add name="jpgHandler" path="*.jpg" verb="*" type="CustomCmsImageHandler, App_Code" resourceType="Unspecified" preCondition="integratedMode" />

    Nothing happens when debugging.
    Or should I map the .sflb.ashx extension?

    Regards,
    Daniel
  11. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    08 Feb 2011
    Link to this post
    Hello Daniel,

    If you request the image with .sflb.ashx, then you should use this path. The handler is processing individual HTTP URLs or groups extensions. If there is no match the handler is not called.

    Best wishes,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  12. Daniel Plomp
    Daniel Plomp avatar
    952 posts
    Registered:
    18 Feb 2004
    08 Feb 2011
    Link to this post
    Hi Ivan,

    Thanks for the info.
    Yes, the images are having the path: .sflb.ashx. There are already handlers for these types, so should I remove them first?

    As said, I only want to be able to add ?crop=true to the URL.
    You mentioned the ResizeImage method before, but I don't know how to achieve this?

    Regards,
    Daniel 
  13. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    09 Feb 2011
    Link to this post
    Hi Daniel,

    You should remove the default ContentHttpHandler and use only the custom one.

    Greetings,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  14. Daniel Plomp
    Daniel Plomp avatar
    952 posts
    Registered:
    18 Feb 2004
    10 Feb 2011
    Link to this post
    Hi Ivan,

    I commented these line in the <handlers>:
    <!--<add name="SitefinityLibraryAdd" path="*.sflb.ashx" verb="*" preCondition="integratedMode" type="Telerik.Cms.Engine.ContentHttpHandler, Telerik.Cms.Engine" />-->

    And this line in the <httpHandlers>:
    <!--<add verb="*" path="*.sflb.ashx" type="Telerik.Cms.Engine.ContentHttpHandler, Telerik.Cms.Engine" />-->

    Then I added this line in the <handlers>:
    <add name="sflbAshxHandler" path="*.sflb.ashx" verb="*" type="CustomCmsContentHandler, App_Code" resourceType="Unspecified" preCondition="integratedMode" />

    And this line in the <httpHandlers>:
    <add verb="*" path="*.sflb.ashx" type="CustomCmsContentHandler, App_Code" />

    Still nothing happens when I run my website. I expect that the ResizeImage() funtion which I override in the custom handler is fired when I have an image in my website like this:

    Am I right about this?
    What could be wrong...?

    Regards,
    Daniel
  15. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    10 Feb 2011
    Link to this post
    Hello Daniel,

    We do not have a query string crop=true that you can use. This means that you need to do some custom logic here. ResizeImage is called if you have width and height built-in query strings in the url. The method is called inside GetContentData which process the request depending on the current context, extension and response settings.

    All the best,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Register for webinar
15 posts, 0 answered