File Coverage

lib/HTML/Object/DOM/File.pm
Criterion Covered Total %
statement 57 60 95.0
branch 7 16 43.7
condition 1 2 50.0
subroutine 18 18 100.0
pod 9 9 100.0
total 92 105 87.6


line stmt bran cond sub pod time code
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     2   3410 use strict;
  2         5  
  2         74  
18 2     2   11 use warnings;
  2         4  
  2         64  
19 2     2   11 use parent qw( Module::Generic::File );
  2         6  
  2         11  
20 2     2   151 use vars qw( $VERSION );
  2         5  
  2         83  
21 2     2   1371 use DateTime::Format::Strptime;
  2         983808  
  2         11  
22 2     2   214 use Module::Generic::Scalar;
  2         6  
  2         129  
23 2     2   45 our $VERSION = 'v0.2.0';
24             };
25              
26 2     2   17 use strict;
  2         10  
  2         56  
27 2     2   17 use warnings;
  2         4  
  2         1165  
28              
29             sub arrayBuffer
30             {
31 1     1 1 848 my $self = shift( @_ );
32 1         7 my $data = $self->load({ binmode => 'raw' });
33 1         2464 return( Module::Generic::Scalar->new( \$data ) );
34             }
35              
36             sub lastModified
37             {
38 1     1 1 142766 my $self = shift( @_ );
39 1         46 my $dt = $self->mtime;
40 1         26862 my $fmt = DateTime::Format::Strptime->new( pattern => '%s' );
41 1         1479 $dt->set_formatter( $fmt );
42 1         172 return( $dt );
43             }
44              
45 1     1 1 11 sub lastModifiedDate { return( shift->mtime ); }
46              
47 1     1 1 4658 sub name { return( shift->basename ); }
48              
49             # Note: sub size is inherited
50              
51             sub slice
52             {
53 1     1 1 45164 my $self = shift( @_ );
54 1         5 my( $start, $end ) = @_;
55 1 50       26 return( $self->error( "start value provided is not an integer." ) ) if( !$self->_is_integer( $start ) );
56 1 50       25 $end = $self->size if( !$self->_is_integer( $end ) );
57 1 50       20 return( Module::Generic::Scalar->new ) if( $end <= $start );
58 1         7 my $len = $end - $start;
59 1         17 my $opened = $self->opened;
60 1         1339 my $io;
61             my $pos;
62 1 50       67 if( $opened )
63             {
64 0         0 $io = $opened;
65 0         0 $pos = $io->seek(0,1);
66             }
67             else
68             {
69 1   50     19 $io = $self->open({ binmode => 'utf-8' }) || return( $self->pass_error );
70             }
71 1 50       6825 $io->seek( $start, 0 ) || return( $self->pass_error );
72 1         131 my $data;
73 1 50       373 $self->read( $data, $len ) || return( $self->pass_error );
74 1 50       1160 if( $opened )
75             {
76 0 0       0 $io->seek( $pos, 0 ) if( defined( $pos ) );
77             }
78             else
79             {
80 1         16 $self->close;
81             }
82 1         1877 return( Module::Generic::Scalar->new( \$data ) );
83             }
84              
85 1     1 1 1443 sub stream { return( shift->open( @_ ) ); }
86              
87             sub text
88             {
89 1     1 1 4771 my $self = shift( @_ );
90 1         24 my $data = $self->load_utf8;
91 1         2876 return( Module::Generic::Scalar->new( \$data ) );
92             }
93              
94 1     1 1 40359 sub type { return( shift->finfo->mime_type ); }
95              
96 1     1 1 1282 sub webkitRelativePath { return( shift->relative ); }
97              
98             1;
99             # NOTE: POD
100             __END__
101              
102             =encoding utf-8
103              
104             =head1 NAME
105              
106             HTML::Object::DOM::File - HTML Object DOM File Class
107              
108             =head1 SYNOPSIS
109              
110             use HTML::Object::DOM::File;
111             my $file = HTML::Object::DOM::File->new ||
112             die( HTML::Object::DOM::File->error, "\n" );
113              
114             =head1 VERSION
115              
116             v0.2.0
117              
118             =head1 DESCRIPTION
119              
120             The C<File> interface provides information about files and allows access to their content.
121              
122             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.
123              
124             It inherits from L<Module::Generic::File>
125              
126             =head1 PROPERTIES
127              
128             =head2 lastModified
129              
130             Read-only.
131              
132             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.
133              
134             See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/File/lastModified>
135              
136             =head2 lastModifiedDate
137              
138             Read-only.
139              
140             Returns the last modified date and time of the file referenced by the file object, as a L<Module::Generic::DateTime> object.
141              
142             See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/File/lastModifiedDate>
143              
144             =head2 name
145              
146             Read-only.
147              
148             Returns the name of the file referenced by the file object.
149              
150             See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/File/name>
151              
152             =head2 webkitRelativePath
153              
154             Read-only.
155              
156             Returns the relative file path.
157              
158             Normally under JavaScript, this works alongside the C<<input>> attribute C<webkitdirectory>:
159              
160             <input type="file" webkitdirectory />
161              
162             allowing a user to select an entire directory instead of just files. So, C<webkitRelativePath> provide the relative file path to that directory uploaded.
163              
164             See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/File/webkitRelativePath>
165              
166             =head2 size
167              
168             Read-only.
169              
170             Returns the size of the file in bytes.
171              
172             See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/File/size>
173              
174             =head2 type
175              
176             Read-only.
177              
178             Returns the MIME type of the file, or C<undef> if it cannot find it.
179              
180             See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/File/type>
181              
182             =head1 METHODS
183              
184             =head2 arrayBuffer
185              
186             Opens the file as C<raw> data and returns its content as a L<scalar object|Module::Generic::Scalar>.
187              
188             See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Blob/arrayBuffer>
189              
190             =head2 slice
191              
192             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>
193              
194             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.
195              
196             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.
197              
198             Returns a new L<scalar object|Module::Generic::Scalar> containing the data in the specified range of bytes of the file.
199              
200             =head2 stream
201              
202             This opens the file and returns its file handle to read the file's contents. You could also do:
203              
204             my $io = $file->open || die( $file->error );
205              
206             See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Blob/stream>
207              
208             =head2 text
209              
210             Opens the file in C<utf-8> and returns its content as a L<scalar object|Module::Generic::Scalar>.
211              
212             See L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/Blob/text>
213              
214             =head1 AUTHOR
215              
216             Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
217              
218             =head1 SEE ALSO
219              
220             L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/File>
221              
222             =head1 COPYRIGHT & LICENSE
223              
224             Copyright(c) 2021 DEGUEST Pte. Ltd.
225              
226             All rights reserved
227              
228             This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
229              
230             =cut