diff --git a/filefield_paths.module b/filefield_paths.module index 7544d94..03b47bb 100644 --- a/filefield_paths.module +++ b/filefield_paths.module @@ -122,12 +122,24 @@ function filefield_paths_form_alter(&$form, $form_state, $form_id) { '#weight' => 10, ); + // File exists behavior. Keep disabled if the file name option is the + // same as the original file, as this can result in the uploaded file + // itself being moved onto itself and then deleted. + $form['instance']['settings']['filefield_paths']['replace_existing_files'] = array( + '#type' => 'checkbox', + '#title' => t('Replace existing files'), + '#description' => t('If a file with the same name already exists in the destination directory, replace it.'), + '#weight' => 11, + '#default_value' => isset($settings['replace_existing_files']) ? $settings['replace_existing_files'] : FALSE, + '#disabled' => FALSE, + ); + // Retroactive updates. $form['instance']['settings']['filefield_paths']['retroactive_update'] = array( '#type' => 'checkbox', '#title' => t('Retroactive update'), '#description' => t('Move and rename previously uploaded files.') . '
' . t('Warning: This feature should only be used on developmental servers or with extreme caution.') . '
', - '#weight' => 11, + '#weight' => 12, ); // Active updating. @@ -136,7 +148,7 @@ function filefield_paths_form_alter(&$form, $form_state, $form_id) { '#title' => t('Active updating'), '#default_value' => isset($settings['active_updating']) ? $settings['active_updating'] : FALSE, '#description' => t('Actively move and rename previously uploaded files as required.') . '
' . t('Warning: This feature should only be used on developmental servers or with extreme caution.') . '
', - '#weight' => 12 + '#weight' => 13, ); } } @@ -271,6 +283,12 @@ function filefield_paths_field_storage_pre_update($entity_type, $entity) { } $files[] = &$file; } + } + + // Store the original entity before we modify it + $entity->original = $entity; + + foreach ($entity->{$field['field_name']} as $langcode => $deltas) { // Invoke hook_filefield_paths_process_file(). foreach (module_implements('filefield_paths_process_file') as $module) { if (function_exists($function = "{$module}_filefield_paths_process_file")) { diff --git a/modules/filefield_paths.inc b/modules/filefield_paths.inc index 55c4cad..662ba72 100644 --- a/modules/filefield_paths.inc +++ b/modules/filefield_paths.inc @@ -53,7 +53,7 @@ function filefield_paths_filefield_paths_field_settings($field, $instance) { function filefield_paths_filefield_paths_process_file($type, $entity, $field, $instance, $langcode, &$items) { if (isset($instance['settings']['filefield_paths'])) { $settings = $instance['settings']['filefield_paths']; - foreach ($items as &$file) { + foreach ($items as $delta => &$file) { if (filefield_paths_uri_is_local($file['uri']) && ($file['timestamp'] == REQUEST_TIME || $settings['active_updating'])) { $token_data = array( 'file' => (object) $file, @@ -74,9 +74,18 @@ function filefield_paths_filefield_paths_process_file($type, $entity, $field, $i $file['uri'] = "{$field['settings']['uri_scheme']}://" . ltrim(filefield_paths_process_string($settings['file_path']['value'], $token_data, $settings['file_path']['options']) . "/{$file['filename']}", '/'); // Finalize file if necessary. - if ($file !== $old_file) { + if ($file['uri'] !== $old_file['uri']) { $dirname = drupal_dirname($file['uri']); - if (file_prepare_directory($dirname, FILE_CREATE_DIRECTORY) && file_move((object) $old_file, $file['uri'])) { + $file_exists_behavior = empty($settings['replace_existing_files']) ? FILE_EXISTS_RENAME : FILE_EXISTS_REPLACE; + if (file_prepare_directory($dirname, FILE_CREATE_DIRECTORY)) { + + $old_file['filename'] = $file['filename']; + $moved_file = file_move((object) $old_file, $file['uri'], $file_exists_behavior); + if ($moved_file && $moved_file->fid != $file['fid']) { + $entity->{$instance['field_name']}[$entity->language][$delta] = (array) $moved_file; + file_delete((object) $old_file, TRUE); + } + // Process regular expression. _filefield_paths_replace_path($old_file['uri'], $file['uri'], $entity);