1 |
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 |
has 'mirror' => ( |
28 |
is => 'rw', |
29 |
isa => 'Boolean', |
30 |
); |
31 |
|
32 |
#use String::TT qw/strip tt/; |
33 |
|
34 |
use pQuery; |
35 |
use File::Slurp; |
36 |
use LWP::Simple (); |
37 |
use File::Path; |
38 |
use Data::Dump qw/dump/; |
39 |
|
40 |
sub template_path { |
41 |
return 'templates/www.carnet.hr/' . shift; |
42 |
} |
43 |
|
44 |
sub mirror_design { |
45 |
my ( $self, $c, $path ) = @_; |
46 |
return if -e $path; |
47 |
|
48 |
return unless $self->mirror; |
49 |
|
50 |
my $url = $self->uri . '/' . $c->req->path . '?' . $c->req->uri->query; |
51 |
|
52 |
my $base_path = $path; |
53 |
$base_path =~ s{/[^/]+$}{}; |
54 |
mkpath $base_path if ! -e $base_path; |
55 |
|
56 |
warn ">> mirror $url -> $path\n"; |
57 |
|
58 |
LWP::Simple::mirror( $url, $path ) or die "can't mirror $url: $!"; |
59 |
} |
60 |
|
61 |
sub handler { |
62 |
my ( $self, $c ) = @_; |
63 |
|
64 |
my $req_dump = dump( $c->req ); |
65 |
my $raw = $c->req->raw_body; |
66 |
my $body; |
67 |
|
68 |
my $path = template_path( $c->req->path ); |
69 |
|
70 |
$path .= '.html' if $path !~ m/\.\w+$/; |
71 |
|
72 |
my $url = $self->uri; |
73 |
|
74 |
$self->mirror_design( $c, $path ); |
75 |
|
76 |
$body .= read_file( $path ); |
77 |
|
78 |
if ( $path =~ m/\.css$/ ) { |
79 |
$c->res->content_type( "text/css" ); |
80 |
} elsif ( $path =~ m/\.(gif|jpe?g|png)$/ ) { |
81 |
my $type = $1; |
82 |
$type =~ s/jpg/jpeg/; |
83 |
$c->res->content_type( "image/$type" ); |
84 |
} else { |
85 |
$c->res->content_type( "text/html" ); |
86 |
|
87 |
# strip full hostname |
88 |
$body =~ s{\Q$url\E}{/}gs; |
89 |
# remove cookie variable from url |
90 |
$body =~ s{CARNetweb=[0-9a-f]+}{}gs; |
91 |
|
92 |
my $dom = pQuery( $body ); |
93 |
# warn dump( $dom->find("body") ); |
94 |
$dom->find(".navigation")->each( sub { |
95 |
my $html = $_->innerHTML; |
96 |
warn $html; |
97 |
# $_->innerHTML(qq{ |
98 |
# <div style="border: 3px dashed black;">$html</div> |
99 |
# }); |
100 |
} ); |
101 |
|
102 |
# $body = $dom->toHtml; |
103 |
|
104 |
} |
105 |
|
106 |
warn "<< ", $c->req->path, |
107 |
" ", -s $path, |
108 |
" ", $c->res->content_type, |
109 |
" ", $c->req->params ? dump( $c->req->params ) : '', |
110 |
"\n"; |
111 |
|
112 |
=for later |
113 |
|
114 |
$body .= strip tt q{ |
115 |
<form method="post"> |
116 |
<input type="text" name="id" /> |
117 |
<input type="submit" /> |
118 |
</form> |
119 |
|
120 |
<form method="post" enctype="multipart/form-data"> |
121 |
<input type="file" name="upload_file" /> |
122 |
<input type="submit" /> |
123 |
</form> |
124 |
|
125 |
<pre>[% raw | html %]</pre> |
126 |
<pre>[% req_dump | html %]</pre> |
127 |
}; |
128 |
|
129 |
=cut |
130 |
|
131 |
$c->res->body($body); |
132 |
} |
133 |
|
134 |
1; |