diff --git a/filefield_paths.module b/filefield_paths.module index 2f9dfce..2439771 100644 --- a/filefield_paths.module +++ b/filefield_paths.module @@ -122,12 +122,23 @@ 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 //isset($settings['file_name']) && $settings['file_name']['value'] == '[file:ffp-name-only-original].[file:ffp-extension-original]' ? TRUE : 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 +147,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 ); } } @@ -254,6 +265,12 @@ function filefield_paths_entity_update($entity, $type) { } $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 42016fc..6bdb25a 100644 --- a/modules/filefield_paths.inc +++ b/modules/filefield_paths.inc @@ -65,9 +65,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][0] = (array) $moved_file; + file_delete((object) $old_file, TRUE); + } + // Process regular expression. _filefield_paths_replace_path($old_file['uri'], $file['uri'], $entity);