Commit 128cd85a authored by 王源's avatar 王源
Browse files

对下载图片进行oss原路径提取,以防重名覆盖bug

parent 7ba4198d
Loading
Loading
Loading
Loading
+31 −9
Original line number Diff line number Diff line
@@ -25,6 +25,8 @@ class Drawer

    private $savePath = '';

    private $ossDomain = 'http://huaperfect-file-service.oss-cn-hangzhou.aliyuncs.com';

    public function __construct()
    {
        $this->config = container(ConfigInterface::class);
@@ -49,16 +51,16 @@ class Drawer
    {
        $url = $this->parseUrl($url);
        // excel画图中下载图片时对图片名做urlencode处理,防止中文名不能正常画图片的bug
        $filename = trim(pathinfo($url, PATHINFO_FILENAME));
        $ext = strtolower(pathinfo($url, PATHINFO_EXTENSION));
        $pathinfo = pathinfo($url);
        $filename = trim($pathinfo['filename']);
        $ext = strtolower($pathinfo['extension']);
        $filename = "$filename.$ext";

        $path = $this->rootPath . '/download/images/' . ($path ?: $this->savePath);
        if (!is_dir($path)) {
        $savePath = $this->parseSavePath($pathinfo['dirname'], $path);
        if (!is_dir($savePath)) {
            // 判断路径是否存在,不存在,则创建
            mkdir($path, 0777, true);
            mkdir($savePath, 0777, true);
        }
        $filePath = $path . '/' . $filename;
        $filePath = $savePath . '/' . $filename;
        if (!file_exists($filePath)) {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 信任任何证书
@@ -87,6 +89,26 @@ class Drawer
        return $this->handleOssUrl($url);
    }

    /**
     * 处理保存路径
     * @param $dirname
     * @param $path
     * @return string
     * @author Zero
     */
    private function parseSavePath($dirname, $path)
    {
        $path = trim($path, '/');
        $path = $this->rootPath . '/download/images/' . ($path ?: $this->savePath) . '/';
        if (strstr($dirname, $this->ossDomain) !== false) {
            // 以oss的原路径为保存路径
            $dirname = str_replace($this->ossDomain, '', $dirname);
            $dirname = trim($dirname, '/');
            $path = $path . $dirname . '/';
        }
        return $path;
    }

    // 替换阿里云图片内部地址
    private function handleOssUrl($url)
    {
@@ -362,7 +384,7 @@ class Drawer

    public function setSavePath($path)
    {
        $this->savePath = $path;
        $this->savePath = trim($path, '/');
    }

    /**