Anyone working on/ interested in a ThumbnailActionResult ?

0 views
Skip to first unread message

ccollie

unread,
Jun 12, 2009, 8:49:40 PM6/12/09
to mvccontrib-discuss
I'm working on a social network in ASP.Net where i have a need to have
multiple versions of a file. I've collected some thumbnailing code,
and figure that instead of going the usual HttpHandler route, that i
would create an action handler so i could hook into routing and have
nicer urls.

i have code, but ive been using MVC for less than a few weeks, so im
sure my code is sub-optimal (and not well tested, unfortunately).
Still it seems like it would make for a useful addition to the
toolkit.

anyone interested ?

Eric Hexter

unread,
Jun 12, 2009, 9:20:02 PM6/12/09
to mvccontri...@googlegroups.com
Can you provide some more details around how this works or what the usage of this looks like?



Eric Hexter

Principal Consultant
Headspring Systems | www.HeadspringSystems.com
email: ehe...@HeadspringSystems.com
blog: Hex.LosTechies.com 
-------------------------------------------------------------------------------------------------------------
Director - Austin .Net Users Group | www.ADNUG.org
Membership Mentor South Texas - INETA | www.INETA.org
Asp Insider | www.ASPInsiders.com

ccollie

unread,
Jun 12, 2009, 10:06:30 PM6/12/09
to mvccontrib-discuss
Sure..
the idea is that instead of using an HttpHandler to generate and cache
thumbnails, we do so using an ActionResult.

Things currently look like the following :

<code>

public class ThumbnailResult : ActionResult
{
public ThumbnailResult()
{
}

public string Original { get; set; }
public int Width { get; set; }
public int Height { get; set; }
public int Quality { get; set; }
public string CacheDirectory { get; set; }
public string MimeType { get; set; }
public string FileNotFoundImage { get; set; }
public TimeSpan? ResponseCacheTimeout { get; set; }
......

</code>

A thumnail of Original is generated based on the given Width and
Height. If CacheDirectory is specified, the thumbnail is cached (the
file name is generated from Original, Width, Height and Quality). If
Original is not found, FileNotFoundImage is sized and sent to the
browser.
ResponseCacheTimeout determines browser cache timeouts. If set, we
generate ETags (based on the specified parameters), and check against
those, as well as comparing if-modified-since against the LastWriteUTC
timestamp of the original file.

I havent done HtmlHelperExtensions yet, thought i dont think they're
necessary, since were talking about an ActionResult which can be
returned by any controller, so any extension method would be project
specific, or handled by Html.ActionLink<T>. My initial use case is
something like this ...

// MemberController

public ActionResult MiniThumbnail(int userId)
{
User user = userRepository.Get(userId);
if (user == null)
return new NotFoundResult();
String profilePhoto = user.ProfileImage;
return GetThumbnailResult(profilePhoto, ImagePresets.MiniPhoto);
}

public ActionResult ProfilePhoto(int userId)
{
User user = userRepository.Get(userId);
if (user == null)
return new NotFoundResult();
String profilePhoto = user.ProfileImage;
return GetThumbnailResult(profilePhoto,
ImagePresets.ProfilePhoto);
}

protected ActionResult GetThumbnailResult(String filename, ImagePreset
preset) {
ThumbnailResult result = new ThumbnailResult(){
Original = filename,
Width = preset.Width,
Height = preset.Height, // either height or width can be
left out
Quality = preset.Quality,
FileNotFoundImage = config.NoPhotoFilename,
CacheDirectory = config.ThumbnailCacheDirectory
};
return result;
}

At this point, we can probably create a simple Html.Image extension
// Profile.aspx

<div id="user-photo">
<%=Html.Image<MemberController>(c=>c.ProfilePhoto(Model.Id),
"Profile Photo") %>
</div>



On Jun 12, 9:20 pm, Eric Hexter <eric.hex...@gmail.com> wrote:
> Can you provide some more details around how this works or what the usage of
> this looks like?
>
> Eric Hexter
>
> Principal Consultant
> Headspring Systems |www.HeadspringSystems.com
> email: ehex...@HeadspringSystems.com

ccollie

unread,
Jun 12, 2009, 10:12:15 PM6/12/09
to mvccontrib-discuss

ExecuteResult is as follows :


public override void ExecuteResult(ControllerContext context)
{
HttpContextBase httpContext = context.HttpContext;

FileInfo originalFi = new FileInfo(Original);

// Nothing else matters if the original doesnt exist
if (!originalFi.Exists)
{
if (!String.IsNullOrEmpty(FileNotFoundImage) &&
File.Exists(FileNotFoundImage))
{
// sends the not found image
SendResizedImage(httpContext, FileNotFoundImage,
Width, Height, Quality);
return;
}
else
{
// sends a 404
SendFileNotFound(httpContext);
return;
}
}

if (ConditionalGet(httpContext, originalFi, Width, Height,
Quality))
return; // already cached in the browser

// lets check to see if requesting a custom resolution?
if (Height != 0 || Width != 0)
{
SendResizedImage(httpContext, Original, Width, Height,
Quality);
}
else
{
// if the file is there and no height or width changes
are required.
// send the original file
SendFile(httpContext, Original);
Reply all
Reply to author
Forward
0 new messages