File Coverage

blib/lib/App/Sky/Manager.pm
Criterion Covered Total %
statement 66 69 95.6
branch 14 18 77.7
condition n/a
subroutine 17 17 100.0
pod 2 2 100.0
total 99 106 93.4


line stmt bran cond sub pod time code
1             package App::Sky::Manager;
2             $App::Sky::Manager::VERSION = '0.4.0';
3 1     1   87013 use strict;
  1         13  
  1         30  
4 1     1   4 use warnings;
  1         2  
  1         25  
5              
6              
7 1     1   4 use Carp ();
  1         2  
  1         14  
8              
9 1     1   524 use Moo;
  1         11338  
  1         4  
10 1     1   1817 use MooX 'late';
  1         10164  
  1         6  
11              
12 1     1   25332 use List::Util qw(first);
  1         3  
  1         118  
13              
14 1     1   554 use URI;
  1         4711  
  1         34  
15 1     1   6 use File::Basename qw(basename);
  1         3  
  1         84  
16              
17 1     1   518 use App::Sky::Module;
  1         5  
  1         37  
18              
19             # For defined-or - "//".
20 1     1   18 use 5.010;
  1         4  
21              
22             has config => ( isa => 'HashRef', is => 'ro', );
23              
24              
25             sub _calc_site_conf
26             {
27 15     15   25 my ( $self, $args ) = @_;
28              
29 15         30 my $config = $self->config;
30              
31 15         39 return $config->{sites}->{ $config->{default_site} };
32             }
33              
34             sub _calc_sect_name
35             {
36 6     6   13 my ( $self, $args, $sections ) = @_;
37              
38 6         9 my $bn = $args->{basename};
39              
40 6         12 my $sect_name = $args->{section};
41              
42 6 100       15 if ( !defined($sect_name) )
43             {
44 5 100       13 if ( $args->{is_dir} )
45             {
46 2         6 $sect_name = $self->_calc_site_conf($args)->{dirs_section};
47             }
48             else
49             {
50             $sect_name = (
51             first
52             {
53 7     7   22 my $re = $sections->{$_}->{basename_re};
54 7         191 $bn =~ /$re/;
55             }
56 3         22 ( keys(%$sections) )
57             );
58             }
59             }
60              
61 6 50       28 if ( !defined($sect_name) )
62             {
63 0         0 Carp::confess("Unknown section for basename '$bn'");
64             }
65              
66 6 50       15 if ( !exists( $sections->{$sect_name} ) )
67             {
68 0         0 Carp::confess("Section '$sect_name' does not exist.");
69             }
70              
71 6         13 return $sect_name;
72             }
73              
74             sub _calc_target_dir
75             {
76 7     7   16 my ( $self, $args ) = @_;
77              
78 7 100       17 if ( defined( $args->{target_dir} ) )
79             {
80 1         5 return $args->{target_dir};
81             }
82             else
83             {
84 6         19 my $sections = $self->_calc_site_conf($args)->{sections};
85              
86 6         16 my $sect_name = $self->_calc_sect_name( $args, $sections );
87              
88 6         36 return $sections->{$sect_name}->{target_dir};
89             }
90             }
91              
92             sub _perform_upload_generic
93             {
94 7     7   16 my ( $self, $is_dir, $args ) = @_;
95              
96             my $filenames = $args->{filenames}
97 7 50       20 or Carp::confess("Missing argument 'filenames'");
98              
99 7 50       23 if ( @$filenames != 1 )
100             {
101 0         0 Carp::confess("More than one file passed to 'filenames'");
102             }
103              
104 7         15 my $site_conf = $self->_calc_site_conf($args);
105              
106             my $backend = App::Sky::Module->new(
107             {
108             base_upload_cmd => $site_conf->{base_upload_cmd},
109             dest_upload_prefix => $site_conf->{dest_upload_prefix},
110             dest_upload_url_prefix => $site_conf->{dest_upload_url_prefix},
111             }
112 7         187 );
113              
114 7         3282 my $fn = $filenames->[0];
115 7         279 my $bn = basename($fn);
116              
117 7 100       30 my @dir = ( $is_dir ? ( is_dir => 1 ) : () );
118              
119             return $backend->get_upload_results(
120             {
121              
122             filenames => (
123             $is_dir
124 7 100       35 ? [ map { my $s = $_; $s =~ s#/+\z##ms; $s } @$filenames ]
  2         3  
  2         10  
  2         13  
125             : $filenames,
126             ),
127             target_dir => $self->_calc_target_dir(
128             {
129             %$args,
130             basename => $bn,
131             @dir,
132             }
133             ),
134             @dir,
135             }
136             );
137              
138             }
139              
140              
141             sub get_upload_results
142             {
143 5     5 1 27131 my ( $self, $args ) = @_;
144              
145 5         13 return $self->_perform_upload_generic( 0, $args );
146             }
147              
148              
149             sub get_recursive_upload_results
150             {
151 2     2 1 5827 my ( $self, $args ) = @_;
152              
153 2         7 return $self->_perform_upload_generic( 1, $args );
154             }
155              
156             1;
157              
158             __END__