| 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 | use strict; | ||
| 18 | use warnings; | ||
| 19 | warnings::register_categories( 'HTML::Object' ); | ||
| 20 | use parent qw( Module::Generic::File ); | ||
| 21 | use vars qw( $VERSION ); | ||
| 22 | use DateTime::Format::Strptime; | ||
| 23 | use Module::Generic::Scalar; | ||
| 24 | our $VERSION = 'v0.2.0'; | ||
| 25 | }; | ||
| 26 | |||
| 27 | use strict; | ||
| 28 | use warnings; | ||
| 29 | |||
| 30 | sub arrayBuffer | ||
| 31 | { | ||
| 32 | my $self = shift( @_ ); | ||
| 33 | my $data = $self->load({ binmode => 'raw' }); | ||
| 34 | return( Module::Generic::Scalar->new( \$data ) ); | ||
| 35 | } | ||
| 36 | |||
| 37 | sub lastModified | ||
| 38 | { | ||
| 39 | my $self = shift( @_ ); | ||
| 40 | my $dt = $self->mtime; | ||
| 41 | my $fmt = DateTime::Format::Strptime->new( pattern => '%s' ); | ||
| 42 | $dt->set_formatter( $fmt ); | ||
| 43 | return( $dt ); | ||
| 44 | } | ||
| 45 | |||
| 46 | sub lastModifiedDate { return( shift->mtime ); } | ||
| 47 | |||
| 48 | sub name { return( shift->basename ); } | ||
| 49 | |||
| 50 | # Note: sub size is inherited | ||
| 51 | |||
| 52 | sub slice | ||
| 53 | { | ||
| 54 | my $self = shift( @_ ); | ||
| 55 | my( $start, $end ) = @_; | ||
| 56 | return( $self->error( "start value provided is not an integer." ) ) if( !$self->_is_integer( $start ) ); | ||
| 57 | $end = $self->size if( !$self->_is_integer( $end ) ); | ||
| 58 | return( Module::Generic::Scalar->new ) if( $end <= $start ); | ||
| 59 | my $len = $end - $start; | ||
| 60 | my $opened = $self->opened; | ||
| 61 | my $io; | ||
| 62 | my $pos; | ||
| 63 | if( $opened ) | ||
| 64 | { | ||
| 65 | $io = $opened; | ||
| 66 | $pos = $io->seek(0,1); | ||
| 67 | } | ||
| 68 | else | ||
| 69 | { | ||
| 70 | $io = $self->open({ binmode => 'utf-8' }) || return( $self->pass_error ); | ||
| 71 | } | ||
| 72 | $io->seek( $start, 0 ) || return( $self->pass_error ); | ||
| 73 | my $data; | ||
| 74 | $self->read( $data, $len ) || return( $self->pass_error ); | ||
| 75 | if( $opened ) | ||
| 76 | { | ||
| 77 | $io->seek( $pos, 0 ) if( defined( $pos ) ); | ||
| 78 | } | ||
| 79 | else | ||
| 80 | { | ||
| 81 | $self->close; | ||
| 82 | } | ||
| 83 | return( Module::Generic::Scalar->new( \$data ) ); | ||
| 84 | } | ||
| 85 | |||
| 86 | sub stream { return( shift->open( @_ ) ); } | ||
| 87 | |||
| 88 | sub text | ||
| 89 | { | ||
| 90 | my $self = shift( @_ ); | ||
| 91 | my $data = $self->load_utf8; | ||
| 92 | return( Module::Generic::Scalar->new( \$data ) ); | ||
| 93 | } | ||
| 94 | |||
| 95 | sub type { return( shift->finfo->mime_type ); } | ||
| 96 | |||
| 97 | sub webkitRelativePath { return( shift->relative ); } | ||
| 98 | |||
| 99 | 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 |