app = $app; $this->link = sysconf('storage.link_type|raw'); $this->init(); } /** * 重构后兼容处理. * @return array|string * @throws Exception */ public function __call(string $method, array $arguments) { if (strtolower($method) === 'builduploadtoken') { if (method_exists($this, 'token')) { return $this->token(...$arguments); } } // 调用方法异常处理 $class = class_basename(static::class); throw new Exception("method not exists: {$class}->{$method}()"); } /** * 获取对象实例. * @return static */ public static function instance() { /* @var \think\admin\contract\StorageInterface */ return Container::getInstance()->make(static::class); } /** * 自定义初始化方法. */ protected function init() {} /** * 获取下载链接后缀 * @param null|string $attname 下载名称 * @param null|string $filename 文件名称 */ protected function getSuffix(?string $attname = null, ?string $filename = null): string { [$class, $suffix] = [class_basename(get_class($this)), '']; if (is_string($filename) && stripos($this->link, 'compress') !== false) { $compress = [ 'LocalStorage' => '', 'QiniuStorage' => '?imageslim', 'UpyunStorage' => '!/format/webp', 'TxcosStorage' => '?imageMogr2/format/webp', 'AliossStorage' => '?x-oss-process=image/format,webp', ]; $extens = strtolower(pathinfo($this->delSuffix($filename), PATHINFO_EXTENSION)); $suffix = in_array($extens, ['png', 'jpg', 'jpeg']) ? ($compress[$class] ?? '') : ''; } if (is_string($attname) && strlen($attname) > 0 && stripos($this->link, 'full') !== false) { if ($class === 'UpyunStorage') { $suffix .= ($suffix ? '&' : '?') . '_upd=' . urlencode($attname); } else { $suffix .= ($suffix ? '&' : '?') . 'attname=' . urlencode($attname); } } return $suffix; } /** * 获取文件基础名称. * @param string $name 文件名称 */ protected function delSuffix(string $name): string { if (strpos($name, '?') !== false) { return strstr($name, '?', true); } if (stripos($name, '!') !== false) { return strstr($name, '!', true); } return $name; } }