Skip to content

Commit

Permalink
Fix saving image by uri (#511)
Browse files Browse the repository at this point in the history
* fix saving image by uri

* fix cs
  • Loading branch information
talyguryn authored Jan 13, 2019
1 parent 77fd88e commit e432939
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 31 deletions.
55 changes: 32 additions & 23 deletions www/application/classes/Controller/Transport.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Controller_Transport extends Controller_Base_preDispatch
);

private $type = null;
private $files = null;
private $file = null;

/**
* Transport file types
Expand All @@ -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)
);
}
Expand All @@ -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;
}
}
21 changes: 13 additions & 8 deletions www/application/classes/Model/Methods.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -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;
Expand All @@ -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'))) {
Expand All @@ -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');

Expand Down

0 comments on commit e432939

Please sign in to comment.