Skip to content

Commit

Permalink
添加静态文件代理
Browse files Browse the repository at this point in the history
  • Loading branch information
deatil committed Jul 10, 2024
1 parent b7b6d77 commit dd5ba10
Show file tree
Hide file tree
Showing 7 changed files with 229 additions and 2 deletions.
7 changes: 7 additions & 0 deletions resources/routes/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,10 @@
Route::post('flash/upload', Controller\Flash::class . '@upload')->name('admin.flash.upload');
})
->middleware(config('laket.route.middleware'));

// 静态文件
Route::get('assets/:flash/:path', Controller\Assets::class . '@show')
->name('flash.assets')
->pattern([
'path' => '(.*)',
]);
38 changes: 38 additions & 0 deletions src/Controller/Assets.php
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 '';
}
}
26 changes: 26 additions & 0 deletions src/Facade/FlashAsset.php
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';
}
}
122 changes: 122 additions & 0 deletions src/Flash/Asset.php
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);
}
}
4 changes: 4 additions & 0 deletions src/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Laket\Admin\View\View;
use Laket\Admin\Event\Events;
use Laket\Admin\Flash\Asset;
use Laket\Admin\Flash\Manager;
use Laket\Admin\Support\Form;
use Laket\Admin\Support\Loader;
Expand Down Expand Up @@ -196,6 +197,9 @@ protected function registerBind()

// 闪存
$this->app->bind('laket-admin.flash', Manager::class);

// 插件静态文件
$this->app->bind('laket-admin.flash-asset', Asset::class);
}

/**
Expand Down
12 changes: 12 additions & 0 deletions src/Support/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,18 @@ protected function loadFilesFrom($path)
}
}

/**
* 注册静态文件路径
*
* @param string $path
* @param string $namespace
* @return void
*/
protected function loadAssetsFrom(string $path, string $namespace)
{
app('laket-admin.flash-asset')->addNamespace($namespace, $path);
}

/**
* 设置推送
*
Expand Down
22 changes: 20 additions & 2 deletions src/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
use Laket\Admin\Model\Attachment as AttachmentModel;

// 版本号
define("LAKET_VERSION", "1.3.9");
define("LAKET_RELEASE", "1.3.9.20240708");
define("LAKET_VERSION", "1.3.10");
define("LAKET_RELEASE", "1.3.10.20240710");

if (! function_exists('make')) {
/**
Expand Down Expand Up @@ -379,6 +379,24 @@ function laket_attachment_urls($ids, $domain = false)
}
}

if (! function_exists('laket_flash_static')) {
/**
* 插件静态文件
*
* @param string $name 插件包名
* @param string $name 文件路径
* @return string
*/
function laket_flash_static(string $name, string $path) {
$url = url('flash.assets', [
'flash' => $name,
'path' => ltrim($path, '/'),
]);

return (string) $url;
}
}

if (! function_exists('laket_flash_setting')) {
/**
* 插件配置信息
Expand Down

0 comments on commit dd5ba10

Please sign in to comment.