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