File Coverage

lib/App/Followme/UploadLocal.pm
Criterion Covered Total %
statement 53 53 100.0
branch 3 6 50.0
condition 2 6 33.3
subroutine 15 15 100.0
pod 7 7 100.0
total 80 87 91.9


line stmt bran cond sub pod time code
1             package App::Followme::UploadLocal;
2              
3 2     2   707 use 5.008005;
  2         8  
4 2     2   17 use strict;
  2         5  
  2         57  
5 2     2   11 use warnings;
  2         4  
  2         54  
6              
7 2     2   515 use lib '../..';
  2         677  
  2         9  
8              
9 2     2   286 use base qw(App::Followme::ConfiguredObject);
  2         5  
  2         571  
10              
11 2     2   1171 use File::Copy;
  2         4837  
  2         117  
12 2     2   62 use File::Path qw(remove_tree);
  2         8  
  2         135  
13 2     2   14 use File::Spec::Functions qw(abs2rel splitdir catfile);
  2         4  
  2         982  
14              
15             our $VERSION = "2.03";
16              
17             #----------------------------------------------------------------------
18             # Read the default parameter values
19              
20             sub parameters {
21 12     12 1 20 my ($pkg) = @_;
22              
23             return (
24 12         36 remote_directory => '',
25             permissions => 0644,
26             );
27             }
28              
29             #----------------------------------------------------------------------
30             # Add a directory to the remote site
31              
32             sub add_directory {
33 2     2 1 7 my ($self, $dir) = @_;
34              
35 2         13 my $new_dir = catfile($self->{remote_directory}, $dir);
36 2         143 my $status = mkdir($new_dir);
37              
38 2 50       27 if ($status) {
39 2         7 my $permissions = $self->{permissions} | 0111;
40 2         45 chmod($permissions, $new_dir);
41             }
42              
43 2         14 return $status;
44             }
45              
46             #----------------------------------------------------------------------
47             # Add a file to the remote site
48              
49             sub add_file {
50 8     8 1 2166 my ($self, $local_filename, $remote_filename) = @_;
51              
52 8         38 my $new_file = catfile($self->{remote_directory}, $remote_filename);
53 8         27 my $status = copy($local_filename, $new_file);
54              
55 8 50       2797 chmod($self->{permissions}, $new_file) if $status;
56 8         49 return $status;
57             }
58              
59             #----------------------------------------------------------------------
60             # Close the connection
61              
62             sub close {
63 1     1 1 782 my ($self) = @_;
64 1         156 return;
65             }
66              
67             #----------------------------------------------------------------------
68             # Delete a directory from the remote site
69              
70             sub delete_directory {
71 1     1 1 797 my ($self, $dir) = @_;
72              
73 1         3 my $err;
74 1         7 my $new_dir = catfile($self->{remote_directory}, $dir);
75 1         281 remove_tree($new_dir, {error => $err});
76              
77 1   33     8 my $status = ! ($err && @$err);
78 1         5 return $status;
79             }
80              
81             #----------------------------------------------------------------------
82             # Delete a file from the remote site
83              
84             sub delete_file {
85 1     1 1 791 my ($self, $filename) = @_;
86              
87 1         8 my $new_file = catfile($self->{remote_directory}, $filename);
88 1         465 my $status = unlink($new_file);
89              
90 1         10 return $status;
91             }
92              
93             #----------------------------------------------------------------------
94             # Open the connection to the remote site
95              
96             sub open {
97 1     1 1 5 my ($self, $user, $password) = @_;
98              
99             # Check existence of remote directory
100 1   33     29 my $found = $self->{remote_directory} && -e $self->{remote_directory};
101              
102 1 50       4 die "Could not find remote_directory: $self->{remote_directory}"
103             unless $found;
104              
105 1         4 return;
106             }
107              
108             1;
109             __END__
110             =encoding utf-8
111              
112             =head1 NAME
113              
114             App::Followme::UploadLocal - Upload files through file copy
115              
116             =head1 SYNOPSIS
117              
118             my $uploader = App::Followme::UploadLocal->new(\%configuration);
119             $uploader->open();
120             $uploader->add_directory($dir);
121             $uploader->add_file($filename);
122             $uploader->delete_directory($dir);
123             $uploader->delete_file($filename);
124             $uploader->close();
125              
126             =head1 DESCRIPTION
127              
128             L<App::Followme::UploadSite> splits off methods that do the actual uploading
129             into a separate package, so it can support more than one method. This package
130             uploads files to the server using a simple file copy.
131              
132             =head1 METHODS
133              
134             The following are the public methods of the interface. The return value
135             indicates if the operation was successful.
136              
137             =over 4
138              
139             =item $flag = $self->add_directory($dir);
140              
141             Create a new directory
142              
143             =item $flag = $self->add_file($filename);
144              
145             Upload a new file. If it already exists, delete it.
146              
147             =item $self->close();
148              
149             Close the connection to the remote site.
150              
151             =item $flag = $self->delete_directory($dir);
152              
153             Delete a directory, including any files it might hold.
154              
155             =item $flag = $self->delete_file($filename);
156              
157             Delete a file on the remote site.
158              
159             =item $self->open();
160              
161             Open the connection to the remote site
162              
163             =item $self->setup();
164              
165             Set up computed fields in the new object
166              
167             =back
168              
169             =head1 CONFIGURATION
170              
171             The following parameters are used from the configuration.
172              
173             =over 4
174              
175             =item remote_directory
176              
177             The top directory of the website the files are being copied to
178              
179             =item permissions
180              
181             The permissions to put on the remote file.
182              
183             =back
184              
185             =head1 LICENSE
186              
187             Copyright (C) Bernie Simon.
188              
189             This library is free software; you can redistribute it and/or modify
190             it under the same terms as Perl itself.
191              
192             =head1 AUTHOR
193              
194             Bernie Simon E<lt>bernie.simon@gmail.comE<gt>
195              
196             =cut