10 * 1024 * 1024) { throw new \RuntimeException('营业执照图片无效'); } if (($attachment['storage'] ?? '') === 'cos') { $config = get_addon_config('cos'); if (!$config || empty($config['bucket']) || empty($config['region']) || empty($config['secretId']) || empty($config['secretKey'])) { throw new \RuntimeException('云存储未配置'); } Loader::addNamespace('Qcloud\\Cos', ROOT_PATH . 'addons/cos/library/Qcloud/Cos/'); $client = new \Qcloud\Cos\Client([ 'region' => $config['region'], 'schema' => 'https', 'credentials' => ['secretId' => $config['secretId'], 'secretKey' => $config['secretKey']], ]); $result = $client->getObject(['Bucket' => $config['bucket'], 'Key' => ltrim($url, '/'), 'Range' => 'bytes=0-10485760']); $bytes = (string)$result['Body']; } elseif (($attachment['storage'] ?? '') === 'local') { $root = realpath(ROOT_PATH . 'public'); $file = realpath(ROOT_PATH . 'public' . $url); if (!$root || !$file || strpos($file, $root . DIRECTORY_SEPARATOR) !== 0 || !is_file($file) || !is_readable($file)) { throw new \RuntimeException('营业执照图片不存在'); } if (filesize($file) > 10 * 1024 * 1024) throw new \RuntimeException('营业执照图片过大'); $bytes = file_get_contents($file); } else { throw new \RuntimeException('不支持的图片存储类型'); } if (!is_string($bytes) || $bytes === '' || strlen($bytes) > 10 * 1024 * 1024) { throw new \RuntimeException('营业执照图片读取失败'); } return $bytes; } }