line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Plack::Middleware::QRCode; |
2
|
1
|
|
|
1
|
|
27316
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
34
|
|
3
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
40
|
|
4
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
5
|
1
|
|
|
1
|
|
915
|
use parent qw(Plack::Middleware); |
|
1
|
|
|
|
|
307
|
|
|
1
|
|
|
|
|
5
|
|
6
|
|
|
|
|
|
|
use Plack::Util::Accessor qw(render config image_type); |
7
|
|
|
|
|
|
|
use Imager; |
8
|
|
|
|
|
|
|
use Imager::QRCode; |
9
|
|
|
|
|
|
|
use Plack::Response; |
10
|
|
|
|
|
|
|
use Plack::Request; |
11
|
|
|
|
|
|
|
use feature qw(say); |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub prepare_app { |
14
|
|
|
|
|
|
|
my $self = shift; |
15
|
|
|
|
|
|
|
my $default_config = { |
16
|
|
|
|
|
|
|
size => 4, |
17
|
|
|
|
|
|
|
margin => 2, |
18
|
|
|
|
|
|
|
version => 1, |
19
|
|
|
|
|
|
|
level => 'M', |
20
|
|
|
|
|
|
|
casesensitive => 1, |
21
|
|
|
|
|
|
|
lightcolor => Imager::Color->new(255, 255, 255), |
22
|
|
|
|
|
|
|
darkcolor => Imager::Color->new(0, 0, 0), |
23
|
|
|
|
|
|
|
}; |
24
|
|
|
|
|
|
|
my $config = { |
25
|
|
|
|
|
|
|
%$default_config , |
26
|
|
|
|
|
|
|
%{ $self->render || { } } }; |
27
|
|
|
|
|
|
|
$self->config( $config ); |
28
|
|
|
|
|
|
|
$self->image_type( 'png' ) unless $self->image_type; |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub call { |
32
|
|
|
|
|
|
|
my ($self,$env) = @_; |
33
|
|
|
|
|
|
|
my $req = Plack::Request->new( $env ); |
34
|
|
|
|
|
|
|
my $params = $req->parameters->mixed; |
35
|
|
|
|
|
|
|
my %config = %{ $self->config }; |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
$config{size} = $params->{s} if exists $params->{s}; |
38
|
|
|
|
|
|
|
$config{margin} = $params->{m}; |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
my $pathinfo = substr $env->{'PATH_INFO'},1; |
41
|
|
|
|
|
|
|
say STDERR "Generating QRCode for '" , $pathinfo , "'"; |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
my @error = ( 500 , [ 'Content-type' => 'text/plain' ] ); |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
return [ @error , [ 'Please enter text for QRCode' ] ] unless $pathinfo; |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
unless ( $config{size} > 0 ) { |
48
|
|
|
|
|
|
|
return [ @error, [ 'Please enter an integer for size param' ] ]; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
my $qrcode = Imager::QRCode->new( %config ); |
52
|
|
|
|
|
|
|
my $img = $qrcode->plot( $pathinfo ); |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
my $data = ''; |
55
|
|
|
|
|
|
|
$img->write( data => \$data , type => $self->image_type ); |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
return [ 200 , [ 'Content-type' => 'image/' . $self->image_type ] , [ $data ] ]; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
1; |
61
|
|
|
|
|
|
|
__END__ |