From 37c8c731ec28985c811b80dadf60123f33f27457 Mon Sep 17 00:00:00 2001 From: Cesar Montoya Date: Sat, 16 May 2015 00:42:41 +0200 Subject: [PATCH 1/6] Services.yml. Put class FileUploader as parameter --- Resources/config/services.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Resources/config/services.yml b/Resources/config/services.yml index bddb8bc..1970e2a 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -62,9 +62,11 @@ parameters: max_width: 1140 max_height: 1140 + punk_ave.file_uploader.class: PunkAve\FileUploaderBundle\Services\FileUploader + services: punk_ave.file_uploader: - class: PunkAve\FileUploaderBundle\Services\FileUploader + class: %punk_ave.file_uploader.class% arguments: - file_base_path: '%file_uploader.file_base_path%' web_base_path: '%file_uploader.web_base_path%' From 93b3fa2b28bfb8f92c9477a216ed9a8e5a1a7f22 Mon Sep 17 00:00:00 2001 From: Cesar Montoya Date: Sat, 16 May 2015 00:42:41 +0200 Subject: [PATCH 2/6] Services.yml. Put class FileUploader as parameter... --- Resources/config/services.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Resources/config/services.yml b/Resources/config/services.yml index bddb8bc..1970e2a 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -62,9 +62,11 @@ parameters: max_width: 1140 max_height: 1140 + punk_ave.file_uploader.class: PunkAve\FileUploaderBundle\Services\FileUploader + services: punk_ave.file_uploader: - class: PunkAve\FileUploaderBundle\Services\FileUploader + class: %punk_ave.file_uploader.class% arguments: - file_base_path: '%file_uploader.file_base_path%' web_base_path: '%file_uploader.web_base_path%' From c4f5696046acb91d7de16d414fe1524972386a51 Mon Sep 17 00:00:00 2001 From: Cesar Montoya Date: Sat, 16 May 2015 01:33:22 +0200 Subject: [PATCH 3/6] Parameter for override files --- BlueImp/UploadHandler.php | 29 +++++++++++++++++++---------- Resources/config/services.yml | 8 +++++++- Services/FileUploader.php | 27 ++++++++++++++++----------- 3 files changed, 42 insertions(+), 22 deletions(-) diff --git a/BlueImp/UploadHandler.php b/BlueImp/UploadHandler.php index e14f5fb..cf7ff27 100644 --- a/BlueImp/UploadHandler.php +++ b/BlueImp/UploadHandler.php @@ -17,7 +17,13 @@ class UploadHandler { protected $options; - function __construct($options=null) { + function __construct($options=null) + { + $this->setOptions($options); + } + + function setOptions($options=null) + { $this->options = array( 'script_url' => $this->getFullUrl().'/', 'upload_dir' => dirname($_SERVER['SCRIPT_FILENAME']).'/files/', @@ -66,7 +72,8 @@ function __construct($options=null) { 'ai', 'psd', 'pdf', - 'eps') + 'eps'), + 'override_files' => false ); if ($options) { $this->options = array_replace_recursive($this->options, $options); @@ -121,7 +128,7 @@ protected function get_file_objects() { protected function create_scaled_image($file_name, $options) { $file_path = $this->options['upload_dir'].$file_name; $new_file_path = $options['upload_dir'].$file_name; - + if (extension_loaded('imagick')) { // Special Postscript case if($this->is_ps_file($file_name)) { @@ -134,11 +141,11 @@ protected function create_scaled_image($file_name, $options) { $new_file_path .= '.png'; $im->writeImage($file_path); } catch (\ImagickException $e) { - return false; + return false; } } } - + list($img_width, $img_height) = @getimagesize($file_path); if (!$img_width || !$img_height) { return false; @@ -275,8 +282,10 @@ protected function trim_file_name($name, $type, $index) { $file_name .= '.'.$matches[1]; } if ($this->options['discard_aborted_uploads']) { - while(is_file($this->options['upload_dir'].$file_name)) { - $file_name = $this->upcount_name($file_name); + if (!$this->options['override_files']) { + while(is_file($this->options['upload_dir'].$file_name)) { + $file_name = $this->upcount_name($file_name); + } } } return $file_name; @@ -292,7 +301,7 @@ protected function orient_image($file_path) { return false; } $orientation = intval(@$exif['Orientation']); - if (!in_array($orientation, array(3, 6, 8))) { + if (!in_array($orientation, array(3, 6, 8))) { return false; } $image = @imagecreatefromjpeg($file_path); @@ -353,7 +362,7 @@ protected function handle_file_upload($uploaded_file, $name, $size, $type, $erro $file->url = $this->options['upload_url'].rawurlencode($file->name); foreach($this->options['image_versions'] as $version => $options) { if ($this->create_scaled_image($file->name, $options)) { - if ($this->options['upload_dir'] !== $options['upload_dir']) { + if ($this->options['upload_dir'] !== $options['upload_dir']) { $file->{$version.'_url'} = $this->add_png_to_ps_extension($options['upload_url'].rawurlencode($file->name)); } else { clearstatcache(); @@ -471,7 +480,7 @@ public function add_png_to_ps_extension($filename) { } return $filename.$thumbnail_ext; } - + public function is_ps_file($filename) { $file_ext = strtolower(substr(strrchr($filename, '.'), 1)); if(in_array($file_ext, $this->options['ps_file_extensions'])) diff --git a/Resources/config/services.yml b/Resources/config/services.yml index 1970e2a..65f36e1 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -62,9 +62,13 @@ parameters: max_width: 1140 max_height: 1140 - punk_ave.file_uploader.class: PunkAve\FileUploaderBundle\Services\FileUploader + punk_ave.file_uploader.class: PunkAve\FileUploaderBundle\Services\FileUploader + punk_ave.upload_handler.class: PunkAve\FileUploaderBundle\BlueImp\UploadHandler services: + punk_ave.upload_handler: + class: %punk_ave.upload_handler.class% + punk_ave.file_uploader: class: %punk_ave.file_uploader.class% arguments: @@ -77,6 +81,7 @@ services: originals: '%file_uploader.originals%' max_number_of_files: '%file_uploader.max_number_of_files%' max_file_size: '%file_uploader.max_file_size%' + - @punk_ave.upload_handler scope: request # You usually won't need this sub-service directly, @@ -94,3 +99,4 @@ services: arguments: [@service_container] tags: - { name: twig.extension } + diff --git a/Services/FileUploader.php b/Services/FileUploader.php index c7186b6..cb5b8c0 100644 --- a/Services/FileUploader.php +++ b/Services/FileUploader.php @@ -5,14 +5,16 @@ class FileUploader { protected $options; + protected $uploadHandler; - public function __construct($options) + public function __construct($options, $uploadHandler) { $this->options = $options; + $this->uploadHandler = $uploadHandler; } /** - * Get a list of files already present. The 'folder' option is required. + * Get a list of files already present. The 'folder' option is required. * If you pass consistent options to this method and handleFileUpload with * regard to paths, then you will get consistent results. */ @@ -34,11 +36,11 @@ public function removeFiles($options = array()) /** * Sync existing files from one folder to another. The 'fromFolder' and 'toFolder' * options are required. As with the 'folder' option elsewhere, these are appended - * to the file_base_path for you, missing parent folders are created, etc. If + * to the file_base_path for you, missing parent folders are created, etc. If * 'fromFolder' does not exist no error is reported as this is common if no files * have been uploaded. If there are files and the sync reports errors an exception * is thrown. - * + * * If you pass consistent options to this method and handleFileUpload with * regard to paths, then you will get consistent results. */ @@ -53,13 +55,13 @@ public function syncFiles($options = array()) * example: * * $id = $this->getRequest()->get('id'); - * // Validate the id, make sure it's just an integer, validate the user's right to edit that + * // Validate the id, make sure it's just an integer, validate the user's right to edit that * // object, then... * $this->get('punkave.file_upload').handleFileUpload(array('folder' => 'photos/' . $id)) - * + * * DOES NOT RETURN. The response is generated in native PHP by BlueImp's UploadHandler class. * - * Note that if %file_uploader.file_path%/$folder already contains files, the user is + * Note that if %file_uploader.file_path%/$folder already contains files, the user is * permitted to delete those in addition to uploading more. This is why we use a * separate folder for each object's associated files. * @@ -103,16 +105,19 @@ public function handleFileUpload($options = array()) } @mkdir($uploadDir, 0777, true); - $upload_handler = new \PunkAve\FileUploaderBundle\BlueImp\UploadHandler( + $upload_handler = $this->uploadHandler; + $upload_handler->setOptions( array( - 'upload_dir' => $uploadDir, - 'upload_url' => $webPath . '/' . $originals['folder'] . '/', + 'upload_dir' => $uploadDir, + 'upload_url' => $webPath . '/' . $originals['folder'] . '/', 'script_url' => $options['request']->getUri(), 'image_versions' => $sizes, 'accept_file_types' => $allowedExtensionsRegex, 'max_number_of_files' => $options['max_number_of_files'], 'max_file_size' => $options['max_file_size'], - )); + 'override_files' => $options['override_files'] + ) + ); // From https://github.com/blueimp/jQuery-File-Upload/blob/master/server/php/index.php // There's lots of REST fanciness here to support different upload methods, so we're From f74ffcc5591e68c38cadc77b3f9d732afa529122 Mon Sep 17 00:00:00 2001 From: Cesar Montoya Date: Sat, 16 May 2015 18:15:59 +0200 Subject: [PATCH 4/6] Add webpath to the file info when receive the json after upload --- BlueImp/UploadHandler.php | 4 ++++ Services/FileUploader.php | 1 + 2 files changed, 5 insertions(+) diff --git a/BlueImp/UploadHandler.php b/BlueImp/UploadHandler.php index cf7ff27..793e590 100644 --- a/BlueImp/UploadHandler.php +++ b/BlueImp/UploadHandler.php @@ -433,6 +433,10 @@ public function post() { ); } header('Vary: Accept'); + foreach ($info as $key => $item) { + $info[$key]->filePath = substr($item->url, strlen($this->options['webpath'])); + $info[$key]->filePath = preg_replace('/(.)\\1/','$1',$info[$key]->filePath); + } $json = json_encode($info); $redirect = isset($_REQUEST['redirect']) ? stripslashes($_REQUEST['redirect']) : null; diff --git a/Services/FileUploader.php b/Services/FileUploader.php index cb5b8c0..63bac68 100644 --- a/Services/FileUploader.php +++ b/Services/FileUploader.php @@ -110,6 +110,7 @@ public function handleFileUpload($options = array()) array( 'upload_dir' => $uploadDir, 'upload_url' => $webPath . '/' . $originals['folder'] . '/', + 'webpath' => $options['web_base_path'], 'script_url' => $options['request']->getUri(), 'image_versions' => $sizes, 'accept_file_types' => $allowedExtensionsRegex, From 4098aa87a7acb652db3710dd30cc47c3d6a6d33e Mon Sep 17 00:00:00 2001 From: Cesar Montoya Date: Sat, 16 May 2015 20:20:48 +0200 Subject: [PATCH 5/6] Apply urldecode to filePath --- BlueImp/UploadHandler.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/BlueImp/UploadHandler.php b/BlueImp/UploadHandler.php index 793e590..23bf585 100644 --- a/BlueImp/UploadHandler.php +++ b/BlueImp/UploadHandler.php @@ -435,7 +435,7 @@ public function post() { header('Vary: Accept'); foreach ($info as $key => $item) { $info[$key]->filePath = substr($item->url, strlen($this->options['webpath'])); - $info[$key]->filePath = preg_replace('/(.)\\1/','$1',$info[$key]->filePath); + $info[$key]->filePath = urldecode(preg_replace('/(.)\\1/','$1',$info[$key]->filePath)); } $json = json_encode($info); $redirect = isset($_REQUEST['redirect']) ? From d1dccbb5b4a092ff7b34449db4330dcd77d768a4 Mon Sep 17 00:00:00 2001 From: Cesar Montoya Date: Sat, 30 May 2015 23:33:53 +0200 Subject: [PATCH 6/6] Parameters for override 2 classes --- BlueImp/UploadHandler.php | 6 ++++-- Resources/config/services.yml | 8 ++++---- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/BlueImp/UploadHandler.php b/BlueImp/UploadHandler.php index 23bf585..afc7329 100644 --- a/BlueImp/UploadHandler.php +++ b/BlueImp/UploadHandler.php @@ -434,8 +434,10 @@ public function post() { } header('Vary: Accept'); foreach ($info as $key => $item) { - $info[$key]->filePath = substr($item->url, strlen($this->options['webpath'])); - $info[$key]->filePath = urldecode(preg_replace('/(.)\\1/','$1',$info[$key]->filePath)); + if (property_exists($item, 'url')) { + $info[$key]->filePath = substr($item->url, strlen($this->options['webpath'])); + $info[$key]->filePath = urldecode(preg_replace('/(.)\\1/','$1',$info[$key]->filePath)); + } } $json = json_encode($info); $redirect = isset($_REQUEST['redirect']) ? diff --git a/Resources/config/services.yml b/Resources/config/services.yml index 65f36e1..c2a8cf3 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -62,15 +62,15 @@ parameters: max_width: 1140 max_height: 1140 - punk_ave.file_uploader.class: PunkAve\FileUploaderBundle\Services\FileUploader - punk_ave.upload_handler.class: PunkAve\FileUploaderBundle\BlueImp\UploadHandler + file_uploader.punk_ave_upload_handler.class: PunkAve\FileUploaderBundle\BlueImp\UploadHandler + file_uploader.punk_ave_file_uploader.class: PunkAve\FileUploaderBundle\Services\FileUploader services: punk_ave.upload_handler: - class: %punk_ave.upload_handler.class% + class: %file_uploader.punk_ave_upload_handler.class% punk_ave.file_uploader: - class: %punk_ave.file_uploader.class% + class: %file_uploader.punk_ave_file_uploader.class% arguments: - file_base_path: '%file_uploader.file_base_path%' web_base_path: '%file_uploader.web_base_path%'