Merge branch 'master' into australorp
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Request / PartData.pm
1 package Catalyst::Request::PartData;
2
3 use Moose;
4 use HTTP::Headers;
5
6 has [qw/raw_data name size/] => (is=>'ro', required=>1);
7
8 has headers => (
9   is=>'ro',
10   required=>1,
11   handles=>[qw/content_type content_encoding content_type_charset/]);
12
13 sub build_from_part_data {
14   my ($class, $part_data) = @_;
15   return $part_data->{data} unless $class->part_data_has_complex_headers($part_data);
16   return $class->new(
17     raw_data => $part_data->{data},
18     name => $part_data->{name},
19     size => $part_data->{size},
20     headers => HTTP::Headers->new(%{ $part_data->{headers} }));
21 }
22
23 sub part_data_has_complex_headers {
24   my ($class, $part_data) = @_;
25   return scalar keys %{$part_data->{headers}} > 1 ? 1:0;
26 }
27
28 __PACKAGE__->meta->make_immutable;
29
30 =head1 NAME
31
32 Catalyst::Request::Upload - handles file upload requests
33
34 =head1 SYNOPSIS
35
36     my $data_part = 
37
38 To specify where Catalyst should put the temporary files, set the 'uploadtmp'
39 option in the Catalyst config. If unset, Catalyst will use the system temp dir.
40
41     __PACKAGE__->config( uploadtmp => '/path/to/tmpdir' );
42
43 See also L<Catalyst>.
44
45 =head1 DESCRIPTION
46
47 =head1 ATTRIBUTES
48
49 This class defines the following immutable attributes
50
51 =head2 raw_data
52
53 The raw data as returned via L<HTTP::Body>.
54
55 =head2 name
56
57 The part name that gets extracted from the content-disposition header.
58
59 =head2 size
60
61 The raw byte count (over http) of the data.  This is not the same as the character
62 length
63
64 =head2 headers
65
66 An L<HTTP::Headers> object that represents the submitted headers of the POST.  This
67 object will handle the following methods:
68
69 =head3 content_type
70
71 =head3 content_encoding
72
73 =head3 content_type_charset
74
75 These three methods are the same as methods described in L<HTTP::Headers>.
76
77 =head1 METHODS
78
79 =head2 build_from_part_data
80
81 Factory method to build an object from part data returned by L<HTTP::Body>
82
83 =head2 part_data_has_complex_headers
84
85 Returns true if there more than one header (indicates the part data is complex and
86 contains content type and encoding information.).
87
88 =head1 AUTHORS
89
90 Catalyst Contributors, see Catalyst.pm
91
92 =head1 COPYRIGHT
93
94 This library is free software. You can redistribute it and/or modify
95 it under the same terms as Perl itself.
96
97 =cut