lib/HTML/Object/DOM/File.pm

filter: Statementstmt 51.6% 32 / 62 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 55.5% 10 / 18 Podpod 100% 9 / 9 totaltotal 47.2% 51 / 108 CC 10 SCAR 37.3
CC
10
Cov
40%
slice
110.0
arrayBuffer
2.0
lastModifiedDate
2.0
CRAP 41.6
« blib/lib/HTML/Object.pm ↑ Summary lib/HTML/Object/XPath/Step.pm »
1 ##----------------------------------------------------------------------------
2 ## HTML Object - ~/lib/HTML/Object/DOM/File.pm
3 ## Version v0.2.0
4 ## Copyright(c) 2021 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2021/12/25
7 ## Modified 2022/09/18
8 ## All rights reserved
9 ##
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 HTML::Object::DOM::File;
15 BEGIN
16 {
17 2 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶ warnings::register_categories( 'HTML::Object' );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶ use parent qw( Module::Generic::File );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶ use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶ use DateTime::Format::Strptime;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶ use Module::Generic::Scalar;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 our $VERSION = 'v0.2.0';
25 };
26  
27 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 sub arrayBuffer
31 {
32 0 ▶ my $self = shift( @_ );
Subroutine arrayBuffer: not called CC=1 SCAR=6.9
Pod documented
33 0 my $data = $self->load({ binmode => 'raw' });
34 0 return( Module::Generic::Scalar->new( \$data ) );
35 }
36  
37 sub lastModified
38 {
39 1 ▶ my $self = shift( @_ );
Subroutine lastModified: called CC=1 SCAR=0.0
Pod documented
40 1 my $dt = $self->mtime;
41 1 my $fmt = DateTime::Format::Strptime->new( pattern => '%s' );
42 1 $dt->set_formatter( $fmt );
43 1 return( $dt );
44 }
45  
46 0 ▶sub lastModifiedDate { return( shift->mtime ); }
Subroutine lastModifiedDate: not called CC=1 SCAR=6.9
Pod documented
47  
48 0 ▶sub name { return( shift->basename ); }
Subroutine name: not called CC=1 SCAR=6.9
Pod documented
49  
50 # Note: sub size is inherited
51  
52 sub slice
53 {
54 0 ▶ my $self = shift( @_ );
Subroutine slice: not called CC=10 SCAR=47.0
Pod documented
55 0 my( $start, $end ) = @_;
56 0 ▶ return( $self->error( "start value provided is not an integer." ) ) if( !$self->_is_integer( $start ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_integer($start) 0 0 0
57 0 ▶ $end = $self->size if( !$self->_is_integer( $end ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_integer($end) 0 0 0
58 0 ▶ return( Module::Generic::Scalar->new ) if( $end <= $start );
Branch: 1 branch
BranchTrueFalseTotal
if $end <= $start 0 0 0
59 0 my $len = $end - $start;
60 0 my $opened = $self->opened;
61 0 my $io;
62 my $pos;
63 0 ▶ if( $opened )
Branch: 1 branch
BranchTrueFalseTotal
if ($opened) { } 0 0 0
64 {
65 0 $io = $opened;
66 0 $pos = $io->seek(0,1);
67 }
68 else
69 {
70 0 ▶ $io = $self->open({ binmode => 'utf-8' }) || return( $self->pass_error );
Condition0%0/2
$self->open({'binmode', 'utf-8'}) || (return $self->pass_error)
l!l
00
MC/DC0%0/1
$self->open({'binmode', 'utf-8'}) || (return $self->pass_error)
$self->open({'binmode', 'utf-8'})
Truth table0%0/2
A
A= $self->open({'binmode', 'utf-8'})
Aresult
11
00
71 }
72 0 ▶ $io->seek( $start, 0 ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$io->seek($start, 0) or return $self->pass_error 0 0 0
73 0 my $data;
74 0 ▶ $self->read( $data, $len ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->read($data, $len) or return $self->pass_error 0 0 0
75 0 ▶ if( $opened )
Branch: 1 branch
BranchTrueFalseTotal
if ($opened) { } 0 0 0
76 {
77 0 ▶ $io->seek( $pos, 0 ) if( defined( $pos ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $pos 0 0 0
78 }
79 else
80 {
81 0 $self->close;
82 }
83 0 return( Module::Generic::Scalar->new( \$data ) );
84 }
85  
86 0 ▶sub stream { return( shift->open( @_ ) ); }
Subroutine stream: not called CC=1 SCAR=6.9
Pod documented
87  
88 sub text
89 {
90 0 ▶ my $self = shift( @_ );
Subroutine text: not called CC=1 SCAR=6.9
Pod documented
91 0 my $data = $self->load_utf8;
92 0 return( Module::Generic::Scalar->new( \$data ) );
93 }
94  
95 0 ▶sub type { return( shift->finfo->mime_type ); }
Subroutine type: not called CC=1 SCAR=6.9
Pod documented
96  
97 0 ▶sub webkitRelativePath { return( shift->relative ); }
Subroutine webkitRelativePath: not called CC=1 SCAR=6.9
Pod documented
98  
99 2 1;
100 # NOTE: POD
101 __END__
102  
103 =encoding utf-8
104  
105 =head1 NAME
106  
107 HTML::Object::DOM::File - HTML Object DOM File Class
108  
109 =head1 SYNOPSIS
110  
111 use HTML::Object::DOM::File;
112 my $file = HTML::Object::DOM::File->new ||
113 die( HTML::Object::DOM::File->error, "\n" );
114  
115 =head1 VERSION
116  
117 v0.2.0
118  
119 =head1 DESCRIPTION
120  
121 The C<File> interface provides information about files and allows access to their content.
122  
123 File objects are generally retrieved from a L<HTML::Object::DOM::FileList> object returned using the C<<input>> L<files|HTML::Object::DOM::Element::Input/files> method.
124  
125 It inherits from L<Module::Generic::File>
126  
127 =head1 PROPERTIES
128  
129 =head2 lastModified
130  
131 Read-only.
132  
133 Returns the last modified time of the file, in second since the UNIX epoch (January 1st, 1970 at Midnight), as a L<Module::Generic::DateTime> object. The DateTime object stringifies to the seconds since epoch.
134  
135 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/File/lastModified>
136  
137 =head2 lastModifiedDate
138  
139 Read-only.
140  
141 Returns the last modified date and time of the file referenced by the file object, as a L<Module::Generic::DateTime> object.
142  
143 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/File/lastModifiedDate>
144  
145 =head2 name
146  
147 Read-only.
148  
149 Returns the name of the file referenced by the file object.
150  
151 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/File/name>
152  
153 =head2 webkitRelativePath
154  
155 Read-only.
156  
157 Returns the relative file path.
158  
159 Normally under JavaScript, this works alongside the C<<input>> attribute C<webkitdirectory>:
160  
161 <input type="file" webkitdirectory />
162  
163 allowing a user to select an entire directory instead of just files. So, C<webkitRelativePath> provide the relative file path to that directory uploaded.
164  
165 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/File/webkitRelativePath>
166  
167 =head2 size
168  
169 Read-only.
170  
171 Returns the size of the file in bytes.
172  
173 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/File/size>
174  
175 =head2 type
176  
177 Read-only.
178  
179 Returns the MIME type of the file, or C<undef> if it cannot find it.
180  
181 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/File/type>
182  
183 =head1 METHODS
184  
185 =head2 arrayBuffer
186  
187 Opens the file as C<raw> data and returns its content as a L<scalar object|Module::Generic::Scalar>.
188  
189 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Blob/arrayBuffer>
190  
191 =head2 slice
192  
193 Provided with a C<start> and an C<end> as a range, and an optional encoding and this will return that range of data from the file, as a L<scalar object|Module::Generic::Scalar>. If no encoding is provided, this will default to C<utf-8>
194  
195 If you specify a negative C<start>, it is treated as an offset from the end of the file's data toward the beginning. For example, C<-10> would be the C<10th> from last byte in the file data. The default value is C<0>. If you specify a value for C<start> that is larger than the size of the file, the returned L<scalar object|Module::Generic::Scalar> has size 0 and contains no data.
196  
197 The C<end> specifies the offset (not the length) of the last byte, without including it, to include in the returned data. If you specify a negative C<end>, it is treated as an offset from the end of the data toward the beginning. For example, C<-10> would be the C<10th> from last byte in the file's data. The default value is the file C<size>, i.e. until the end of the file's data.
198  
199 Returns a new L<scalar object|Module::Generic::Scalar> containing the data in the specified range of bytes of the file.
200  
201 =head2 stream
202  
203 This opens the file and returns its file handle to read the file's contents. You could also do:
204  
205 my $io = $file->open || die( $file->error );
206  
207 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Blob/stream>
208  
209 =head2 text
210  
211 Opens the file in C<utf-8> and returns its content as a L<scalar object|Module::Generic::Scalar>.
212  
213 See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Blob/text>
214  
215 =head1 AUTHOR
216  
217 Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
218  
219 =head1 SEE ALSO
220  
221 L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/File>
222  
223 =head1 COPYRIGHT & LICENSE
224  
225 Copyright(c) 2021 DEGUEST Pte. Ltd.
226  
227 All rights reserved
228  
229 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
230  
231 =cut
« blib/lib/HTML/Object.pm ↑ Summary lib/HTML/Object/XPath/Step.pm »