File Coverage

blib/lib/CPAN/Upload/Tiny.pm
Criterion Covered Total %
statement 37 56 66.0
branch 9 26 34.6
condition 2 6 33.3
subroutine 8 11 72.7
pod 3 4 75.0
total 59 103 57.2


line stmt bran cond sub pod time code
1             package CPAN::Upload::Tiny;
2             $CPAN::Upload::Tiny::VERSION = '0.008';
3 1     1   73810 use strict;
  1         2  
  1         24  
4 1     1   4 use warnings;
  1         2  
  1         20  
5              
6 1     1   4 use Carp ();
  1         1  
  1         11  
7 1     1   3 use File::Basename ();
  1         2  
  1         9  
8 1     1   388 use MIME::Base64 ();
  1         551  
  1         20  
9 1     1   567 use HTTP::Tiny;
  1         40508  
  1         34  
10 1     1   370 use HTTP::Tiny::Multipart;
  1         892  
  1         557  
11              
12             my $UPLOAD_URI = $ENV{CPAN_UPLOADER_UPLOAD_URI} || 'https://pause.perl.org/pause/authenquery?ACTION=add_uri';
13              
14             sub new {
15 0     0 1 0 my ($class, $user, $password) = @_;
16 0         0 return bless {
17             user => $user,
18             password => $password,
19             }, $class;
20             }
21              
22             sub new_from_config {
23 0     0 1 0 my ($class, $filename) = @_;
24 0         0 my $config = read_config_file($filename);
25 0         0 bless $config, $class;
26             }
27              
28             sub upload_file {
29 0     0 1 0 my ($self, $filename) = @_;
30              
31 0 0       0 open my $fh, '<:raw', $filename or die "Could not open $filename: $!";
32 0         0 my $content = do { local $/; <$fh> };
  0         0  
  0         0  
33              
34 0         0 my $tiny = HTTP::Tiny->new(verify_SSL => 1);
35              
36 0         0 my $auth = 'Basic ' . MIME::Base64::encode("$self->{user}:$self->{password}", '');
37              
38             my $result = $tiny->post_multipart($UPLOAD_URI, {
39             HIDDENNAME => $self->{user},
40 0         0 CAN_MULTIPART => 1,
41             pause99_add_uri_httpupload => {
42             filename => File::Basename::basename($filename),
43             content => $content,
44             content_type => 'application/gzip',
45             },
46             pause99_add_uri_uri => '',
47             SUBMIT_pause99_add_uri_httpupload => ' Upload this file from my disk ',
48             }, { headers => { Authorization => $auth } });
49              
50 0 0       0 if (!$result->{success}) {
51 0 0       0 my $key = $result->{status} == 599 ? 'content' : 'reason';
52 0         0 die "Upload failed: $result->{$key}\n";
53             }
54              
55 0         0 return;
56             }
57              
58             sub read_config_file {
59 1   33 1 0 672 my $filename = shift || glob('~/.pause');
60 1 50       14 die 'Missing configuration file' unless -r $filename;
61              
62 1         3 my %conf;
63 1 50       2 if ( eval { require Config::Identity } ) {
  1         171  
64 0         0 %conf = Config::Identity->load($filename);
65 0 0       0 $conf{user} = delete $conf{username} unless $conf{user};
66             }
67             else { # Process .pause manually
68 1 50       31 open my $pauserc, '<', $filename or die "can't open $filename for reading: $!";
69              
70 1         13 while (<$pauserc>) {
71 2         5 chomp;
72 2 50       5 Carp::croak "$filename seems to be encrypted. Maybe you need to install Config::Identity?" if /BEGIN PGP MESSAGE/;
73              
74 2 50 33     10 next if not length or $_ =~ /^\s*#/;
75              
76 2 50       16 if (my ($k, $v) = / ^ \s* (user|password) \s+ (.+?) \s* $ /x) {
77 2 50       12 Carp::croak "Multiple entries for $k" if $conf{$k};
78 2         21 $conf{$k} = $v;
79             }
80             }
81             }
82 1 50       6 Carp::croak('No user set in configuration file') if not $conf{user};
83 1 50       3 Carp::croak('No password set in configuration file') if not $conf{password};
84              
85 1         4 return \%conf;
86             }
87              
88             1;
89              
90             #ABSTRACT: A tiny CPAN uploader
91              
92             __END__