From fd3c345f4522c4aa2f901d1b3f78703409bd9425 Mon Sep 17 00:00:00 2001 From: Joel Corominas Date: Tue, 29 Apr 2014 12:04:54 +0200 Subject: [PATCH 01/21] modified composer.json --- composer.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index f7b0639..6e92381 100644 --- a/composer.json +++ b/composer.json @@ -1,9 +1,9 @@ { - "name": "punkave/symfony2-file-uploader-bundle", + "name": "kyto-gmbh/symfony2-file-uploader-bundle", "type": "symfony-bundle", "description": "Multiple file uploads for Symfony2 with the BlueImp uploader. Also scales uploaded images", "keywords": ["upload", "file", "multiple file upload", "uploader", "blueimp", "symfony", "symfony bundle", "bundle", "punkave", "image transform", "symfony-2.0", "symfony-2.1"], - "homepage": "https://github.com/punkave/symfony2-file-uploader-bundle", + "homepage": "https://github.com/kyto-gmbh/symfony2-file-uploader-bundle", "license": "MIT", "authors": [ { @@ -20,9 +20,9 @@ "symfony/framework-bundle": "2.*" }, "autoload": { - "psr-0": { "PunkAve\\FileUploaderBundle": "" } + "psr-0": { "kyto-gmbh\\FileUploaderBundle": "" } }, - "target-dir": "PunkAve/FileUploaderBundle", + "target-dir": "kyto-gmbh/FileUploaderBundle", "minimum-stability": "dev", "extra": { "branch-alias": { From f01c090370015c94ae5d7bca8009119a1c500b07 Mon Sep 17 00:00:00 2001 From: Joel Corominas Date: Tue, 29 Apr 2014 14:15:47 +0200 Subject: [PATCH 02/21] fixed composer.json --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 6e92381..8919787 100644 --- a/composer.json +++ b/composer.json @@ -20,9 +20,9 @@ "symfony/framework-bundle": "2.*" }, "autoload": { - "psr-0": { "kyto-gmbh\\FileUploaderBundle": "" } + "psr-0": { "PunkAve\\FileUploaderBundle": "" } }, - "target-dir": "kyto-gmbh/FileUploaderBundle", + "target-dir": "PunkAve/FileUploaderBundle", "minimum-stability": "dev", "extra": { "branch-alias": { From a0c7946451418db981020b6a55f5f9c110f8eede Mon Sep 17 00:00:00 2001 From: Joel Corominas Date: Tue, 29 Apr 2014 14:19:24 +0200 Subject: [PATCH 03/21] reverted changes on composer.json --- composer.json | 60 +++++++++++++++++++++++++-------------------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/composer.json b/composer.json index 8919787..567af75 100644 --- a/composer.json +++ b/composer.json @@ -1,32 +1,32 @@ { - "name": "kyto-gmbh/symfony2-file-uploader-bundle", - "type": "symfony-bundle", - "description": "Multiple file uploads for Symfony2 with the BlueImp uploader. Also scales uploaded images", - "keywords": ["upload", "file", "multiple file upload", "uploader", "blueimp", "symfony", "symfony bundle", "bundle", "punkave", "image transform", "symfony-2.0", "symfony-2.1"], - "homepage": "https://github.com/kyto-gmbh/symfony2-file-uploader-bundle", - "license": "MIT", - "authors": [ - { - "name": "Thomas Boutell", - "email": "tom@punkave.com" - }, - { - "name": "Wes John-Alder", - "email": "wes@punkave.com" - } - ], - "require": { - "php": ">=5.3.2", - "symfony/framework-bundle": "2.*" - }, - "autoload": { - "psr-0": { "PunkAve\\FileUploaderBundle": "" } - }, - "target-dir": "PunkAve/FileUploaderBundle", - "minimum-stability": "dev", - "extra": { - "branch-alias": { - "dev-master": "1.0.x-dev" - } + "name": "punkave/symfony2-file-uploader-bundle", + "type": "symfony-bundle", + "description": "Multiple file uploads for Symfony2 with the BlueImp uploader. Also scales uploaded images", + "keywords": ["upload", "file", "multiple file upload", "uploader", "blueimp", "symfony", "symfony bundle", "bundle", "punkave", "image transform", "symfony-2.0", "symfony-2.1"], + "homepage": "https://github.com/punkave/symfony2-file-uploader-bundle", + "license": "MIT", + "authors": [ + { + "name": "Thomas Boutell", + "email": "tom@punkave.com" + }, + { + "name": "Wes John-Alder", + "email": "wes@punkave.com" } -} + ], + "require": { + "php": ">=5.3.2", + "symfony/framework-bundle": "2.*" + }, + "autoload": { + "psr-0": { "PunkAve\\FileUploaderBundle": "" } + }, + "target-dir": "PunkAve/FileUploaderBundle", + "minimum-stability": "dev", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + } +} \ No newline at end of file From a707ec8e00b23aa8e8496093389abbd41fd57d05 Mon Sep 17 00:00:00 2001 From: Joel Corominas Date: Tue, 29 Apr 2014 16:09:06 +0200 Subject: [PATCH 04/21] added a test for outputing the errors --- Resources/public/js/FileUploader.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Resources/public/js/FileUploader.js b/Resources/public/js/FileUploader.js index 280e649..5a8b97c 100644 --- a/Resources/public/js/FileUploader.js +++ b/Resources/public/js/FileUploader.js @@ -106,6 +106,9 @@ function PunkAveFileUploader(options) success: function() { file.remove(); }, + error: function() { + console.log('testing error output'); + }, dataType: 'json' }); return false; From 62e0b08a4be1be028aa64a2318272a3e9c669cf5 Mon Sep 17 00:00:00 2001 From: Joel Corominas Date: Tue, 29 Apr 2014 17:21:04 +0200 Subject: [PATCH 05/21] display the error in the console --- Resources/public/js/FileUploader.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Resources/public/js/FileUploader.js b/Resources/public/js/FileUploader.js index 5a8b97c..11d4c02 100644 --- a/Resources/public/js/FileUploader.js +++ b/Resources/public/js/FileUploader.js @@ -106,8 +106,8 @@ function PunkAveFileUploader(options) success: function() { file.remove(); }, - error: function() { - console.log('testing error output'); + error: function ( xhr , msg, optional ) { + console.log('error', msg, optional); }, dataType: 'json' }); From ec75a3df6a2aa319ce0afc9bb526bc2dd9325da7 Mon Sep 17 00:00:00 2001 From: Joel Corominas Date: Tue, 29 Apr 2014 17:28:22 +0200 Subject: [PATCH 06/21] append error as a div --- Resources/public/js/FileUploader.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Resources/public/js/FileUploader.js b/Resources/public/js/FileUploader.js index 11d4c02..56818bb 100644 --- a/Resources/public/js/FileUploader.js +++ b/Resources/public/js/FileUploader.js @@ -107,7 +107,9 @@ function PunkAveFileUploader(options) file.remove(); }, error: function ( xhr , msg, optional ) { - console.log('error', msg, optional); + if ( optional == 'Locked') { + file.append('
This image can not be deleted, it is still in use in some document!
'); + } }, dataType: 'json' }); From c30977c1291ff663b97ae5316b3c3ed1b8cb35c5 Mon Sep 17 00:00:00 2001 From: Joel Corominas Date: Tue, 29 Apr 2014 17:43:10 +0200 Subject: [PATCH 07/21] display the message in a box and make it fadeout after 3 seconds --- Resources/public/js/FileUploader.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Resources/public/js/FileUploader.js b/Resources/public/js/FileUploader.js index 56818bb..ea87d6c 100644 --- a/Resources/public/js/FileUploader.js +++ b/Resources/public/js/FileUploader.js @@ -108,7 +108,8 @@ function PunkAveFileUploader(options) }, error: function ( xhr , msg, optional ) { if ( optional == 'Locked') { - file.append('
This image can not be deleted, it is still in use in some document!
'); + file.append(''); + $("#error-image-used").show().delay(3000).fadeOut(); } }, dataType: 'json' From 9d8edd493fb2262084194a3950a13b2bf4ee1abb Mon Sep 17 00:00:00 2001 From: Joel Corominas Date: Wed, 30 Apr 2014 09:25:55 +0200 Subject: [PATCH 08/21] fixed the appearence of the error message so it can be translated --- Resources/public/js/FileUploader.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Resources/public/js/FileUploader.js b/Resources/public/js/FileUploader.js index ea87d6c..497a2e6 100644 --- a/Resources/public/js/FileUploader.js +++ b/Resources/public/js/FileUploader.js @@ -108,8 +108,7 @@ function PunkAveFileUploader(options) }, error: function ( xhr , msg, optional ) { if ( optional == 'Locked') { - file.append(''); - $("#error-image-used").show().delay(3000).fadeOut(); + file.find(".error-image-used").show().delay(3000).fadeOut(); } }, dataType: 'json' From bd002d76de5cf5fa69eb4bc48eddb7a2c2e81d9a Mon Sep 17 00:00:00 2001 From: Oliver Tischlinger Date: Mon, 2 Feb 2015 15:12:04 +0100 Subject: [PATCH 09/21] better exception if rsync failed --- Services/FileManager.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Services/FileManager.php b/Services/FileManager.php index 5098457..b87b193 100644 --- a/Services/FileManager.php +++ b/Services/FileManager.php @@ -112,7 +112,7 @@ public function syncFiles($options = array()) system("rsync -a --delete " . escapeshellarg($from . '/') . " " . escapeshellarg($to), $result); if ($result !== 0) { - throw new \Exception("Sync failed"); + throw new \Exception("Sync failed with errorcode '$result'!"); } if (isset($options['remove_from_folder']) && $options['remove_from_folder']) { From fbb378ef2e8a23a6cc8846050c00019ca3e062ee Mon Sep 17 00:00:00 2001 From: Christoph Ebeling Date: Tue, 28 Jul 2015 11:04:41 +0200 Subject: [PATCH 10/21] Strip special characters for UploadHandler --- BlueImp/UploadHandler.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/BlueImp/UploadHandler.php b/BlueImp/UploadHandler.php index 8edfb18..340b399 100644 --- a/BlueImp/UploadHandler.php +++ b/BlueImp/UploadHandler.php @@ -246,6 +246,13 @@ protected function trim_file_name($name, $type, $index) { // into different directories or replacing hidden system files. // Also remove control characters and spaces (\x00..\x20) around the filename: $file_name = trim(basename(stripslashes($name)), ".\x00..\x20"); + $file_name = str_replace("ä", "ae", $file_name); + $file_name = str_replace("ö", "oe", $file_name); + $file_name = str_replace("ü", "ue", $file_name); + $file_name = str_replace("ß", "ss", $file_name); + //remove all special characters + $file_name = iconv("utf-8", "ascii//TRANSLIT//IGNORE", $file_name); + // Add missing file extension for known image types: if (strpos($file_name, '.') === false && preg_match('/^image\/(gif|jpe?g|png)/', $type, $matches)) { @@ -269,7 +276,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); @@ -435,4 +442,4 @@ public function delete() { echo json_encode($success); } -} \ No newline at end of file +} From 0bcdf8023807ad990d1decae58f8cc06f262a272 Mon Sep 17 00:00:00 2001 From: Christoph Ebeling Date: Thu, 30 Jul 2015 11:56:34 +0200 Subject: [PATCH 11/21] Fix file upload issue --- BlueImp/UploadHandler.php | 111 +++++++++++++++-------------- Test/BlueImp/UploadHandlerTest.php | 45 ++++++++++++ Test/bootstrap.php | 5 ++ phpunit.xml | 27 +++++++ 4 files changed, 136 insertions(+), 52 deletions(-) create mode 100644 Test/BlueImp/UploadHandlerTest.php create mode 100644 Test/bootstrap.php create mode 100644 phpunit.xml diff --git a/BlueImp/UploadHandler.php b/BlueImp/UploadHandler.php index 340b399..f9427c2 100644 --- a/BlueImp/UploadHandler.php +++ b/BlueImp/UploadHandler.php @@ -13,6 +13,8 @@ * http://www.opensource.org/licenses/MIT */ +use Locale; + class UploadHandler { protected $options; @@ -69,13 +71,13 @@ function __construct($options=null) { } protected function getFullUrl() { - return - (isset($_SERVER['HTTPS']) ? 'https://' : 'http://'). - (isset($_SERVER['REMOTE_USER']) ? $_SERVER['REMOTE_USER'].'@' : ''). - (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : ($_SERVER['SERVER_NAME']. - (isset($_SERVER['HTTPS']) && $_SERVER['SERVER_PORT'] === 443 || - $_SERVER['SERVER_PORT'] === 80 ? '' : ':'.$_SERVER['SERVER_PORT']))). - substr($_SERVER['SCRIPT_NAME'],0, strrpos($_SERVER['SCRIPT_NAME'], '/')); + return + (isset($_SERVER['HTTPS']) ? 'https://' : 'http://'). + (isset($_SERVER['REMOTE_USER']) ? $_SERVER['REMOTE_USER'].'@' : ''). + (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : ($_SERVER['SERVER_NAME']. + (isset($_SERVER['HTTPS']) && $_SERVER['SERVER_PORT'] === 443 || + $_SERVER['SERVER_PORT'] === 80 ? '' : ':'.$_SERVER['SERVER_PORT']))). + substr($_SERVER['SCRIPT_NAME'],0, strrpos($_SERVER['SCRIPT_NAME'], '/')); } protected function set_file_delete_url($file) { @@ -160,14 +162,14 @@ protected function create_scaled_image($file_name, $options) { $src_img = null; } $success = $src_img && @imagecopyresampled( - $new_img, - $src_img, - 0, 0, 0, 0, - $new_width, - $new_height, - $img_width, - $img_height - ) && $write_image($new_img, $new_file_path, $image_quality); + $new_img, + $src_img, + 0, 0, 0, 0, + $new_width, + $new_height, + $img_width, + $img_height + ) && $write_image($new_img, $new_file_path, $image_quality); // Free up memory (imagedestroy does not delete files): @imagedestroy($src_img); @imagedestroy($new_img); @@ -195,7 +197,7 @@ protected function validate($uploaded_file, $file, $error, $index) { if ($this->options['max_file_size'] && ( $file_size > $this->options['max_file_size'] || $file->size > $this->options['max_file_size']) - ) { + ) { $file->error = 'maxFileSize'; return false; } @@ -206,19 +208,19 @@ protected function validate($uploaded_file, $file, $error, $index) { } if (is_int($this->options['max_number_of_files']) && ( count($this->get_file_objects()) >= $this->options['max_number_of_files']) - ) { + ) { $file->error = 'maxNumberOfFiles'; return false; } list($img_width, $img_height) = @getimagesize($uploaded_file); if (is_int($img_width)) { if ($this->options['max_width'] && $img_width > $this->options['max_width'] || - $this->options['max_height'] && $img_height > $this->options['max_height']) { + $this->options['max_height'] && $img_height > $this->options['max_height']) { $file->error = 'maxResolution'; return false; } if ($this->options['min_width'] && $img_width < $this->options['min_width'] || - $this->options['min_height'] && $img_height < $this->options['min_height']) { + $this->options['min_height'] && $img_height < $this->options['min_height']) { $file->error = 'minResolution'; return false; } @@ -241,17 +243,22 @@ protected function upcount_name($name) { ); } - protected function trim_file_name($name, $type, $index) { + protected function trim_file_name($file_name, $type, $index) { // Remove path information and dots around the filename, to prevent uploading // into different directories or replacing hidden system files. // Also remove control characters and spaces (\x00..\x20) around the filename: - $file_name = trim(basename(stripslashes($name)), ".\x00..\x20"); + + //remove all special characters $file_name = str_replace("ä", "ae", $file_name); + $file_name = str_replace("Ä", "Ae", $file_name); $file_name = str_replace("ö", "oe", $file_name); + $file_name = str_replace("Ö", "Oe", $file_name); $file_name = str_replace("ü", "ue", $file_name); + $file_name = str_replace("Ü", "Ue", $file_name); $file_name = str_replace("ß", "ss", $file_name); - //remove all special characters - $file_name = iconv("utf-8", "ascii//TRANSLIT//IGNORE", $file_name); + + $file_name = trim(basename(stripslashes($file_name)), ".\x00..\x20"); + $file_name = iconv("UTF-8", "ascii//TRANSLIT//IGNORE", $file_name); // Add missing file extension for known image types: if (strpos($file_name, '.') === false && @@ -271,32 +278,32 @@ protected function handle_form_data($file, $index) { } protected function orient_image($file_path) { - $exif = @exif_read_data($file_path); + $exif = @exif_read_data($file_path); if ($exif === false) { return false; } - $orientation = intval(@$exif['Orientation']); - if (!in_array($orientation, array(3, 6, 8))) { - return false; - } - $image = @imagecreatefromjpeg($file_path); - switch ($orientation) { - case 3: - $image = @imagerotate($image, 180, 0); - break; - case 6: - $image = @imagerotate($image, 270, 0); - break; - case 8: - $image = @imagerotate($image, 90, 0); - break; - default: - return false; - } - $success = imagejpeg($image, $file_path); - // Free up memory (imagedestroy does not delete files): - @imagedestroy($image); - return $success; + $orientation = intval(@$exif['Orientation']); + if (!in_array($orientation, array(3, 6, 8))) { + return false; + } + $image = @imagecreatefromjpeg($file_path); + switch ($orientation) { + case 3: + $image = @imagerotate($image, 180, 0); + break; + case 6: + $image = @imagerotate($image, 270, 0); + break; + case 8: + $image = @imagerotate($image, 90, 0); + break; + default: + return false; + } + $success = imagejpeg($image, $file_path); + // Free up memory (imagedestroy does not delete files): + @imagedestroy($image); + return $success; } protected function handle_file_upload($uploaded_file, $name, $size, $type, $error, $index) { @@ -331,9 +338,9 @@ protected function handle_file_upload($uploaded_file, $name, $size, $type, $erro } $file_size = filesize($file_path); if ($file_size === $file->size) { - if ($this->options['orient_image']) { - $this->orient_image($file_path); - } + if ($this->options['orient_image']) { + $this->orient_image($file_path); + } $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)) { @@ -398,13 +405,13 @@ public function post() { isset($upload['tmp_name']) ? $upload['tmp_name'] : null, isset($_SERVER['HTTP_X_FILE_NAME']) ? $_SERVER['HTTP_X_FILE_NAME'] : (isset($upload['name']) ? - $upload['name'] : null), + $upload['name'] : null), isset($_SERVER['HTTP_X_FILE_SIZE']) ? $_SERVER['HTTP_X_FILE_SIZE'] : (isset($upload['size']) ? - $upload['size'] : null), + $upload['size'] : null), isset($_SERVER['HTTP_X_FILE_TYPE']) ? $_SERVER['HTTP_X_FILE_TYPE'] : (isset($upload['type']) ? - $upload['type'] : null), + $upload['type'] : null), isset($upload['error']) ? $upload['error'] : null ); } diff --git a/Test/BlueImp/UploadHandlerTest.php b/Test/BlueImp/UploadHandlerTest.php new file mode 100644 index 0000000..f0814b5 --- /dev/null +++ b/Test/BlueImp/UploadHandlerTest.php @@ -0,0 +1,45 @@ +getMethod('trim_file_name'); + $method->setAccessible(true); + $result = $method->invokeArgs($uploadHandler, ['../Ääüö.jpg', 'jpg', null]); + $this->assertEquals('Aeaeueoe.jpg', $result); + } + +} + +/** + * Class UploadHandlerMock + * @package PunkAve\FileUploaderBundle\BlueImp\Tests + */ +class UploadHandlerMock extends UploadHandler +{ + /** + * + */ + public function __construct(){ + + } +} + diff --git a/Test/bootstrap.php b/Test/bootstrap.php new file mode 100644 index 0000000..da8c360 --- /dev/null +++ b/Test/bootstrap.php @@ -0,0 +1,5 @@ + + + + + + ./Test/BlueImp/ + + + + + + ./BlueImp/ + ./FileUpload/ + ./Services/ + + + \ No newline at end of file From 71fc11fc8e546ccdd6570f7dccbf9b377e454f56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Albert=20Casadess=C3=BAs?= Date: Wed, 15 Jun 2016 11:01:16 +0200 Subject: [PATCH 12/21] check for basename to exist for any of the allowed extensions in order to add a suffix at the end --- .gitignore | 1 + BlueImp/UploadHandler.php | 24 +++++++++++++++++++++++- Test/BlueImp/UploadHandlerTest.php | 28 ++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 5509140..a02f5c6 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ *.DS_Store +.idea \ No newline at end of file diff --git a/BlueImp/UploadHandler.php b/BlueImp/UploadHandler.php index f9427c2..18242be 100644 --- a/BlueImp/UploadHandler.php +++ b/BlueImp/UploadHandler.php @@ -266,13 +266,35 @@ protected function trim_file_name($file_name, $type, $index) { $file_name .= '.'.$matches[1]; } if ($this->options['discard_aborted_uploads']) { - while(is_file($this->options['upload_dir'].$file_name)) { + while($this->is_basename_existing($this->options['upload_dir'], $file_name)) { $file_name = $this->upcount_name($file_name); } } return $file_name; } + protected function is_basename_existing($dir, $fileName) + { + $extensions = $this->get_accepted_extensions_from_options_regex($this->options['accept_file_types']); + $pathParts = pathinfo($fileName); + $nameWithoutExtension = $pathParts['filename']; + foreach ($extensions as $extension) { + if (is_file($dir . $nameWithoutExtension . $extension)) { + return true; + } + } + return false; + } + + protected function get_accepted_extensions_from_options_regex($regex) + { + preg_match_all('(\.[a-z]+)', $regex, $matches); + $extensions = $matches[0]; + $extensions[] = ''; + + return $extensions; + } + protected function handle_form_data($file, $index) { // Handle form data, e.g. $_REQUEST['description'][$index] } diff --git a/Test/BlueImp/UploadHandlerTest.php b/Test/BlueImp/UploadHandlerTest.php index f0814b5..e1b8279 100644 --- a/Test/BlueImp/UploadHandlerTest.php +++ b/Test/BlueImp/UploadHandlerTest.php @@ -13,6 +13,24 @@ class UploadHandlerTest extends \PHPUnit_Framework_TestCase { + /** + * + */ + public function testGetExtensionsFromRegex() + { + $regex = '/(\.jpg|\.jpeg|\.gif|\.png|\.pdf)$/i'; + $uploadHandler = new UploadHandlerMock(); + $extensions = $uploadHandler->get_accepted_extensions_from_options_regex_wrapper($regex); + + $this->assertCount(6, $extensions); + $this->assertEquals('.jpg', $extensions[0]); + $this->assertEquals('.jpeg', $extensions[1]); + $this->assertEquals('.gif', $extensions[2]); + $this->assertEquals('.png', $extensions[3]); + $this->assertEquals('.pdf', $extensions[4]); + $this->assertEquals('', $extensions[5]); + } + /** * */ @@ -41,5 +59,15 @@ class UploadHandlerMock extends UploadHandler public function __construct(){ } + + public function get_accepted_extensions_from_options_regex_wrapper($regex) + { + return $this->get_accepted_extensions_from_options_regex($regex); + } + + public function is_basename_existing_wrapper() + { + return $this->is_basename_existing(); + } } From bf6e157be87f2a358a4d8f1eeea39f6f63e5edb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Albert=20Casadess=C3=BAs?= Date: Wed, 15 Jun 2016 11:30:58 +0200 Subject: [PATCH 13/21] added tests --- BlueImp/UploadHandler.php | 11 ++++++----- Test/BlueImp/UploadHandlerTest.php | 21 ++++++++++++++++++--- Test/upload-dir/test.jpg | 0 3 files changed, 24 insertions(+), 8 deletions(-) create mode 100644 Test/upload-dir/test.jpg diff --git a/BlueImp/UploadHandler.php b/BlueImp/UploadHandler.php index 18242be..07b1e30 100644 --- a/BlueImp/UploadHandler.php +++ b/BlueImp/UploadHandler.php @@ -265,17 +265,18 @@ protected function trim_file_name($file_name, $type, $index) { preg_match('/^image\/(gif|jpe?g|png)/', $type, $matches)) { $file_name .= '.'.$matches[1]; } + $acceptedFileTypesRegex = $this->options['accept_file_types']; if ($this->options['discard_aborted_uploads']) { - while($this->is_basename_existing($this->options['upload_dir'], $file_name)) { + while($this->is_basename_existing($this->options['upload_dir'], $file_name, $acceptedFileTypesRegex)) { $file_name = $this->upcount_name($file_name); } } return $file_name; } - protected function is_basename_existing($dir, $fileName) + protected function is_basename_existing($dir, $fileName, $acceptedFileTypesRegex) { - $extensions = $this->get_accepted_extensions_from_options_regex($this->options['accept_file_types']); + $extensions = $this->get_accepted_extensions_from_options_regex($acceptedFileTypesRegex); $pathParts = pathinfo($fileName); $nameWithoutExtension = $pathParts['filename']; foreach ($extensions as $extension) { @@ -286,9 +287,9 @@ protected function is_basename_existing($dir, $fileName) return false; } - protected function get_accepted_extensions_from_options_regex($regex) + protected function get_accepted_extensions_from_options_regex($acceptedFileTypesRegex) { - preg_match_all('(\.[a-z]+)', $regex, $matches); + preg_match_all('(\.[a-z]+)', $acceptedFileTypesRegex, $matches); $extensions = $matches[0]; $extensions[] = ''; diff --git a/Test/BlueImp/UploadHandlerTest.php b/Test/BlueImp/UploadHandlerTest.php index e1b8279..baafa30 100644 --- a/Test/BlueImp/UploadHandlerTest.php +++ b/Test/BlueImp/UploadHandlerTest.php @@ -12,7 +12,6 @@ */ class UploadHandlerTest extends \PHPUnit_Framework_TestCase { - /** * */ @@ -31,6 +30,22 @@ public function testGetExtensionsFromRegex() $this->assertEquals('', $extensions[5]); } + /** + * + */ + public function testIsBasenameExisting() + { + $regex = '/(\.jpg|\.jpeg|\.gif|\.png|\.pdf)$/i'; + $dir = __DIR__ . '/../upload-dir/'; + $uploadHandler = new UploadHandlerMock(); + + $result = $uploadHandler->is_basename_existing_wrapper($dir, 'test.png', $regex); + $this->assertTrue($result); + + $result = $uploadHandler->is_basename_existing_wrapper($dir, 'I-do-not-exist.jpg', $regex); + $this->assertFalse($result); + } + /** * */ @@ -65,9 +80,9 @@ public function get_accepted_extensions_from_options_regex_wrapper($regex) return $this->get_accepted_extensions_from_options_regex($regex); } - public function is_basename_existing_wrapper() + public function is_basename_existing_wrapper($directory, $filename, $regex) { - return $this->is_basename_existing(); + return $this->is_basename_existing($directory, $filename, $regex); } } diff --git a/Test/upload-dir/test.jpg b/Test/upload-dir/test.jpg new file mode 100644 index 0000000..e69de29 From f33d50be52726a83d2a3a6b8a37d73b03eeb363c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Albert=20Casadess=C3=BAs?= Date: Wed, 15 Jun 2016 12:05:29 +0200 Subject: [PATCH 14/21] added docblock --- BlueImp/UploadHandler.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/BlueImp/UploadHandler.php b/BlueImp/UploadHandler.php index 07b1e30..520acdf 100644 --- a/BlueImp/UploadHandler.php +++ b/BlueImp/UploadHandler.php @@ -274,6 +274,14 @@ protected function trim_file_name($file_name, $type, $index) { return $file_name; } + /** + * Returns true if a file with the same basename exists in the directory for any of the allowed file types. + * + * @param string $dir + * @param string $fileName + * @param string $acceptedFileTypesRegex + * @return bool + */ protected function is_basename_existing($dir, $fileName, $acceptedFileTypesRegex) { $extensions = $this->get_accepted_extensions_from_options_regex($acceptedFileTypesRegex); @@ -287,6 +295,12 @@ protected function is_basename_existing($dir, $fileName, $acceptedFileTypesRegex return false; } + /** + * Returns an array with the dotted allowed extensions plus no extension. e.g. ['.jpeg', '.png', ''] + * + * @param string $acceptedFileTypesRegex + * @return array + */ protected function get_accepted_extensions_from_options_regex($acceptedFileTypesRegex) { preg_match_all('(\.[a-z]+)', $acceptedFileTypesRegex, $matches); From 9f299347af1b92c86f39b6c5615a29082a6dd532 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Albert=20Casadess=C3=BAs?= Date: Wed, 15 Jun 2016 14:05:21 +0200 Subject: [PATCH 15/21] added test --- Test/BlueImp/UploadHandlerTest.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Test/BlueImp/UploadHandlerTest.php b/Test/BlueImp/UploadHandlerTest.php index baafa30..4b79a33 100644 --- a/Test/BlueImp/UploadHandlerTest.php +++ b/Test/BlueImp/UploadHandlerTest.php @@ -44,6 +44,10 @@ public function testIsBasenameExisting() $result = $uploadHandler->is_basename_existing_wrapper($dir, 'I-do-not-exist.jpg', $regex); $this->assertFalse($result); + + $regex = '/(\.pdf)$/i'; + $result = $uploadHandler->is_basename_existing_wrapper($dir, 'test.jpg', $regex); + $this->assertFalse($result); } /** From 669849c3fb337a3a9fe03f0d29285b9c9db0ad60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Albert=20Casadess=C3=BAs?= Date: Wed, 15 Jun 2016 14:38:06 +0200 Subject: [PATCH 16/21] only return allowed extensions, not empty ones --- BlueImp/UploadHandler.php | 3 +-- Test/BlueImp/UploadHandlerTest.php | 12 ++++++++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/BlueImp/UploadHandler.php b/BlueImp/UploadHandler.php index 520acdf..e10e897 100644 --- a/BlueImp/UploadHandler.php +++ b/BlueImp/UploadHandler.php @@ -296,7 +296,7 @@ protected function is_basename_existing($dir, $fileName, $acceptedFileTypesRegex } /** - * Returns an array with the dotted allowed extensions plus no extension. e.g. ['.jpeg', '.png', ''] + * Returns an array with the dotted allowed extensions. e.g. ['.jpeg', '.png'] * * @param string $acceptedFileTypesRegex * @return array @@ -305,7 +305,6 @@ protected function get_accepted_extensions_from_options_regex($acceptedFileTypes { preg_match_all('(\.[a-z]+)', $acceptedFileTypesRegex, $matches); $extensions = $matches[0]; - $extensions[] = ''; return $extensions; } diff --git a/Test/BlueImp/UploadHandlerTest.php b/Test/BlueImp/UploadHandlerTest.php index 4b79a33..49281b0 100644 --- a/Test/BlueImp/UploadHandlerTest.php +++ b/Test/BlueImp/UploadHandlerTest.php @@ -21,13 +21,17 @@ public function testGetExtensionsFromRegex() $uploadHandler = new UploadHandlerMock(); $extensions = $uploadHandler->get_accepted_extensions_from_options_regex_wrapper($regex); - $this->assertCount(6, $extensions); + $this->assertCount(5, $extensions); $this->assertEquals('.jpg', $extensions[0]); $this->assertEquals('.jpeg', $extensions[1]); $this->assertEquals('.gif', $extensions[2]); $this->assertEquals('.png', $extensions[3]); $this->assertEquals('.pdf', $extensions[4]); - $this->assertEquals('', $extensions[5]); + + $regex = '/()$/i'; + $extensions = $uploadHandler->get_accepted_extensions_from_options_regex_wrapper($regex); + $this->assertCount(0, $extensions); + } /** @@ -48,6 +52,10 @@ public function testIsBasenameExisting() $regex = '/(\.pdf)$/i'; $result = $uploadHandler->is_basename_existing_wrapper($dir, 'test.jpg', $regex); $this->assertFalse($result); + + $regex = '/()$/i'; + $result = $uploadHandler->is_basename_existing_wrapper($dir, 'test.jpg', $regex); + $this->assertFalse($result); } /** From 356cec2697dff38a1173f8d602c15b26cedc60ec Mon Sep 17 00:00:00 2001 From: AnastasiosDrosos Date: Fri, 17 Mar 2017 17:17:41 +0100 Subject: [PATCH 17/21] Escape thumbnail preview url for existing images to be same as delete and post --- Resources/public/js/FileUploader.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Resources/public/js/FileUploader.js b/Resources/public/js/FileUploader.js index 497a2e6..6fb47fa 100644 --- a/Resources/public/js/FileUploader.js +++ b/Resources/public/js/FileUploader.js @@ -20,7 +20,7 @@ function PunkAveFileUploader(options) _.each(files, function(file) { appendEditableImage({ // cmsMediaUrl is a global variable set by the underscoreTemplates partial of MediaItems.html.twig - 'thumbnail_url': viewUrl + '/thumbnails/' + file, + 'thumbnail_url': viewUrl + '/thumbnails/' + encodeURIComponent(file), 'url': viewUrl + '/originals/' + file, 'name': file }); From fb13977385882643677fc57c4270118c0e33f6b8 Mon Sep 17 00:00:00 2001 From: AnastasiosDrosos Date: Mon, 27 Mar 2017 09:42:58 +0200 Subject: [PATCH 18/21] Fix image url --- Resources/public/js/FileUploader.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Resources/public/js/FileUploader.js b/Resources/public/js/FileUploader.js index 6fb47fa..ee52b50 100644 --- a/Resources/public/js/FileUploader.js +++ b/Resources/public/js/FileUploader.js @@ -21,7 +21,7 @@ function PunkAveFileUploader(options) appendEditableImage({ // cmsMediaUrl is a global variable set by the underscoreTemplates partial of MediaItems.html.twig 'thumbnail_url': viewUrl + '/thumbnails/' + encodeURIComponent(file), - 'url': viewUrl + '/originals/' + file, + 'url': viewUrl + '/originals/' + encodeURIComponent(file), 'name': file }); }); From abc27227389b0f7349dbe167e71103e916182efe Mon Sep 17 00:00:00 2001 From: AnastasiosDrosos Date: Mon, 27 Mar 2017 10:22:24 +0200 Subject: [PATCH 19/21] Create encoded file variable --- Resources/public/js/FileUploader.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Resources/public/js/FileUploader.js b/Resources/public/js/FileUploader.js index ee52b50..3ab4236 100644 --- a/Resources/public/js/FileUploader.js +++ b/Resources/public/js/FileUploader.js @@ -18,10 +18,11 @@ function PunkAveFileUploader(options) self.addExistingFiles = function(files) { _.each(files, function(file) { + var encodedFile = encodeURIComponent(file); appendEditableImage({ // cmsMediaUrl is a global variable set by the underscoreTemplates partial of MediaItems.html.twig - 'thumbnail_url': viewUrl + '/thumbnails/' + encodeURIComponent(file), - 'url': viewUrl + '/originals/' + encodeURIComponent(file), + 'thumbnail_url': viewUrl + '/thumbnails/' + encodedFile, + 'url': viewUrl + '/originals/' + encodedFile, 'name': file }); }); From 59c3e30c26d2d20cfcd8cf707fc9b52e22796864 Mon Sep 17 00:00:00 2001 From: Oliver Tischlinger Date: Thu, 1 Jun 2017 12:25:28 +0200 Subject: [PATCH 20/21] handle UTF-8 filenames the right way --- BlueImp/UploadHandler.php | 14 ++++---------- Services/FileManager.php | 8 ++++++-- Test/BlueImp/UploadHandlerTest.php | 2 +- Test/bootstrap.php | 2 +- composer.json | 6 +++++- 5 files changed, 17 insertions(+), 15 deletions(-) diff --git a/BlueImp/UploadHandler.php b/BlueImp/UploadHandler.php index e10e897..a790cfb 100644 --- a/BlueImp/UploadHandler.php +++ b/BlueImp/UploadHandler.php @@ -248,17 +248,9 @@ protected function trim_file_name($file_name, $type, $index) { // into different directories or replacing hidden system files. // Also remove control characters and spaces (\x00..\x20) around the filename: - //remove all special characters - $file_name = str_replace("ä", "ae", $file_name); - $file_name = str_replace("Ä", "Ae", $file_name); - $file_name = str_replace("ö", "oe", $file_name); - $file_name = str_replace("Ö", "Oe", $file_name); - $file_name = str_replace("ü", "ue", $file_name); - $file_name = str_replace("Ü", "Ue", $file_name); - $file_name = str_replace("ß", "ss", $file_name); - + $locale = setlocale(LC_ALL, 0); + setlocale(LC_ALL, 'en_US.utf8'); $file_name = trim(basename(stripslashes($file_name)), ".\x00..\x20"); - $file_name = iconv("UTF-8", "ascii//TRANSLIT//IGNORE", $file_name); // Add missing file extension for known image types: if (strpos($file_name, '.') === false && @@ -271,6 +263,8 @@ protected function trim_file_name($file_name, $type, $index) { $file_name = $this->upcount_name($file_name); } } + setlocale(LC_ALL, $locale); + return $file_name; } diff --git a/Services/FileManager.php b/Services/FileManager.php index b87b193..e79ca51 100644 --- a/Services/FileManager.php +++ b/Services/FileManager.php @@ -19,6 +19,8 @@ public function __construct($options) public function getFiles($options = array()) { $options = array_merge($this->options, $options); + $locale = setlocale(LC_ALL, 0); + setlocale(LC_ALL, 'en_US.utf8'); $folder = $options['file_base_path'] . '/' . $options['folder']; if (file_exists($folder)) @@ -33,12 +35,14 @@ public function getFiles($options = array()) $dirs = array(); } $result = array_map(function($s) { return basename($s); }, $dirs); - return $result; } else { - return array(); + $result = array(); } + setlocale(LC_ALL, $locale); + + return $result; } /** diff --git a/Test/BlueImp/UploadHandlerTest.php b/Test/BlueImp/UploadHandlerTest.php index 49281b0..85029ab 100644 --- a/Test/BlueImp/UploadHandlerTest.php +++ b/Test/BlueImp/UploadHandlerTest.php @@ -69,7 +69,7 @@ public function testUploadHandlerWithUmlauts() $method = $uploadHandlerReflection->getMethod('trim_file_name'); $method->setAccessible(true); $result = $method->invokeArgs($uploadHandler, ['../Ääüö.jpg', 'jpg', null]); - $this->assertEquals('Aeaeueoe.jpg', $result); + $this->assertEquals('Ääüö.jpg', $result); } } diff --git a/Test/bootstrap.php b/Test/bootstrap.php index da8c360..e4c6b4f 100644 --- a/Test/bootstrap.php +++ b/Test/bootstrap.php @@ -2,4 +2,4 @@ error_reporting(E_ALL | E_STRICT); -require __DIR__ . '/../../../../../../vendor/autoload.php'; +require __DIR__ . '/../vendor/autoload.php'; diff --git a/composer.json b/composer.json index 567af75..c092602 100644 --- a/composer.json +++ b/composer.json @@ -19,14 +19,18 @@ "php": ">=5.3.2", "symfony/framework-bundle": "2.*" }, + "require-dev": { + "phpunit/phpunit": "~5.0" + }, "autoload": { "psr-0": { "PunkAve\\FileUploaderBundle": "" } }, "target-dir": "PunkAve/FileUploaderBundle", "minimum-stability": "dev", + "prefer-stable": true, "extra": { "branch-alias": { "dev-master": "1.0.x-dev" } } -} \ No newline at end of file +} From 8564859ff4cf73c61a2cc0ebcf5cc1e9d385a721 Mon Sep 17 00:00:00 2001 From: Shahid Afridi Date: Tue, 13 Feb 2018 09:57:28 +0100 Subject: [PATCH 21/21] File name with different extion renaming issue : Revert to Original change --- BlueImp/UploadHandler.php | 39 ++------------------- Test/BlueImp/UploadHandlerTest.php | 55 ------------------------------ 2 files changed, 2 insertions(+), 92 deletions(-) diff --git a/BlueImp/UploadHandler.php b/BlueImp/UploadHandler.php index a790cfb..492846b 100644 --- a/BlueImp/UploadHandler.php +++ b/BlueImp/UploadHandler.php @@ -257,9 +257,9 @@ protected function trim_file_name($file_name, $type, $index) { preg_match('/^image\/(gif|jpe?g|png)/', $type, $matches)) { $file_name .= '.'.$matches[1]; } - $acceptedFileTypesRegex = $this->options['accept_file_types']; + if ($this->options['discard_aborted_uploads']) { - while($this->is_basename_existing($this->options['upload_dir'], $file_name, $acceptedFileTypesRegex)) { + while (is_file($this->options['upload_dir'] . $file_name)) { $file_name = $this->upcount_name($file_name); } } @@ -268,41 +268,6 @@ protected function trim_file_name($file_name, $type, $index) { return $file_name; } - /** - * Returns true if a file with the same basename exists in the directory for any of the allowed file types. - * - * @param string $dir - * @param string $fileName - * @param string $acceptedFileTypesRegex - * @return bool - */ - protected function is_basename_existing($dir, $fileName, $acceptedFileTypesRegex) - { - $extensions = $this->get_accepted_extensions_from_options_regex($acceptedFileTypesRegex); - $pathParts = pathinfo($fileName); - $nameWithoutExtension = $pathParts['filename']; - foreach ($extensions as $extension) { - if (is_file($dir . $nameWithoutExtension . $extension)) { - return true; - } - } - return false; - } - - /** - * Returns an array with the dotted allowed extensions. e.g. ['.jpeg', '.png'] - * - * @param string $acceptedFileTypesRegex - * @return array - */ - protected function get_accepted_extensions_from_options_regex($acceptedFileTypesRegex) - { - preg_match_all('(\.[a-z]+)', $acceptedFileTypesRegex, $matches); - $extensions = $matches[0]; - - return $extensions; - } - protected function handle_form_data($file, $index) { // Handle form data, e.g. $_REQUEST['description'][$index] } diff --git a/Test/BlueImp/UploadHandlerTest.php b/Test/BlueImp/UploadHandlerTest.php index 85029ab..e43b3d9 100644 --- a/Test/BlueImp/UploadHandlerTest.php +++ b/Test/BlueImp/UploadHandlerTest.php @@ -12,51 +12,6 @@ */ class UploadHandlerTest extends \PHPUnit_Framework_TestCase { - /** - * - */ - public function testGetExtensionsFromRegex() - { - $regex = '/(\.jpg|\.jpeg|\.gif|\.png|\.pdf)$/i'; - $uploadHandler = new UploadHandlerMock(); - $extensions = $uploadHandler->get_accepted_extensions_from_options_regex_wrapper($regex); - - $this->assertCount(5, $extensions); - $this->assertEquals('.jpg', $extensions[0]); - $this->assertEquals('.jpeg', $extensions[1]); - $this->assertEquals('.gif', $extensions[2]); - $this->assertEquals('.png', $extensions[3]); - $this->assertEquals('.pdf', $extensions[4]); - - $regex = '/()$/i'; - $extensions = $uploadHandler->get_accepted_extensions_from_options_regex_wrapper($regex); - $this->assertCount(0, $extensions); - - } - - /** - * - */ - public function testIsBasenameExisting() - { - $regex = '/(\.jpg|\.jpeg|\.gif|\.png|\.pdf)$/i'; - $dir = __DIR__ . '/../upload-dir/'; - $uploadHandler = new UploadHandlerMock(); - - $result = $uploadHandler->is_basename_existing_wrapper($dir, 'test.png', $regex); - $this->assertTrue($result); - - $result = $uploadHandler->is_basename_existing_wrapper($dir, 'I-do-not-exist.jpg', $regex); - $this->assertFalse($result); - - $regex = '/(\.pdf)$/i'; - $result = $uploadHandler->is_basename_existing_wrapper($dir, 'test.jpg', $regex); - $this->assertFalse($result); - - $regex = '/()$/i'; - $result = $uploadHandler->is_basename_existing_wrapper($dir, 'test.jpg', $regex); - $this->assertFalse($result); - } /** * @@ -86,15 +41,5 @@ class UploadHandlerMock extends UploadHandler public function __construct(){ } - - public function get_accepted_extensions_from_options_regex_wrapper($regex) - { - return $this->get_accepted_extensions_from_options_regex($regex); - } - - public function is_basename_existing_wrapper($directory, $filename, $regex) - { - return $this->is_basename_existing($directory, $filename, $regex); - } }