diff --git a/app/Http/Controllers/Admin/SettingController.php b/app/Http/Controllers/Admin/SettingController.php index 25ff35a6..b674af36 100644 --- a/app/Http/Controllers/Admin/SettingController.php +++ b/app/Http/Controllers/Admin/SettingController.php @@ -45,12 +45,17 @@ class SettingController extends Controller { $version = Utils::config(ConfigKey::AppVersion); $service = new UpgradeService($version); - $data = [ - 'is_update' => $service->check(), - ]; - if ($data['is_update']) { - $data['version'] = $service->getVersions()->first(); + try { + $data = [ + 'is_update' => $service->check(), + ]; + if ($data['is_update']) { + $data['version'] = $service->getVersions()->first(); + } + } catch (\Exception $e) { + return $this->error($e->getMessage()); } + return $this->success('success', $data); } diff --git a/app/Services/UpgradeService.php b/app/Services/UpgradeService.php index 55ee7fb8..95cc3cd2 100644 --- a/app/Services/UpgradeService.php +++ b/app/Services/UpgradeService.php @@ -4,6 +4,8 @@ namespace App\Services; use App\Enums\ConfigKey; use App\Models\Config; +use App\Utils; +use Illuminate\Http\Client\PendingRequest; use Illuminate\Support\Collection; use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\Cache; @@ -15,7 +17,7 @@ use League\Flysystem\Local\LocalFilesystemAdapter; class UpgradeService { - const ApiUrl = 'https://api.lsky.pro'; + const ApiUrl = 'https://api.lsky.pro/v1'; /** @var array|array[] 所有版本 */ protected array $versions = []; @@ -28,8 +30,11 @@ class UpgradeService protected Filesystem $filesystem; + protected PendingRequest $http; + public function __construct(protected string $version) { + $this->http = Http::baseUrl(self::ApiUrl)->withOptions(['timeout' => 30])->timeout(30); $this->filesystem = new Filesystem(new LocalFilesystemAdapter(base_path())); } @@ -37,38 +42,27 @@ class UpgradeService * 是否需要更新 * * @return bool + * @throws \Exception */ public function check(): bool { return version_compare($this->version, $this->getVersions()->first()['name']) === -1; } + /** + * @throws \Exception + */ public function getVersions(): Collection { if (! $this->versions) { - // TODO 获取所有版本 - $this->versions = [ - [ - 'icon' => 'https://docs.lsky.pro/logo.png', - 'name' => 'V 2.0.1', - 'size' => '33.5 MB', - 'md5' => '12', - 'changelog' => (new \Parsedown())->parse('### Added -- 一键复制全部链接 ([#167](https://github.com/wisp-x/lsky-pro/issues/167)) - -### Changed -- 将所有静态资源放置本地 -- 接口增加刷新 token 属性 -- 个人中心、后台显示用户注册时间 ([#263](https://github.com/wisp-x/lsky-pro/pull/263)) - -FAQ: -- 为了保证可用性,此次更新主要是为了静态文件放置本地,不再使用第三方静态资源托管服务。 -- 如没有特殊情况,这次更新为 1.x 版本最后一个小版本。最新版本动态请[戳我](https://github.com/wisp-x/lsky-pro/projects/1) - -'), - 'pushed_at' => '2022-02-26 12:21', - ], - ]; + $response = $this->http->timeout(30)->get('/versions'); + if (! $response->successful()) { + throw new \Exception('无法请求升级服务器'); + } + $this->versions = $response->json() ?: []; + foreach ($this->versions as &$version) { + $version['changelog'] = (new \Parsedown())->text($version['changelog']); + } } return collect($this->versions); } @@ -87,44 +81,37 @@ FAQ: file_put_contents(base_path($this->lock), ''); $this->setProgress('准备升级...'); - // TODO 获取差异信息 - $diff = [ - 'server_url' => 'https://raw.githubusercontent.com/wisp-x/lsky-pro/dev', - 'files' => [ - [ - 'path' => '.env.example', - 'md5' => 'c931d81183b69204496923f2fbe3dfd5', - 'action' => 'copied', // in added, deleted, copied - ], - ], - ]; + // 获取差异信息 + $response = $this->http->timeout(30)->get('/diff/'.urlencode(Utils::config(ConfigKey::AppVersion))); + if (! $response->successful()) { + throw new \Exception('无法请求升级服务器'); + } + $diff = $response->json(); + $this->setProgress('下载补丁包...'); - $timeout = 30; foreach ($diff['files'] as $file) { if ($file['action'] === 'deleted') continue; - $response = Http::withOptions([ - 'timeout' => $timeout, - ])->timeout($timeout)->get($diff['server_url'].'/'.$file['path']); + $response = Http::get($diff['server_url'].'/'.$file['pathname']); if (! $response->successful()) { - throw new \Exception("补丁文件 {$file['path']} 下载失败。"); + throw new \Exception("补丁文件 {$file['pathname']} 下载失败。"); } - $this->filesystem->write($this->temp.'/'.$file['path'], $response->body()); + $this->filesystem->write($this->temp.'/'.$file['pathname'], $response->body()); // 校验文件 - if ($file['md5'] !== md5_file(base_path($this->temp).'/'.$file['path'])) { - throw new \Exception("补丁文件 {$file['path']} 校验失败。"); + if ($file['md5'] !== md5_file(base_path($this->temp).'/'.$file['pathname'])) { + throw new \Exception("补丁文件 {$file['pathname']} 校验失败。"); } } $this->setProgress('执行升级...'); foreach ($diff['files'] as $file) { match ($file['action']) { - 'added', 'copied' => $this->filesystem->copy($this->temp.'/'.$file['path'], $file['path']), - 'deleted' => $this->filesystem->delete($file['path']), + 'added', 'copied' => $this->filesystem->copy($this->temp.'/'.$file['pathname'], $file['pathname']), + 'deleted' => $this->filesystem->delete($file['pathname']), }; } $this->setProgress('清除缓存...'); // 更新版本号 $version = $this->getVersions()->first()['name']; - //Config::query()->where('name', ConfigKey::AppVersion)->update(['value' => $version]); + Config::query()->where('name', ConfigKey::AppVersion)->update(['value' => $version]); // 执行数据库迁移 Artisan::call('migrate'); // 清除配置缓存