fix(upload): 修复云存储上传失败时静默返回空save_name的问题

This commit is contained in:
augushong
2026-05-11 21:23:00 +08:00
parent 1c99e74c2e
commit 81706debbb
2 changed files with 33 additions and 19 deletions

View File

@@ -113,6 +113,7 @@ class UploadServiceBase
$model_file->mime_type = $file->getMime(); $model_file->mime_type = $file->getMime();
} }
try {
$save_name = Filesystem::disk($this->uploadType)->putFile($upload_dir, $file, function () use ($save_name, $file) { $save_name = Filesystem::disk($this->uploadType)->putFile($upload_dir, $file, function () use ($save_name, $file) {
if (!is_null($save_name)) { if (!is_null($save_name)) {
$ext_name = $file->extension(); $ext_name = $file->extension();
@@ -130,6 +131,13 @@ class UploadServiceBase
return date('Ymd') . '/' . uniqid(); return date('Ymd') . '/' . uniqid();
}); });
} catch (\Throwable $e) {
throw new \Exception('文件上传失败:' . $e->getMessage());
}
if ($save_name === false) {
throw new \Exception('文件上传失败:存储写入失败');
}
$url = $this->url($save_name); $url = $this->url($save_name);

View File

@@ -19,6 +19,7 @@ use League\Flysystem\UnableToWriteFile;
use RuntimeException; use RuntimeException;
use think\Cache; use think\Cache;
use think\File; use think\File;
use think\facade\Log;
/** /**
* Class Driver * Class Driver
@@ -104,11 +105,13 @@ abstract class Driver
$stream = fopen($file->getRealPath(), 'r'); $stream = fopen($file->getRealPath(), 'r');
$path = trim($path . '/' . $name, '/'); $path = trim($path . '/' . $name, '/');
try {
$result = $this->put($path, $stream, $options); $result = $this->put($path, $stream, $options);
} finally {
if (is_resource($stream)) { if (is_resource($stream)) {
fclose($stream); fclose($stream);
} }
}
return $result ? $path : false; return $result ? $path : false;
} }
@@ -116,9 +119,12 @@ abstract class Driver
protected function put(string $path, $contents, array $options = []) protected function put(string $path, $contents, array $options = [])
{ {
try { try {
$this->writeStream($path, $contents, $options); is_resource($contents)
? $this->writeStream($path, $contents, $options)
: $this->write($path, $contents, $options);
} catch (UnableToWriteFile|UnableToSetVisibility $e) { } catch (UnableToWriteFile|UnableToSetVisibility $e) {
return false; Log::error('Filesystem write failed: ' . $e->getMessage());
throw $e;
} }
return true; return true;
} }