| 1 | # $Id: /mirror/gungho/lib/Gungho/Handler/FileWriter/Simple.pm 1749 2007-06-10T08:52:31.230704Z lestrrat $ | ||
| 2 | # | ||
| 3 | # Copyrigt (c) 2007 Daisuke Maki <daisuke@endeworks.jp> | ||
| 4 | # All rights reserved. | ||
| 5 | |||
| 6 | package Gungho::Handler::FileWriter::Simple; | ||
| 7 | use strict; | ||
| 8 | use warnings; | ||
| 9 | use base qw(Gungho::Handler); | ||
| 10 | use File::Spec; | ||
| 11 | use Path::Class(); | ||
| 12 | use URI::Escape qw(uri_escape); | ||
| 13 | |||
| 14 | __PACKAGE__->mk_accessors($_) for qw(dir); | ||
| 15 | |||
| 16 | sub setup | ||
| 17 | { | ||
| 18 | my ($self, $c) = @_; | ||
| 19 | |||
| 20 | $self->dir( | ||
| 21 | Path::Class::Dir->new( $self->config->{dir} || File::Spec->tmpdir) | ||
| 22 | ); | ||
| 23 | $self->next::method($c); | ||
| 24 | } | ||
| 25 | |||
| 26 | sub path_to | ||
| 27 | { | ||
| 28 | my ($self, $req, $res) = @_; | ||
| 29 | |||
| 30 | # Just writes to a file name that has been "properly" (for better | ||
| 31 | # or for worse...) URl-encoded | ||
| 32 | |||
| 33 | return $self->dir->file( uri_escape( $req->uri ) ); | ||
| 34 | } | ||
| 35 | |||
| 36 | sub handle_response | ||
| 37 | { | ||
| 38 | my ($self, $c, $req, $res) = @_; | ||
| 39 | |||
| 40 | my $file = $self->path_to($req, $res); | ||
| 41 | my $fh = $file->openw() or die; | ||
| 42 | |||
| 43 | $c->log->debug("Writing " . $req->uri . " to $file"); | ||
| 44 | |||
| 45 | $fh->print($res->content); | ||
| 46 | $fh->close; | ||
| 47 | } | ||
| 48 | |||
| 49 | 1; | ||
| 50 | |||
| 51 | __END__ |