Skip to content

A new parameter override_files #77

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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
35 changes: 25 additions & 10 deletions BlueImp/UploadHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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/',
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)) {
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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']))
Expand Down
10 changes: 9 additions & 1 deletion Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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%'
Expand All @@ -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,
Expand All @@ -92,3 +99,4 @@ services:
arguments: [@service_container]
tags:
- { name: twig.extension }

28 changes: 17 additions & 11 deletions Services/FileUploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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.
*/
Expand All @@ -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.
*
Expand Down Expand Up @@ -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
Expand Down