Add built local::lib
[catagits/Gitalist.git] / local-lib5 / lib / perl5 / Catalyst / Request / Upload.pm
CommitLineData
3fea05b9 1package Catalyst::Request::Upload;
2
3use Moose;
4with 'MooseX::Emulate::Class::Accessor::Fast';
5
6use Catalyst::Exception;
7use File::Copy ();
8use IO::File ();
9use File::Spec::Unix;
10
11has filename => (is => 'rw');
12has headers => (is => 'rw');
13has size => (is => 'rw');
14has tempname => (is => 'rw');
15has type => (is => 'rw');
16has basename => (is => 'ro', lazy_build => 1);
17
18has fh => (
19 is => 'rw',
20 required => 1,
21 lazy => 1,
22 default => sub {
23 my $self = shift;
24
25 my $fh = IO::File->new($self->tempname, IO::File::O_RDONLY);
26 unless ( defined $fh ) {
27 my $filename = $self->tempname;
28 Catalyst::Exception->throw(
29 message => qq/Can't open '$filename': '$!'/ );
30 }
31
32 return $fh;
33 },
34);
35
36sub _build_basename {
37 my $self = shift;
38 my $basename = $self->filename;
39 $basename =~ s|\\|/|g;
40 $basename = ( File::Spec::Unix->splitpath($basename) )[2];
41 $basename =~ s|[^\w\.-]+|_|g;
42 return $basename;
43}
44
45no Moose;
46
47=head1 NAME
48
49Catalyst::Request::Upload - handles file upload requests
50
51=head1 SYNOPSIS
52
53 my $upload = $c->req->upload('field');
54
55 $upload->basename;
56 $upload->copy_to;
57 $upload->fh;
58 $upload->filename;
59 $upload->headers;
60 $upload->link_to;
61 $upload->size;
62 $upload->slurp;
63 $upload->tempname;
64 $upload->type;
65
66To specify where Catalyst should put the temporary files, set the 'uploadtmp'
67option in the Catalyst config. If unset, Catalyst will use the system temp dir.
68
69 __PACKAGE__->config( uploadtmp => '/path/to/tmpdir' );
70
71See also L<Catalyst>.
72
73=head1 DESCRIPTION
74
75This class provides accessors and methods to handle client upload requests.
76
77=head1 METHODS
78
79=head2 $upload->new
80
81Simple constructor.
82
83=head2 $upload->copy_to
84
85Copies the temporary file using L<File::Copy>. Returns true for success,
86false for failure.
87
88 $upload->copy_to('/path/to/target');
89
90=cut
91
92sub copy_to {
93 my $self = shift;
94 return File::Copy::copy( $self->tempname, @_ );
95}
96
97=head2 $upload->fh
98
99Opens a temporary file (see tempname below) and returns an L<IO::File> handle.
100
101=head2 $upload->filename
102
103Returns the client-supplied filename.
104
105=head2 $upload->headers
106
107Returns an L<HTTP::Headers> object for the request.
108
109=head2 $upload->link_to
110
111Creates a hard link to the temporary file. Returns true for success,
112false for failure.
113
114 $upload->link_to('/path/to/target');
115
116=cut
117
118sub link_to {
119 my ( $self, $target ) = @_;
120 return CORE::link( $self->tempname, $target );
121}
122
123=head2 $upload->size
124
125Returns the size of the uploaded file in bytes.
126
127=head2 $upload->slurp
128
129Returns a scalar containing the contents of the temporary file.
130
131=cut
132
133sub slurp {
134 my ( $self, $layer ) = @_;
135
136 unless ($layer) {
137 $layer = ':raw';
138 }
139
140 my $content = undef;
141 my $handle = $self->fh;
142
143 binmode( $handle, $layer );
144
145 while ( $handle->sysread( my $buffer, 8192 ) ) {
146 $content .= $buffer;
147 }
148
149 return $content;
150}
151
152=head2 $upload->basename
153
154Returns basename for C<filename>.
155
156=head2 $upload->tempname
157
158Returns the path to the temporary file.
159
160=head2 $upload->type
161
162Returns the client-supplied Content-Type.
163
164=head2 meta
165
166Provided by Moose
167
168=head1 AUTHORS
169
170Catalyst Contributors, see Catalyst.pm
171
172=head1 COPYRIGHT
173
174This library is free software. You can redistribute it and/or modify
175it under the same terms as Perl itself.
176
177=cut
178
179__PACKAGE__->meta->make_immutable;
180
1811;