Sunday, March 20, 2011

Serving the right file suffix

Sometimes if you use applications other than the browser to open up links (i.e.: Excel), they behave better if the file served has the neat, correct suffix at the end.
This messes up all internal paths in Catalyst though. So I figured out that if I could clean up the suffix at the end of the URI, and keep it as the format parameter, I'd solved the problem. So here's the code:



# inside My::Catalyst::App

after 'prepare_path' => sub {
my ($c) = @_;
local $_ = $c->request->path;
s/\.([a-z]+)$//i;
$c->stash->{format} = lc $1 || 'html';
$c->request->path($_);
};

No comments: