Small refactoring in Upload class (#927)

This commit is contained in:
Roman Kelesidis 2023-10-02 13:53:41 +07:00 committed by GitHub
commit 4c708d578b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -15,7 +15,12 @@ namespace TorrentPier\Legacy\Common;
*/ */
class Upload class Upload
{ {
public $cfg = [ /**
* Default config pattern
*
* @var array
*/
public array $cfg = [
'max_size' => 0, 'max_size' => 0,
'max_width' => 0, 'max_width' => 0,
'max_height' => 0, 'max_height' => 0,
@ -23,24 +28,56 @@ class Upload
'upload_path' => '', 'upload_path' => '',
'up_allowed' => false, 'up_allowed' => false,
]; ];
public $file = [
/**
* File params pattern
*
* @var array
*/
public array $file = [
'name' => '', 'name' => '',
'type' => '', 'type' => '',
'size' => 0, 'size' => 0,
'tmp_name' => '', 'tmp_name' => '',
'error' => UPLOAD_ERR_NO_FILE, 'error' => UPLOAD_ERR_NO_FILE,
]; ];
/**
* Trash...
*
* TODO: Refactoring
* @var string
*/
public $orig_name = ''; public $orig_name = '';
public $file_path = ''; // Stored file path public $file_path = '';
public $file_ext = ''; public $file_ext = '';
public $file_ext_id = ''; public $file_ext_id = '';
public $file_size = '';
public $ext_ids = []; // array_flip($bb_cfg['file_id_ext']) /**
public $errors = []; * File size
*
* @var int
*/
public int $file_size = 0;
/**
* All allowed extensions to upload
*
* @var array
*/
public array $ext_ids = [];
/**
* Store caught errors while uploading
*
* @var array
*/
public array $errors = [];
/** /**
* Image types array * Image types array
* *
* @see https://www.php.net/manual/en/image.constants.php
* @var array|string[] * @var array|string[]
*/ */
public array $img_types = [ public array $img_types = [
@ -52,12 +89,14 @@ class Upload
]; ];
/** /**
* Initialize uploader
*
* @param array $cfg * @param array $cfg
* @param array $post_params * @param array $post_params
* @param bool $uploaded_only * @param bool $uploaded_only
* @return bool * @return bool
*/ */
public function init(array $cfg = [], array $post_params = [], $uploaded_only = true) public function init(array $cfg = [], array $post_params = [], bool $uploaded_only = true): bool
{ {
global $bb_cfg, $lang; global $bb_cfg, $lang;
@ -77,12 +116,14 @@ class Upload
$this->errors[] = $msg; $this->errors[] = $msg;
return false; return false;
} }
// file_exists
// Check file exists
if (!file_exists($this->file['tmp_name'])) { if (!file_exists($this->file['tmp_name'])) {
$this->errors[] = "Uploaded file not exists: {$this->file['tmp_name']}"; $this->errors[] = "Uploaded file not exists: {$this->file['tmp_name']}";
return false; return false;
} }
// size
// Check file size
if (!$this->file_size = filesize($this->file['tmp_name'])) { if (!$this->file_size = filesize($this->file['tmp_name'])) {
$this->errors[] = "Uploaded file is empty: {$this->file['tmp_name']}"; $this->errors[] = "Uploaded file is empty: {$this->file['tmp_name']}";
return false; return false;
@ -91,17 +132,20 @@ class Upload
$this->errors[] = sprintf($lang['UPLOAD_ERROR_SIZE'], humn_size($this->cfg['max_size'])); $this->errors[] = sprintf($lang['UPLOAD_ERROR_SIZE'], humn_size($this->cfg['max_size']));
return false; return false;
} }
// is_uploaded_file // is_uploaded_file
if ($uploaded_only && !is_uploaded_file($this->file['tmp_name'])) { if ($uploaded_only && !is_uploaded_file($this->file['tmp_name'])) {
$this->errors[] = "Not uploaded file: {$this->file['tmp_name']}"; $this->errors[] = "Not uploaded file: {$this->file['tmp_name']}";
return false; return false;
} }
// get ext
$this->ext_ids = array_flip($bb_cfg['file_id_ext']); // Got file extension
$file_name_ary = explode('.', $this->file['name']); $file_name_ary = explode('.', $this->file['name']);
$this->file_ext = strtolower(end($file_name_ary)); $this->file_ext = strtolower(end($file_name_ary));
// img $this->ext_ids = array_flip($bb_cfg['file_id_ext']);
// Actions for images [E.g. Change avatar]
if ($this->cfg['max_width'] || $this->cfg['max_height']) { if ($this->cfg['max_width'] || $this->cfg['max_height']) {
if ($img_info = getimagesize($this->file['tmp_name'])) { if ($img_info = getimagesize($this->file['tmp_name'])) {
[$width, $height, $type, $attr] = $img_info; [$width, $height, $type, $attr] = $img_info;
@ -123,7 +167,8 @@ class Upload
return false; return false;
} }
} }
// check ext
// Check extension
if ($uploaded_only && (!isset($this->ext_ids[$this->file_ext]) || !\in_array($this->file_ext, $this->cfg['allowed_ext'], true))) { if ($uploaded_only && (!isset($this->ext_ids[$this->file_ext]) || !\in_array($this->file_ext, $this->cfg['allowed_ext'], true))) {
$this->errors[] = sprintf($lang['UPLOAD_ERROR_NOT_ALLOWED'], htmlCHR($this->file_ext)); $this->errors[] = sprintf($lang['UPLOAD_ERROR_NOT_ALLOWED'], htmlCHR($this->file_ext));
return false; return false;
@ -134,11 +179,13 @@ class Upload
} }
/** /**
* Store uploaded file
*
* @param string $mode * @param string $mode
* @param array $params * @param array $params
* @return bool * @return bool
*/ */
public function store($mode = '', array $params = []) public function store(string $mode = '', array $params = [])
{ {
if ($mode == 'avatar') { if ($mode == 'avatar') {
delete_avatar($params['user_id'], $params['avatar_ext_id']); delete_avatar($params['user_id'], $params['avatar_ext_id']);
@ -155,6 +202,8 @@ class Upload
} }
/** /**
* Move file to target path
*
* @param $file_path * @param $file_path
* @return bool * @return bool
*/ */