~kosova/+junk/tuxfamily-twiki

« back to all changes in this revision

Viewing changes to foswiki/lib/Foswiki/Request/Upload.pm

  • Committer: James Michael DuPont
  • Date: 2009-07-18 19:58:49 UTC
  • Revision ID: jamesmikedupont@gmail.com-20090718195849-vgbmaht2ys791uo2
added foswiki

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# See bottom of file for license and copyright information
 
2
 
 
3
=begin TML
 
4
 
 
5
---+!! package Foswiki::Request::Upload
 
6
 
 
7
Class to encapsulate uploaded file info.
 
8
 
 
9
=cut
 
10
 
 
11
package Foswiki::Request::Upload;
 
12
 
 
13
use strict;
 
14
use IO::File;
 
15
 
 
16
=begin TML
 
17
 
 
18
---++ ClassMethod new()
 
19
 
 
20
Constructs a Foswiki::Request::Upload object
 
21
 
 
22
=cut
 
23
 
 
24
sub new {
 
25
    my ( $proto, %args ) = @_;
 
26
    my $class = ref($proto) || $proto;
 
27
    my $this = {
 
28
        headers => $args{headers},
 
29
        tmpname => $args{tmpname},
 
30
    };
 
31
    return bless $this, $class;
 
32
}
 
33
 
 
34
=begin TML
 
35
 
 
36
---++ ObjectMethod finish()
 
37
 
 
38
Deletes temp file associated.
 
39
 
 
40
=cut
 
41
 
 
42
# Note to developers; please undef *all* fields in the object explicitly,
 
43
# whether they are references or not. That way this method is "golden
 
44
# documentation" of the live fields in the object.
 
45
sub finish {
 
46
    my $this = shift;
 
47
    undef $this->{headers};
 
48
 
 
49
    #SMELL: Note: untaint filename. Taken from CGI.pm
 
50
    # (had to be updated for OSX in Dec2008)
 
51
    $this->tmpFileName =~ m{^([a-zA-Z0-9_\+ \'\":/.\$\\~-]+)$};
 
52
    my $file = $1;
 
53
    if ( scalar( unlink($file) ) != 1 ) {
 
54
        throw Error::Simple( "unable to unlink : " . $file );
 
55
    }
 
56
    undef $this->{tmpname};
 
57
}
 
58
 
 
59
=begin TML
 
60
 
 
61
---++ ObjectMethod uploadInfo() -> $headers
 
62
 
 
63
Returns a hashref to information about uploaded
 
64
file as sent by browser.
 
65
 
 
66
=cut
 
67
 
 
68
sub uploadInfo {
 
69
    return $_[0]->{headers};
 
70
}
 
71
 
 
72
=begin TML
 
73
 
 
74
---++ ObjectMethod handle() -> ( $fh )
 
75
 
 
76
Returns an open filehandle to uploaded file.
 
77
 
 
78
=cut
 
79
 
 
80
sub handle {
 
81
    my $fh = new IO::File( $_[0]->{tmpname}, '<' );
 
82
    binmode $fh;
 
83
    return $fh;
 
84
}
 
85
 
 
86
=begin TML
 
87
 
 
88
---++ ObjectMethod tmpFileName() -> ( $tmpName )
 
89
 
 
90
Returns the names of temporarly created file.
 
91
 
 
92
=cut
 
93
 
 
94
sub tmpFileName {
 
95
    return $_[0]->{tmpname};
 
96
}
 
97
 
 
98
1;
 
99
__DATA__
 
100
# Module of Foswiki - The Free and Open Source Wiki, http://foswiki.org/
 
101
#
 
102
# Copyright (C) 2008-2009 Foswiki Contributors. Foswiki Contributors
 
103
# are listed in the AUTHORS file in the root of this distribution.
 
104
# NOTE: Please extend that file, not this notice.
 
105
#
 
106
# Additional copyrights apply to some or all of the code in this
 
107
# file as follows:
 
108
#
 
109
# Copyright (C) 1999-2007 Peter Thoeny, peter@thoeny.org
 
110
# and TWiki Contributors. All Rights Reserved. TWiki Contributors
 
111
# are listed in the AUTHORS file in the root of this distribution.
 
112
#
 
113
# This module is based/inspired on Catalyst framework. Refer to
 
114
#
 
115
# http://search.cpan.org/~mramberg/Catalyst-Runtime-5.7010/lib/Catalyst.pm
 
116
#
 
117
# for credits and liscence details.
 
118
#
 
119
# This program is free software; you can redistribute it and/or
 
120
# modify it under the terms of the GNU General Public License
 
121
# as published by the Free Software Foundation; either version 2
 
122
# of the License, or (at your option) any later version. For
 
123
# more details read LICENSE in the root of this distribution.
 
124
#
 
125
# This program is distributed in the hope that it will be useful,
 
126
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
127
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
128
#
 
129
# As per the GPL, removal of this notice is prohibited.