Exposing the MCMS Resource Gallery with simpler urls

We've been moving more of the resources out of the project structure and into the ResourceGallery.

The main drawback to this is the unfriendly urls that the gallery uses. It is very difficult to put resource urls into the css files.

I've implemented this simple IHttpHandler that does the job without an ISAPI, but this is a first implementation and I haven't measured anything yet.

To use it, I configure the httpHandler path="AppName/Resources.ashx" then call the url   http://localhost/AppName/Resources.ashx/RootGalleryName/Onions/GeneralMiao.jpg


using System;
using System.Web;
using Microsoft.ContentManagement.Publishing;

namespace Georgia.Gdecd.WebSite
{
 /// <summary>
 /// Summary description for McmsResourceGalleryHandler.
 /// </summary>
 public class McmsResourceGalleryHandler : System.Web.IHttpHandler
 {
  public McmsResourceGalleryHandler()
  {
  }
  public void ProcessRequest(System.Web.HttpContext context)
  {
   HttpRequest Request = context.Request;
   string resourcePath = Request.Path.Substring(Request.FilePath.Length + 1);
   Resource res = CmsHttpContext.Current.RootResourceGallery.GetByRelativePath(resourcePath) as Resource;
   if(res!=null)
   {
    using(System.IO.Stream stream = res.OpenReadStream())
    {
     byte[] buffer = new byte[32];
     while(stream.Read(buffer,0,32) > 0)
     {
      context.Response.BinaryWrite(buffer);
     }
    }
   }
  }
  public bool IsReusable
  {
   get{return false;}
  }
 }
}

No Comments