From 577b904aa03b9c335a70164e8402dd9b58c090e4 Mon Sep 17 00:00:00 2001 From: Wisp X <1591788658@qq.com> Date: Thu, 10 Mar 2022 12:22:32 +0800 Subject: [PATCH] fix a bug --- app/Console/Commands/Install.php | 9 ++------- app/Http/Controllers/Api/V1/ImageController.php | 4 ++-- app/Http/Controllers/Controller.php | 11 +++-------- app/Models/Image.php | 9 +++++++-- app/Services/ImageService.php | 2 +- app/Services/UpgradeService.php | 2 +- app/Utils.php | 11 +++++++++++ 7 files changed, 27 insertions(+), 21 deletions(-) diff --git a/app/Console/Commands/Install.php b/app/Console/Commands/Install.php index e1790382..24a68ca4 100644 --- a/app/Console/Commands/Install.php +++ b/app/Console/Commands/Install.php @@ -2,10 +2,10 @@ namespace App\Console\Commands; +use App\Utils; use Illuminate\Console\Command; use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\Config; -use Illuminate\Support\Facades\Log; class Install extends Command { @@ -96,12 +96,7 @@ class Install extends Command } catch (\Throwable $e) { $this->warn("Installation error!\n"); $this->error($e->getMessage()); - Log::error('执行数据库安装程序时出现异常', [ - 'file' => $e->getFile(), - 'line' => $e->getLine(), - 'msg' => $e->getMessage(), - 'trace' => $e->getTraceAsString(), - ]); + Utils::e($e, '执行数据库安装程序时出现异常'); return 1; } diff --git a/app/Http/Controllers/Api/V1/ImageController.php b/app/Http/Controllers/Api/V1/ImageController.php index fb7628ae..7b73b806 100644 --- a/app/Http/Controllers/Api/V1/ImageController.php +++ b/app/Http/Controllers/Api/V1/ImageController.php @@ -7,11 +7,11 @@ use App\Http\Controllers\Controller; use App\Models\Image; use App\Models\User; use App\Services\ImageService; +use App\Utils; use Illuminate\Auth\AuthenticationException; use Illuminate\Http\Request; use Illuminate\Http\Response; use Illuminate\Support\Facades\Auth; -use Illuminate\Support\Facades\Log; class ImageController extends Controller { @@ -44,7 +44,7 @@ class ImageController extends Controller } catch (UploadException $e) { return $this->fail($e->getMessage()); } catch (\Throwable $e) { - Log::error("Api 上传文件时发生异常,", ['message' => $e->getMessage(), 'trace' => $e->getTraceAsString()]); + Utils::e($e, 'Api 上传文件时发生异常'); if (config('app.debug')) { return $this->fail($e->getMessage()); } diff --git a/app/Http/Controllers/Controller.php b/app/Http/Controllers/Controller.php index 0d261a3e..e7908842 100644 --- a/app/Http/Controllers/Controller.php +++ b/app/Http/Controllers/Controller.php @@ -11,6 +11,7 @@ use App\Models\Image; use App\Models\Strategy; use App\Models\User; use App\Services\ImageService; +use App\Utils; use Illuminate\Foundation\Auth\Access\AuthorizesRequests; use Illuminate\Foundation\Bus\DispatchesJobs; use Illuminate\Foundation\Validation\ValidatesRequests; @@ -20,7 +21,6 @@ use Illuminate\Routing\Controller as BaseController; use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Hash; -use Illuminate\Support\Facades\Log; use Illuminate\View\View; use League\Flysystem\FilesystemException; use Symfony\Component\Console\Output\BufferedOutput; @@ -100,12 +100,7 @@ class Controller extends BaseController $user->save(); } catch (\Throwable $e) { @unlink(base_path('installed.lock')); - Log::error('执行安装程序时出现异常', [ - 'file' => $e->getFile(), - 'line' => $e->getLine(), - 'msg' => $e->getMessage(), - 'trace' => $e->getTraceAsString(), - ]); + Utils::e($e, '执行安装程序时出现异常'); return $this->fail($e->getMessage()); } return $this->success(); @@ -121,7 +116,7 @@ class Controller extends BaseController } catch (UploadException $e) { return $this->fail($e->getMessage()); } catch (\Throwable $e) { - Log::error("Web 上传文件时发生异常,", ['message' => $e->getMessage(), 'trace' => $e->getTraceAsString()]); + Utils::e($e, 'Web 上传文件时发生异常'); if (config('app.debug')) { return $this->fail($e->getMessage()); } diff --git a/app/Models/Image.php b/app/Models/Image.php index cf8f9319..f5eb0edb 100644 --- a/app/Models/Image.php +++ b/app/Models/Image.php @@ -5,6 +5,7 @@ namespace App\Models; use App\Enums\GroupConfigKey; use App\Enums\ImagePermission; use App\Services\ImageService; +use App\Utils; use Carbon\Carbon; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Casts\Attribute; @@ -110,8 +111,12 @@ class Image extends Model ) { // 删除本地缓存文件 Cache::forget("image_thumb_{$image->key}"); - // 删除物理文件 - $image->filesystem()->delete($image->pathname); + try { + // 删除物理文件 + $image->filesystem()->delete($image->pathname); + } catch (\Throwable $e) { + Utils::e($e, '删除物理文件时发生异常'); + } } }); } diff --git a/app/Services/ImageService.php b/app/Services/ImageService.php index f52ce482..2f2228b9 100644 --- a/app/Services/ImageService.php +++ b/app/Services/ImageService.php @@ -176,7 +176,7 @@ class ImageService try { $filesystem->writeStream($pathname, $handle); } catch (FilesystemException $e) { - Log::error('保存图片时出现异常', ['message' => $e->getMessage(), 'trace' => $e->getTraceAsString()]); + Utils::e($e, '保存图片时出现异常'); throw new UploadException(config('app.debug', false) ? $e->getMessage() : '图片上传失败'); } @fclose($handle); diff --git a/app/Services/UpgradeService.php b/app/Services/UpgradeService.php index 332c802f..0f06fd21 100644 --- a/app/Services/UpgradeService.php +++ b/app/Services/UpgradeService.php @@ -115,7 +115,7 @@ class UpgradeService Cache::forget('configs'); Artisan::call('package:discover'); } catch (\Throwable $e) { - Log::error('升级失败', ['message' => $e->getMessage(), $e->getTraceAsString()]); + Utils::e($e, '升级失败'); $this->setProgress($e->getMessage(), 'fail'); @unlink(base_path($this->lock)); return false; diff --git a/app/Utils.php b/app/Utils.php index 7c9b6f36..1de3eef3 100644 --- a/app/Utils.php +++ b/app/Utils.php @@ -7,9 +7,20 @@ use App\Models\Config; use Illuminate\Support\Carbon; use Illuminate\Support\Collection; use Illuminate\Support\Facades\Cache; +use Illuminate\Support\Facades\Log; class Utils { + public static function e(\Throwable $e, $message = '', $level = 'error') + { + Log::{$level}($message, [ + 'file' => $e->getFile(), + 'line' => $e->getLine(), + 'message' => $e->getMessage(), + 'trace' => $e->getTraceAsString(), + ]); + } + /** * 获取头像地址 *