Skip to content
Snippets Groups Projects
Commit 97f8db7f authored by cboyden's avatar cboyden Committed by Joseph Olstad
Browse files

Issue # 179932: File replacement logic improvement and allowed file formats fixes.

parent 541a1cde
Branches
No related merge requests found
Pipeline #225053 passed with warnings
......@@ -831,39 +831,34 @@ function file_entity_edit($form, &$form_state, $file) {
// Set up replacement file validation.
$replacement_options = !empty($form_state['#upload_options']) ? $form_state['#upload_options'] : array();
// The replacement file must have an extension valid for the original type.
$file_extensions = array();
$file_type_name = isset($file->type) ? $file->type : file_get_type($file);
if (!empty($replacement_options['file_extensions'])) {
$file_extensions = explode(' ', $replacement_options['file_extensions']);
}
elseif ($file_type_name && ($file_type = file_type_load($file_type_name))) {
$file_extensions = file_type_get_valid_extensions($file_type);
}
// Set allowed file extensions.
if (!empty($file_extensions)) {
// Set to type based file extensions.
$replacement_options['file_extensions'] = implode(' ', $file_extensions);
// Replacement file must have the same extension as the original file.
$replacement_options['file_extensions'] = pathinfo($file->uri, PATHINFO_EXTENSION);
// Replacement file must also be one of the allowed filetypes.
$allowed_filetypes = explode(' ', variable_get('file_entity_default_allowed_extensions', 'jpg jpeg gif png txt doc docx xls xlsx pdf ppt pptx pps ppsx odt ods odp mp3 mov mp4 m4a m4v mpeg avi ogg oga ogv weba webp webm'));
if (!empty($replacement_options['file_extensions']) && in_array($replacement_options['file_extensions'], $allowed_filetypes)) {
$form['replace_upload'] = array(
'#type' => 'file',
'#title' => t('Replace file'),
'#description' => t('This file will replace the existing file. This action cannot be undone.'),
'#upload_validators' => file_entity_get_upload_validators($replacement_options),
'#pre_render' => array('file_entity_upload_validators_pre_render'),
);
$form['replace_keep_original_filename'] = array(
'#type' => 'checkbox',
'#title' => t('Keep original filename'),
'#default_value' => variable_get('file_entity_file_replace_options_keep_original_filename', FALSE),
'#description' => t('Rename the newly uploaded file to the name of the original file. This action cannot be undone.'),
);
}
else {
// Fallback to the extension of the current file.
$replacement_options['file_extensions'] = pathinfo($file->uri, PATHINFO_EXTENSION);
$form['replace_upload'] = array(
'#type' => 'item',
'#title' => t('Replace file'),
'#description' => t('This file cannot be replaced.'),
);
}
$form['replace_upload'] = array(
'#type' => 'file',
'#title' => t('Replace file'),
'#description' => t('This file will replace the existing file. This action cannot be undone.'),
'#upload_validators' => file_entity_get_upload_validators($replacement_options),
'#pre_render' => array('file_entity_upload_validators_pre_render'),
);
$form['replace_keep_original_filename'] = array(
'#type' => 'checkbox',
'#title' => t('Keep original filename'),
'#default_value' => variable_get('file_entity_file_replace_options_keep_original_filename', FALSE),
'#description' => t('Rename the newly uploaded file to the name of the original file. This action cannot be undone.'),
);
}
$form['preview'] = file_view_file($file, 'preview');
......
......@@ -56,7 +56,7 @@ class FileEntityTestHelper extends DrupalWebTestCase {
protected function createFileEntity($settings = array()) {
// Populate defaults array.
$settings += array(
'filepath' => 'Файл для тестирования ' . $this->randomName(), // Prefix with non-latin characters to ensure that all file-related tests work with international filenames.
'filepath' => 'Файл для тестирования ' . $this->randomName() . '.txt', // Prefix with non-latin characters to ensure that all file-related tests work with international filenames.
'filemime' => 'text/plain',
'uid' => 1,
'timestamp' => REQUEST_TIME,
......@@ -1087,6 +1087,16 @@ class FileEntityReplaceTestCase extends FileEntityTestHelper {
$this->drupalPost('file/' . $file->fid . '/edit', $edit, t('Save'));
$this->assertRaw(t('The specified file %file could not be uploaded. Only files with the following extensions are allowed:', array('%file' => $image->filename)), 'File validation works, upload failed correctly.');
// Restrict allowed document filetypes to exclude plain text files.
variable_set('file_entity_default_allowed_extensions', 'doc docx pdf');
// Test that the file can't be replaced if there's a problem with the original file's extension.
$this->drupalGet('file/' . $file->fid . '/edit');
$this->assertRaw(t('This file cannot be replaced.'), 'File extension filter works, upload prevented correctly.');
// Clean up variable.
variable_del('file_entity_default_allowed_extensions');
// Create a non-local file record.
$file2 = new stdClass();
$file2->uri = 'oembed://' . $this->randomName();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment