diff --git a/app/Http/Requests/Admin/StrategyRequest.php b/app/Http/Requests/Admin/StrategyRequest.php index 5e2d8927..af32a6a1 100644 --- a/app/Http/Requests/Admin/StrategyRequest.php +++ b/app/Http/Requests/Admin/StrategyRequest.php @@ -5,6 +5,7 @@ namespace App\Http\Requests\Admin; use App\Enums\StrategyKey; use App\Http\Requests\FormRequest; use App\Models\Strategy; +use Illuminate\Support\Collection; class StrategyRequest extends FormRequest { @@ -15,6 +16,38 @@ class StrategyRequest extends FormRequest */ public function rules() { + $checkUrl = function ($attribute, $value, $fail) { + if ($this->input('key') == StrategyKey::Local) { + $folders = ['i', 'fonts', 'css', 'js']; + $symlink = Strategy::getRootPath($value); + if (! $symlink) { + return $fail('访问域名缺少根路径'); + } + if (in_array($symlink, $folders)) { + return $fail('系统保留路径'); + } + if (false !== strpbrk($symlink, "\\/?%*:|\"<>")) { + return $fail('根路径名称不符合规则'); + } + // 修改时单独判断 + if ($this->method() === 'PUT') { + $symlinks = Strategy::query() + ->where('id', '<>', $this->route('id')) + ->pluck('configs') + ->transform(function (Collection $configs) { + return Strategy::getRootPath($configs->get('url')); + })->merge($folders)->values()->toArray(); + if (in_array($symlink, $symlinks)) { + return $fail('根路径已经存在'); + } + } else { + if (realpath(public_path($symlink))) { + return $fail('根路径已经存在'); + } + } + } + }; + return [ 'groups' => 'array', 'name' => 'required|max:60', @@ -30,20 +63,7 @@ class StrategyRequest extends FormRequest } } }], - 'configs.url' => ['required', 'url', function ($attribute, $value, $fail) { - if ($this->input('key') == StrategyKey::Local) { - $symlink = Strategy::getRootPath($value); - if (! $symlink) { - return $fail('访问域名缺少根路径'); - } - if (false !== strpbrk($symlink, "\\/?%*:|\"<>")) { - return $fail('根路径名称不符合规则'); - } - if (realpath(public_path($symlink))) { - return $fail('根路径已经存在'); - } - } - }], + 'configs.url' => ['required', 'url', $checkUrl], ]; } diff --git a/app/Models/Strategy.php b/app/Models/Strategy.php index b35c0748..cdb13835 100644 --- a/app/Models/Strategy.php +++ b/app/Models/Strategy.php @@ -62,7 +62,9 @@ class Strategy extends Model if ($strategy->key == StrategyKey::Local) { $symlink = self::getRootPath($strategy->configs['url']); $target = $strategy->configs['root'] ?: config('filesystems.disks.uploads.root'); - (new Filesystem())->link($target, $symlink); + if (! realpath(public_path($symlink))) { + (new Filesystem())->link($target, $symlink); + } // 是否需要移除旧的符号链接 $oldSymlink = self::getRootPath($strategy->getOriginal('configs')['url']); if ($oldSymlink != $symlink) {