diff --git a/BlueImp/UploadHandler.php b/BlueImp/UploadHandler.php index e14f5fb..afc7329 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(); @@ -424,6 +433,12 @@ public function post() { ); } header('Vary: Accept'); + foreach ($info as $key => $item) { + 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']) ? stripslashes($_REQUEST['redirect']) : null; @@ -471,7 +486,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 bddb8bc..c2a8cf3 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -62,9 +62,15 @@ parameters: max_width: 1140 max_height: 1140 + 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: %file_uploader.punk_ave_upload_handler.class% + punk_ave.file_uploader: - class: PunkAve\FileUploaderBundle\Services\FileUploader + 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%' @@ -75,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, @@ -92,3 +99,4 @@ services: arguments: [@service_container] tags: - { name: twig.extension } + diff --git a/Services/FileUploader.php b/Services/FileUploader.php index c7186b6..63bac68 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,20 @@ 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'] . '/', + 'webpath' => $options['web_base_path'], '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