Update Files, File and Image

This commit is contained in:
Visman 2023-03-30 21:25:32 +07:00
parent c98726469b
commit 5cdec31ed1
3 changed files with 82 additions and 65 deletions

View file

@ -10,6 +10,7 @@ declare(strict_types=1);
namespace ForkBB\Core;
use ForkBB\Core\Files;
use ForkBB\Core\Exceptions\FileException;
use InvalidArgumentException;
@ -54,7 +55,7 @@ class File
* Флаг автопереименования файла
* @var bool
*/
protected $rename = false;
protected $rename = false;
/**
* Флаг перезаписи файла
@ -66,10 +67,18 @@ class File
* Паттерн для pathinfo
* @var string
*/
protected $pattern = '%^(?!.*?\.\.)([\w.\x5C/:-]*[\x5C/])?(\*|[\w.-]+)\.(\*|[a-z\d]+)$%i';
protected $pattern = '%^(?!.*?\.\.)([\w.\x5C/:-]*[\x5C/])?(\*|[\w.-]+)\.(\*|[a-z\d]+)$%iD';
public function __construct(string $path, array $options)
/**
* @var Files
*/
protected $files;
public function __construct(string $path, string $name, string $ext, Files $files)
{
if ($files->isBadPath($path)) {
throw new FileException('Bad path to file');
}
if (! \is_file($path)) {
throw new FileException('File not found');
}
@ -77,24 +86,13 @@ class File
throw new FileException('File can not be read');
}
$this->path = $path;
$this->data = null;
$this->files = $files;
$this->path = $path;
$this->name = $name;
$this->ext = $ext;
$this->data = null;
$this->size = \is_string($this->data) ? \strlen($this->data) : \filesize($path);
$name = null;
$ext = null;
if (isset($options['basename'])) {
if (false === ($pos = \strrpos($options['basename'], '.'))) {
$name = $options['basename'];
} else {
$name = \substr($options['basename'], 0, $pos);
$ext = \substr($options['basename'], $pos + 1);
}
}
$this->name = isset($options['filename']) && \is_string($options['filename']) ? $options['filename'] : $name;
$this->ext = isset($options['extension']) && \is_string($options['extension']) ? $options['extension'] : $ext;
$this->size = \is_string($this->data) ? \strlen($this->data) : \filesize($path);
if (! $this->size) {
throw new FileException('File size is undefined');
}
@ -234,31 +232,48 @@ class File
{
$info = $this->pathinfo($path);
if (empty($info)) {
return false;
}
if ($this->files->isBadPath($info['dirname'])) {
$this->error = 'Bad path to file';
return false;
}
if (
null === $info
|| ! $this->dirProc($info['dirname'])
! $this->dirProc($info['dirname'])
) {
return false;
}
if ($this->rename) {
$old = $info['filename'];
$i = 1;
while (\file_exists($info['dirname'] . $info['filename'] . '.' . $info['extension'])) {
++$i;
$info['filename'] = $old . '_' . $i;
$name = $info['filename'];
$i = 1;
while (true) {
$path = $info['dirname'] . $info['filename'] . '.' . $info['extension'];
if ($this->files->isBadPath($path)) {
$this->error = 'Bad path to file';
return false;
}
} elseif (
! $this->rewrite
&& \file_exists($info['dirname'] . $info['filename'] . '.' . $info['extension'])
) {
$this->error = 'Such file already exists';
return false;
if (\file_exists($path)) {
if ($this->rename) {
++$i;
$info['filename'] = $name . '_' . $i;
continue;
} elseif (! $this->rewrite) {
$this->error = 'Such file already exists';
return false;
}
}
break;
}
$path = $info['dirname'] . $info['filename'] . '.' . $info['extension'];
if ($this->fileProc($path)) {
$this->size = \filesize($path);

View file

@ -56,6 +56,12 @@ class Files
*/
protected $imageDriver;
/**
* Список имён драйверов
* @var array
*/
protected $imageDrivers;
/**
* Список mime типов считающихся картинками
* @var array
@ -857,7 +863,8 @@ class Files
public function __construct(/* string|int */ $maxFileSize, /* string|int */ $maxImgSize, array $imageDrivers, Container $c)
{
$this->c = $c;
$this->c = $c;
$this->imageDrivers = $imageDrivers;
$init = \min(
\PHP_INT_MAX,
@ -875,19 +882,22 @@ class Files
$this->size($maxFileSize),
$init
);
$this->imageDriver = $this->curImageDriver($imageDrivers);
}
/**
* Возращает драйвер для работы с изображениями
*/
protected function curImageDriver(array $drivers): DefaultDriver
public function imageDriver(): DefaultDriver
{
foreach ($drivers as $class) {
$driver = new $class($this);
if ($this->imageDriver instanceof DefaultDriver) {
return $this->imageDriver;
}
if (true === $driver->ready()) {
return $driver;
foreach ($this->imageDrivers as $class) {
$this->imageDriver = new $class($this);
if (true === $this->imageDriver->ready()) {
return $this->imageDriver;
}
}
@ -1100,12 +1110,12 @@ class Files
return null;
}
if (\preg_match('%^(.+)\.([^.\\\/]++)$%D', $file['name'], $matches)) {
$name = $matches[1];
$ext = $matches[2];
} else {
if (false === ($pos = \strrpos($file['name'], '.'))) {
$name = $file['name'];
$ext = '';
$exy = '';
} else {
$name = \substr($file['name'], 0, $pos);
$ext = \substr($file['name'], $pos + 1);
}
$imageExt = $this->imageExt($file['tmp_name']);
@ -1145,18 +1155,10 @@ class Files
return null;
}
$options = [
'filename' => $name,
'extension' => $ext,
'basename' => $name . '.' . $ext,
'mime' => $mimeType, //$file['type'],
// 'size' => $file['size'],
];
$level = $this->c->ErrorHandler->logOnly(\E_WARNING);
try {
$result = new $className($file['tmp_name'], $options, $this->imageDriver);
$result = new $className($file['tmp_name'], $name, $ext, $this);
} catch (FileException $e) {
$this->error = $e->getMessage();

View file

@ -40,22 +40,22 @@ class Image extends File
* Паттерн для pathinfo
* @var string
*/
protected $pattern = '%^(?!.*?\.\.)([\w.\x5C/:-]*[\x5C/])?(\*|[\w.-]+)\.(\*|[a-z\d]+|\([a-z\d]+(?:\|[a-z\d]+)*\))$%i';
protected $pattern = '%^(?!.*?\.\.)([\w.\x5C/:-]*[\x5C/])?(\*|[\w.-]+)\.(\*|[a-z\d]+|\([a-z\d]+(?:\|[a-z\d]+)*\))$%iD';
public function __construct(string $path, array $options, DefaultDriver $imgDriver)
public function __construct(string $path, string $name, string $ext, Files $files)
{
parent::__construct($path, $options);
parent::__construct($path, $name, $ext, $files);
if ($imgDriver::DEFAULT) {
$this->imgDriver = $files->imageDriver();
if ($this->imgDriver::DEFAULT) {
throw new FileException('No library for work with images');
}
$this->imgDriver = $imgDriver;
if (\is_string($this->data)) {
$this->image = $imgDriver->readFromStr($this->data);
$this->image = $this->imgDriver->readFromStr($this->data);
} else {
$this->image = $imgDriver->readFromPath($this->path);
$this->image = $this->imgDriver->readFromPath($this->path);
}
if (false === $this->image) {