ImageResizer on Mono

<Update>

I just heard from Nathanael, ImageResizer's owner. He said this fix will be included in the 3.1.5 release.

</Update>

I was busy writing .NET MVC extension methods to mimic Drupal's ImageCache plugin when I happened upon a ready-made solution. ImageResizer is a mature .NET image manipulation library designed to work seamlessly with ASP.NET web applications. In short, it lets you choose image sizes in your MVC views, and if those sizes don't exist, it'll create them.

As is the plight of the Mono developer, though, ImageResizer doesn't "just work" unless you're on Microsoft's platform. Thankfully, I've been in the Mono game for a while and know where to look when stuff blows up. In this case, the fix was a one-liner.

ImageResizer uses a call to UrlAuthorizationModule.CheckUrlAccessForPrincipal to determine if the application's current user has permission to access the image requested to be resized. Mono does not yet implement it.

I decided to simply wrap the call in a try...catch block that catches a NotImplementedException. The user gets permission for the file by default, so this seemed like the easiest and cleanest solution. I thought about doing a Unix check before the call, but that wouldn't stop projects running on Windows machines in the Mono runtime from blowing up.

The code is below. The ImageResizer source is here. The fix has to go in the method CheckRequest_PostAuthorizeRequest of the InterceptModule class. That class lives in the file InterceptModule.cs, which is in the root of the ImageResizer project directory. The call is around line 110 (see the picture for details).

/*
 These are the lines that explode projects running in the Mono runtime.
 The call to 'CheckUrlAccessForPrincipal' throws a NotImplementedException.
*/
bool isAllowed = true;
if (canCheckUrl) isAllowed = UrlAuthorizationModule.CheckUrlAccessForPrincipal(virtualPath, user, "GET");
 
/*
 To fix it, I wrapped the call in a try...catch block.
*/
bool isAllowed = true;
if (canCheckUrl) 
    try { isAllowed = UrlAuthorizationModule.CheckUrlAccessForPrincipal(virtualPath, user, "GET"); }
    catch (NotImplementedException) { /* The Mono runtime does not implement CheckUrlAccessForPrincipal */ }
 
/*
 You could also try to determine if the operating system is Unix by using the example below.
 I don't like this solution as much, though, because it's possible that the program is running
 in the Mono runtime on a Windows machine. Plus, if the Mono runtime ever implements
 CheckUrlAccessForPrincipal, the above method will automatically use the new functionality.
 A solution that depends on the operating-system type won't.
*/
int p = (int) Environment.OSVersion.Platform;
if ((p == 4) || (p == 128)) {
    Console.WriteLine ("Running on Unix");
} else {
    Console.WriteLine ("NOT running on Unix");
}

No comments for 'ImageResizer on Mono'

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • Image links with 'rel="lightbox"' in the <a> tag will appear in a Lightbox when clicked on.

More information about formatting options

CAPTCHA
Please verify your species.