Telegram/InputFile.pm

filter: Statementstmt 28.8% 26 / 90 Branchbran 0.0% 0 / 60 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 56.2% 9 / 16 Podpod 85.7% 6 / 7 totaltotal 21.0% 41 / 195 SCAR 67.5
CC
37
Cov
16%
save_as
182.0
init
132.0
file
56.0
CRAP 852.7
« Telegram/Generic.pm ↑ Summary Telegram/Number.pm »
1 # -*- perl -*-
2 ##----------------------------------------------------------------------------
3 ## Telegram API - ~/lib/Net/API/Telegram/InputFile.pm
4 ## Version 0.1
5 ## Copyright(c) 2019 Jacques Deguest
6 ## Author: Jacques Deguest <jack@deguest.jp>
7 ## Created 2019/05/29
8 ## Modified 2019/10/31
9 ## All rights reserved
10 ##
11 ## This program is free software; you can redistribute it and/or modify it
12 ## under the same terms as Perl itself.
13 ##----------------------------------------------------------------------------
14 package Net::API::Telegram::InputFile;
15 BEGIN
16 {
17 1 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶ use parent qw( Net::API::Telegram::Generic );
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶ use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶ use File::Copy ();
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶ use Cwd ();
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶ use File::Type;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶ use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶ our( $VERSION ) = '0.1';
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶ use Devel::Confess;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 };
27  
28 sub init
29 {
30 0 ▶ my $self = shift( @_ );
Subroutine init: not called CC=11 SCAR=48.8
Pod documented
31 0 my $init = shift( @_ );
32 0 ▶ return( $self->error( sprintf( "Wrong number of parameters. I found %d, but am expecting an odd number.", scalar( @_ ) ) ) ) if( !( scalar( @_ ) % 2 ) );
Branch: 1 branch
BranchTrueFalseTotal
unless scalar(@_) % 2 0 0 0
33 0 ▶ my $this = shift( @_ ) || return( $self->error( "No file content or file path was provided." ) );
Condition0%0/2
shift @_ || (return $self->error('No file content or file path was provided.'))
l!l
00
MC/DC0%0/1
shift @_ || (return $self->error('No file content or file path was provided.'))
shift @_
Truth table0%0/2
A
A= shift @_
Aresult
11
00
34 0 $self->SUPER::init( $init );
35 0 $self->{content} = '';
36 0 $self->{file} = '';
37 0 $self->{filename} = '';
38 0 ▶ if( ref( $this ) eq 'SCALAR' )
Branch: 2 branches
BranchTrueFalseTotal
if (ref $this eq 'SCALAR') { } 0 0 0
elsif (builtin::blessed($this)) { } 0 0 0
39 {
40 0 $self->{content} = $$this;
41 }
42 elsif( Scalar::Util::blessed( $this ) )
43 {
44 0 ▶ return( $self->error( "Do not know what to do with this object \"", ref( $this ), "\". If an object is provided, it should be a Net::API::Telegram::InputFile object." ) ) if( !$this->isa( 'Net::API::Telegram::InputFile' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $this->isa("Net::API::Telegram::InputFile") 0 0 0
45 0 ▶ $self->{content} = $this->{content} if( $this->{content} );
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"content"} 0 0 0
46 0 ▶ $self->{file} = $this->{file} if( $this->{file} );
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"file"} 0 0 0
47 0 ▶ $self->{filename} = $this->{filenme} if( $this->{filename} );
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"filename"} 0 0 0
48 0 ▶ return( $self->error( "The object provided has no file name or file content." ) ) if( !$this->{file} && !length( $this->{content} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $this->{"file"} or CORE::length $this->{"content"} 0 0 0
Condition0%0/3
$this->{"file"} or CORE::length $this->{"content"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$this->{"file"} or CORE::length $this->{"content"}
$this->{"file"} CORE::length $this->{"content"}
Truth table0%0/3
A or B
A= $this->{"file"}B= CORE::length $this->{"content"}
ABresult
1X1
011
000
49 }
50 else
51 {
52 0 $self->{file} = $this;
53 }
54 0 return( $self );
55 }
56  
57 sub content
58 {
59 0 ▶ my $self = shift( @_ );
Subroutine content: not called CC=5 SCAR=34.0
Pod documented
60 0 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
61 {
62 0 $self->{content} = shift( @_ );
63 }
64 0 ▶ return( $self->{content} ) if( length( $self->{content} ) );
Branch: 1 branch
BranchTrueFalseTotal
if CORE::length $self->{"content"} 0 0 0
65 0 ▶ if( $self->{file} )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"file"}) 0 0 0
66 {
67 0 ▶ my $ct = $self->_load_file( $self->{file} ) || return( $self->error( "Unable to load file \"$self->{file}\": $!" ) );
Condition0%0/2
$self->_load_file($self->{'file'}) || (return $self->error(qq[Unable to load file "$self->{'file'}": $!]))
l!l
00
MC/DC0%0/1
$self->_load_file($self->{'file'}) || (return $self->error(qq[Unable to load file "$self->{'file'}": $!]))
$self->_load_file($self->{'file'})
Truth table0%0/2
A
A= $self->_load_file($self->{'file'})
Aresult
11
00
68 0 $self->{content} = $ct;
69 0 return( $ct );
70 }
71 0 return;
72 }
73  
74 sub file
75 {
76 0 ▶ my $self = shift( @_ );
Subroutine file: not called CC=7 SCAR=40.3
Pod documented
77 0 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
78 {
79 0 my $file = shift( @_ );
80 0 ▶ return( $self->error( "File provided \"$file\" does not exist." ) ) if( !-e( $file ) );
Branch: 1 branch
BranchTrueFalseTotal
unless -e $file 0 0 0
81 0 ▶ return( $self->error( "File provided \"$file\" is empty." ) ) if( !-z( $file ) );
Branch: 1 branch
BranchTrueFalseTotal
unless -z $file 0 0 0
82 0 ▶ return( $self->error( "File provided \"$file\" is not a file." ) ) if( !-f( $file ) );
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 0 0 0
83 0 ▶ return( $self->error( "File provided \"$file\" is not readable." ) ) if( !-r( $file ) );
Branch: 1 branch
BranchTrueFalseTotal
unless -r $file 0 0 0
84 0 my( $dir, $base, $suffix ) = File::Basename::fileparse( $file, qr/\.[^\.]+$/ );
85 0 ▶ $self->{filename} = "${base}${suffix}" if( !length( $self->{filename} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless CORE::length $self->{"filename"} 0 0 0
86 0 $self->{file} = $file;
87 }
88 0 return( $self->{file} );
89 }
90  
91 0 ▶sub filename { return( shift->_set_get( 'filename', @_ ) ); }
Subroutine filename: not called CC=1 SCAR=6.9
Pod documented
92  
93 sub length
94 {
95 0 ▶ my $self = shift( @_ );
Subroutine length: not called CC=3 SCAR=24.8
Pod undocumented
96 0 ▶ return( length( $self->{content} ) ) if( $self->{content} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"content"} 0 0 0
97 0 ▶ return( -s( $self->{file} ) ) if( $self->{file} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"file"} 0 0 0
98 0 return( 0 );
99 }
100  
101 sub save_as
102 {
103 0 ▶ my $self = shift( @_ );
Subroutine save_as: not called CC=13 SCAR=52.0
Pod documented
104 0 ▶ my $path = shift( @_ ) || return( $self->error( "No file path to save as was provided." ) );
Condition0%0/2
shift @_ || (return $self->error('No file path to save as was provided.'))
l!l
00
MC/DC0%0/1
shift @_ || (return $self->error('No file path to save as was provided.'))
shift @_
Truth table0%0/2
A
A= shift @_
Aresult
11
00
105 0 $path = Cwd::abs_path( $path );
106 0 ▶ return( $self->error( "No file or file content set to save." ) ) if( !$self->{file} && !length( $self->{content} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"file"} or CORE::length $self->{"content"} 0 0 0
Condition0%0/3
$self->{"file"} or CORE::length $self->{"content"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"file"} or CORE::length $self->{"content"}
$self->{"file"} CORE::length $self->{"content"}
Truth table0%0/3
A or B
A= $self->{"file"}B= CORE::length $self->{"content"}
ABresult
1X1
011
000
107 0 my( $dir, $base, $suffix ) = File::Basename::fileparse( $path, qr/\.[^\.]+$/ );
108 0 ▶ return( $self->error( "File directory \"$path\" does not exist." ) ) if( !-e( $dir ) );
Branch: 1 branch
BranchTrueFalseTotal
unless -e $dir 0 0 0
109 0 ▶ return( $self->error( "File directory \"$path\" exists, but it is not a directory." ) ) if( !-d( $dir ) );
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 0 0
110 0 ▶ return( $self->error( "File directory \"$path\" is not accessible. Not enoug permission to enter it." ) ) if( !-x( $dir ) );
Branch: 1 branch
BranchTrueFalseTotal
unless -x $dir 0 0 0
111 0 ▶ if( $self->{content} )
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'content'}) { } 0 0 0
elsif ($self->{'file'}) { } 0 0 0
112 {
113 0 ▶ my $fh = IO::File->new( ">$path" ) || return( $self->error( "Unable to open file \"$path\" in write mode: $!" ) );
Condition0%0/2
'IO::File'->new(">$path") || (return $self->error(qq[Unable to open file "$path" in write mode: $!]))
l!l
00
MC/DC0%0/1
'IO::File'->new(">$path") || (return $self->error(qq[Unable to open file "$path" in write mode: $!]))
'IO::File'->new(">$path")
Truth table0%0/2
A
A= 'IO::File'->new(">$path")
Aresult
11
00
114 0 $fh->binmode;
115 0 $fh->autoflush( 1 );
116 0 ▶ $fh->print( $self->{content} ) || return( $self->error( sprintf( "Unable to write %d bytes of data into file \"$path\": $!", length( $self->{content} ) ) ) );
Branch: 1 branch
BranchTrueFalseTotal
$fh->print($self->{"content"}) or return $self->error(sprintf(qq[Unable to write %d bytes of data into file "$path": $!], CORE::length $self->{"content"})) 0 0 0
117 0 $fh->close;
118 }
119 elsif( $self->{file} )
120 {
121 0 ▶ return( $self->error( "Source and target file \"$path\" are identical." ) ) if( $path eq $self->{file} );
Branch: 1 branch
BranchTrueFalseTotal
if $path eq $self->{"file"} 0 0 0
122 0 ▶ File::Copy::copy( $self->{file}, $path ) ||
Branch: 1 branch
BranchTrueFalseTotal
File::Copy::copy($self->{"file"}, $path) or return $self->error(qq[Unable to copy file "$self->{"file"}" to "$path": #!]) 0 0 0
123 return( $self->error( "Unable to copy file \"$self->{file}\" to \"$path\": #!" ) );
124 }
125 }
126  
127 sub type
128 {
129 0 ▶ my $self = shift( @_ );
Subroutine type: not called CC=3 SCAR=24.8
Pod documented
130 0 my $t = File::Type->new;
131 0 ▶ if( $self->{content} )
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'content'}) { } 0 0 0
elsif ($self->{'file'}) { } 0 0 0
132 {
133 0 return( $t->mime_type( $self->{content} ) );
134 }
135 elsif( $self->{file} )
136 {
137 0 return( $t->mime_type( $self->{file} ) );
138 }
139 else
140 {
141 ## Return empty, not undef
142 0 return( '' );
143 }
144 }
145  
146 1 1;
147  
148 __END__
149  
150 =encoding utf-8
151  
152 =head1 NAME
153  
154 Net::API::Telegram::InputFile - The contents of a file to be uploaded
155  
156 =head1 SYNOPSIS
157  
158 my $msg = Net::API::Telegram::InputFile->new( %data ) ||
159 die( Net::API::Telegram::InputFile->error, "\n" );
160  
161 =head1 DESCRIPTION
162  
163 L<Net::API::Telegram::InputFile> is a Telegram Message Object as defined here L<https://core.telegram.org/bots/api#inputfile>
164  
165 =head1 METHODS
166  
167 =over 4
168  
169 =item B<new>( {INIT HASH REF}, SCALAR REF | FILE PATH, %PARAMETERS )
170  
171 B<new>() will create a new object for the package, pass any argument it might receive
172 to the special standard routine B<init> that I<must> exist.
173 It takes one mandatory parameter which is either a scalar for raw data or a file path.
174  
175 Then it returns what returns B<init>().
176  
177 The valid parameters are as follow. Methods available here are also parameters to the B<new> method.
178  
179 =over 8
180  
181 =item * I<verbose>
182  
183 =item * I<debug>
184  
185 =back
186  
187 =item B<content>( [ DATA ] )
188  
189 Set or get the raw data for this object.
190  
191 If no data was set, but a I<file> was set then this will read the file and return its content.
192  
193 It returns the current content set.
194  
195 =item B<file>( [ FILE PATH ] )
196  
197 Set or get the file path of the file for this object.
198  
199 This method will perform some basic sanitary checks on the accessibility of the given file path, and its permissions and return error if the file has problems.
200  
201 It returns the current file set.
202  
203 =item B<save_as>( FILE PATH )
204  
205 Given a file path, this method will save the content of the file in this object to the specified file path.
206  
207 Before doing so, this method will perform some sanity check on the parent directory to ensure the action can actually be done. It will return an error if problems were found.
208  
209 =back
210  
211 =head1 COPYRIGHT
212  
213 Copyright (c) 2000-2019 DEGUEST Pte. Ltd.
214  
215 =head1 CREDITS
216  
217 Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
218  
219 =head1 SEE ALSO
220  
221 L<Net::API::Telegram>
222  
223 =cut
224  
« Telegram/Generic.pm ↑ Summary Telegram/Number.pm »