// Uploader helper part 3
This commit is contained in:
+24
-21
@@ -48,9 +48,6 @@ class UploaderCore
|
||||
|
||||
public function getAcceptTypes()
|
||||
{
|
||||
if (!isset($this->_accept_types))
|
||||
$this->setAcceptTypes('/.+$/i');
|
||||
|
||||
return $this->_accept_types;
|
||||
}
|
||||
|
||||
@@ -101,6 +98,21 @@ class UploaderCore
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getPostMaxSizeBytes() {
|
||||
$post_max_size = ini_get('post_max_size');
|
||||
$bytes = trim($post_max_size);
|
||||
$last = strtolower($post_max_size[strlen($post_max_size) - 1]);
|
||||
|
||||
switch ($last)
|
||||
{
|
||||
case 'g': $bytes *= 1024;
|
||||
case 'm': $bytes *= 1024;
|
||||
case 'k': $bytes *= 1024;
|
||||
}
|
||||
|
||||
return $bytes;
|
||||
}
|
||||
|
||||
public function getSavePath()
|
||||
{
|
||||
if (!isset($this->_save_path))
|
||||
@@ -134,8 +146,11 @@ class UploaderCore
|
||||
$this->files[] = $this->upload($tmp[$index]);
|
||||
}
|
||||
}
|
||||
else
|
||||
elseif ($upload)
|
||||
{
|
||||
|
||||
$this->files[] = $this->upload($upload);
|
||||
}
|
||||
|
||||
return $this->files;
|
||||
}
|
||||
@@ -173,7 +188,7 @@ class UploaderCore
|
||||
|
||||
protected function validate($file)
|
||||
{
|
||||
$post_max_size = $this->_getPostMaxSizeBytes();
|
||||
$post_max_size = $this->getPostMaxSizeBytes();
|
||||
|
||||
if ($post_max_size && ($this->_getServerVars('CONTENT_LENGTH') > $post_max_size))
|
||||
{
|
||||
@@ -181,7 +196,10 @@ class UploaderCore
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!preg_match($this->getAcceptTypes(), $file['name']))
|
||||
$types = $this->getAcceptTypes();
|
||||
|
||||
//TODO check mime type.
|
||||
if (isset($types) && !in_array(pathinfo($file['name'], PATHINFO_EXTENSION), $types))
|
||||
{
|
||||
$file['error'] = Tools::displayError('Filetype not allowed');
|
||||
return false;
|
||||
@@ -203,21 +221,6 @@ class UploaderCore
|
||||
return filesize($file_path);
|
||||
}
|
||||
|
||||
protected function _getPostMaxSizeBytes() {
|
||||
$post_max_size = ini_get('post_max_size');
|
||||
$bytes = trim($post_max_size);
|
||||
$last = strtolower($post_max_size[strlen($post_max_size) - 1]);
|
||||
|
||||
switch ($last)
|
||||
{
|
||||
case 'g': $bytes *= 1024;
|
||||
case 'm': $bytes *= 1024;
|
||||
case 'k': $bytes *= 1024;
|
||||
}
|
||||
|
||||
return $bytes;
|
||||
}
|
||||
|
||||
protected function _getServerVars($var)
|
||||
{
|
||||
return (isset($_SERVER[$var]) ? $_SERVER[$var] : '');
|
||||
|
||||
@@ -118,6 +118,7 @@ class HelperFormCore extends Helper
|
||||
$uploader->setUrl(isset($params['url'])?$params['url']:null);
|
||||
$uploader->setMultiple(isset($params['multiple'])?$params['multiple']:false);
|
||||
$uploader->setUseAjax(isset($params['ajax'])?$params['ajax']:false);
|
||||
$uploader->setMaxFiles(isset($params['max_files'])?$params['max_files']:null);
|
||||
|
||||
if (isset($params['files']) && $params['files'])
|
||||
$uploader->setFiles($params['files']);
|
||||
@@ -139,7 +140,6 @@ class HelperFormCore extends Helper
|
||||
'download_url' => isset($params['file'])?$params['file']:null
|
||||
)));
|
||||
|
||||
|
||||
$uploader->setThumb(isset($params['thumb'])?$params['thumb']:null);
|
||||
$uploader->setTitle(isset($params['title'])?$params['title']:null);
|
||||
|
||||
|
||||
@@ -26,35 +26,25 @@
|
||||
|
||||
class HelperImageUploaderCore extends HelperUploader
|
||||
{
|
||||
private $_temporary_path;
|
||||
|
||||
public function getMaxSize()
|
||||
{
|
||||
return (int)Configuration::get('PS_PRODUCT_PICTURE_MAX_SIZE');
|
||||
}
|
||||
|
||||
public function setTemporaryPath($value)
|
||||
public function getSavePath()
|
||||
{
|
||||
$this->_temporary_path = $value;
|
||||
}
|
||||
|
||||
public function getTemporaryPath()
|
||||
{
|
||||
if (!isset($this->_temporary_path))
|
||||
$this->_temporary_path = _PS_TMP_IMG_DIR_;
|
||||
|
||||
return $this->_normalizeDirectory($this->_temporary_path);
|
||||
return $this->_normalizeDirectory(_PS_TMP_IMG_DIR_);
|
||||
}
|
||||
|
||||
public function getFilePath($file_name = null)
|
||||
{
|
||||
//Force file path
|
||||
return tempnam($this->getTemporaryPath(), $this->getUniqueFileName());
|
||||
return tempnam($this->getSavePath(), $this->getUniqueFileName());
|
||||
}
|
||||
|
||||
protected function validate($file)
|
||||
{
|
||||
$post_max_size = $this->_getPostMaxSizeBytes();
|
||||
$post_max_size = $this->getPostMaxSizeBytes();
|
||||
|
||||
if ($post_max_size && ($this->_getServerVars('CONTENT_LENGTH') > $post_max_size))
|
||||
{
|
||||
@@ -62,7 +52,10 @@ class HelperImageUploaderCore extends HelperUploader
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!preg_match($this->getAcceptTypes(), $file['name']))
|
||||
$types = $this->getAcceptTypes();
|
||||
|
||||
//TODO check mime type.
|
||||
if (isset($types) && !in_array(pathinfo($file['name'], PATHINFO_EXTENSION), $types))
|
||||
{
|
||||
$file['error'] = Tools::displayError('Filetype not allowed');
|
||||
return false;
|
||||
|
||||
@@ -104,6 +104,7 @@ class HelperOptionsCore extends Helper
|
||||
$uploader->setUrl(isset($field['url'])?$field['url']:null);
|
||||
$uploader->setMultiple(isset($field['multiple'])?$field['multiple']:false);
|
||||
$uploader->setUseAjax(isset($field['ajax'])?$field['ajax']:false);
|
||||
$uploader->setMaxFiles(isset($field['max_files'])?$field['max_files']:null);
|
||||
|
||||
if (isset($field['files']) && $field['files'])
|
||||
$uploader->setFiles($field['files']);
|
||||
|
||||
@@ -243,22 +243,28 @@ class HelperUploaderCore extends Uploader
|
||||
.'/themes/'.$bo_theme.'/js/vendor/jquery.ui.widget.js"></script>';
|
||||
$html .= '<script type="text/javascript" src="'.__PS_BASE_URI__.$admin_webpath
|
||||
.'/themes/'.$bo_theme.'/js/jquery.iframe-transport.js"></script>';
|
||||
$html .= '<script type="text/javascript" src="'.__PS_BASE_URI__.$admin_webpath
|
||||
$html .= '<script type="text/javascript" src="'.__PS_BASE_URI__.$admin_webpath
|
||||
.'/themes/'.$bo_theme.'/js/jquery.fileupload.js"></script>';
|
||||
$html .= '<script type="text/javascript" src="'.__PS_BASE_URI__.$admin_webpath
|
||||
.'/themes/'.$bo_theme.'/js/jquery.fileupload-process.js"></script>';
|
||||
$html .= '<script type="text/javascript" src="'.__PS_BASE_URI__.$admin_webpath
|
||||
.'/themes/'.$bo_theme.'/js/jquery.fileupload-validate.js"></script>';
|
||||
$html .= '<script type="text/javascript" src="'.__PS_BASE_URI__.'/js/vendor/spin.js"></script>';
|
||||
$html .= '<script type="text/javascript" src="'.__PS_BASE_URI__.'/js/vendor/ladda.js"></script>';
|
||||
}
|
||||
else
|
||||
{
|
||||
$html = '';
|
||||
$this->getContext()->controller->addJs(__PS_BASE_URI__.$admin_webpath
|
||||
.'/themes/'.$bo_theme.'/js/vendor/jquery.ui.widget.js');
|
||||
//$context->controller->addJs('http://blueimp.github.io/JavaScript-Load-Image/js/load-image.min.js');
|
||||
$this->getContext()->controller->addJs(__PS_BASE_URI__.$admin_webpath
|
||||
.'/themes/'.$bo_theme.'/js/jquery.iframe-transport.js');
|
||||
$this->getContext()->controller->addJs(__PS_BASE_URI__.$admin_webpath
|
||||
.'/themes/'.$bo_theme.'/js/jquery.fileupload.js');
|
||||
/*$context->controller->addJs(__PS_BASE_URI__.$admin_webpath
|
||||
.'/themes/'.$bo_theme.'/js/jquery.fileupload-image.js');*/
|
||||
|
||||
$this->getContext()->controller->addJs(__PS_BASE_URI__.$admin_webpath
|
||||
.'/themes/'.$bo_theme.'/js/jquery.fileupload-process.js');
|
||||
$this->getContext()->controller->addJs(__PS_BASE_URI__.$admin_webpath
|
||||
.'/themes/'.$bo_theme.'/js/jquery.fileupload-validate.js');
|
||||
$this->getContext()->controller->addJs(__PS_BASE_URI__.'/js/vendor/spin.js');
|
||||
$this->getContext()->controller->addJs(__PS_BASE_URI__.'/js/vendor/ladda.js');
|
||||
}
|
||||
@@ -270,8 +276,6 @@ class HelperUploaderCore extends Uploader
|
||||
$this->getTemplateFile($this->getTemplate()), $this->getContext()->smarty
|
||||
);
|
||||
|
||||
$max_files = $this->getMaxFiles();
|
||||
|
||||
$template->assign(array(
|
||||
'id' => $this->getId(),
|
||||
'name' => $this->getName(),
|
||||
@@ -280,7 +284,8 @@ class HelperUploaderCore extends Uploader
|
||||
'files' => $this->getFiles(),
|
||||
'thumb' => $this->getThumb(),
|
||||
'title' => $this->getTitle(),
|
||||
'max_files' => (isset($max_files) && $max_files > 0) ? ($max_files-count($this->getFiles())) : $max_files
|
||||
'max_files' => $this->getMaxFiles(),
|
||||
'post_max_size' => $this->getPostMaxSizeBytes()
|
||||
));
|
||||
|
||||
$html .= $template->fetch();
|
||||
|
||||
Reference in New Issue
Block a user