Create branch register_actions.
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Request / Upload.pm
CommitLineData
146554c5 1package Catalyst::Request::Upload;
2
ae29b412 3use Moose;
4with 'MooseX::Emulate::Class::Accessor::Fast';
146554c5 5
a2f2cde9 6use Catalyst::Exception;
47ae6960 7use File::Copy ();
8use IO::File ();
3e22baa5 9use File::Spec::Unix;
146554c5 10
ae29b412 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}
146554c5 44
ae29b412 45no Moose;
146554c5 46
47=head1 NAME
48
bab3a22c 49Catalyst::Request::Upload - handles file upload requests
146554c5 50
51=head1 SYNOPSIS
52
ae29b412 53 my $upload = $c->req->upload('field');
54
3e22baa5 55 $upload->basename;
bab3a22c 56 $upload->copy_to;
57 $upload->fh;
146554c5 58 $upload->filename;
4be535b1 59 $upload->headers;
3ffaf022 60 $upload->link_to;
146554c5 61 $upload->size;
32d4bba8 62 $upload->slurp;
146554c5 63 $upload->tempname;
64 $upload->type;
65
7257e9db 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
146554c5 71See also L<Catalyst>.
72
73=head1 DESCRIPTION
74
bab3a22c 75This class provides accessors and methods to handle client upload requests.
146554c5 76
77=head1 METHODS
78
b5ecfcf0 79=head2 $upload->new
cd3bb248 80
bab3a22c 81Simple constructor.
cd3bb248 82
b5ecfcf0 83=head2 $upload->copy_to
47ae6960 84
bab3a22c 85Copies the temporary file using L<File::Copy>. Returns true for success,
86false for failure.
47ae6960 87
3ffaf022 88 $upload->copy_to('/path/to/target');
89
47ae6960 90=cut
91
3ffaf022 92sub copy_to {
c462faf0 93 my $self = shift;
94 return File::Copy::copy( $self->tempname, @_ );
47ae6960 95}
96
b5ecfcf0 97=head2 $upload->fh
146554c5 98
bab3a22c 99Opens a temporary file (see tempname below) and returns an L<IO::File> handle.
146554c5 100
b5ecfcf0 101=head2 $upload->filename
146554c5 102
bab3a22c 103Returns the client-supplied filename.
146554c5 104
b5ecfcf0 105=head2 $upload->headers
4be535b1 106
bab3a22c 107Returns an L<HTTP::Headers> object for the request.
4be535b1 108
b5ecfcf0 109=head2 $upload->link_to
146554c5 110
bab3a22c 111Creates a hard link to the temporary file. Returns true for success,
112false for failure.
146554c5 113
3ffaf022 114 $upload->link_to('/path/to/target');
146554c5 115
116=cut
117
3ffaf022 118sub link_to {
47ae6960 119 my ( $self, $target ) = @_;
5c0ff128 120 return CORE::link( $self->tempname, $target );
146554c5 121}
122
b5ecfcf0 123=head2 $upload->size
146554c5 124
bab3a22c 125Returns the size of the uploaded file in bytes.
146554c5 126
b5ecfcf0 127=head2 $upload->slurp
32d4bba8 128
bab3a22c 129Returns a scalar containing the contents of the temporary file.
32d4bba8 130
131=cut
132
133sub slurp {
134 my ( $self, $layer ) = @_;
135
4be535b1 136 unless ($layer) {
32d4bba8 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
3e22baa5 152=head2 $upload->basename
153
154Returns basename for C<filename>.
155
b5ecfcf0 156=head2 $upload->tempname
146554c5 157
bab3a22c 158Returns the path to the temporary file.
146554c5 159
b5ecfcf0 160=head2 $upload->type
146554c5 161
bab3a22c 162Returns the client-supplied Content-Type.
146554c5 163
ae29b412 164=head2 meta
165
166Provided by Moose
167
bab3a22c 168=head1 AUTHORS
146554c5 169
0bf7ab71 170Catalyst Contributors, see Catalyst.pm
146554c5 171
172=head1 COPYRIGHT
173
174This program is free software, you can redistribute it and/or modify
175it under the same terms as Perl itself.
176
177=cut
178
ae29b412 179__PACKAGE__->meta->make_immutable;
180
146554c5 1811;