diff --git a/imce.module b/imce.module index f3dc4bf..3b10df0 100644 --- a/imce.module +++ b/imce.module @@ -5,6 +5,7 @@ * Implements the necessary hooks for the file browser to work properly. */ +define('FILE_EXISTS_RENAME_EXISTING', 4); /** * Implements hook_menu(). */ diff --git a/inc/imce.admin.inc b/inc/imce.admin.inc index d88cd28..868f2e2 100644 --- a/inc/imce.admin.inc +++ b/inc/imce.admin.inc @@ -79,7 +79,8 @@ function imce_admin_form($form, &$form_state) { '#options' => array( FILE_EXISTS_RENAME => t('Keep the existing file renaming the new one'), FILE_EXISTS_ERROR => t('Keep the existing file rejecting the new one'), - FILE_EXISTS_REPLACE => t('Replace the existing file with the new one') + FILE_EXISTS_REPLACE => t('Replace the existing file with the new one'), + FILE_EXISTS_RENAME_EXISTING => t('Rename the existing file') ), ); $form['common']['thumb_method'] = array( diff --git a/inc/imce.page.inc b/inc/imce.page.inc index bcb00b2..f922038 100644 --- a/inc/imce.page.inc +++ b/inc/imce.page.inc @@ -292,10 +292,17 @@ function imce_upload_submit($form, &$form_state) { //save uploaded file. $replace = variable_get('imce_settings_replace', FILE_EXISTS_RENAME); - if ($file = file_save_upload('imce', $validators, $diruri, $replace)) { + if ($replace == FILE_EXISTS_RENAME_EXISTING) { + $rename_existing = TRUE; + $replace = FILE_EXISTS_RENAME; + } + else { + $rename_existing = FALSE; + } + if ($file = file_save_upload('imce', $validators, $diruri, $replace)) { //core bug #54223. - if ($replace == FILE_EXISTS_RENAME) { + if ($replace == FILE_EXISTS_RENAME && !$rename_existing) { $name = basename($file->uri); if ($name != $file->filename) { $file->filename = $name; @@ -306,6 +313,49 @@ function imce_upload_submit($form, &$form_state) { $file->uid = $imce['uid'];//global user may not be the owner. $file->status = FILE_STATUS_PERMANENT; file_save($file); + + + // If the existing file should be renamed, check to see if the newly- + // uploaded file was renamed (which would indicate that a file already + // existed with that file name) and swap the two files + if ($rename_existing) { + $new_filename = basename($file->uri); + // Check if the file was renamed + if ($new_filename != $file->filename) { + // File has been renamed: swap the two file names, so that the + // existing file is the one with the new name. + $existing_file_target_uri = $file->uri; + $new_file_target_uri = dirname($file->uri) . DIRECTORY_SEPARATOR . $file->filename; + // Find the existing file ID, by checking the URI where the new file + // should go. + $existing_fid = db_select('file_managed', 'f') + ->fields('f', array('fid')) + ->condition('uri', $new_file_target_uri) + ->execute() + ->fetchField(); + + if ($existing_fid > 0) { + $existing_file = file_load($existing_fid); + // First, move the new file to a dummy, so it's out of the way + if ($new_file = file_move($file, $file->uri . '.dummy')) { + // Move the old file + $existing_file = file_move($existing_file, $existing_file_target_uri); + // Update file list to show the old, newly-moved file + $img = imce_image_info($existing_file->uri); + $existing_file->width = $img ? $img['width'] : 0; + $existing_file->height = $img ? $img['height'] : 0; + imce_add_file($existing_file, $imce); + + // Move the new file back to where it belongs + if ($new_file = file_move($new_file, $new_file_target_uri)) { + // Reset the $file object + $file = $new_file; + } + } + } + } + } + imce_file_register($file); drupal_set_message(t('%filename has been uploaded.', array('%filename' => $file->filename)));