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