Created
September 12, 2010 16:53
-
-
Save aklaswad/576237 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### Movable Type content viewer | |
use Plack::Builder; | |
use lib qw( lib extlib ); | |
use Encode; | |
use MT; | |
use MT::FileMgr::Local; | |
use MT::WeblogPublisher; | |
my $mt = MT->new; | |
my $pub = MT::WeblogPublisher->new; | |
my $ContentTypes = { | |
html => 'text/html', | |
css => 'text/css', | |
js => 'text/javascript', | |
xml => 'application/xml', | |
}; | |
my $app = sub { | |
my $env = shift; | |
my $path = $env->{PATH_INFO}; | |
## FIXME: get index file name from blog setting. | |
$path .= 'index.html' if $path =~ m!/$!; | |
my ($suffix) = $path =~ m!\.([^\.]+)!; | |
my $type = $ContentTypes->{$suffix}; | |
my $fi = MT->model('fileinfo')->load({ url => $path }) | |
or return [ 404, [ "Content-Type", "text/plain" ], [ "No such file" ] ]; | |
my $out; | |
{ | |
## MT does **NOT** have function to get output from fileinfo objects, | |
## So did hack like this... | |
no strict qw( refs ); | |
local *MT::FileMgr::Local::put_data = sub { $out = $_[1] }; | |
$pub->rebuild_from_fileinfo($fi); | |
} | |
return [ 200, [ "Content-Type", $type ], [ Encode::encode_utf8($out) ] ]; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment