Changeset View
Changeset View
Standalone View
Standalone View
branches/5.2.x/core/units/helpers/upload_helper.php
Show First 20 Lines • Show All 99 Lines • ▼ Show 20 Line(s) | |||||
$thumbs_path = preg_replace('/^' . preg_quote(FULL_PATH, '/') . '/', '', $tmp_path, 1); | $thumbs_path = preg_replace('/^' . preg_quote(FULL_PATH, '/') . '/', '', $tmp_path, 1); | ||||
$thumbs_path = FULL_PATH . THUMBS_PATH . $thumbs_path; | $thumbs_path = FULL_PATH . THUMBS_PATH . $thumbs_path; | ||||
if ( file_exists($thumbs_path) ) { | if ( file_exists($thumbs_path) ) { | ||||
$this->deleteTempFiles($thumbs_path); | $this->deleteTempFiles($thumbs_path); | ||||
} | } | ||||
return preg_replace('/^' . preg_quote($id, '/') . '_/', '', $filename); | return preg_replace('/^' . preg_quote($id, '/') . '_/', '', basename($file_path)); | ||||
} | } | ||||
/** | /** | ||||
* Resizes uploaded file. | * Resizes uploaded file. | ||||
* | * | ||||
* @param string $file_path File path. | * @param string $file_path File path. | ||||
* @param string $format Format. | * @param string $format Format. | ||||
* | * | ||||
* @return boolean | * @return boolean | ||||
*/ | */ | ||||
public function resizeUploadedFile($file_path, $format) | public function resizeUploadedFile(&$file_path, $format) | ||||
{ | { | ||||
/** @var ImageHelper $image_helper */ | /** @var ImageHelper $image_helper */ | ||||
$image_helper = $this->Application->recallObject('ImageHelper'); | $image_helper = $this->Application->recallObject('ImageHelper'); | ||||
// Add extension, so that "ImageHelper::ResizeImage" can work. | // Add extension, so that "ImageHelper::ResizeImage" can work. | ||||
$resize_file_path = tempnam(sys_get_temp_dir(), 'uploaded_') . '.jpg'; | $resize_file_path = tempnam(sys_get_temp_dir(), 'uploaded_') . '.jpg'; | ||||
if ( rename($file_path, $resize_file_path) === false ) { | if ( rename($file_path, $resize_file_path) === false ) { | ||||
return false; | return false; | ||||
} | } | ||||
$resized_file_path = $this->fileHelper->urlToPath( | $resized_file_path = $this->fileHelper->urlToPath( | ||||
$image_helper->ResizeImage($resize_file_path, $format) | $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); | 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. | * Sends headers to ensure, that response is never cached. | ||||
* | * | ||||
* @return void | * @return void | ||||
*/ | */ | ||||
protected function disableBrowserCache() | protected function disableBrowserCache() | ||||
{ | { | ||||
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); | header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); | ||||
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); | header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); | ||||
▲ Show 20 Lines • Show All 218 Lines • Show Last 20 Lines |