Do a load of small refatoring to remove direct hash accesses, update todo, bump dates...
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Request / Upload.pm
1 package Catalyst::Request::Upload;
2
3 use Moose;
4 with 'MooseX::Emulate::Class::Accessor::Fast';
5
6 use Catalyst::Exception;
7 use File::Copy ();
8 use IO::File   ();
9 use File::Spec::Unix;
10
11 has filename => (is => 'rw');
12 has headers => (is => 'rw');
13 has size => (is => 'rw');
14 has tempname => (is => 'rw');
15 has type => (is => 'rw');
16 has basename => (is => 'ro', lazy_build => 1);
17
18 has 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
36 sub _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
45 no Moose;
46
47 =head1 NAME
48
49 Catalyst::Request::Upload - handles file upload requests
50
51 =head1 SYNOPSIS
52
53     $upload->basename;
54     $upload->copy_to;
55     $upload->fh;
56     $upload->filename;
57     $upload->headers;
58     $upload->link_to;
59     $upload->size;
60     $upload->slurp;
61     $upload->tempname;
62     $upload->type;
63
64 To specify where Catalyst should put the temporary files, set the 'uploadtmp'
65 option in the Catalyst config. If unset, Catalyst will use the system temp dir.
66
67     __PACKAGE__->config( uploadtmp => '/path/to/tmpdir' );
68
69 See also L<Catalyst>.
70
71 =head1 DESCRIPTION
72
73 This class provides accessors and methods to handle client upload requests.
74
75 =head1 METHODS
76
77 =head2 $upload->new
78
79 Simple constructor.
80
81 =head2 $upload->copy_to
82
83 Copies the temporary file using L<File::Copy>. Returns true for success,
84 false for failure.
85
86      $upload->copy_to('/path/to/target');
87
88 =cut
89
90 sub copy_to {
91     my $self = shift;
92     return File::Copy::copy( $self->tempname, @_ );
93 }
94
95 =head2 $upload->fh
96
97 Opens a temporary file (see tempname below) and returns an L<IO::File> handle.
98
99 =head2 $upload->filename
100
101 Returns the client-supplied filename.
102
103 =head2 $upload->headers
104
105 Returns an L<HTTP::Headers> object for the request.
106
107 =head2 $upload->link_to
108
109 Creates a hard link to the temporary file. Returns true for success, 
110 false for failure.
111
112     $upload->link_to('/path/to/target');
113
114 =cut
115
116 sub link_to {
117     my ( $self, $target ) = @_;
118     return CORE::link( $self->tempname, $target );
119 }
120
121 =head2 $upload->size
122
123 Returns the size of the uploaded file in bytes.
124
125 =head2 $upload->slurp
126
127 Returns a scalar containing the contents of the temporary file.
128
129 =cut
130
131 sub slurp {
132     my ( $self, $layer ) = @_;
133
134     unless ($layer) {
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
150 =head2 $upload->basename
151
152 Returns basename for C<filename>.
153
154 =head2 $upload->tempname
155
156 Returns the path to the temporary file.
157
158 =head2 $upload->type
159
160 Returns the client-supplied Content-Type.
161
162 =head2 meta
163
164 Provided by Moose
165
166 =head1 AUTHORS
167
168 Catalyst Contributors, see Catalyst.pm
169
170 =head1 COPYRIGHT
171
172 This program is free software, you can redistribute it and/or modify
173 it under the same terms as Perl itself.
174
175 =cut
176
177 __PACKAGE__->meta->make_immutable;
178
179 1;