Explorar o código

文件存在判断

shish %!s(int64=5) %!d(string=hai) anos
pai
achega
5bffb4ced2
Modificáronse 2 ficheiros con 22 adicións e 2 borrados
  1. 13 0
      common/components/oss.php
  2. 9 2
      common/services/ImageService.php

+ 13 - 0
common/components/oss.php

@@ -39,4 +39,17 @@ class oss
         }
     }
 
+    //判断文件是否存在 shish 2021.4.18
+    public static function fileExist($object)
+    {
+        $ossClient = self::getOssClient();
+        $bucket = Yii::$app->params['ossBucket'];
+        $ossClient->doesObjectExist($bucket, $object);
+        try {
+            $ossClient->doesObjectExist($bucket, $object);
+        } catch (OssException $e) {
+            util::fail($e->getMessage());
+        }
+    }
+
 }

+ 9 - 2
common/services/ImageService.php

@@ -28,6 +28,13 @@ class ImageService
         $name = $middleName . '.jpg';
         $fullFileName = $path . $middle . $name;
         $fileName = $middle . $name;
+
+        $respond = oss::fileExist($fileName);
+        if ($respond) {
+            Yii::info('url' . $url . ' 已经创建过了');
+            return $fileName;
+        }
+
         $curl = new curl\Curl();
         $result = $curl->get($url);
 
@@ -37,7 +44,7 @@ class ImageService
 
         Yii::info('url:' . $url);
         //上传oss
-        oss::uploadImage($fileName,$fullFileName);
+        oss::uploadImage($fileName, $fullFileName);
 
         $imgInfo = getimagesize($fullFileName);
         if (empty($imgInfo) || isset($imgInfo[0]) == false) {
@@ -56,7 +63,7 @@ class ImageService
         $smallFullPathFile = $path . $middle . $middleName . '_small.jpg';
         Image::thumbnail($fullFileName, $smallWidth, $smallHeight)->save($smallFullPathFile, ['quality' => 100]);
 
-        oss::uploadImage($middle . $middleName . '_small.jpg',$smallFullPathFile);
+        oss::uploadImage($middle . $middleName . '_small.jpg', $smallFullPathFile);
 
         return $fileName;
     }