[LON-CAPA-cvs] cvs: loncom /publisher loncfile.pm

albertel lon-capa-cvs@mail.lon-capa.org
Wed, 12 Feb 2003 19:56:14 -0000


albertel		Wed Feb 12 14:56:14 2003 EDT

  Modified files:              
    /loncom/publisher	loncfile.pm 
  Log:
  - can rename a directory (but can't move it)
  
  
Index: loncom/publisher/loncfile.pm
diff -u loncom/publisher/loncfile.pm:1.26 loncom/publisher/loncfile.pm:1.27
--- loncom/publisher/loncfile.pm:1.26	Mon Feb 10 18:26:15 2003
+++ loncom/publisher/loncfile.pm	Wed Feb 12 14:56:14 2003
@@ -9,7 +9,7 @@
 #  and displays a page showing the results of the action.
 #
 #
-# $Id: loncfile.pm,v 1.26 2003/02/10 23:26:15 albertel Exp $
+# $Id: loncfile.pm,v 1.27 2003/02/12 19:56:14 albertel Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -501,6 +501,12 @@
 		}
 	    }
 	    $request->print(&checksuffix($filename, $newfilename));
+	    #renaming a dir, delete the trailing /
+            #remove last element for current dir
+	    if ($filename =~ m|/$|) {
+		$filename =~ s|/$||;
+		$dir =~ s|/[^/]*$||;
+	    }
 	    my $return=&exists($user, $domain, $dir, $newfilename);
 	    $request->print($return);
 	    if ($return =~/^Error:/) {
@@ -837,9 +843,11 @@
 	    '<input type="hidden" name="action" value="'.$ENV{'form.action'}.'" />');
   
   if ($ENV{'form.action'} eq 'rename') {
-    
-    &Rename1($r, $fn, $uname, $udom, $dir);
-    
+      if (!defined($dir)) {
+	  $fn=~m:(.*)/:;
+	  $dir=$1;
+      }
+      &Rename1($r, $fn, $uname, $udom, $dir);
   } elsif ($ENV{'form.action'} eq 'delete') { 
     
     &Delete1($r, $uname, $fn);
@@ -913,8 +921,19 @@
 	 $newfile);
 
   if(-e $oldfile) {
-      unless(rename($oldfile,
-		    $directory.'/'.$newfile)) {
+      my $dest;
+
+      if ($oldfile =~ m|/$|) {
+	  #renaming a dir
+	  $oldfile =~ s|/$||;
+	  $dest=$directory;
+	  $dest=~s|(/)([^/]*)$|$1|;
+	  $dest.='/'.$newfile;
+      } else {
+	  $dest=$directory.'/'.$newfile;
+      }
+
+      unless(rename($oldfile,$dest)) {
 	  $request->print('<font color="red">Error: '.$!.'</font>');
 	  return 0;
       } else {}
@@ -1092,11 +1111,16 @@
     
     # Break down the file into it's component pieces.
     
-    $fn=~/(.*)\/([^\/]+)\.(\w+)$/;
-    my $dir=$1;			# Directory path
-    my $main=$2;		# Filename.
-    my $suffix=$3;		# Extension.
-    
+    my $dir;		# Directory path
+    my $main;		# Filename.
+    my $suffix;		# Extension.
+
+    if ($fn=~m:(.*)/([^/]+)\.(\w+)$:) {
+	$dir=$1;		# Directory path
+	$main=$2;		# Filename.
+	$suffix=$3;		# Extension.
+    }
+        
     my $dest;                   # On success this is where we'll go.
     
     &Debug($r, 
@@ -1116,12 +1140,19 @@
     
     if ($ENV{'form.action'} eq 'rename') { # Rename.
 	if($ENV{'form.newfilename'}) {
+	    if (!defined($dir)) {
+		$fn=~m:^(.*)/:;
+		$dir=$1;
+	    }
 	    if(!&Rename2($r, $uname, $dir, $fn, $ENV{'form.newfilename'})) {
 		return;
 	    }
 	    # Prepend the directory to the new name to form the basis of the
 	    # url of the new resource.
 	    #
+	    #renaming a dir
+            #remove last element for current dir
+	    if ($fn =~ m|/$|) { $dir =~ s|/[^/]*$||; }
 	    $dest = $dir."/".$ENV{'form.newfilename'};
 	}
     } elsif ($ENV{'form.action'} eq 'delete') {