File Coverage

blib/lib/Dancer2/Core/Request/Upload.pm
Criterion Covered Total %
statement 41 41 100.0
branch 5 6 83.3
condition n/a
subroutine 12 12 100.0
pod 6 6 100.0
total 64 65 98.4


line stmt bran cond sub pod time code
1             # ABSTRACT: Class representing file upload requests
2             $Dancer2::Core::Request::Upload::VERSION = '0.400000';
3             use Moo;
4 145     145   2467  
  145         17674  
  145         1217  
5             use Carp;
6 145     145   53579 use File::Spec;
  145         351  
  145         8643  
7 145     145   900 use Module::Runtime 'require_module';
  145         363  
  145         4515  
8 145     145   876  
  145         348  
  145         1091  
9             use Dancer2::Core::Types;
10 145     145   6489 use Dancer2::FileUtils qw(open_file);
  145         365  
  145         988  
11 145     145   1165015  
  145         374  
  145         63774  
12             has filename => (
13             is => 'ro',
14             isa => Str,
15             );
16              
17             has tempname => (
18             is => 'ro',
19             isa => Str,
20             );
21              
22             has headers => (
23             is => 'ro',
24             isa => HashRef,
25             );
26              
27             has size => (
28             is => 'ro',
29             isa => Num,
30             );
31              
32             my ($self) = @_;
33             return $self->{_fh} if defined $self->{_fh};
34 23     23 1 7273 my $fh = open_file( '<', $self->tempname );
35 23 100       55 $self->{_fh} = $fh;
36 20         59 }
37 17         39  
38             my ( $self, $target ) = @_;
39             require_module('File::Copy');
40             File::Copy::copy( $self->tempname, $target );
41 3     3 1 16 }
42 3         8  
43 3         2490 my ( $self, $target ) = @_;
44             CORE::link( $self->tempname, $target );
45             }
46              
47 3     3 1 1621 my ( $self, $layer ) = @_;
48 3         90 return $self->{_content}
49             if defined $self->{_content};
50              
51             $layer = ':raw' unless $layer;
52 20     20 1 68  
53             my $content = undef;
54 20 100       388 my $handle = $self->file_handle;
55              
56 17 50       39 binmode( $handle, $layer );
57              
58 17         46 while ( $handle->read( my $buffer, 8192 ) ) {
59 17         35 $content .= $buffer;
60             }
61 17         68  
62             $self->{_content} = $content;
63 17         57 }
64 17         527  
65             my ($self) = @_;
66             require_module('File::Basename');
67 17         199 File::Basename::basename( $self->filename );
68             }
69              
70             my $self = shift;
71 3     3 1 913 return $self->headers->{'Content-Type'};
72 3         11 }
73 3         199  
74             1;
75              
76              
77 3     3 1 5 =pod
78 3         21  
79             =encoding UTF-8
80              
81             =head1 NAME
82              
83             Dancer2::Core::Request::Upload - Class representing file upload requests
84              
85             =head1 VERSION
86              
87             version 0.400000
88              
89             =head1 DESCRIPTION
90              
91             This class implements a representation of file uploads for Dancer2.
92             These objects are accessible within route handlers via the request->uploads
93             keyword. See L<Dancer2::Core::Request> for details.
94              
95             =head1 ATTRIBUTES
96              
97             =head2 filename
98              
99             Filename as sent by client. optional. May not be undef.
100              
101             =head2 tempname
102              
103             The name of the temporary file the data has been saved to. Optional. May not be undef.
104              
105             =head2 headers
106              
107             A hash ref of the headers associated with this upload. optional. is read-write and a HashRef.
108              
109             =head2 size
110              
111             The size of the upload, in bytes. Optional.
112              
113             =head1 METHODS
114              
115             =head2 my $filename=$upload->filename;
116              
117             Returns the filename (full path) as sent by the client.
118              
119             =head2 my $tempname=$upload->tempname;
120              
121             Returns the name of the temporary file the data has been saved to.
122              
123             For example, in directory /tmp, and given a random name, with no file extension.
124              
125             =head2 my $href=$upload->headers;
126              
127             Returns a hashRef of the headers associated with this upload.
128              
129             =head2 my $fh=$upload->file_handle;
130              
131             Returns a read-only file handle on the temporary file.
132              
133             =head2 $upload->copy_to('/path/to/target')
134              
135             Copies the temporary file using File::Copy. Returns true for success,
136             false for failure.
137              
138             =head2 $upload->link_to('/path/to/target');
139              
140             Creates a hard link to the temporary file. Returns true for success,
141             false for failure.
142              
143             =head2 my $content=$upload->content;
144              
145             Returns a scalar containing the contents of the temporary file.
146              
147             =head2 my $basename=$upload->basename;
148              
149             Returns basename for "filename".
150              
151             =head2 $upload->type
152              
153             Returns the Content-Type of this upload.
154              
155             =head1 SEE ALSO
156              
157             L<Dancer2>
158              
159             =head1 AUTHOR
160              
161             Dancer Core Developers
162              
163             =head1 COPYRIGHT AND LICENSE
164              
165             This software is copyright (c) 2022 by Alexis Sukrieh.
166              
167             This is free software; you can redistribute it and/or modify it under
168             the same terms as the Perl 5 programming language system itself.
169              
170             =cut