Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File uploader renames with other ext #81

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
fd3c345
modified composer.json
Joe04 Apr 29, 2014
f01c090
fixed composer.json
Joe04 Apr 29, 2014
a0c7946
reverted changes on composer.json
Joe04 Apr 29, 2014
a707ec8
added a test for outputing the errors
Joe04 Apr 29, 2014
62e0b08
display the error in the console
Joe04 Apr 29, 2014
ec75a3d
append error as a div
Joe04 Apr 29, 2014
c30977c
display the message in a box and make it fadeout after 3 seconds
Joe04 Apr 29, 2014
9d8edd4
fixed the appearence of the error message so it can be translated
Joe04 Apr 30, 2014
bd002d7
better exception if rsync failed
Metabor Feb 2, 2015
fbb378e
Strip special characters for UploadHandler
Jul 28, 2015
67e522f
Merge pull request #1 from christophebeling/master
Jul 28, 2015
0bcdf80
Fix file upload issue
Jul 30, 2015
834ccea
Merge pull request #3 from christophebeling/master
Jul 30, 2015
71fc11f
check for basename to exist for any of the allowed extensions in orde…
Jun 15, 2016
bf6e157
added tests
Jun 15, 2016
f33d50b
added docblock
Jun 15, 2016
9f29934
added test
Jun 15, 2016
669849c
only return allowed extensions, not empty ones
Jun 15, 2016
da634c9
Merge pull request #4 from kyto-gmbh/check-for-basename-to-not-exist
llissssss Jun 16, 2016
356cec2
Escape thumbnail preview url for existing images to be same as delete…
AnastasiosDrosos Mar 17, 2017
abe06f0
Merge pull request #5 from kyto-gmbh/bugfix/escape-thumbnail-url
luksurious Mar 20, 2017
fb13977
Fix image url
AnastasiosDrosos Mar 27, 2017
abc2722
Create encoded file variable
AnastasiosDrosos Mar 27, 2017
ec1aefc
Merge pull request #6 from kyto-gmbh/bugfix/fix-image-url
drososanastasios Mar 27, 2017
59c3e30
handle UTF-8 filenames the right way
Jun 1, 2017
8564859
File name with different extion renaming issue : Revert to Original c…
Feb 13, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
*.DS_Store
.idea
113 changes: 61 additions & 52 deletions BlueImp/UploadHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
* http://www.opensource.org/licenses/MIT
*/

use Locale;

class UploadHandler
{
protected $options;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -241,21 +243,28 @@ 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");

$locale = setlocale(LC_ALL, 0);
setlocale(LC_ALL, 'en_US.utf8');
$file_name = trim(basename(stripslashes($file_name)), ".\x00..\x20");

// Add missing file extension for known image types:
if (strpos($file_name, '.') === false &&
preg_match('/^image\/(gif|jpe?g|png)/', $type, $matches)) {
$file_name .= '.'.$matches[1];
}

if ($this->options['discard_aborted_uploads']) {
while(is_file($this->options['upload_dir'].$file_name)) {
while (is_file($this->options['upload_dir'] . $file_name)) {
$file_name = $this->upcount_name($file_name);
}
}
setlocale(LC_ALL, $locale);

return $file_name;
}

Expand All @@ -264,32 +273,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) {
Expand Down Expand Up @@ -324,9 +333,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)) {
Expand Down Expand Up @@ -391,13 +400,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
);
}
Expand Down Expand Up @@ -435,4 +444,4 @@ public function delete() {
echo json_encode($success);
}

}
}
10 changes: 8 additions & 2 deletions Resources/public/js/FileUploader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/' + file,
'url': viewUrl + '/originals/' + file,
'thumbnail_url': viewUrl + '/thumbnails/' + encodedFile,
'url': viewUrl + '/originals/' + encodedFile,
'name': file
});
});
Expand Down Expand Up @@ -106,6 +107,11 @@ function PunkAveFileUploader(options)
success: function() {
file.remove();
},
error: function ( xhr , msg, optional ) {
if ( optional == 'Locked') {
file.find(".error-image-used").show().delay(3000).fadeOut();
}
},
dataType: 'json'
});
return false;
Expand Down
10 changes: 7 additions & 3 deletions Services/FileManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -112,7 +116,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'])
{
Expand Down
45 changes: 45 additions & 0 deletions Test/BlueImp/UploadHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace PunkAve\FileUploaderBundle\BlueImp\Tests;

use PunkAve\FileUploaderBundle\BlueImp\UploadHandler;

/**
* Created by PhpStorm.
* User: cebeling
* Date: 30/07/15
* Time: 10:30
*/
class UploadHandlerTest extends \PHPUnit_Framework_TestCase
{

/**
*
*/
public function testUploadHandlerWithUmlauts()
{
$uploadHandler = new UploadHandlerMock();

$uploadHandlerReflection = new \ReflectionClass(UploadHandler::class);
$method = $uploadHandlerReflection->getMethod('trim_file_name');
$method->setAccessible(true);
$result = $method->invokeArgs($uploadHandler, ['../Ääüö.jpg', 'jpg', null]);
$this->assertEquals('Ääüö.jpg', $result);
}

}

/**
* Class UploadHandlerMock
* @package PunkAve\FileUploaderBundle\BlueImp\Tests
*/
class UploadHandlerMock extends UploadHandler
{
/**
*
*/
public function __construct(){

}
}

5 changes: 5 additions & 0 deletions Test/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

error_reporting(E_ALL | E_STRICT);

require __DIR__ . '/../vendor/autoload.php';
Empty file added Test/upload-dir/test.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
62 changes: 33 additions & 29 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
{
"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": "[email protected]"
},
{
"name": "Wes John-Alder",
"email": "[email protected]"
}
],
"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": "[email protected]"
},
{
"name": "Wes John-Alder",
"email": "[email protected]"
}
],
"require": {
"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"
}
}
}
Loading