Page MenuHomeIn-Portal Phabricator

D320.diff
No OneTemporary

File Metadata

Created
Mon, Jan 6, 3:47 AM

D320.diff

Index: branches/5.2.x/core/kernel/utility/formatters/upload_formatter.php
===================================================================
--- branches/5.2.x/core/kernel/utility/formatters/upload_formatter.php
+++ branches/5.2.x/core/kernel/utility/formatters/upload_formatter.php
@@ -245,19 +245,13 @@
$real_name = $this->_getRealFilename($value['name'], $options, $object);
$file_name = $this->FullPath . $real_name;
+ $moved = move_uploaded_file($value['tmp_name'], $file_name);
$storage_format = isset($options['storage_format']) ? $options['storage_format'] : false;
if ( $storage_format ) {
- /** @var ImageHelper $image_helper */
- $image_helper = $this->Application->recallObject('ImageHelper');
-
- move_uploaded_file($value['tmp_name'], $value['tmp_name'] . '.jpg'); // add extension, so ResizeImage can work
- $url = $image_helper->ResizeImage($value['tmp_name'] . '.jpg', $storage_format);
- $tmp_name = preg_replace('/^' . preg_quote($this->Application->BaseURL(), '/') . '/', '/', $url);
- $moved = rename($tmp_name, $file_name);
- }
- else {
- $moved = move_uploaded_file($value['tmp_name'], $file_name);
+ /** @var kUploadHelper $upload_helper */
+ $upload_helper = $this->Application->recallObject('kUploadHelper');
+ $moved = $upload_helper->resizeUploadedFile($file_name, $storage_format);
}
if ( !$moved ) {
Index: branches/5.2.x/core/units/helpers/upload_helper.php
===================================================================
--- branches/5.2.x/core/units/helpers/upload_helper.php
+++ branches/5.2.x/core/units/helpers/upload_helper.php
@@ -16,12 +16,21 @@
{
/**
+ * File helper reference
+ *
+ * @var FileHelper
+ */
+ protected $fileHelper = null;
+
+ /**
* Creates kUploadHelper instance.
*/
public function __construct()
{
parent::__construct();
+ $this->fileHelper = $this->Application->recallObject('FileHelper');
+
// 5 minutes execution time
@set_time_limit(5 * 60);
}
@@ -77,22 +86,12 @@
throw new kUploaderException('Write permissions not set on the server, please contact server administrator.', 500);
}
- /** @var FileHelper $file_helper */
- $file_helper = $this->Application->recallObject('FileHelper');
- $filename = $file_helper->ensureUniqueFilename($tmp_path, $filename);
+ $filename = $this->fileHelper->ensureUniqueFilename($tmp_path, $filename);
$storage_format = $this->getStorageFormat($this->Application->GetVar('field'), $event);
+ $this->moveUploadedFile($tmp_path . $filename);
if ( $storage_format ) {
- /** @var ImageHelper $image_helper */
- $image_helper = $this->Application->recallObject('ImageHelper');
-
- $this->moveUploadedFile($value['tmp_name'] . '.jpg'); // add extension, so ResizeImage can work
- $url = $image_helper->ResizeImage($value['tmp_name'] . '.jpg', $storage_format);
- $tmp_name = preg_replace('/^' . preg_quote($this->Application->BaseURL(), '/') . '/', '/', $url);
- rename($tmp_name, $tmp_path . $filename);
- }
- else {
- $this->moveUploadedFile($tmp_path . $filename);
+ $this->resizeUploadedFile($tmp_path . $filename, $storage_format);
}
$this->deleteTempFiles($tmp_path);
@@ -108,6 +107,33 @@
}
/**
+ * Resizes uploaded file.
+ *
+ * @param string $file_path File path.
+ * @param string $format Format.
+ *
+ * @return boolean
+ */
+ public function resizeUploadedFile($file_path, $format)
+ {
+ /** @var ImageHelper $image_helper */
+ $image_helper = $this->Application->recallObject('ImageHelper');
+
+ // Add extension, so that "ImageHelper::ResizeImage" can work.
+ $resize_file_path = tempnam(sys_get_temp_dir(), 'uploaded_') . '.jpg';
+
+ if ( rename($file_path, $resize_file_path) === false ) {
+ return false;
+ }
+
+ $resized_file_path = $this->fileHelper->urlToPath(
+ $image_helper->ResizeImage($resize_file_path, $format)
+ );
+
+ return rename($resized_file_path, $file_path);
+ }
+
+ /**
* Sends headers to ensure, that response is never cached.
*
* @return void

Event Timeline