Index: private_upload.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/private_upload/private_upload.module,v
retrieving revision 1.13.2.7
diff -u -p -r1.13.2.7 private_upload.module
--- private_upload.module	26 Jul 2008 00:49:36 -0000	1.13.2.7
+++ private_upload.module	29 Apr 2009 16:20:03 -0000
@@ -264,39 +264,53 @@ function private_upload_nodeapi(&$node, 
             $file = (object)$file;  // Convert file to object for compatibility
             $fid = $file->fid; // for the cases where we have temp fid for uploaded files
             $success = false;
-	          $filepath = $file->filepath; // need copy if file_move fails.
-	          $public = file_directory_path();
+            $filepath = $file->filepath; // need copy if file_move fails.
+            // save original name
+            $filepath_orig = $filepath;
+            $public = file_directory_path();
             $private_path = _private_upload_path(); // actual path of private files
             $file_is_private = _private_upload_is_file_private($filepath);
             if( $file->private && !$file_is_private ) { 
-	            // private flag is set, but file NOT yet listed as being in private repo,
+              // private flag is set, but file NOT yet listed as being in private repo,
               //   so try and move it from public area to private repo
-              if( file_move($filepath, $private_path, FILE_EXISTS_REPLACE) ) {
+              if( file_move($filepath, $private_path, FILE_EXISTS_RENAME) ) {
+                // check whether the file was renamed
+                if ($filepath_orig != $filepath) {
+                  // update the filename in the object if so
+                  $file->filename = basename($filepath);
+                  $file->filepath = $filepath;
+                }
                 $success = true;          
               }
               else {
                 drupal_set_message( "Could not move the file ($file->filepath) to the private directory ($private_path).", 'error' );
-		          }	                         
+              }                          
             }
             else if (!$file->private && $file_is_private) { 
               // private flag is false, but file IS g in private repo
               // so try and move it from private repo to public area
-              if (file_move($filepath, $public, FILE_EXISTS_REPLACE)) {
+              if (file_move($filepath, $public, FILE_EXISTS_RENAME)) {
+                // update the filepath
+                $file->filepath = $filepath;
+                // check whether the file was renamed
+                if ($filepath_orig != $filepath) {
+                  // update the filename in the object if so
+                  $file->filename = basename($filepath);
+                }
                 $success = true;
-	            }
-		          else {
-		            drupal_set_message( "Could not move the file ($file->filepath) to the public directory ($public).", 'error' );
-		          }	            
+              }
+              else {
+                drupal_set_message( "Could not move the file ($file->filepath) to the public directory ($public).", 'error' );
+              }             
             }
             
             if( $success ) { // we were able to move the file, so update filepath in db.
-              _private_upload_update_filepath($filepath, $fid);
+              _private_upload_update_filepath($file);
               $row_count = db_affected_rows();
               if( $row_count != 1 ) {
                 drupal_set_message( "Error: Unable to make uploaded file private! (". $row_count .")", 'error'  );
               }
             }
-                     
           } // Done with all the files.
         }        
       }
@@ -587,9 +601,17 @@ function _private_upload_migrate_private
   while( $file = db_fetch_object($result) ) {
     // file is attached to a private node, but is a public file, so move it.
     $filepath = $file->filepath;
-    if( file_move($filepath, $private_path, FILE_EXISTS_REPLACE) ) {
+    $filepath_orig = $filepath;
+    if( file_move($filepath, $private_path, FILE_EXISTS_RENAME) ) {
+      // update the file path
+      $file->filepath = $filepath;
+      // check whether the file was renamed
+      if ($filepath_orig != $filepath) {
+        // update the filename in the object if so
+        $file->filename = basename($filepath);
+      }
       $output .= t("Making !filename private", array('!filename'=>$file->filename)). "<br>";     
-      _private_upload_update_filepath($filepath, $file->fid);
+      _private_upload_update_filepath($file);
     }
     else {
       $output .= t("Could not move %filepath to private directory (fid: %fid attached to node: nid).", 
@@ -602,11 +624,11 @@ function _private_upload_migrate_private
 /**
  * Set the filepath for the file in the db.
  *
- * @param string $filepath
- * @param int $fid: Unique id for file. 
+ * @param object $file
  */
-function _private_upload_update_filepath($filepath, $fid) {
-  db_query("UPDATE {files} SET filepath = '%s' WHERE fid=%d", $filepath, $fid); 
+function _private_upload_update_filepath($file) {
+  db_query("UPDATE {files} SET filepath = '%s', filename = '%s' WHERE fid=%d", 
+           $file->filepath, $file->filename, $file->fid); 
 }
 
 // *****************************************************************************
