Debian Codegraph @

lib/Dancer2/Core/Request/Upload.pm

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