diff --git a/www/application/classes/Controller/Transport.php b/www/application/classes/Controller/Transport.php index e0c901827..b5c4ca7a5 100644 --- a/www/application/classes/Controller/Transport.php +++ b/www/application/classes/Controller/Transport.php @@ -7,7 +7,7 @@ class Controller_Transport extends Controller_Base_preDispatch ); private $type = null; - private $files = null; + private $file = null; /** * Transport file types @@ -19,42 +19,51 @@ class Controller_Transport extends Controller_Base_preDispatch */ public function action_file_uploader() { - if (isset($_POST['file'])) { - $url = Arr::get($_POST, 'file'); - $imageData = $this->methods->saveImageByUrl($url, 'upload/redactor_images/'); + $json_data = json_decode(file_get_contents( + 'php://input' + )); + + if (isset($json_data->url)) { + $url = $json_data->url; + + if (!$url) { + $this->transportResponse['message'] = 'Image URL is missing'; + goto finish; + } + + $this->file = $this->methods->getFile($url); + $imageData = $this->saveEditorImage(); if ($imageData) { $this->transportResponse['success'] = 1; - $this->transportResponse['data'] = array( - 'file' => array( - 'url' => Arr::get($imageData, 'name'), - 'width' => Arr::get($imageData, 'width', 0), - 'height' => Arr::get($imageData, 'height', 0) - ) + $this->transportResponse['file'] = array( + 'url' => '/upload/redactor_images/o_' . Arr::get($imageData, 'name'), + 'width' => Arr::get($imageData, 'width', 0), + 'height' => Arr::get($imageData, 'height', 0) ); } goto finish; } - $this->files = Arr::get($_FILES, 'image'); + $this->file = Arr::get($_FILES, 'image'); - if (!$this->files || !Upload::not_empty($this->files) || !Upload::valid($this->files)) { + if ( ! $this->file || ! Upload::not_empty($this->file) || ! Upload::valid($this->file)) { $this->transportResponse['message'] = 'File is missing or damaged'; goto finish; } - if (!Upload::size($this->files, '30M')) { + if ( ! Upload::size($this->file, '30M')) { $this->transportResponse['message'] = 'File size exceeded limit'; goto finish; } - $imageData = $this->saveEditorImage(); + $imageData = $this->saveEditorImage($this->file); if ($imageData) { $this->transportResponse['success'] = 1; - $this->transportResponse['file'] = array( - 'url' => '/upload/redactor_images/o_' . Arr::get($imageData, 'name'), - 'width' => Arr::get($imageData, 'width', 0), + $this->transportResponse['file'] = array( + 'url' => '/upload/redactor_images/o_' . Arr::get($imageData, 'name'), + 'width' => Arr::get($imageData, 'width', 0), 'height' => Arr::get($imageData, 'height', 0) ); } @@ -65,19 +74,19 @@ public function action_file_uploader() } private function saveEditorImage() - { - if (Upload::type($this->files, array('jpg', 'jpeg', 'png', 'gif'))) { - $imageData = $this->methods->saveImage($this->files, 'upload/redactor_images/'); - + { + if (Upload::type($this->file, array('jpg', 'jpeg', 'png', 'gif'))) { + $imageData = $this->methods->saveImage($this->file, 'upload/redactor_images/'); + if ($imageData) { return $imageData; } - + $this->transportResponse['message'] = 'Error while saving'; } else { $this->transportResponse['message'] = 'Unsupported file type'; } - + return false; } } diff --git a/www/application/classes/Model/Methods.php b/www/application/classes/Model/Methods.php index 316bbf4e5..f299873ba 100755 --- a/www/application/classes/Model/Methods.php +++ b/www/application/classes/Model/Methods.php @@ -91,20 +91,20 @@ public function save_cover($cover) public function saveImage($file, $path) { /** - * Проверки на Upload::valid($file) OR Upload::not_empty($file) OR Upload::size($file, '8M') делаются в контроллере. + * Проверки на Upload::valid($file) OR Upload::not_empty($file) OR Upload::size($file, '8M') делаются в контроллере. */ if (!Upload::type($file, array('jpg', 'jpeg', 'png', 'gif'))) { return false; } + if (!is_dir($path)) { mkdir($path); } - $copy_file = $file; - - if ($file = Upload::save($file, null, $path)) { + if (isset($file['tmp_name'])) { $filename = bin2hex(openssl_random_pseudo_bytes(16)) . '.jpg'; - $image = Image::factory($file); + $image = Image::factory($file['tmp_name']); + $image->save($path . $filename); $originalWidth = $image->width; $originalHeight = $image->height; @@ -128,10 +128,13 @@ public function saveImage($file, $path) $image->resize($width, $height, true); } } + $image->save($path . $prefix . '_' . $filename); } + // Delete the temporary file - unlink($file); + unlink($file['tmp_name']); + return array( 'width' => $originalWidth, 'height' => $originalHeight, @@ -145,6 +148,7 @@ public function saveImage($file, $path) public function saveRedactorsImage($file) { $filename = uniqid() . '.jpg'; + $image = Image::factory($file['tmp_name'])->save('upload/redactor_images/' . $filename); return $filename; @@ -156,7 +160,7 @@ public function saveRedactorsImage($file) */ public function saveImageByUrl($url, $path, $sizes = null, $forcedSizes = null) { - $file = $this->getFiles($url); + $file = $this->getFile($url); if ($file) { if (!Upload::type($file, array('jpg', 'jpeg', 'png', 'gif'))) { @@ -165,13 +169,14 @@ public function saveImageByUrl($url, $path, $sizes = null, $forcedSizes = null) if (!is_dir($path)) { mkdir($path); } + return $this->saveRedactorsImage($file); } return false; } - public function getFiles($url) + public function getFile($url) { $tempName = tempnam('/tmp', 'tmp_files');