1 |
dpavlin |
164 |
package Frey::Designer; |
2 |
|
|
|
3 |
|
|
use Moose; |
4 |
|
|
use Moose::Util::TypeConstraints; |
5 |
|
|
|
6 |
|
|
use URI (); |
7 |
|
|
|
8 |
|
|
subtype 'Uri' |
9 |
|
|
=> as 'Object' |
10 |
|
|
=> where { $_->isa('URI') }; |
11 |
|
|
|
12 |
|
|
coerce 'Uri' |
13 |
|
|
=> from 'Object' |
14 |
|
|
=> via { $_->isa('URI') |
15 |
|
|
? $_ |
16 |
|
|
: Params::Coerce::coerce( 'URI', $_ ) } |
17 |
|
|
=> from 'Str' |
18 |
|
|
=> via { URI->new( $_ ) }; |
19 |
|
|
|
20 |
|
|
has 'uri' => ( |
21 |
|
|
is => 'rw', |
22 |
|
|
isa => 'Uri', |
23 |
|
|
coerce => 1, |
24 |
|
|
required => 1, |
25 |
|
|
); |
26 |
|
|
|
27 |
|
|
#use String::TT qw/strip tt/; |
28 |
|
|
|
29 |
|
|
use pQuery; |
30 |
|
|
use File::Slurp; |
31 |
|
|
use LWP::Simple; |
32 |
|
|
use File::Path; |
33 |
|
|
use Data::Dump qw/dump/; |
34 |
|
|
|
35 |
|
|
sub template_path { |
36 |
|
|
return 'templates/www.carnet.hr/' . shift; |
37 |
|
|
} |
38 |
|
|
|
39 |
|
|
sub mirror_design { |
40 |
|
|
my ( $url, $path ) = @_; |
41 |
|
|
return if -e $path; |
42 |
|
|
|
43 |
|
|
my $base_path = $path; |
44 |
|
|
$base_path =~ s{/[^/]+$}{}; |
45 |
|
|
mkpath $base_path if ! -e $base_path; |
46 |
|
|
|
47 |
|
|
warn ">> mirror $url -> $path\n"; |
48 |
|
|
|
49 |
|
|
mirror( $url, $path ) or die "can't mirror $url: $!"; |
50 |
|
|
} |
51 |
|
|
|
52 |
|
|
sub handler { |
53 |
|
|
my ( $self, $c ) = @_; |
54 |
|
|
|
55 |
|
|
my $req_dump = dump( $c->req ); |
56 |
|
|
my $raw = $c->req->raw_body; |
57 |
|
|
my $body; |
58 |
|
|
|
59 |
|
|
my $path = template_path( $c->req->path ); |
60 |
|
|
|
61 |
|
|
$path .= '.html' if $path !~ m/\.\w+$/; |
62 |
|
|
|
63 |
|
|
my $url = $self->uri; |
64 |
|
|
|
65 |
|
|
mirror_design( $url . $c->req->path, $path ); |
66 |
|
|
|
67 |
|
|
$body .= read_file( $path ); |
68 |
|
|
|
69 |
|
|
if ( $path =~ m/\.css$/ ) { |
70 |
|
|
$c->res->content_type( "text/css" ); |
71 |
|
|
} elsif ( $path =~ m/\.(gif|jpe?g|png)$/ ) { |
72 |
|
|
my $type = $1; |
73 |
|
|
$type =~ s/jpg/jpeg/; |
74 |
|
|
$c->res->content_type( "image/$type" ); |
75 |
|
|
} else { |
76 |
|
|
$c->res->content_type( "text/html" ); |
77 |
|
|
|
78 |
|
|
# strip full hostname |
79 |
|
|
$body =~ s{\Q$url\E}{/}gs; |
80 |
|
|
# remove cookie variable from url |
81 |
|
|
$body =~ s{CARNetweb=[0-9a-f]+}{}gs; |
82 |
|
|
|
83 |
|
|
my $dom = pQuery( $body ); |
84 |
|
|
# warn dump( $dom->find("body") ); |
85 |
|
|
$dom->find(".navigation")->each( sub { |
86 |
|
|
warn dump( $_->innerHTML ); |
87 |
|
|
} ); |
88 |
|
|
|
89 |
|
|
} |
90 |
|
|
|
91 |
|
|
warn "<< ", $c->req->path, " ", -s $path, " ", $c->res->content_type, "\n"; |
92 |
|
|
|
93 |
|
|
=for later |
94 |
|
|
|
95 |
|
|
$body .= strip tt q{ |
96 |
|
|
<form method="post"> |
97 |
|
|
<input type="text" name="id" /> |
98 |
|
|
<input type="submit" /> |
99 |
|
|
</form> |
100 |
|
|
|
101 |
|
|
<form method="post" enctype="multipart/form-data"> |
102 |
|
|
<input type="file" name="upload_file" /> |
103 |
|
|
<input type="submit" /> |
104 |
|
|
</form> |
105 |
|
|
|
106 |
|
|
<pre>[% raw | html %]</pre> |
107 |
|
|
<pre>[% req_dump | html %]</pre> |
108 |
|
|
}; |
109 |
|
|
|
110 |
|
|
=cut |
111 |
|
|
|
112 |
|
|
$c->res->body($body); |
113 |
|
|
} |
114 |
|
|
|
115 |
|
|
1; |