blib/lib/PowerTools/Upload/File.pm | |||
---|---|---|---|
Criterion | Covered | Total | % |
statement | 13 | 15 | 86.6 |
branch | n/a | ||
condition | n/a | ||
subroutine | 5 | 5 | 100.0 |
pod | n/a | ||
total | 18 | 20 | 90.0 |
line | stmt | bran | cond | sub | pod | time | code |
---|---|---|---|---|---|---|---|
1 | package PowerTools::Upload::File; | ||||||
2 | |||||||
3 | 1 | 1 | 40344 | use 5.008008; | |||
1 | 4 | ||||||
1 | 40 | ||||||
4 | 1 | 1 | 5 | use strict; | |||
1 | 1 | ||||||
1 | 34 | ||||||
5 | 1 | 1 | 5 | use warnings; | |||
1 | 7 | ||||||
1 | 40 | ||||||
6 | 1 | 1 | 6 | use Carp; | |||
1 | 2 | ||||||
1 | 113 | ||||||
7 | 1 | 1 | 489 | use File::Scan::ClamAV; | |||
0 | |||||||
0 | |||||||
8 | |||||||
9 | require Exporter; | ||||||
10 | |||||||
11 | our @ISA = qw(Exporter); | ||||||
12 | |||||||
13 | # Items to export into callers namespace by default. Note: do not export | ||||||
14 | # names by default without a very good reason. Use EXPORT_OK instead. | ||||||
15 | # Do not simply export all your public functions/methods/constants. | ||||||
16 | |||||||
17 | # This allows declaration use PowerTools::Upload::File ':all'; | ||||||
18 | # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK | ||||||
19 | # will save memory. | ||||||
20 | our %EXPORT_TAGS = ( 'all' => [ qw(upload | ||||||
21 | |||||||
22 | ) ] ); | ||||||
23 | |||||||
24 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | ||||||
25 | |||||||
26 | our @EXPORT = qw( | ||||||
27 | upload | ||||||
28 | ); | ||||||
29 | |||||||
30 | our $VERSION = '0.03'; | ||||||
31 | |||||||
32 | |||||||
33 | # Below is stub documentation for your module. You'd better edit it! | ||||||
34 | |||||||
35 | =head1 NAME | ||||||
36 | |||||||
37 | PowerTools::Upload::File - Additional Perl tool for Apache::ASP data uploading | ||||||
38 | |||||||
39 | =head1 SYNOPSIS | ||||||
40 | |||||||
41 | use PowerTools::Upload::File; | ||||||
42 | |||||||
43 | my $up = PowerTools::Upload::File->new( # Create new object | ||||||
44 | path => 'E:/instale/test', # Path to directory where files will be stored (default: '/tmp') | ||||||
45 | field => 'plik', # Form field name (, default: 'file') | ||||||
46 | limit => $Server->Config("FileUploadMax"), # File size limit (default 100000000) | ||||||
47 | request => $Request, # Request object | ||||||
48 | clamav => 1, # Scan with ClamAV when uploading (0 -> no / 1 -> yes, default: 0) | ||||||
49 | overwrite => 0 # Overwrite file (0 -> no / 1 -> yes, default: 1) | ||||||
50 | ); | ||||||
51 | |||||||
52 | my $ret = $up->upload(); # Upload file | ||||||
53 | print $ret->{'filename'}." "; # Returns filename |
||||||
54 | print $ret->{'filesize'}." "; # Returns filesize |
||||||
55 | print $ret->{'filepath'}." "; # Returns filepath |
||||||
56 | print $ret->{'filescan'}." "; # Returns filescan |
||||||
57 | print $ret->{'filemime'}." "; # Returns filemime |
||||||
58 | print $ret->{'copytime'}." "; # Returns copytime |
||||||
59 | print $ret->{'status'}; # Returns upload status | ||||||
60 | |||||||
61 | |||||||
62 | =head1 AUTHOR | ||||||
63 | |||||||
64 | Piotr Ginalski, E |
||||||
65 | |||||||
66 | =head1 COPYRIGHT AND LICENSE | ||||||
67 | |||||||
68 | Copyright (C) 2007 by Piotr Ginalski | ||||||
69 | |||||||
70 | This library is free software; you can redistribute it and/or modify | ||||||
71 | it under the same terms as Perl itself, either Perl version 5.8.8 or, | ||||||
72 | at your option, any later version of Perl 5 you may have available. | ||||||
73 | |||||||
74 | |||||||
75 | =cut | ||||||
76 | |||||||
77 | sub new { | ||||||
78 | my $class = shift; | ||||||
79 | my (%options) = @_; | ||||||
80 | return bless \%options, $class; | ||||||
81 | } | ||||||
82 | |||||||
83 | sub upload { | ||||||
84 | my $self = shift; | ||||||
85 | |||||||
86 | my $field = $self->{field} || "file"; | ||||||
87 | my $path = $self->{path} || "/tmp"; | ||||||
88 | my $limit = $self->{limit} || 100000000; | ||||||
89 | my $r = $self->{request}; | ||||||
90 | my $owerwrite = $self->{overwrite} || 1; | ||||||
91 | |||||||
92 | $self->{'filename'} = ''; | ||||||
93 | $self->{'filesize'} = ''; | ||||||
94 | $self->{'filepath'} = ''; | ||||||
95 | $self->{'filescan'} = ''; | ||||||
96 | $self->{'filemime'} = ''; | ||||||
97 | $self->{'copytime'} = ''; | ||||||
98 | |||||||
99 | $self->{'status'} = ''; | ||||||
100 | |||||||
101 | if($r) { | ||||||
102 | |||||||
103 | my $ct = $r->FileUpload( $field, 'ContentType'); | ||||||
104 | my $bf = $r->FileUpload( $field, 'BrowserFile'); | ||||||
105 | my $fh = $r->FileUpload( $field, 'FileHandle'); | ||||||
106 | my $mh = $r->FileUpload( $field, 'Mime-Header'); | ||||||
107 | my $tf = $r->FileUpload( $field, 'TempFile'); | ||||||
108 | |||||||
109 | $self->{'filemime'} = $ct; | ||||||
110 | |||||||
111 | my $file = $bf; | ||||||
112 | $file =~ s/.*[\/\\](.*)/$1/; | ||||||
113 | my $filepath = $path."\\".$file; | ||||||
114 | |||||||
115 | $self->{'filename'} = $file; | ||||||
116 | $self->{'filepath'} = $filepath; | ||||||
117 | |||||||
118 | my $code = "OK"; | ||||||
119 | my ($var, $virus); | ||||||
120 | |||||||
121 | my $size = -s $fh; | ||||||
122 | $self->{'filesize'} = $size; | ||||||
123 | |||||||
124 | if($self->{clamav} == 1) { | ||||||
125 | my $av = new File::Scan::ClamAV(port => 3310); | ||||||
126 | if($av->ping){ | ||||||
127 | my ($code,$virus) = $av->streamscan($var); | ||||||
128 | $self->{'filescan'} = $code; | ||||||
129 | } | ||||||
130 | } | ||||||
131 | |||||||
132 | if( ($code eq 'OK') && ($size <= $limit) ) { | ||||||
133 | |||||||
134 | if( ($owerwrite == 0) && (-e $filepath) ) { | ||||||
135 | return $self; | ||||||
136 | } else { | ||||||
137 | my $start_time = time(); | ||||||
138 | open(TMP,">$filepath") or carp "Can't open filepath $filepath"; | ||||||
139 | my ($bytes,$buffer,$tempsize); | ||||||
140 | while($bytes = read($fh,$buffer,1024) ) { | ||||||
141 | $tempsize += $bytes; | ||||||
142 | binmode TMP; | ||||||
143 | print TMP $buffer; | ||||||
144 | } | ||||||
145 | close(TMP); | ||||||
146 | my $time_took = time() - $start_time; | ||||||
147 | $self->{'copytime'} = $time_took; | ||||||
148 | |||||||
149 | if(-e $filepath) { | ||||||
150 | $self->{'status'} = 'OK'; | ||||||
151 | } else { | ||||||
152 | $self->{'status'} = 'Error writing file'; | ||||||
153 | carp $self->{'status'}; | ||||||
154 | } | ||||||
155 | } | ||||||
156 | |||||||
157 | } else { | ||||||
158 | $self->{'status'} = 'File containing virus or size overlimit'; | ||||||
159 | carp $self->{'status'}; | ||||||
160 | } | ||||||
161 | } else { | ||||||
162 | $self->{'status'} = 'No request object found'; | ||||||
163 | carp $self->{'status'}; | ||||||
164 | } | ||||||
165 | |||||||
166 | return $self; | ||||||
167 | |||||||
168 | } | ||||||
169 | |||||||
170 | 1; | ||||||
171 | __END__ |