start using Class::C3, may need to add a reinitalize bit later, not sure
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Request / Upload.pm
CommitLineData
146554c5 1package Catalyst::Request::Upload;
2
0fc2d522 3use Class::C3;
e5ecd5bc 4use Moose;
146554c5 5
a2f2cde9 6use Catalyst::Exception;
47ae6960 7use File::Copy ();
8use IO::File ();
3e22baa5 9use File::Spec::Unix;
146554c5 10
5fb12dbb 11has filename => (is => 'rw');
12has headers => (is => 'rw');
13has size => (is => 'rw');
14has tempname => (is => 'rw');
15has type => (is => 'rw');
16has basename => (is => 'rw');
059c085b 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
36no Moose;
146554c5 37
38=head1 NAME
39
bab3a22c 40Catalyst::Request::Upload - handles file upload requests
146554c5 41
42=head1 SYNOPSIS
43
3e22baa5 44 $upload->basename;
bab3a22c 45 $upload->copy_to;
46 $upload->fh;
146554c5 47 $upload->filename;
4be535b1 48 $upload->headers;
3ffaf022 49 $upload->link_to;
146554c5 50 $upload->size;
32d4bba8 51 $upload->slurp;
146554c5 52 $upload->tempname;
53 $upload->type;
54
7257e9db 55To specify where Catalyst should put the temporary files, set the 'uploadtmp'
56option in the Catalyst config. If unset, Catalyst will use the system temp dir.
57
58 __PACKAGE__->config( uploadtmp => '/path/to/tmpdir' );
59
7c48f3f6 60It is provided a way to have configurable temporary directory.
61If there is no config uploadtmp, system temprary directory will used.
62
63 __PACKAGE__->config( uploadtmp => '/path/to/tmpdir' );
64
146554c5 65See also L<Catalyst>.
66
67=head1 DESCRIPTION
68
bab3a22c 69This class provides accessors and methods to handle client upload requests.
146554c5 70
71=head1 METHODS
72
b5ecfcf0 73=head2 $upload->new
cd3bb248 74
bab3a22c 75Simple constructor.
cd3bb248 76
b5ecfcf0 77=head2 $upload->copy_to
47ae6960 78
bab3a22c 79Copies the temporary file using L<File::Copy>. Returns true for success,
80false for failure.
47ae6960 81
3ffaf022 82 $upload->copy_to('/path/to/target');
83
47ae6960 84=cut
85
3ffaf022 86sub copy_to {
c462faf0 87 my $self = shift;
88 return File::Copy::copy( $self->tempname, @_ );
47ae6960 89}
90
b5ecfcf0 91=head2 $upload->fh
146554c5 92
bab3a22c 93Opens a temporary file (see tempname below) and returns an L<IO::File> handle.
146554c5 94
b5ecfcf0 95=head2 $upload->filename
146554c5 96
bab3a22c 97Returns the client-supplied filename.
146554c5 98
b5ecfcf0 99=head2 $upload->headers
4be535b1 100
bab3a22c 101Returns an L<HTTP::Headers> object for the request.
4be535b1 102
b5ecfcf0 103=head2 $upload->link_to
146554c5 104
ac5c933b 105Creates a hard link to the temporary file. Returns true for success,
bab3a22c 106false for failure.
146554c5 107
3ffaf022 108 $upload->link_to('/path/to/target');
146554c5 109
110=cut
111
3ffaf022 112sub link_to {
47ae6960 113 my ( $self, $target ) = @_;
5c0ff128 114 return CORE::link( $self->tempname, $target );
146554c5 115}
116
b5ecfcf0 117=head2 $upload->size
146554c5 118
bab3a22c 119Returns the size of the uploaded file in bytes.
146554c5 120
b5ecfcf0 121=head2 $upload->slurp
32d4bba8 122
bab3a22c 123Returns a scalar containing the contents of the temporary file.
32d4bba8 124
125=cut
126
127sub slurp {
128 my ( $self, $layer ) = @_;
129
4be535b1 130 unless ($layer) {
32d4bba8 131 $layer = ':raw';
132 }
133
134 my $content = undef;
135 my $handle = $self->fh;
136
137 binmode( $handle, $layer );
138
139 while ( $handle->sysread( my $buffer, 8192 ) ) {
140 $content .= $buffer;
141 }
142
143 return $content;
144}
145
3e22baa5 146sub basename {
147 my $self = shift;
148 unless ( $self->{basename} ) {
149 my $basename = $self->filename;
150 $basename =~ s|\\|/|g;
151 $basename = ( File::Spec::Unix->splitpath($basename) )[2];
152 $basename =~ s|[^\w\.-]+|_|g;
153 $self->{basename} = $basename;
154 }
155
156 return $self->{basename};
157}
158
159=head2 $upload->basename
160
161Returns basename for C<filename>.
162
b5ecfcf0 163=head2 $upload->tempname
146554c5 164
bab3a22c 165Returns the path to the temporary file.
146554c5 166
b5ecfcf0 167=head2 $upload->type
146554c5 168
bab3a22c 169Returns the client-supplied Content-Type.
146554c5 170
059c085b 171=head2 meta
172
173Provided by Moose
174
bab3a22c 175=head1 AUTHORS
146554c5 176
177Sebastian Riedel, C<sri@cpan.org>
bab3a22c 178
146554c5 179Christian Hansen, C<ch@ngmedia.com>
180
181=head1 COPYRIGHT
182
183This program is free software, you can redistribute it and/or modify
184it under the same terms as Perl itself.
185
186=cut
187
e5ecd5bc 188__PACKAGE__->meta->make_immutable;
189
146554c5 1901;