-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
229 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
declare (strict_types = 1); | ||
|
||
namespace Laket\Admin\Controller; | ||
|
||
use think\response\File; | ||
|
||
/** | ||
* 代理静态文件 | ||
* | ||
* @create 2024-7-10 | ||
* @author deatil | ||
*/ | ||
class Assets | ||
{ | ||
/** | ||
* 代理静态文件 | ||
*/ | ||
public function show($flash, $path) | ||
{ | ||
$path = $path . '.' . request()->ext(); | ||
$contents = app('laket-admin.flash-asset')->getContent($flash, $path); | ||
|
||
$content = $contents['content'] ?? ''; | ||
$type = $contents['type'] ?? ''; | ||
|
||
if ($content && $type) { | ||
$response = response($content, 200, [ | ||
'Content-Type' => $type, | ||
]); | ||
|
||
return $response; | ||
} | ||
|
||
return ''; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
declare (strict_types = 1); | ||
|
||
namespace Laket\Admin\Facade; | ||
|
||
use think\Facade; | ||
|
||
/** | ||
* 插件静态文件 | ||
* | ||
* @create 2024-7-10 | ||
* @author deatil | ||
*/ | ||
class FlashAsset extends Facade | ||
{ | ||
/** | ||
* 获取当前Facade对应类名(或者已经绑定的容器对象标识) | ||
* @access protected | ||
* @return string | ||
*/ | ||
protected static function getFacadeClass() | ||
{ | ||
return 'laket-admin.flash-asset'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
<?php | ||
|
||
declare (strict_types = 1); | ||
|
||
namespace Laket\Admin\Flash; | ||
|
||
use think\File; | ||
|
||
class Asset | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
protected array $namespaces = []; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected string $mimeType; | ||
|
||
/** | ||
* 设置文件类型 | ||
* | ||
* @param string $filename 文件名 | ||
* @return $this | ||
*/ | ||
public function withMimeType(string $mimeType) | ||
{ | ||
$this->mimeType = $mimeType; | ||
return $this; | ||
} | ||
|
||
/** | ||
* 添加命名空间 | ||
* | ||
* @param $namespace | ||
* @param $path | ||
* @return $this | ||
*/ | ||
public function addNamespace(string $namespace, string $path): self | ||
{ | ||
$this->namespaces[$namespace] = rtrim($path, '/\\'); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* 获取文件类型 | ||
* | ||
* @param $flashName | ||
* @param $file | ||
* @return array | ||
*/ | ||
public function getContent(string $flashName, string $file) | ||
{ | ||
if (! isset($this->namespaces[$flashName])) { | ||
return []; | ||
} | ||
|
||
$prefix = $this->namespaces[$flashName]; | ||
|
||
$filePath = realpath($prefix . '/' . ltrim($file, '/\\')) ?: ''; | ||
if (! str_starts_with($filePath, realpath($prefix))) { | ||
return []; | ||
} | ||
|
||
if (is_file($filePath)) { | ||
$extension = (new File($filePath))->extension(); | ||
|
||
return [ | ||
'type' => $this->getMimeType($filePath), | ||
'content' => file_get_contents($filePath), | ||
]; | ||
} | ||
|
||
return []; | ||
} | ||
|
||
/** | ||
* 获取路径 | ||
* | ||
* @param $flashName | ||
* @param $file | ||
* @return string | ||
*/ | ||
public function getPath(string $flashName, string $file) | ||
{ | ||
if (! isset($this->namespaces[$flashName])) { | ||
return ''; | ||
} | ||
|
||
$prefix = $this->namespaces[$flashName]; | ||
|
||
$filePath = realpath($prefix . '/' . ltrim($file, '/\\')) ?: ''; | ||
if (! str_starts_with($filePath, realpath($prefix))) { | ||
return ''; | ||
} | ||
|
||
if (is_file($filePath)) { | ||
return $filePath; | ||
} | ||
|
||
return ''; | ||
} | ||
|
||
/** | ||
* 获取文件类型信息 | ||
* | ||
* @param string $filename | ||
* @return string | ||
*/ | ||
protected function getMimeType(string $filename): string | ||
{ | ||
if (!empty($this->mimeType)) { | ||
return $this->mimeType; | ||
} | ||
|
||
$finfo = finfo_open(FILEINFO_MIME_TYPE); | ||
|
||
return finfo_file($finfo, $filename); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters