From 2b742cea13427983bead97eaa96ae9694d675dc8 Mon Sep 17 00:00:00 2001 From: Christian Kilb Date: Thu, 17 Mar 2016 22:06:54 +0100 Subject: [PATCH 01/23] add possibility to use a callback function as naming attribute for images --- .gitignore | 1 + src/Frozennode/Administrator/Fields/File.php | 86 +++++++++++++++++- src/Frozennode/Administrator/Fields/Image.php | 3 +- .../Administrator/Includes/Multup.php | 90 +++++-------------- src/controllers/AdminController.php | 36 +++++++- src/routes.php | 4 +- 6 files changed, 139 insertions(+), 81 deletions(-) diff --git a/.gitignore b/.gitignore index 205c3ee52..322e2f127 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ composer.lock .DS_Store /nbproject/private/ nbproject +/.idea diff --git a/src/Frozennode/Administrator/Fields/File.php b/src/Frozennode/Administrator/Fields/File.php index 3cde16662..ce63c9724 100644 --- a/src/Frozennode/Administrator/Fields/File.php +++ b/src/Frozennode/Administrator/Fields/File.php @@ -2,9 +2,16 @@ namespace Frozennode\Administrator\Fields; use Frozennode\Administrator\Includes\Multup; +use Illuminate\Support\Facades\Input; +use Illuminate\Support\Str; +use Symfony\Component\HttpFoundation\File\UploadedFile; +use Illuminate\Support\Facades\File as LaravelFile; class File extends Field { + const NAMING_KEEP = 'keep'; + const RANDOM_DEFAULT_LENGTH = 32; + /** * The specific defaults for subclasses to override * @@ -25,7 +32,6 @@ class File extends Field { */ protected $rules = array( 'location' => 'required|string|directory', - 'naming' => 'in:keep,random', 'length' => 'integer|min:0', 'mimes' => 'string', ); @@ -42,7 +48,16 @@ public function build() $route = $this->config->getType() === 'settings' ? 'admin_settings_file_upload' : 'admin_file_upload'; //set the upload url to the proper route - $this->suppliedOptions['upload_url'] = $url->route($route, array($this->config->getOption('name'), $this->suppliedOptions['field_name'])); + $model = $this->config->getDataModel(); + + $this->suppliedOptions['upload_url'] = $url->route( + $route, + array( + 'model' => $this->config->getOption('name'), + 'field' => $this->suppliedOptions['field_name'], + 'id' => $model ? $model->{$model->getKeyName()} : null, + ) + ); } /** @@ -56,10 +71,73 @@ public function doUpload() //use the multup library to perform the upload $result = Multup::open('file', 'max:' . $this->getOption('size_limit') * 1000 . $mimes, $this->getOption('location'), - $this->getOption('naming') === 'random') - ->set_length($this->getOption('length')) + $this->getFilename()) ->upload(); return $result[0]; } + + /** + * @return UploadedFile + */ + protected function getFile() + { + return Input::file('file'); + } + + /** + * @return string + */ + protected function getFileExtension() + { + $file = $this->getFile(); + return LaravelFile::extension($file->getClientOriginalName()); + } + + /** + * @return null + */ + protected function getFilename() + { + $naming = $this->suppliedOptions['naming']; + + if (self::NAMING_KEEP === $naming) { + return $this->getFile()->getClientOriginalName(); + } + + $filename = $this->getCustomFilename(); + + if ($filename) { + return $filename; + } + + return $this->getRandomFileName(); + } + + /** + * @return string|null + */ + protected function getCustomFilename() + { + $naming = $this->suppliedOptions['naming']; + + if (!is_callable($naming)) { + return null; + } + + return call_user_func($naming, $this->config->getDataModel(), $this->getFile()); + } + + /** + * @return string + */ + protected function getRandomFilename() + { + $length = self::RANDOM_DEFAULT_LENGTH; + + if (isset($this->suppliedOptions['length'])) { + $length = $this->suppliedOptions['length']; + } + return Str::random($length) . '.' . $this->getFileExtension(); + } } \ No newline at end of file diff --git a/src/Frozennode/Administrator/Fields/Image.php b/src/Frozennode/Administrator/Fields/Image.php index 214412463..8a142be3a 100644 --- a/src/Frozennode/Administrator/Fields/Image.php +++ b/src/Frozennode/Administrator/Fields/Image.php @@ -32,9 +32,8 @@ public function doUpload() { //use the multup library to perform the upload $result = Multup::open('file', 'image|max:' . $this->getOption('size_limit') * 1000, $this->getOption('location'), - $this->getOption('naming') === 'random') + $this->getFilename()) ->sizes($this->getOption('sizes')) - ->set_length($this->getOption('length')) ->upload(); return $result[0]; diff --git a/src/Frozennode/Administrator/Includes/Multup.php b/src/Frozennode/Administrator/Includes/Multup.php index 2916a6368..c6ef9dd92 100644 --- a/src/Frozennode/Administrator/Includes/Multup.php +++ b/src/Frozennode/Administrator/Includes/Multup.php @@ -3,7 +3,6 @@ use Illuminate\Support\Facades\Input; use Illuminate\Support\Facades\Validator; -use Illuminate\Support\Facades\File; use Illuminate\Support\Str; //Use Admin\Libraries\Includes\Resize as Resize; @@ -30,9 +29,9 @@ class Multup { private $rules; /* - randomize uploaded filename + uploaded filename */ - private $random; + private $filename; /* path relative to /public/ that the image should be saved in @@ -44,16 +43,6 @@ class Multup { */ private $input; - /* - How long the random filename should be - */ - private $random_length = 32; - - /* - * Callback function for setting your own random filename - */ - private $random_cb; - /* * Sizing information for thumbs to create * array ( width, height, crop_type, path_to_save, quality) @@ -76,12 +65,12 @@ class Multup { * Instantiates the Multup * @param mixed $file The file array provided by Laravel's Input::file('field_name') or a path to a file */ - public function __construct($input, $rules, $path, $random) + public function __construct($input, $rules, $path, $filename) { - $this->input = $input; - $this->rules = $rules; - $this->path = $path; - $this->random = $random; + $this->input = $input; + $this->rules = $rules; + $this->path = $path; + $this->filename = $filename; } /** @@ -90,23 +79,12 @@ public function __construct($input, $rules, $path, $random) * @param string $input name of the file to upload * @param string $rules laravel style validation rules string * @param string $path relative to /public/ to move the images if valid - * @param bool $random Whether or not to randomize the filename, the filename will be set to a 32 character string if true + * @param string $filename filename of the uploaded file. if null, original name is kept * @return Multup */ - public static function open($input, $rules, $path, $random = true) - { - return new Multup( $input, $rules, $path, $random ); - } - - /* - * Set the length of the randomized filename - * @param int $len - */ - public function set_length($len) + public static function open($input, $rules, $path, $filename = null) { - $this->random_length = $len; - - return $this; + return new Multup( $input, $rules, $path, $filename ); } /* @@ -185,35 +163,21 @@ private function upload_image() $errors = array(); $original_name = $this->image[$this->input]->getClientOriginalName(); $path = ''; - $filename = ''; $resizes = ''; if($validation->fails()){ /* use the messages object for the erros */ $errors = implode('. ', $validation->messages()->all()); } else { - - if($this->random){ - if(is_callable($this->random_cb)){ - $filename = call_user_func( $this->random_cb, $original_name ); - } else { - $ext = File::extension($original_name); - $filename = $this->generate_random_filename().'.'.$ext; - } - } else { - $filename = $original_name; - } - /* upload the file */ - $save = $this->image[$this->input]->move($this->path, $filename); - //$save = Input::upload($this->input, $this->path, $filename); + $save = $this->image[$this->input]->move($this->path, $this->filename); if($save){ - $path = $this->path.$filename; + $path = $this->path.$this->filename; if(is_array($this->image_sizes)){ $resizer = new Resize(); - $resizes = $resizer->create($save, $this->path, $filename, $this->image_sizes); + $resizes = $resizer->create($save, $this->path, $this->filename, $this->image_sizes); } } else { @@ -221,27 +185,13 @@ private function upload_image() } } - return compact('errors', 'path', 'filename', 'original_name', 'resizes' ); - } - - /* - * Default random filename generation - */ - private function generate_random_filename() - { - return Str::random($this->random_length); - } - - /* - * Default random filename generation - */ - public function filename_callback( $func ) - { - if(is_callable($func)){ - $this->random_cb = $func; - } - - return $this; + return [ + 'errors' => $errors, + 'path' => $path, + 'filename' => $this->filename, + 'original_name' => $original_name, + 'resizes' => $resizes + ]; } /* diff --git a/src/controllers/AdminController.php b/src/controllers/AdminController.php index f069dd496..bf52b6bfd 100644 --- a/src/controllers/AdminController.php +++ b/src/controllers/AdminController.php @@ -125,7 +125,7 @@ public function save($modelName, $id = false) $config = app('itemconfig'); $fieldFactory = app('admin_field_factory'); $actionFactory = app('admin_action_factory'); - + if (array_key_exists('form_request', $config->getOptions()) && $this->formRequestErrors !== null) { return response()->json(array( 'success' => false, @@ -451,11 +451,41 @@ public function displayFile() * * @return JSON */ - public function fileUpload($modelName, $fieldName) + public function fileUpload($modelName, $fieldName, $id = false) { - $fieldFactory = app('admin_field_factory'); + $config = app('itemconfig'); + $columnFactory = app('admin_column_factory'); + $fieldFactory = app('admin_field_factory'); + $fields = $fieldFactory->getEditFields(); + $model = $config->getModel($id, $fields, $columnFactory->getIncludedColumns($fields)); + + if ($model) { + $config->setDataModel($model); + } + + return $this->executeFileUpload($fieldName); + } + + /** + * @param string $modelName + * @param string $fieldName + * + * @return JSON + */ + public function settingsFileUpload($modelName, $fieldName) + { + return $this->executeFileUpload($fieldName); + } + /** + * @param string $fieldName + * + * @return JSON + */ + protected function executeFileUpload($fieldName) + { //get the model and the field object + $fieldFactory = app('admin_field_factory'); $field = $fieldFactory->findField($fieldName); return response()->JSON($field->doUpload()); diff --git a/src/routes.php b/src/routes.php index dcd6b9332..254252384 100644 --- a/src/routes.php +++ b/src/routes.php @@ -63,7 +63,7 @@ //Settings file upload Route::post('settings/{settings}/{field}/file_upload', array( 'as' => 'admin_settings_file_upload', - 'uses' => 'Frozennode\Administrator\AdminController@fileUpload' + 'uses' => 'Frozennode\Administrator\AdminController@settingsFileUpload' )); }); @@ -125,7 +125,7 @@ )); //File Uploads - Route::post('{model}/{field}/file_upload', array( + Route::post('{model}/{field}/{id?}/file_upload', array( 'as' => 'admin_file_upload', 'uses' => 'Frozennode\Administrator\AdminController@fileUpload' )); From a70e4fd5a8401d0e290821cd2ebfbbc4fa432eff Mon Sep 17 00:00:00 2001 From: Christian Kilb Date: Thu, 17 Mar 2016 22:25:41 +0100 Subject: [PATCH 02/23] fix tests for image naming callback function --- tests/Fields/FileTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/Fields/FileTest.php b/tests/Fields/FileTest.php index 4cc8dffa8..ea422f3c5 100644 --- a/tests/Fields/FileTest.php +++ b/tests/Fields/FileTest.php @@ -60,7 +60,8 @@ public function testBuild() $this->validator->shouldReceive('arrayGet')->times(3) ->shouldReceive('getUrlInstance')->once()->andReturn($url); $this->config->shouldReceive('getType')->once() - ->shouldReceive('getOption')->once(); + ->shouldReceive('getOption')->once() + ->shouldReceive('getDataModel')->once(); $this->field->build(); } From 965c2faaa4f9f147046ee49cdc5b1daaeee4aa30 Mon Sep 17 00:00:00 2001 From: Christian Kilb Date: Thu, 17 Mar 2016 22:34:30 +0100 Subject: [PATCH 03/23] allow naming option to be optional again --- src/Frozennode/Administrator/Fields/File.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Frozennode/Administrator/Fields/File.php b/src/Frozennode/Administrator/Fields/File.php index ce63c9724..edc045de2 100644 --- a/src/Frozennode/Administrator/Fields/File.php +++ b/src/Frozennode/Administrator/Fields/File.php @@ -99,7 +99,7 @@ protected function getFileExtension() */ protected function getFilename() { - $naming = $this->suppliedOptions['naming']; + $naming = $this->getOption('naming'); if (self::NAMING_KEEP === $naming) { return $this->getFile()->getClientOriginalName(); @@ -119,7 +119,7 @@ protected function getFilename() */ protected function getCustomFilename() { - $naming = $this->suppliedOptions['naming']; + $naming = $this->getOption('naming'); if (!is_callable($naming)) { return null; From 56fe6f8a9c2ab49aceda6583f72e92d27eac0301 Mon Sep 17 00:00:00 2001 From: Christian Kilb Date: Fri, 22 Apr 2016 16:25:33 +0200 Subject: [PATCH 04/23] allow for image size width or height to be null to calculate it automatically --- src/Frozennode/Administrator/Includes/Resize.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Frozennode/Administrator/Includes/Resize.php b/src/Frozennode/Administrator/Includes/Resize.php index 4241e9879..cd018436c 100644 --- a/src/Frozennode/Administrator/Includes/Resize.php +++ b/src/Frozennode/Administrator/Includes/Resize.php @@ -68,7 +68,6 @@ public function create($file, $path, $filename, $sizes ){ $resized = array(); foreach($sizes as $size){ - $this->new_width = $size[0]; //$new_width; $this->new_height = $size[1]; //$new_height; $this->option = $size[2]; //crop type @@ -100,6 +99,15 @@ private function do_resize( $image, $save_path, $image_quality ) $this->width = imagesx( $image ); $this->height = imagesy( $image ); + + if (!$this->new_height) { + $this->new_height = $this->height / $this->width * $this->new_width; + } + + if (!$this->new_width) { + $this->new_width = $this->width / $this->height * $this->new_height; + } + // Get optimal width and height - based on $option. $option_array = $this->get_dimensions( $this->new_width , $this->new_height , $this->option ); From 56046b8882db4d02ab2754228f822a881f4d0436 Mon Sep 17 00:00:00 2001 From: Christian Kilb Date: Fri, 22 Apr 2016 16:47:12 +0200 Subject: [PATCH 05/23] fix settings --- src/views/settings.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/views/settings.php b/src/views/settings.php index f0ff7ebe4..238c8d7f1 100644 --- a/src/views/settings.php +++ b/src/views/settings.php @@ -3,7 +3,7 @@