rename $upload->link to $upload->link_to and $upload->copy to $upload->copy_to
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Request / Upload.pm
1 package Catalyst::Request::Upload;
2
3 use strict;
4 use base 'Class::Accessor::Fast';
5
6 use File::Copy ();
7 use IO::File   ();
8
9 __PACKAGE__->mk_accessors(qw/filename size tempname type/);
10
11 sub new { shift->SUPER::new( ref( $_[0] ) ? $_[0] : {@_} ) }
12
13 =head1 NAME
14
15 Catalyst::Request::Upload - Catalyst Request Upload Class
16
17 =head1 SYNOPSIS
18
19     $upload->copy_to
20     $upload->fh
21     $upload->filename;
22     $upload->link_to;
23     $upload->size;
24     $upload->tempname;
25     $upload->type;
26
27 See also L<Catalyst>.
28
29 =head1 DESCRIPTION
30
31 This is the Catalyst Request Upload class, which provides a set of accessors 
32 to the upload data.
33
34 =head1 METHODS
35
36 =over 4
37
38 =item $upload->copy_to
39
40 Copies tempname using C<File::Copy>. Returns true for success, false otherwise.
41
42      $upload->copy_to('/path/to/target');
43
44 =cut
45
46 sub copy_to {
47     my ( $self, $target, $buffer ) = @_;
48     return File::Copy::copy( $self->tempname, $target, $buffer );
49 }
50
51 =item $upload->fh
52
53 Opens tempname and returns a C<IO::File> handle.
54
55 =cut
56
57 sub fh {
58     my $self = shift;
59
60     my $fh = IO::File->new( $self->tempname, IO::File::O_RDONLY )
61       or die( "Can't open ", $self->tempname, ": ", $! );
62
63     return $fh;
64 }
65
66 =item $upload->filename
67
68 Contains client supplied filename.
69
70 =item $upload->link_to
71
72 Creates a hard link to the tempname.  Returns true for success, 
73 false otherwise.
74
75     $upload->link_to('/path/to/target');
76
77 =cut
78
79 sub link_to {
80     my ( $self, $target ) = @_;
81     return CORE::link( $self->tempname, $target );
82 }
83
84 =item $upload->size
85
86 Contains size of the file in bytes.
87
88 =item $upload->tempname
89
90 Contains path to the temporary spool file.
91
92 =item $upload->type
93
94 Contains client supplied Content-Type.
95
96 =back
97
98 =head1 AUTHOR
99
100 Sebastian Riedel, C<sri@cpan.org>
101 Christian Hansen, C<ch@ngmedia.com>
102
103 =head1 COPYRIGHT
104
105 This program is free software, you can redistribute it and/or modify
106 it under the same terms as Perl itself.
107
108 =cut
109
110 1;