3 Commits
Author SHA1 Message Date
as2252258 614f21b240 ea 2026-07-08 15:39:43 +08:00
as2252258 1bca442f55 eee 2026-07-08 10:45:21 +08:00
as2252258 aac6d5b80a eee 2026-07-07 17:00:05 +08:00
2 changed files with 174 additions and 132 deletions
+158 -123
View File
@@ -9,146 +9,181 @@ class File
{
private string $name;
private string $tmp_name;
private int $error;
private string $type;
private int $size;
private int $limit = 1024000;
private int $offset = 0;
private string $name;
private string $tmp_name;
private int $error;
private string $type;
private int $size;
private int $limit = 1024000;
private int $offset = 0;
/**
* @param array $array
*/
public function __construct(array $array)
{
$this->name = $array['name'];
$this->tmp_name = $array['tmp_name'];
$this->error = $array['error'];
$this->type = $array['type'];
$this->size = $array['size'];
}
/**
* @param array $array
*/
public function __construct(array $array)
{
$this->name = $array['name'];
$this->tmp_name = $array['tmp_name'];
$this->error = $array['error'];
$this->type = $array['type'];
$this->size = $array['size'];
}
const array errorInfo = [
0 => 'UPLOAD_ERR_OK.',
1 => 'The uploaded file exceeds the upload_max_filesize directive in php.ini.',
2 => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.',
3 => 'The uploaded file was only partially uploaded.',
4 => 'No file was uploaded.',
6 => 'Missing a temporary folder.',
7 => 'Failed to write file to disk.',
8 => 'A PHP extension stopped the file upload.'
];
const array errorInfo = [
0 => 'UPLOAD_ERR_OK.',
1 => 'The uploaded file exceeds the upload_max_filesize directive in php.ini.',
2 => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.',
3 => 'The uploaded file was only partially uploaded.',
4 => 'No file was uploaded.',
6 => 'Missing a temporary folder.',
7 => 'Failed to write file to disk.',
8 => 'A PHP extension stopped the file upload.',
];
/**
* @param string $path
* @return bool
* @throws
*/
public function saveTo(string $path): bool
{
if ($this->hasError()) {
throw new Exception($this->getErrorInfo());
}
/**
* @param string $path
* @return bool
* @throws
*/
public function saveTo(string $path): bool
{
if ($this->hasError()) {
throw new Exception($this->getErrorInfo());
}
@move_uploaded_file($this->tmp_name, $path);
if (!file_exists($path)) {
return false;
}
return true;
}
@move_uploaded_file($this->tmp_name, $path);
if (!file_exists($path)) {
return false;
}
return true;
}
/**
* @return string
* @throws
*/
public function rename(): string
{
if (!empty($this->newName)) return $this->newName;
if (!file_exists($this->getTmpPath())) {
throw new Exception('(' . $this->name . ')Failed to open stream: No such file or directory');
}
/**
* @return string
* @throws
*/
public function rename(): string
{
if (!empty($this->newName))
return $this->newName;
if (!file_exists($this->getTmpPath())) {
throw new Exception('(' . $this->name . ')Failed to open stream: No such file or directory');
}
$hash = md5_file($this->getTmpPath());
$hash = md5_file($this->getTmpPath());
$later = match ($this->type) {
'image/jpeg', 'image/jpg' => '.jpeg',
'image/gif' => '.gif',
'image/png' => '.png',
default => '.' . $this->exif_imageType(),
};
$later = '.' . exif_imagetype($this->getTmpPath());
$match = '/(\w{12})(\w{5})(\w{9})(\w{6})/';
$tmp = preg_replace($match, '$1-$2-$3-$4', $hash);
$match = '/(\w{12})(\w{5})(\w{9})(\w{6})/';
$tmp = preg_replace($match, '$1-$2-$3-$4', $hash);
return $this->name = strtoupper($tmp) . $later;
}
return $this->name = strtoupper($tmp) . $later;
}
/**
* @return string
*/
public function getType(): string
{
return $this->type;
}
/**
* @return int|null
* @throws Exception
*/
private function exif_imageType(): ?int
{
return match (\exif_imagetype($this->tmp_name)) {
IMAGETYPE_GIF => '.gif',
IMAGETYPE_JPEG => '.jpg',
IMAGETYPE_PNG => '.png',
IMAGETYPE_SWF => '.swf',
IMAGETYPE_PSD => '.psd',
IMAGETYPE_BMP => '.bmp',
IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM => '.tiff',
IMAGETYPE_JPC => '.jpc',
IMAGETYPE_JP2 => '.jp2',
IMAGETYPE_JPX => '.jpx',
IMAGETYPE_JB2 => '.jb2',
IMAGETYPE_SWC => '.swc',
IMAGETYPE_IFF => '.iff',
IMAGETYPE_WBMP => '.wbmp',
IMAGETYPE_XBM => '.xbm',
IMAGETYPE_ICO => '.ico',
IMAGETYPE_WEBP => '.webp',
IMAGETYPE_AVIF => '.avif',
default => throw new Exception('未知的图片类型'),
};
}
/**
* @return int
*/
public function getSize(): int
{
return $this->size;
}
/**
* @return string
*/
public function getType(): string
{
return $this->type;
}
/**
* @return string
* @throws
*/
public function getContent(): string
{
$open = fopen($this->getTmpPath(), 'r');
$content = '';
while ($file = fread($open, $this->limit)) {
$content .= $file;
fseek($open, $this->offset);
if ($this->offset >= $this->getSize()) {
break;
}
$this->offset += $this->limit;
}
fclose($open);
$this->offset = 0;
return $content;
}
/**
* @return int
*/
public function getSize(): int
{
return $this->size;
}
/**
* @return string
*/
public function getTmpPath(): string
{
return $this->tmp_name;
}
/**
* @return string
* @throws
*/
public function getContent(): string
{
$open = fopen($this->getTmpPath(), 'r');
$content = '';
while ($file = fread($open, $this->limit)) {
$content .= $file;
fseek($open, $this->offset);
if ($this->offset >= $this->getSize()) {
break;
}
$this->offset += $this->limit;
}
fclose($open);
$this->offset = 0;
return $content;
}
/**
* @return bool
*
* check file have error
*/
public function hasError(): bool
{
return $this->error !== 0;
}
/**
* @return mixed
*
* get upload error info
*/
public function getErrorInfo(): mixed
{
if (!isset(self::errorInfo[$this->error])) {
return 'Unknown upload error.';
}
return self::errorInfo[$this->error];
}
/**
* @return string
*/
public function getTmpPath(): string
{
return $this->tmp_name;
}
/**
* @return bool
*
* check file have error
*/
public function hasError(): bool
{
return $this->error !== 0;
}
/**
* @return mixed
*
* get upload error info
*/
public function getErrorInfo(): mixed
{
if (!isset(self::errorInfo[$this->error])) {
return 'Unknown upload error.';
}
return self::errorInfo[$this->error];
}
}
+15 -8
View File
@@ -239,8 +239,8 @@ class Router
);
$scanner->setConfig($scanConfig);
$normalizedAppPath = str_replace('\\', '/', APP_PATH . 'app');
$normalizedRoutePath = str_replace('\\', '/', APP_PATH . 'routes');
$normalizedAppPath = str_replace('\\', '/', realpath(APP_PATH . 'app') ?: APP_PATH . 'app');
$normalizedRoutePath = str_replace('\\', '/', realpath(APP_PATH . 'routes') ?: APP_PATH . 'routes');
$routeChanged = false;
$appChangedFiles = [];
@@ -255,8 +255,12 @@ class Router
}
}
if (!empty($appChangedFiles)) {
$appChangedFiles = $scanner->expandDependentFiles($appChangedFiles, APP_PATH . 'app/');
}
$usedArtifact = false;
if (($scanConfig['cache_enabled'] ?? false) && !$routeChanged && $artifactState->has(static::$type)) {
if (($scanConfig['cache_enabled'] ?? false) && $artifactState->has(static::$type)) {
$artifact = $artifactState->load(static::$type);
$router = $container->get(DataGrip::class)->get(static::$type);
$usedArtifact = $router->importArtifact($artifact, $appChangedFiles);
@@ -266,8 +270,8 @@ class Router
// route artifact 只加速注解路由,不能替代 routes/*.php 的注册副作用。
$this->read_dir_file(APP_PATH . 'routes');
if (!$routeChanged && !empty($appChangedFiles) && ($scanConfig['cache_enabled'] ?? false)) {
$scanner->scanFiles($appChangedFiles, APP_PATH . 'app/', null, !$usedArtifact);
if (!empty($appChangedFiles) && ($scanConfig['cache_enabled'] ?? false)) {
$scanner->scanFiles($appChangedFiles, APP_PATH . 'app/', null, true);
} elseif (!$usedArtifact) {
$scanner->scan(APP_PATH . 'app/');
} else {
@@ -462,8 +466,12 @@ class Router
private function resolve_file($files): void
{
try {
static::$currentSourceFile = str_replace('\\', '/', realpath($files) ?: $files);
include "$files";
$file = realpath($files) ?: $files;
static::$currentSourceFile = str_replace('\\\\', '/', $file);
if (function_exists('opcache_invalidate')) {
@opcache_invalidate($file, true);
}
include $file;
} catch (\Throwable $throwable) {
\Kiri::getLogger()->json_log($throwable);
} finally {
@@ -471,7 +479,6 @@ class Router
}
}
public static function getCurrentSourceFile(): ?string
{
return static::$currentSourceFile;