[LON-CAPA-cvs] cvs: loncom /lonnet/perl lonnet.pm lonuploadrep.pm

albertel lon-capa-cvs@mail.lon-capa.org
Thu, 02 Sep 2004 18:01:52 -0000


albertel		Thu Sep  2 14:01:52 2004 EDT

  Modified files:              
    /loncom/lonnet/perl	lonnet.pm lonuploadrep.pm 
  Log:
  - modified getfile,repcopy, and added repcopy_userfile
      repcopy hands userfile repcopies off to repcopy_userfile
      repcopy_userfile can take either urls or paths
  - lonuploadrep changed to use the repcopy instead of using getfile and trhrowing the contents away
  
  
Index: loncom/lonnet/perl/lonnet.pm
diff -u loncom/lonnet/perl/lonnet.pm:1.537 loncom/lonnet/perl/lonnet.pm:1.538
--- loncom/lonnet/perl/lonnet.pm:1.537	Tue Aug 31 11:40:49 2004
+++ loncom/lonnet/perl/lonnet.pm	Thu Sep  2 14:01:52 2004
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # TCP networking package
 #
-# $Id: lonnet.pm,v 1.537 2004/08/31 15:40:49 albertel Exp $
+# $Id: lonnet.pm,v 1.538 2004/09/02 18:01:52 albertel Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -1067,7 +1067,12 @@
 sub repcopy {
     my $filename=shift;
     $filename=~s/\/+/\//g;
-    if ($filename=~/^\/home\/httpd\/html\/adm\//) { return OK; }
+    if ($filename=~m|^/home/httpd/html/adm/|) { return OK; }
+    if ($filename=~m|^/home/httpd/html/lonUsers/|) { return OK; }
+    if ($filename=~m|^/home/httpd/html/userfiles/| or
+	$filename=~m|^/*uploaded/|) { 
+	return &repcopy_userfile($filename);
+    }
     $filename=~s/[\n\r]//g;
     my $transname="$filename.in.transfer";
     if ((-e $filename) || (-e $transname)) { return OK; }
@@ -4931,30 +4936,32 @@
 # the local server.   
 
 sub getfile {
-    my ($file,$caller) = @_;
+    my ($file) = @_;
 
-    if ($file !~ m|^/*uploaded/(\w+)/(\w+)/(.+)$|) {
-	# normal file from res space
-	&repcopy($file);
-        return &readfile($file);
-    }
-
-    my $info;
-    my $cdom = $1;
-    my $cnum = $2;
-    my $filename = $3;
-    my $path = $Apache::lonnet::perlvar{'lonDocRoot'}.'/userfiles';
-    my ($lwpresp,$rtncode);
-    my $localfile = $path.'/'.$cdom.'/'.$cnum.'/'.$filename;
-    if (-e "$localfile") {
-	my @fileinfo = stat($localfile);
-	$lwpresp = &getuploaded('HEAD',$file,$cdom,$cnum,\$info,\$rtncode);
+    if ($file =~ m|^/*uploaded/|) { $file=&filelocation("",$file); }
+    &repcopy($file);
+    return &readfile($file);
+}
+
+sub repcopy_userfile {
+    my ($file)=@_;
+
+    if ($file =~ m|^/*uploaded/|) { $file=&filelocation("",$file); }
+    if ($file =~ m|^/home/httpd/html/lonUsers/|) { return OK; }
+
+    my ($cdom,$cnum,$filename) = 
+	($file=~m|^\Q$perlvar{'lonDocRoot'}\E/+userfiles/+([^/]+)/+([^/]+)/+(.*)|);
+    my ($info,$rtncode);
+    my $uri="/uploaded/$cdom/$cnum/$filename";
+    if (-e "$file") {
+	my @fileinfo = stat($file);
+	my $lwpresp = &getuploaded('HEAD',$uri,$cdom,$cnum,\$info,\$rtncode);
 	if ($lwpresp ne 'ok') {
 	    if ($rtncode eq '404') {
-		unlink($localfile);
+		unlink($file);
 	    }
 	    #my $ua=new LWP::UserAgent;
-	    #my $request=new HTTP::Request('GET',&tokenwrapper($file));
+	    #my $request=new HTTP::Request('GET',&tokenwrapper($uri));
 	    #my $response=$ua->request($request);
 	    #if ($response->is_success()) {
 	#	return $response->content;
@@ -4964,21 +4971,21 @@
 	    return -1;
 	}
 	if ($info < $fileinfo[9]) {
-	    return &readfile($localfile);
+	    return OK;
 	}
 	$info = '';
-	$lwpresp = &getuploaded('GET',$file,$cdom,$cnum,\$info,\$rtncode);
+	$lwpresp = &getuploaded('GET',$uri,$cdom,$cnum,\$info,\$rtncode);
 	if ($lwpresp ne 'ok') {
 	    return -1;
 	}
     } else {
-	$lwpresp = &getuploaded('GET',$file,$cdom,$cnum,\$info,\$rtncode);
+	my $lwpresp = &getuploaded('GET',$uri,$cdom,$cnum,\$info,\$rtncode);
 	if ($lwpresp ne 'ok') {
 	    my $ua=new LWP::UserAgent;
-	    my $request=new HTTP::Request('GET',&tokenwrapper($file));
+	    my $request=new HTTP::Request('GET',&tokenwrapper($uri));
 	    my $response=$ua->request($request);
 	    if ($response->is_success()) {
-		return $response->content;
+		$info=$response->content;
 	    } else {
 		return -1;
 	    }
@@ -4987,6 +4994,7 @@
 	if ($filename =~ m|^(.+)/[^/]+$|) {
 	    push @parts, split(/\//,$1);
 	}
+	my $path = $perlvar{'lonDocRoot'}.'/userfiles';
 	foreach my $part (@parts) {
 	    $path .= '/'.$part;
 	    if (!-e $path) {
@@ -4994,13 +5002,10 @@
 	    }
 	}
     }
-    open (FILE,">$localfile");
+    open(FILE,">$file");
     print FILE $info;
     close(FILE);
-    if ($caller eq 'uploadrep') {
-	return 'ok';
-    }
-    return $info;
+    return OK;
 }
 
 sub tokenwrapper {
@@ -5264,10 +5269,6 @@
 	 $hostip{$id}=$ip;
 	 $iphost{$ip}=$id;
 	 if ($role eq 'library') { $libserv{$id}=$name; }
-       } else {
-	 if ($configline) {
-	   &logthis("Skipping hosts.tab line -$configline-");
-	 }
        }
     }
     close($config);
Index: loncom/lonnet/perl/lonuploadrep.pm
diff -u loncom/lonnet/perl/lonuploadrep.pm:1.3 loncom/lonnet/perl/lonuploadrep.pm:1.4
--- loncom/lonnet/perl/lonuploadrep.pm:1.3	Mon Jul 19 16:16:40 2004
+++ loncom/lonnet/perl/lonuploadrep.pm	Thu Sep  2 14:01:52 2004
@@ -51,8 +51,8 @@
                 }
             }
         }
-        my $response=&Apache::lonnet::getfile($r->uri,'uploadrep');
-        if ($response eq 'ok') {
+        my $response=&Apache::lonnet::repcopy_userfile($r->uri);
+        if ($response eq OK) {
             $r->filename($filename);
             return OK;
         }