| 1 | package MojoMojo::Controller::Image; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 35 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 35 | ▶ | use parent 'Catalyst::Controller'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 35 | ▶ | use IO::File; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 35 | ▶ | use URI::Escape (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | =head1 NAME | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | MojoMojo::Controller::Image - Image controller | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | This controller is used to see images in particular url | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | Formatter::Dir must be configured in mojomojo.conf. | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | If url begin with 'prefix_url', so images can be displayed. | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | Images are stored in 'whitelisting' directory. | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | View usage with 'script/util/dir2mojomojo.pl' | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | =cut | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | =head2 file_not_found | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | Private action to return a 404 not found page. | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | =cut | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 0 | ▶ | sub file_not_found : Private { | ||||||||||||||||||||||||||
|
Subroutine
file_not_found: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 35 | 0 | my ( $self, $c ) = @_; | |||||||||||||||||||||||||||
| 36 | 0 | $c->stash->{template} = 'message.tt'; | |||||||||||||||||||||||||||
| 37 | 0 | $c->stash->{message} = $c->loc("file not found."); | |||||||||||||||||||||||||||
| 38 | 0 | return ( $c->res->status(404) ); | |||||||||||||||||||||||||||
| 39 | 35 | ▶ | } | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | =head2 png | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | =cut | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 0 | ▶ | sub png : Global { | ||||||||||||||||||||||||||
|
Subroutine
png: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 46 | 0 | my ( $self, $c, $path ) = @_; | |||||||||||||||||||||||||||
| 47 | 0 | $c->forward('view'); | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 35 | ▶ | } | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | =head2 jpg | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | =cut | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 0 | ▶ | sub jpg : Global { | ||||||||||||||||||||||||||
|
Subroutine
jpg: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 56 | 0 | my ( $self, $c ) = @_; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 0 | $c->forward('view'); | |||||||||||||||||||||||||||
| 59 | 35 | ▶ | } | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | =head2 gif | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | =cut | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 0 | ▶ | sub gif : Global { | ||||||||||||||||||||||||||
|
Subroutine
gif: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 66 | 0 | my ( $self, $c, $path ) = @_; | |||||||||||||||||||||||||||
| 67 | 0 | $c->forward('view'); | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 35 | ▶ | } | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | =head2 tiff | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | =cut | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 0 | ▶ | sub tiff : Global { | ||||||||||||||||||||||||||
|
Subroutine
tiff: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 76 | 0 | my ( $self, $c, $path ) = @_; | |||||||||||||||||||||||||||
| 77 | 0 | $c->forward('view'); | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 35 | ▶ | } | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | =head2 view | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | png/jpg/gif are forwarded here. This action is used to see images | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | =cut | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 0 | ▶ | sub view : Private { | ||||||||||||||||||||||||||
|
Subroutine
view: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 88 | 0 | my ( $self, $c, $path ) = @_; | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 0 | ▶ | $c->detach('unauthorized', ['view']) if not $c->check_view_permission; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 0 | ▶ | if ( ! defined $c->config->{'Formatter::Dir'}{prefix_url} || | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $c->config->{"Formatter::Dir"}{"prefix_url"} and defined $c->config->{"Formatter::Dir"}{"whitelisting"}
MC/DC0%0/2
defined $c->config->{"Formatter::Dir"}{"prefix_url"} and defined $c->config->{"Formatter::Dir"}{"whitelisting"}
defined $c->config->{"Formatter::Dir"}{"prefix_url"}
defined $c->config->{"Formatter::Dir"}{"whitelisting"}
Truth table0%0/3
A and B
A= defined $c->config->{"Formatter::Dir"}{"prefix_url"}B= defined $c->config->{"Formatter::Dir"}{"whitelisting"}
| |||||||||||||||||||||||||||||
| 94 | ! defined $c->config->{'Formatter::Dir'}{whitelisting} ){ | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 0 | $c->stash->{message} = "Formatter::Dir is not configured"; | |||||||||||||||||||||||||||
| 97 | 0 | $c->stash->{template} = 'message.tt'; | |||||||||||||||||||||||||||
| 98 | 0 | return ( $c->res->status(404) ); | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | # Show image only if url start by prefix_url configured | ||||||||||||||||||||||||||||
| 102 | # in mojomojo.conf with Formatter::Dir | ||||||||||||||||||||||||||||
| 103 | 0 | my $prefix_url = $c->config->{'Formatter::Dir'}{prefix_url}; | |||||||||||||||||||||||||||
| 104 | 0 | $prefix_url =~ s|\/$||; | |||||||||||||||||||||||||||
| 105 | 0 | $prefix_url .= '/'; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 0 | my $file = $c->stash->{path}; | |||||||||||||||||||||||||||
| 109 | 0 | my $suffix = $c->req->uri->path; | |||||||||||||||||||||||||||
| 110 | 0 | $suffix =~ s|^\/||; | |||||||||||||||||||||||||||
| 111 | 0 | ▶ | if ( $file !~ s/^$prefix_url// ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 0 | $c->stash->{message} = $c->loc( | |||||||||||||||||||||||||||
| 113 | 'The requested URL was not found: x', | ||||||||||||||||||||||||||||
| 114 | "$file.$suffix"); | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 0 | $c->stash->{template} = 'message.tt'; | |||||||||||||||||||||||||||
| 117 | 0 | return ( $c->res->status(404) ); | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 0 | my $filename = "$file.$suffix"; | |||||||||||||||||||||||||||
| 122 | 0 | my $dir = $c->config->{'Formatter::Dir'}{whitelisting}; | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 0 | ▶ | my $io_file = IO::File->new("$dir/$filename") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | or $c->detach('file_not_found'); | ||||||||||||||||||||||||||||
| 126 | 0 | $io_file->binmode; | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 0 | $c->res->output( $io_file ); | |||||||||||||||||||||||||||
| 129 | 0 | $c->res->header( 'content-type', $c->action ); | |||||||||||||||||||||||||||
| 130 | 0 | $c->res->header( | |||||||||||||||||||||||||||
| 131 | "Content-Disposition" => "inline; filename=" . URI::Escape::uri_escape_utf8( $filename ) ); | ||||||||||||||||||||||||||||
| 132 | 0 | $c->res->header( 'Cache-Control', 'max-age=86400, must-revalidate' ); | |||||||||||||||||||||||||||
| 133 | 35 | ▶ | } | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | Daniel Brosseau <dab@catapulse.org> | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | This library is free software. You can redistribute it and/or modify | ||||||||||||||||||||||||||||
| 143 | it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | =cut | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 35 | 1; | |||||||||||||||||||||||||||