diff --git a/app/common/library/Upload.php b/app/common/library/Upload.php index 4bf2a7da..1952a72c 100644 --- a/app/common/library/Upload.php +++ b/app/common/library/Upload.php @@ -106,35 +106,38 @@ class Upload /** * 获取上传驱动句柄 + * @param ?string $driver 驱动名称 + * @param bool $noDriveException 找不到驱动是否抛出异常 + * @return bool|Driver */ - public function getDriver(?string $driver = null): Driver + public function getDriver(?string $driver = null, bool $noDriveException = true): bool|Driver { if (is_null($driver)) { $driver = $this->driver['name']; } if (!isset($this->driver['handler'][$driver])) { - $class = $this->resolveDriverClass($driver); - $this->driver['handler'][$driver] = new $class(); + $class = $this->resolveDriverClass($driver); + if ($class) { + $this->driver['handler'][$driver] = new $class(); + } elseif ($noDriveException) { + throw new InvalidArgumentException("Driver [$driver] not supported."); + } } - return $this->driver['handler'][$driver]; + return $this->driver['handler'][$driver] ?? false; } /** * 获取驱动类 - * @param string $driver - * @return string */ - protected function resolveDriverClass(string $driver): string + protected function resolveDriverClass(string $driver): bool|string { if ($this->driver['namespace'] || str_contains($driver, '\\')) { $class = str_contains($driver, '\\') ? $driver : $this->driver['namespace'] . Str::studly($driver); - if (class_exists($class)) { return $class; } } - - throw new InvalidArgumentException("Driver [$driver] not supported."); + return false; } /** diff --git a/app/common/model/Attachment.php b/app/common/model/Attachment.php index bed84ac6..4b62d650 100644 --- a/app/common/model/Attachment.php +++ b/app/common/model/Attachment.php @@ -43,7 +43,8 @@ class Attachment extends Model public function getFullUrlAttr($value, $row): string { - return self::$upload->getDriver($row['storage'])->url($row['url']); + $driver = self::$upload->getDriver($row['storage'], false); + return $driver ? $driver->url($row['url']) : full_url($row['url']); } /** @@ -58,7 +59,8 @@ class Attachment extends Model ['storage', '=', $model->storage], ])->find(); if ($repeat) { - if (!self::$upload->getDriver($repeat->storage)->exists($repeat->url)) { + $driver = self::$upload->getDriver($repeat->storage, false); + if ($driver && !$driver->exists($repeat->url)) { $repeat->delete(); return true; } else { @@ -92,8 +94,8 @@ class Attachment extends Model { Event::trigger('AttachmentDel', $model); - $driver = self::$upload->getDriver($model->storage); - if ($driver->exists($model->url)) { + $driver = self::$upload->getDriver($model->storage, false); + if ($driver && $driver->exists($model->url)) { $driver->delete($model->url); } }