blib/lib/IIIF/ImageAPI.pm

filter: Statementstmt 41.5% 49 / 118 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 62.5% 15 / 24 Podpod 22.2% 2 / 9 totaltotal 29.6% 66 / 223 CC 30 SCAR 44.7
CC
30
Cov
28%
response
272.0
call
20.0
new
12.0
CRAP 87.6
↑ Summary blib/lib/IIIF/Request.pm »
1 package IIIF::ImageAPI;
2 1 ▶use 5.014001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 1 our $VERSION = "0.06";
5  
6 1 ▶use parent 'Plack::Component';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use IIIF::Magick qw(info convert);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Plack::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use IIIF::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use JSON::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use File::Temp qw(tempdir);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Digest::MD5 qw(md5_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use HTTP::Date;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 ▶use Plack::MIME;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 Plack::MIME->add_type( '.jp2', 'image/jp2' );
20 1 Plack::MIME->add_type( '.webp', 'image/webp' );
21  
22 1 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use Plack::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 use Plack::Util::Accessor
26 1 ▶ qw(images base cache formats rights service canonical magick_args);
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 1 our @FORMATS = qw(jpg png gif);
29  
30 sub new {
31 0 ▶ my $class = shift;
Subroutine new: not called CC=3 SCAR=24.8
Pod documented
32 0 my $self = $class->SUPER::new(@_);
33  
34 0 ▶ $self->images('.') unless $self->images;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->images 0 0 0
35 0 ▶ $self->formats( [qw{jpg png gif}] ) unless $self->formats;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->formats 0 0 0
36  
37 0 $self;
38 }
39  
40 sub call {
41 0 ▶ my ( $self, $env ) = @_;
Subroutine call: not called CC=4 SCAR=30.0
Pod documented
42 0 my $req = Plack::Request->new($env);
43  
44 0 ▶ if ( $req->path_info =~ qr{^/([^/]+)/?(.*)$} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req->path_info =~ qr"^/([^/]+)/?(.*)$"u) 0 0 0
45 0 my ( $identifier, $request ) = ( $1, $2 );
46 0 ▶ if ( my $file = $self->file($identifier) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $file = $self->file($identifier)) 0 0 0
47 0 ▶ $file->{id} = ( $self->base // $req->base ) . $identifier;
Condition0%0/3
$self->base // $req->base
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->base // $req->base
$self->base $req->base
Truth table0%0/3
A // B
A= $self->baseB= $req->base
ABresult
1X1
011
000
48 0 return $self->response( $file, $request );
49 }
50 }
51  
52 0 return error_response( 404, "Not Found" );
53 }
54  
55 sub response {
56 0 ▶ my ( $self, $file, $local ) = @_;
Subroutine response: not called CC=16 SCAR=56.1
Pod undocumented
57  
58 # Image Information Request
59 0 ▶ if ( $local eq '' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($local eq '') { } 0 0 0
elsif ($local eq 'info.json') { } 0 0 0
60 0 return redirect( $file->{id} . "/info.json" );
61 }
62 elsif ( $local eq 'info.json' ) {
63 0 return $self->info_response($file);
64 }
65  
66 # allow abbreviated requests, redirect to full form
67 0 my $request = eval { IIIF::Request->new($local) };
68 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
69 0 return error_response( 400, ( split( " at ", $@ ) )[0] );
70 }
71  
72 0 ▶ $request->{format} = $request->{format} // $file->{format};
Condition0%0/3
$request->{"format"} // $file->{"format"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$request->{"format"} // $file->{"format"}
$request->{"format"} $file->{"format"}
Truth table0%0/3
A // B
A= $request->{"format"}B= $file->{"format"}
ABresult
1X1
011
000
73  
74 return error_response( 400, "unsupported format" )
75 0 ▶ unless grep { $_ eq $request->{format} } @{ $self->formats };
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $request->{"format"};} @{$self->formats;} 0 0 0
76  
77 0 ▶ if ( $self->canonical ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->canonical) 0 0 0
78 0 my $info = info( $file->{path} );
79 my $canonical = $request->canonical( $info->{width}, $info->{height} )
80 0 ▶ or return error_response();
Branch: 1 branch
BranchTrueFalseTotal
my $canonical = $request->canonical($info->{"width"}, $info->{"height"}) or return error_response() 0 0 0
81 0 $request = IIIF::Request->new($canonical);
82 }
83  
84 0 ▶ if ( "$request" ne $local ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("$request" ne $local) 0 0 0
85 0 return redirect( $file->{id} . "/$request" );
86 }
87  
88 # Image Request
89  
90 # directly serve unmodified image
91 0 ▶ if ( $request->is_default && $request->{format} eq $file->{format} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($request->is_default and $request->{"format"} eq $file->{"format"}) 0 0 0
Condition0%0/3
$request->is_default and $request->{"format"} eq $file->{"format"}
!ll&&!rl&&r
000
MC/DC0%0/2
$request->is_default and $request->{"format"} eq $file->{"format"}
$request->is_default $request->{"format"} eq $file->{"format"}
Truth table0%0/3
A and B
A= $request->is_defaultB= $request->{"format"} eq $file->{"format"}
ABresult
0X0
100
111
92 0 return image_response( $file->{path} );
93 }
94  
95 0 ▶ my $cache = $self->cache // $self->cache( tempdir( CLEANUP => 1 ) );
Condition0%0/3
$self->cache // $self->cache(tempdir("CLEANUP", 1))
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->cache // $self->cache(tempdir("CLEANUP", 1))
$self->cache $self->cache(tempdir("CLEANUP", 1))
Truth table0%0/3
A // B
A= $self->cacheB= $self->cache(tempdir("CLEANUP", 1))
ABresult
1X1
011
000
96 0 my $cache_file = File::Spec->catfile( $cache,
97 md5_hex("$request") . ".$request->{format}" );
98  
99 0 ▶ if ( -r $cache_file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-r $cache_file) { } 0 0 0
100 0 return image_response($cache_file);
101 }
102 else {
103  
104 # TODO: only get image dimensions once and only if actually needed
105 0 my $info = info( $file->{path} );
106 0 ▶ if ( !$request->canonical( $info->{width}, $info->{height} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($request->canonical($info->{"width"}, $info->{"height"})) 0 0 0
107 0 return error_response();
108 }
109  
110 0 my @args = ( $request, $file->{path}, $cache_file );
111 0 ▶ push @args, @{ $self->{magick_args} || [] };
Branch: 1 branch
BranchTrueFalseTotal
$self->{"magick_args"} or [] 0 0 0
112 0 ▶ return image_response($cache_file) if convert(@args);
Branch: 1 branch
BranchTrueFalseTotal
if convert(@args) 0 0 0
113 }
114  
115 0 error_response( 500, "Conversion failed" );
116 }
117  
118 sub info_response {
119 0 ▶ my ( $self, $file ) = @_;
Subroutine info_response: not called CC=3 SCAR=24.8
Pod undocumented
120  
121 my $info = info(
122 $file->{path},
123 id => $file->{id},
124 0 profile => 'level2',
125  
126 # TODO: maxWidth or maxArea, maxHeight (required!)
127  
128 extraQualities => [qw(color gray bitonal default)],
129 extraFormats => $self->formats,
130 extraFeatures => [
131 qw(
132 baseUriRedirect cors jsonldMediaType mirroring
133 profileLinkHeader
134 regionByPct regionByPx regionSquare rotationArbitrary rotationBy90s
135 sizeByConfinedWh sizeByH sizeByPct sizeByW sizeByWh sizeUpscaling
136 )
137 ]
138 );
139  
140 # TODO: canonicalLinkHeader?
141  
142 0 ▶ $info->{rights} = $self->rights if $self->rights;
Branch: 1 branch
BranchTrueFalseTotal
if $self->rights 0 0 0
143 0 ▶ $info->{service} = $self->service if $self->service;
Branch: 1 branch
BranchTrueFalseTotal
if $self->service 0 0 0
144  
145 0 return json_response( 200, $info,
146 'application/ld+json;profile="http://iiif.io/api/image/3/context.json"'
147 );
148 }
149  
150 sub file {
151 0 ▶ my ( $self, $identifier ) = @_;
Subroutine file: not called CC=3 SCAR=24.8
Pod undocumented
152  
153 0 for my $format ( @{ $self->formats } ) {
154 0 my $path = File::Spec->catfile( $self->images, "$identifier.$format" );
155 0 ▶ if ( -r $path ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-r $path) 0 0 0
156 return {
157 0 path => $path,
158 format => $format
159 };
160 }
161 }
162 }
163  
164 sub redirect {
165 0 ▶ return [ 303, [ Location => $_[0] ], [] ];
Subroutine redirect: not called CC=1 SCAR=6.9
Pod undocumented
166 }
167  
168 # adopted from Plack::App::File
169 sub image_response {
170 0 ▶ my ($file) = @_;
Subroutine image_response: not called CC=3 SCAR=24.8
Pod undocumented
171  
172 0 ▶ open my $fh, "<:raw", $file
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $file or return error_response(403, "Forbidden") 0 0 0
173 or return error_response( 403, "Forbidden" );
174  
175 0 ▶ my $type = Plack::MIME->mime_type($file) // 'image';
Condition0%0/2
"Plack::MIME"->mime_type($file) // "image"
l!l
00
MC/DC0%0/1
"Plack::MIME"->mime_type($file) // "image"
"Plack::MIME"->mime_type($file)
Truth table0%0/2
A
A= "Plack::MIME"->mime_type($file)
Aresult
11
00
176 0 my @stat = stat $file;
177  
178 0 Plack::Util::set_io_path( $fh, Cwd::realpath($file) );
179  
180 return [
181 0 200,
182 [
183 'Content-Type' => $type,
184 'Content-Length' => $stat[7],
185 'Last-Modified' => HTTP::Date::time2str( $stat[9] ),
186 'Link' => '<http://iiif.io/api/image/3/level2.json>;rel="profile"'
187 ],
188 $fh,
189 ];
190 }
191  
192 sub json_response {
193 0 ▶ my ( $code, $body, $type ) = @_;
Subroutine json_response: not called CC=2 SCAR=17.9
Pod undocumented
194  
195 0 state $JSON = JSON::PP->new->pretty->canonical(1);
196  
197 [
198 0 ▶ $code,
Condition0%0/2
$type // "application/json"
l!l
00
MC/DC0%0/1
$type // "application/json"
$type
Truth table0%0/2
A
A= $type
Aresult
11
00
199 [
200 'Content-Type' => $type // 'application/json',
201 'Link' => '<http://iiif.io/api/image/3/level2.json>;rel="profile"'
202 ],
203 [ $JSON->encode($body) ]
204 ];
205 }
206  
207 sub error_response {
208 0 ▶ my $code = shift // 400;
Subroutine error_response: not called CC=3 SCAR=24.8
Pod undocumented
Condition0%0/2
shift() // 400
l!l
00
MC/DC0%0/1
shift() // 400
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
209 0 ▶ my $message = shift
Condition0%0/2
shift() // "Invalid IIIF Image API Request: region or size out of bounds"
l!l
00
MC/DC0%0/1
shift() // "Invalid IIIF Image API Request: region or size out of bounds"
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
210 // "Invalid IIIF Image API Request: region or size out of bounds";
211 0 json_response( $code, { message => $message } );
212 }
213  
214 1 1;
215  
216 =head1 NAME
217  
218 IIIF::ImageAPI - IIIF Image API implementation as Plack application
219  
220 =head1 SYNOPSIS
221  
222 use Plack::Builder;
223 use IIIF::ImageAPI;
224  
225 builder {
226 enable 'CrossOrigin', origins => '*';
227 IIIF::ImageAPI->new(
228 images => 'path/to/images',
229 base => 'https://example.org/iiif/',
230 formats => [qw(jpg png gif tif pdf webp jp2)],
231 );
232 }
233  
234 =head1 CONFIGURATION
235  
236 =over
237  
238 =item images
239  
240 Image directory
241  
242 =item cache
243  
244 Cache directory. Set to a temporary per-process directory by default.
245  
246 =item base
247  
248 Base URI which the service is hosted at, including trailing slash. Likely
249 required if the service is put behind a web proxy.
250  
251 =item canonical
252  
253 Redirect requests to the L<canonical URI syntax|https://iiif.io/api/image/3.0/#47-canonical-uri-syntax>
254 and include (disabled by default).
255  
256 =item formats
257  
258 List of supported image formats. Set to C<['jpg', 'png', 'gif']> by default. On
259 configuration with other formats make sure ImageMagick supports them (see
260 L<IIIF::Magick/REQUIREMENTS>).
261  
262 =item rights
263  
264 Optional string that identifies a license or rights statement for all images,
265 to be included in image information responses.
266  
267 =item service
268  
269 Optional array with L<Services|https://iiif.io/api/image/3.0/#58-linking-properties>
270 to be included in image information responses.
271  
272 =item magick_args
273  
274 Additional command line arguments always used when calling ImageMagick. For
275 instance C<[qw(-limit memory 1GB -limit disk 1GB)]> to limit resources.
276  
277 =back
278  
279 =cut
↑ Summary blib/lib/IIIF/Request.pm »