Mojolicious/Plugin/Gallery.pm

filter: Statementstmt 90.0% 18 / 20 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 92.5% 25 / 27 CC 1 SCAR 0.4
CC
1
Cov
90%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Command/resize.pm ↑ Summary
1 package Mojolicious::Plugin::Gallery;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use 5.008_005;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 our $VERSION = '0.10';
6  
7 1 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Mojo::File 'path';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use YAML::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 0 ▶use DDP;
Subroutine BEGIN: called CC=1 SCAR=2.6
12  
13 sub register {
14 my ($self, $app) = @_;
15 my $config = $app->config->{gallery};
16  
17 my $path = path $config->{main_path};
18 my $galleries = [];
19  
20 my $items = $path->list({dir => 1});
21 for my $gallery ($items->each) {
22 my $meta_source = $gallery->list->first(qr/yml|yaml/);
23 die 'meta is undef' unless $meta_source;
24 my $meta = Load $meta_source->slurp;
25 next unless $meta->{show};
26  
27 my $gallery_name = $gallery->basename;
28 my $gallery_path = $gallery->to_string;
29 my $photos_path = sprintf '%s/large', $gallery->to_string;
30  
31 my $photos = path($photos_path)->list->grep(qr/jpg|png/);
32 my $text = '';
33 my $photo_items;
34 for my $photo ($photos->each) {
35 my $filename = $photo->basename;
36 my $gallery_path_withot_public = $gallery_path;
37  
38 # some Workaround. Fix it
39 $gallery_path_withot_public =~ s/public\///;
40 say $gallery_path_withot_public;
41  
42 push @$photo_items, {
43 large => sprintf('/%s/large/%s', $gallery_path_withot_public, $filename),
44 medium => sprintf('/%s/medium/%s', $gallery_path_withot_public, $filename),
45 thumbnail => sprintf('/%s/thumbnail/%s', $gallery_path_withot_public, $filename),
46 };
47 }
48  
49 my $gallery_url = sprintf 'photos/%s', $meta->{url} || $gallery_name;
50  
51 my $c = $app->build_controller;
52 $app->routes->get($gallery_url)->to(cb => sub {
53 my $c = shift;
54 $c->render(template => 'gallery_item',
55 page_title => $meta->{title},
56 photos => $photo_items,
57 meta => $meta,
58 );
59 });
60  
61 push @$galleries, {
62 meta => $meta,
63 url => $gallery_url,
64 photos => [ splice(@$photo_items, 0, 3) ],
65 };
66 }
67  
68 $app->routes->get('/gallery')->to(cb => sub {
69 my $c = shift;
70 $c->render(template => 'gallery_list',
71 page_title => 'Somoe Photos',
72 galleries => $galleries,
73 );
74 });
75  
76 }
77  
78 1;
79 __END__
« Command/resize.pm ↑ Summary