'', ]; protected $casts = [ 'id' => 'integer', 'key' => 'integer', 'configs' => 'collection', ]; const DRIVERS = [ StrategyKey::Local => '本地', StrategyKey::S3 => 'AWS S3', StrategyKey::Oss => '阿里云 OSS', StrategyKey::Cos => '腾讯云 COS', StrategyKey::Kodo => '七牛云 Kodo', StrategyKey::Uss => '又拍云 USS', StrategyKey::Sftp => 'SFTP', StrategyKey::Ftp => 'FTP', StrategyKey::Webdav => 'WebDav', StrategyKey::Minio => 'Minio', ]; protected static function booted() { static::saving(function (self $strategy) { $strategy->configs['root'] = $strategy->configs->get('root'); $strategy->configs['url'] = rtrim($strategy->configs->get('url'), '/'); if ($strategy->key == StrategyKey::Local) { $symlink = self::getRootPath($strategy->configs['url']); $target = $strategy->configs['root'] ?: config('filesystems.disks.uploads.root'); if (! is_dir(public_path($symlink))) { (new Filesystem())->link($target, $symlink); } // 是否需要移除旧的符号链接 $url = $strategy->getOriginal('configs')['url'] ?? ''; if ($url) { $oldSymlink = self::getRootPath($url); if ($oldSymlink != $symlink) { @unlink($oldSymlink); } } } }); } public static function getRootPath($url): string { $path = (parse_url($url)['path'] ?? ''); return current(array_filter(explode('/', $path))); } public function intro(): Attribute { return new Attribute( set: fn ($value) => $value ?: '', ); } public function groups(): BelongsToMany { return $this->belongsToMany(Group::class, 'group_strategy', 'strategy_id', 'group_id'); } public function images(): HasMany { return $this->hasMany(Image::class, 'strategy_id', 'id'); } }