Index: upload_replace.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/upload_replace/upload_replace.module,v
retrieving revision 1.1.2.5
diff -u -p -r1.1.2.5 upload_replace.module
--- upload_replace.module	30 Dec 2009 21:03:28 -0000	1.1.2.5
+++ upload_replace.module	9 Jul 2010 16:51:50 -0000
@@ -17,6 +17,7 @@
  * Implementation of hook_file_update()
  */
 function upload_replace_file_update(&$new_file) {
+  $replace_mode = variable_get('upload_replace_replace_mode', FILE_EXISTS_RENAME);
   if (!$new_file->fid) {
     //Nothing to do if no fileid
     return;
@@ -58,20 +59,27 @@ function upload_replace_file_update(&$ne
 
       $old_destination = $db_path;
       $query = "UPDATE {files} SET filepath='%s' WHERE fid=%d";
+      $query_size = "UPDATE {files} SET filesize = %d WHERE fid = %d";
+      
       //Swap the files
       if ($is_blocked) {
-        //move the blocking file to a temparary location
         $from = $desired_destination; //because $from gets overwritten by file_move
-        if (!file_move($from, $tmp_destination)) {
-          drupal_set_message('The file %old could not be moved to %new', array('%old' => $desired_destination, '%new' => $tmp_destination), 'error');
-          return;
+        if ($replace_mode == FILE_EXISTS_REPLACE) {
+          // delete the old file, letting the new one replace it
+          file_delete($from);
+        }
+        else {
+          //move the blocking file to a temporary location
+          if (!file_move($from, $tmp_destination)) {
+            drupal_set_message('The file %old could not be moved to %new', array('%old' => $desired_destination, '%new' => $tmp_destination), 'error');
+            return;
+          }
+          $tmp_destination = $from; //since the file copy may have placed the file in a modified location update the tmp destination with the actual location
+          //move blocking file was successful, update the DB
+          db_query($query, $tmp_destination, $blocking_file->fid);
         }
-        $tmp_destination = $from; //since the file copy may have placed the file in a modified location update the tmp destination with the actual location
-        //move blocking file was successful, update the DB
-        db_query($query, $tmp_destination, $blocking_file->fid);
       }
 
-
       //move the newfile to the prefered location
       $from = $old_destination;//because $from gets overwritten by file_move
       if (!file_move($from, $desired_destination)) {
@@ -84,15 +92,28 @@ function upload_replace_file_update(&$ne
 
 
       if ($is_blocked) {
-        //move the older file from temp to the new _0 location
-        if (!file_move($tmp_destination, $old_destination)) {
-          drupal_set_message('The file %old could not be moved to %new', array('%old' => $tmp_destination, '%new' => $old_destination), 'error');
-          return;
+        if ($replace_mode == FILE_EXISTS_REPLACE) {
+          // we want to replace the old file, making sure that nodes that refer
+          // to the file that previously had the new file's filename do not
+          // point to the old copy (with _0...n appended)
+          // update files table to set entry for old file to point to new one
+          $newfile_location = $from;
+          db_query($query, $newfile_location, $blocking_file->fid);
+          db_query($query_size, filesize($newfile_location), $blocking_file->fid);
+          //set the blocking file's path
+          $blocking_file->filepath = $newfile_location;
+        }
+        else {
+          //move the older file from temp to the new _0 location
+          if (!file_move($tmp_destination, $old_destination)) {
+            drupal_set_message('The file %old could not be moved to %new', array('%old' => $tmp_destination, '%new' => $old_destination), 'error');
+            return;
+          }
+          //move blocking file was successful, update the DB with the actual location after file copy, so we use tmp_destination as it was updated during the move
+          db_query($query, $tmp_destination, $blocking_file->fid);
+          //set the blocking files newpath
+          $blocking_file->filepath = $tmp_destination;
         }
-        //move blocking file was successful, update the DB with the actual location after file copy, so we use tmp_destination as it was updated during the move
-        db_query($query, $tmp_destination, $blocking_file->fid);
-        //set the blocking files newpath
-        $blocking_file->filepath = $tmp_destination;
       }
     }
   }
@@ -149,3 +170,44 @@ function upload_replace_file_update(&$ne
 function upload_replace_file_delete(&$file) {
   $file->filepath = db_result(db_query("SELECT filepath FROM {files} WHERE fid = %d", $file->fid));
 }
+
+/**
+ * Menu callback for settings
+ */
+function upload_replace_admin() {
+
+  include_once('./includes/file.inc');
+
+  $form = array();
+
+  $form['upload_replace_replace_mode'] = array(
+    '#type' => 'radios',
+    '#title' => t('Replace mode'),
+    '#description' => t('This setting controls what happens if a file is uploaded and one by the same filename already exists.'),
+    '#default_value' => variable_get('upload_replace_replace_mode', FILE_EXISTS_RENAME),
+    '#options' => array(
+      FILE_EXISTS_RENAME => t('Rename the previous file appending an incrementing number to the filename (filename_0.ext ... filename_<em>n</em>.ext)'),
+      FILE_EXISTS_REPLACE => t('Replace the previously existing file and keep the filename')
+    ),
+  );
+
+  return system_settings_form($form);
+}
+
+/**
+ * Implementation of hook_menu()
+ */
+function upload_replace_menu() {
+  $items = array();
+
+  $items['admin/settings/upload_replace'] = array(
+    'title' => 'Upload Replace',
+    'description' => 'Configure settings for Upload Replace.',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('upload_replace_admin'),
+    'access arguments' => array('administer site configuration'),
+    'type' => MENU_NORMAL_ITEM,
+  );
+
+  return $items;
+}
\ No newline at end of file
