Index: branches/5.2.x/core/units/helpers/upload_helper.php =================================================================== diff -u -r16616 -r16650 --- branches/5.2.x/core/units/helpers/upload_helper.php (.../upload_helper.php) (revision 16616) +++ branches/5.2.x/core/units/helpers/upload_helper.php (.../upload_helper.php) (revision 16650) @@ -105,7 +105,7 @@ $this->deleteTempFiles($thumbs_path); } - return preg_replace('/^' . preg_quote($id, '/') . '_/', '', $filename); + return preg_replace('/^' . preg_quote($id, '/') . '_/', '', basename($file_path)); } /** @@ -116,7 +116,7 @@ * * @return boolean */ - public function resizeUploadedFile($file_path, $format) + public function resizeUploadedFile(&$file_path, $format) { /** @var ImageHelper $image_helper */ $image_helper = $this->Application->recallObject('ImageHelper'); @@ -132,10 +132,50 @@ $image_helper->ResizeImage($resize_file_path, $format) ); + $file_path = $this->replaceFileExtension( + $file_path, + pathinfo($resized_file_path, PATHINFO_EXTENSION) + ); + return rename($resized_file_path, $file_path); } /** + * Replace extension of uploaded file. + * + * @param string $file_path File path. + * @param string $new_file_extension New file extension. + * + * @return string + */ + protected function replaceFileExtension($file_path, $new_file_extension) + { + $file_path_without_temp_file_extension = kUtil::removeTempExtension($file_path); + $current_file_extension = pathinfo($file_path_without_temp_file_extension, PATHINFO_EXTENSION); + + // Format of resized file wasn't changed. + if ( $current_file_extension === $new_file_extension ) { + return $file_path; + } + + $ret = preg_replace( + '/\.' . preg_quote($current_file_extension, '/') . '$/', + '.' . $new_file_extension, + $file_path_without_temp_file_extension + ); + + // Add ".tmp" later, since it was removed. + if ( $file_path_without_temp_file_extension !== $file_path ) { + $ret .= '.tmp'; + } + + // After file extension change resulting filename might not be unique in that folder anymore. + $path = pathinfo($ret, PATHINFO_DIRNAME); + + return $path . '/' . $this->fileHelper->ensureUniqueFilename($path, basename($ret)); + } + + /** * Sends headers to ensure, that response is never cached. * * @return void