From 81706debbb85359de6f3ff9dfefa623c40bb8ebb Mon Sep 17 00:00:00 2001 From: augushong Date: Mon, 11 May 2026 21:23:00 +0800 Subject: [PATCH] =?UTF-8?q?fix(upload):=20=E4=BF=AE=E5=A4=8D=E4=BA=91?= =?UTF-8?q?=E5=AD=98=E5=82=A8=E4=B8=8A=E4=BC=A0=E5=A4=B1=E8=B4=A5=E6=97=B6?= =?UTF-8?q?=E9=9D=99=E9=BB=98=E8=BF=94=E5=9B=9E=E7=A9=BAsave=5Fname?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../base/common/service/UploadServiceBase.php | 34 ++++++++++++------- extend/think/filesystem/Driver.php | 18 ++++++---- 2 files changed, 33 insertions(+), 19 deletions(-) diff --git a/extend/base/common/service/UploadServiceBase.php b/extend/base/common/service/UploadServiceBase.php index f4282cf..f1ce399 100644 --- a/extend/base/common/service/UploadServiceBase.php +++ b/extend/base/common/service/UploadServiceBase.php @@ -113,23 +113,31 @@ class UploadServiceBase $model_file->mime_type = $file->getMime(); } - $save_name = Filesystem::disk($this->uploadType)->putFile($upload_dir, $file, function () use ($save_name, $file) { - if (!is_null($save_name)) { - $ext_name = $file->extension(); + try { + $save_name = Filesystem::disk($this->uploadType)->putFile($upload_dir, $file, function () use ($save_name, $file) { + if (!is_null($save_name)) { + $ext_name = $file->extension(); - if (empty($ext_name)) { - return $save_name; + if (empty($ext_name)) { + return $save_name; + } + + $list_name = explode('.', $save_name); + + array_pop($list_name); + + return implode('.', $list_name); } - $list_name = explode('.', $save_name); + return date('Ymd') . '/' . uniqid(); + }); + } catch (\Throwable $e) { + throw new \Exception('文件上传失败:' . $e->getMessage()); + } - array_pop($list_name); - - return implode('.', $list_name); - } - - return date('Ymd') . '/' . uniqid(); - }); + if ($save_name === false) { + throw new \Exception('文件上传失败:存储写入失败'); + } $url = $this->url($save_name); diff --git a/extend/think/filesystem/Driver.php b/extend/think/filesystem/Driver.php index e804141..154d783 100644 --- a/extend/think/filesystem/Driver.php +++ b/extend/think/filesystem/Driver.php @@ -19,6 +19,7 @@ use League\Flysystem\UnableToWriteFile; use RuntimeException; use think\Cache; use think\File; +use think\facade\Log; /** * Class Driver @@ -104,10 +105,12 @@ abstract class Driver $stream = fopen($file->getRealPath(), 'r'); $path = trim($path . '/' . $name, '/'); - $result = $this->put($path, $stream, $options); - - if (is_resource($stream)) { - fclose($stream); + try { + $result = $this->put($path, $stream, $options); + } finally { + if (is_resource($stream)) { + fclose($stream); + } } return $result ? $path : false; @@ -116,9 +119,12 @@ abstract class Driver protected function put(string $path, $contents, array $options = []) { try { - $this->writeStream($path, $contents, $options); + is_resource($contents) + ? $this->writeStream($path, $contents, $options) + : $this->write($path, $contents, $options); } catch (UnableToWriteFile|UnableToSetVisibility $e) { - return false; + Log::error('Filesystem write failed: ' . $e->getMessage()); + throw $e; } return true; }