| 1 | package CGI::AppBuilder::File; | ||
| 2 | |||
| 3 | # Perl standard modules | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use Getopt::Std; | ||
| 7 | use POSIX qw(strftime); | ||
| 8 | use Carp; | ||
| 9 | use CGI qw(:standard); | ||
| 10 | use CGI::AppBuilder; | ||
| 11 | use CGI::AppBuilder::Message qw(:echo_msg); | ||
| 12 | use File::Copy; | ||
| 13 | use File::Basename; | ||
| 14 | |||
| 15 | our $VERSION = 0.10; | ||
| 16 | require Exporter; | ||
| 17 | our @ISA = qw(Exporter CGI::AppBuilder); | ||
| 18 | our @EXPORT = qw(); | ||
| 19 | our @EXPORT_OK = qw(disp_file copy_file prt_bin_file | ||
| 20 | ); | ||
| 21 | our %EXPORT_TAGS = ( | ||
| 22 | file => [qw(disp_file copy_file)], | ||
| 23 | all => [@EXPORT_OK] | ||
| 24 | ); | ||
| 25 | |||
| 26 | =head1 NAME | ||
| 27 | |||
| 28 | CGI::AppBuilder::File - File module | ||
| 29 | |||
| 30 | =head1 SYNOPSIS | ||
| 31 | |||
| 32 | use CGI::AppBuilder::File; | ||
| 33 | |||
| 34 | my $ab = CGI::AppBuilder::File->new( | ||
| 35 | 'ifn', 'my_init.cfg', 'opt', 'vhS:a:'); | ||
| 36 | my ($q, $ar, $ar_log) = $ab->start_app($0, \%ARGV); | ||
| 37 | my $fn = 'test.txt'; | ||
| 38 | print $ab->disp_file($fn, $ar); | ||
| 39 | |||
| 40 | =head1 DESCRIPTION | ||
| 41 | |||
| 42 | This class provides methods for reading and parsing configuration | ||
| 43 | files. | ||
| 44 | |||
| 45 | =cut | ||
| 46 | |||
| 47 | =head2 new (ifn => 'file.cfg', opt => 'hvS:') | ||
| 48 | |||
| 49 | This is a inherited method from CGI::AppBuilder. See the same method | ||
| 50 | in CGI::AppBuilder for more details. | ||
| 51 | |||
| 52 | =cut | ||
| 53 | |||
| 54 | sub new { | ||
| 55 | my ($s, %args) = @_; | ||
| 56 | return $s->SUPER::new(%args); | ||
| 57 | } | ||
| 58 | |||
| 59 | =head2 disp_file ($fn, $ar) | ||
| 60 | |||
| 61 | Input variables: | ||
| 62 | |||
| 63 | $q - CGI object | ||
| 64 | $ar - array ref for parameters | ||
| 65 | $fn - file name | ||
| 66 | $rt - whether to return the text to caller | ||
| 67 | $st - search text | ||
| 68 | |||
| 69 | Variables used or routines called: | ||
| 70 | N/A | ||
| 71 | |||
| 72 | How to use: | ||
| 73 | |||
| 74 | print $self->disp_file($ar,'test.txt'); | ||
| 75 | my $txt = $self->disp_file($ar,'test.txt',1); | ||
| 76 | |||
| 77 | Return: none or text | ||
| 78 | |||
| 79 | =cut | ||
| 80 | |||
| 81 | sub disp_file { | ||
| 82 | my ($s,$q,$ar,$fn,$rt,$st) = @_; | ||
| 83 | |||
| 84 | # $s->disp_param($ar); | ||
| 85 | |||
| 86 | my $prg = 'AppBuilder::File->disp_file'; | ||
| 87 | my $f1 = $s->set_param('f', $ar); | ||
| 88 | $fn = (!$fn && $f1)? $f1 : ''; | ||
| 89 | my $txt_ext = 'txt|log|sql|pl|csv|lis|ddl|trc|cmd|bat|pm|trm|htm|html'; | ||
| 90 | |||
| 91 | if (!$fn) { | ||
| 92 | $s->echo_msg("ERR: ($prg) no files to be displayed.", 0); | ||
| 93 | return; | ||
| 94 | } | ||
| 95 | my ($fname, $path, $sfx) = fileparse($fn,qr{\..*}); | ||
| 96 | if ($fn !~ /($txt_ext)$/i) { | ||
| 97 | $s->prt_bin_file($fn); | ||
| 98 | return; | ||
| 99 | } | ||
| 100 | |||
| 101 | my $f_html = ($sfx =~ /(htm|html)$/i) ? 1 : 0; | ||
| 102 | my $t = ($f_html) ? '' : "<center><b>$fname$sfx</b></center>\n<hr>\n<pre>\n"; | ||
| 103 | |||
| 104 | open FILE, "<$fn" or die "ERR: could not open $fn: $!\n"; | ||
| 105 | while (<FILE>) { | ||
| 106 | next if $_ =~ /^\s*$/; | ||
| 107 | if (! $f_html) { | ||
| 108 | s/</\</g; s/>/\>/g; | ||
| 109 | s//^L/g; # change the non-printable char to printable char | ||
| 110 | } | ||
| 111 | if ($st) { | ||
| 112 | s/($st)/<font color=red>$1<\/font>/ig; | ||
| 113 | } | ||
| 114 | my ($tt, $i, $n) = ($_, -1, 120); | ||
| 115 | # if (length($tt) < $n) { | ||
| 116 | # $t .= $tt; next; | ||
| 117 | # } | ||
| 118 | # while (length($tt) >= $n) { | ||
| 119 | # ++$i; | ||
| 120 | # if ($i) { # the second line | ||
| 121 | # $t .= " "x4 . substr($tt, 0, $n) . "\n"; | ||
| 122 | # } else { # first line | ||
| 123 | # $t .= substr($tt, 0, $n) . "\n"; | ||
| 124 | # } | ||
| 125 | # $tt = substr($tt, $n); | ||
| 126 | # } | ||
| 127 | # $t .= " "x4 . $tt; | ||
| 128 | $t .= $tt; | ||
| 129 | } | ||
| 130 | close FILE; | ||
| 131 | $t = "<pre>\n$t</pre>\n<hr>\n" if !$f_html; | ||
| 132 | return $t if $rt; | ||
| 133 | print $t; | ||
| 134 | } | ||
| 135 | |||
| 136 | |||
| 137 | =head2 prt_bin_file ($fn) | ||
| 138 | |||
| 139 | Input variables: | ||
| 140 | |||
| 141 | $fn - file name | ||
| 142 | |||
| 143 | Variables used or routines called: | ||
| 144 | N/A | ||
| 145 | |||
| 146 | How to use: | ||
| 147 | |||
| 148 | print $self->prt_bin_file('test.tar'); | ||
| 149 | |||
| 150 | Return: none or text | ||
| 151 | |||
| 152 | =cut | ||
| 153 | |||
| 154 | sub prt_bin_file { | ||
| 155 | my ($s, $fn) = @_; | ||
| 156 | |||
| 157 | my $cr = { | ||
| 158 | '.xls' => 'application/msexcel', | ||
| 159 | # '.csv' => 'application/msexcel', | ||
| 160 | '.doc' => 'application/msword', | ||
| 161 | '.rtf' => 'application/msword', | ||
| 162 | '.pdf' => 'application/pdf', | ||
| 163 | '.vsd' => 'application/visio', | ||
| 164 | '.jpg' => 'image/jpeg', | ||
| 165 | '.gif' => 'image/gif', | ||
| 166 | '.txt' => 'text/html', | ||
| 167 | '.html' => 'text/html', | ||
| 168 | '.htm' => 'text/html', | ||
| 169 | '.ddl' => 'text/html', | ||
| 170 | '.trc' => 'text/html', | ||
| 171 | '.xml' => 'text/xml', | ||
| 172 | '.ppt' => 'application/powerpoint', | ||
| 173 | '.pptx' => 'application/powerpoint', | ||
| 174 | '.tgz' => 'application/tar', | ||
| 175 | '.mov' => 'video/quicktime', | ||
| 176 | '.erwin' => 'application/ERwin', | ||
| 177 | '.zip' => 'application/tar', | ||
| 178 | }; | ||
| 179 | |||
| 180 | my ($fname, $path, $sfx) = fileparse($fn,qr{\..*$}); | ||
| 181 | my $ct = (exists $cr->{$sfx}) ? $cr->{$sfx} : $cr->{'.jpg'}; | ||
| 182 | |||
| 183 | print "Content-type: $ct\n"; | ||
| 184 | print "Content-Disposition: inline; filename=$fn\n\n"; | ||
| 185 | |||
| 186 | binmode STDOUT; | ||
| 187 | my $buffer; | ||
| 188 | open FILE, "$fn" or die "ERR: could not open $fn: $!\n"; | ||
| 189 | binmode(FILE); | ||
| 190 | while ( | ||
| 191 | read (FILE, $buffer, 65536) # read in (up to) 64k chunks | ||
| 192 | && print $buffer | ||
| 193 | ) {}; | ||
| 194 | close FILE; | ||
| 195 | } | ||
| 196 | |||
| 197 | |||
| 198 | =head2 copy_file ($f1, $f2, $txt) | ||
| 199 | |||
| 200 | Input variables: | ||
| 201 | |||
| 202 | $f1 - source file name | ||
| 203 | $f2 - target file name | ||
| 204 | $txt - text to be appended to $f2 | ||
| 205 | |||
| 206 | Variables used or routines called: | ||
| 207 | |||
| 208 | File::Copy | ||
| 209 | copy - copy files | ||
| 210 | |||
| 211 | How to use: | ||
| 212 | |||
| 213 | my $f1 = 'text1.txt'; | ||
| 214 | my $f2 = 'text1.out'; | ||
| 215 | # duplicate the file and add 'quit' in the end | ||
| 216 | $self->copy_file($f1, $f2, 'quit'); | ||
| 217 | |||
| 218 | Return: none | ||
| 219 | |||
| 220 | =cut | ||
| 221 | |||
| 222 | sub copy_file { | ||
| 223 | my ($s,$f1,$f2,$txt) = @_; | ||
| 224 | |||
| 225 | return if !$f1 || !-f $f1; | ||
| 226 | # return if !$f2 || !-f $f2; | ||
| 227 | copy($f1,$f2) or croak "ERR: Copy failed from $f1 to $f2: $!\n"; | ||
| 228 | return if !$txt; | ||
| 229 | open FF, ">>$f2" or croak "ERR: could not write to $f2: $!\n"; | ||
| 230 | print FF "$txt\n"; | ||
| 231 | close FF; | ||
| 232 | } | ||
| 233 | |||
| 234 | |||
| 235 | 1; | ||
| 236 | |||
| 237 | =head1 HISTORY | ||
| 238 | |||
| 239 | =over 4 | ||
| 240 | |||
| 241 | =item * Version 0.10 | ||
| 242 | |||
| 243 | This version extracts the disp_form method from CGI::Getopt class, | ||
| 244 | inherits the new constructor from CGI::AppBuilder, and adds | ||
| 245 | new methods of replace_named_variables, explode_variable, and | ||
| 246 | explode_html. | ||
| 247 | |||
| 248 | =item * Version 0.20 | ||
| 249 | |||
| 250 | =cut | ||
| 251 | |||
| 252 | =head1 SEE ALSO (some of docs that I check often) | ||
| 253 | |||
| 254 | Oracle::Loader, Oracle::Trigger, CGI::Getopt, File::Xcopy, | ||
| 255 | CGI::AppBuilder, CGI::AppBuilder::Message, CGI::AppBuilder::Log, | ||
| 256 | CGI::AppBuilder::Config, etc. | ||
| 257 | |||
| 258 | =head1 AUTHOR | ||
| 259 | |||
| 260 | Copyright (c) 2005 Hanming Tu. All rights reserved. | ||
| 261 | |||
| 262 | This package is free software and is provided "as is" without express | ||
| 263 | or implied warranty. It may be used, redistributed and/or modified | ||
| 264 | under the terms of the Perl Artistic License (see | ||
| 265 | http://www.perl.com/perl/misc/Artistic.html) | ||
| 266 | |||
| 267 | =cut | ||
| 268 |