
Шустрый

Профиль
Группа: Участник
Сообщений: 76
Регистрация: 21.10.2004
Репутация: нет Всего: 2
|
В продолжение темы, начатой мною на http://forum.vingrad.ru/topic-131345.html, выкладываю свой скрипт закачки по фтп. Возможно кому-нибудь пригодится Код |
#!/usr/bin/perl use strict; use Fcntl; use CGI; use Net::FTP; use CGI::Carp qw(fatalsToBrowser);
## CONFIG ###################################
my %server =( 'addr' => '192.168.1.2', 'user' => 'ftpLogin', 'passwd' => 'ftpPassword' );
my $logFile = 'errors.log'; my $file_path = '/domains/boom.ge/public_html/converter.rar';
##############################################
my($httpStatus,$fileSize,$contentSize,$offsetSize,$isFinished);
$file_path =~ m/^.*(\\|\/)(.*)/; my $file_name = $2;
my $cgi = new CGI; my $ftp = Net::FTP->new($server{'addr'}, Debug => 0) or handleError(__LINE__,'Connect failed','503');
$ftp->login($server{'user'},$server{'passwd'}) or handleError(__LINE__,$ftp->message,'503');
$ftp->binary(); $fileSize = $ftp->size($file_path) or handleError(__LINE__,$ftp->message,'404');
if(defined $ENV{'HTTP_RANGE'}){
$httpStatus = '206 Partial content'; ($offsetSize = $ENV{'HTTP_RANGE'}) =~ s![^\d]!!g; $contentSize = $fileSize - $offsetSize; } else{
$httpStatus = '200 Ok'; $offsetSize = 0; $contentSize = $fileSize; }
print $cgi->header( -status => $httpStatus, -Date => getGMT(), -Pragma => 'no-cache', -Cache_Control => 'None', -Expires => 'Thu, 19 Nov 1981 08:52:00 GMT', -Content_type => 'application/octet-stream; name="'.$file_name.'"', -Content_Disposition => 'attachment; filename="'.$file_name.'"', -Accept_Ranges => 'bytes', -Content_Length => $contentSize, -Content_Range => 'bytes '.$offsetSize.'-'.($fileSize-1).'/'.$fileSize, -Connection => 'close', );
$isFinished = $ftp->get($file_path,\*STDOUT,$offsetSize); if($isFinished){ # The file is completely downloaded # Increase downloads counter or do semething else } $ftp->quit;
1;
## FUNCTIONS #################################
sub getGMT(){ # GMT DateTime Format: Thu, 19 Nov 1981 08:52:00 GMT my @dayweek = ('Sun','Mon','Tue','Thi','Thu','Fri','Sat'); my @month = ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'); my($sec,$min,$hour,$day,$mon,$year,$wday) = gmtime; sprintf("%s, %02d %s %d %02d:%02d:%02d GMT",$dayweek[$wday],$day,$month[$mon],$year+1900,$hour,$min,$sec); }
sub handleError{ my ($errLine,$errMsg,$httpCode) = @_; my ($sec,$min,$hour,$day,$mon,$year) = gmtime; my $error = getServerError($httpCode); open(OUT, ">>$logFile"); print OUT "[".sprintf("%02d.%02d.%d %02d:%02d",$day,$mon,$year+1900,$hour,$min)."] Error on line ".chomp($errLine).": $errMsg\n"; close OUT; print $cgi->header( -status => $error->{'header'}, -Content_type => 'text/html', -Pragma => 'no-cache', -Cache_Control => 'None', -Expires => 'Thu, 19 Nov 1981 08:52:00 GMT', ); print $error->{'body'}; exit; }
sub getServerError{ my $errCode = @_[0]; my $answers = { '404' => { 'header' => '404 Not Found', 'body' => "<html>\n<head>\n<title>404 Not Found</title>\n</head>\n <body><h1>Not Found</h1><p>The server has not found anything matching the Request-URI.</p></body></html>" }, '503' => { 'header' => '503 Service Unavailable', 'body' => "<html>\n<head>\n<title>503 Service Unavailable</title>\n</head>\n <body><h1>Service Unavailable</h1><p>The server is currently unable to handle the request due to a temporary overloading or maintenance of the server.</p></body></html>\n" } };
$answers->{$errCode}; }
|
Это сообщение отредактировал(а) Black Rabbit - 14.1.2007, 01:04
|