Sfoglia il codice sorgente

梳理采集图片逻辑

magicblack 3 anni fa
parent
commit
a29adf3a2a

+ 0 - 1
application/admin/controller/Urlsend.php

@@ -195,7 +195,6 @@ class Urlsend extends Base
             $this->_param = $pp;
         }
         $ac = $this->_param['ac'];
-
         $cp = 'app\\common\\extend\\urlsend\\' . ucfirst($ac);
         if (class_exists($cp)) {
             $data = $this->data();

+ 18 - 0
application/common.php

@@ -11,6 +11,24 @@ use think\View;
 
 error_reporting(E_ERROR | E_PARSE );
 
+
+
+if (!function_exists('str_starts_with')) {
+    function str_starts_with($haystack, $needle) {
+        return (string)$needle !== '' && strncmp($haystack, $needle, strlen($needle)) === 0;
+    }
+}
+if (!function_exists('str_ends_with')) {
+    function str_ends_with($haystack, $needle) {
+        return $needle !== '' && substr($haystack, -strlen($needle)) === (string)$needle;
+    }
+}
+if (!function_exists('str_contains')) {
+    function str_contains($haystack, $needle) {
+        return $needle !== '' && mb_strpos($haystack, $needle) !== false;
+    }
+}
+
 //访问日志记录,根目录创建log目录
 function slog($logs)
 {

+ 14 - 13
application/common/model/Collect.php

@@ -390,25 +390,26 @@ class Collect extends Base {
      * @param string $flag
      * @return array
      */
-    private function syncImages($pic_status,$pic_url,$flag='vod')
+    private function syncImages($pic_status, $pic_url, $flag = 'vod')
     {
-        $config = (array)config('maccms.upload');
-        if($pic_status == 1){
-            $img_url = model('Image')->down_load($pic_url, $config, $flag);
-            if(substr($img_url,0,7)=='upload/'){
-                $link = MAC_PATH . $img_url;
-            }
-            else{
-                $link = str_replace('mac:', $config['protocol'].':', $img_url);
-            }
-            if ($img_url == $pic_url) {
+        $img_url_downloaded = $pic_url;
+        if ($pic_status == 1) {
+            $config = (array)config('maccms.upload');
+            $img_url_downloaded = model('Image')->down_load($pic_url, $config, $flag);
+            if ($img_url_downloaded == $pic_url) {
+                // 下载失败,显示老图信息
                 $des = '<a href="' . $pic_url . '" target="_blank">' . $pic_url . '</a><font color=red>'.lang('download_err').'!</font>';
             } else {
-                $pic_url = $img_url;
+                // 下载成功,显示新图信息
+                if (str_starts_with($img_url_downloaded, 'upload/')) {
+                    $link = MAC_PATH . $img_url_downloaded;
+                } else {
+                    $link = str_replace('mac:', $config['protocol'] . ':', $img_url_downloaded);
+                }
                 $des = '<a href="' . $link . '" target="_blank">' . $link . '</a><font color=green>'.lang('download_ok').'!</font>';
             }
         }
-        return ['pic'=>$pic_url,'msg'=>$des];
+        return ['pic' => $img_url_downloaded, 'msg' => $des];
     }
 
     public function vod_data($param,$data,$show=1)

+ 59 - 62
application/common/model/Image.php

@@ -4,45 +4,42 @@ use think\image\Exception;
 
 class Image extends Base {
 
-    public function down_load($url,$config,$flag='vod')
+    public function down_load($url, $config, $flag = 'vod')
     {
-        if(substr($url,0,4)=='http'){
-            return $this->down_exec($url,$config,$flag);
-        }
-        else{
+        if (substr($url, 0, 4) == 'http') {
+            return $this->down_exec($url, $config, $flag);
+        } else {
             return $url;
         }
     }
 
-    public function down_exec($url,$config,$flag='vod')
+    public function down_exec($url, $config, $flag = 'vod')
     {
         $upload_image_ext = 'jpg,jpeg,png,gif,webp';
         $ext = strtolower(pathinfo($url, PATHINFO_EXTENSION));
-        if(!in_array($ext, explode(',', $upload_image_ext))){
+        if (!in_array($ext, explode(',', $upload_image_ext))) {
             $ext = 'jpg';
         }
         $img = mac_curl_get($url);
-        if($img){
-            $file_name = md5(uniqid()) .'.' . $ext;
-            // 上传附件路径
-            $_upload_path = ROOT_PATH . 'upload' . '/' . $flag . '/';
-            // 附件访问路径
-            $_save_path = 'upload'. '/' . $flag . '/' ;
-            $ymd = date('Ymd');
-            $n_dir = $ymd;
-            for($i=1;$i<=100;$i++){
-                $n_dir = $ymd .'-'.$i;
-                $path1 = $_upload_path . $n_dir. '/';
-                if(file_exists($path1)){
-                    $farr = glob($path1.'*.*');
-                    if($farr){
-                        $fcount = count($farr);
-                        if($fcount>999){
-                            continue;
-                        }
-                        else{
-                            break;
-                        }
+        if (!$img) {
+            return $url;
+        }
+        $file_name = md5(uniqid()) .'.' . $ext;
+        // 上传附件路径
+        $_upload_path = ROOT_PATH . 'upload' . '/' . $flag . '/';
+        // 附件访问路径
+        $_save_path = 'upload'. '/' . $flag . '/' ;
+        $ymd = date('Ymd');
+        $n_dir = $ymd;
+        for($i=1;$i<=100;$i++){
+            $n_dir = $ymd .'-'.$i;
+            $path1 = $_upload_path . $n_dir. '/';
+            if(file_exists($path1)){
+                $farr = glob($path1.'*.*');
+                if($farr){
+                    $fcount = count($farr);
+                    if($fcount>999){
+                        continue;
                     }
                     else{
                         break;
@@ -52,45 +49,45 @@ class Image extends Base {
                     break;
                 }
             }
+            else{
+                break;
+            }
+        }
 
-            $_upload_path .= $n_dir . '/';
-            $_save_path .= $n_dir . '/';
+        $_upload_path .= $n_dir . '/';
+        $_save_path .= $n_dir . '/';
 
-            //附件访问地址
-            $_file_path = $_save_path.$file_name;
-            //写入文件
-            $r = mac_write_file($_upload_path.$file_name,$img);
-            if(!$r){
-                return $url;
-            }
-            $file_size = filesize($_upload_path.$file_name);
-            // 水印
-            if ($config['watermark'] == 1) {
-                $this->watermark($_file_path,$config,$flag);
-            }
-            // 缩略图
-            if ($config['thumb'] == 1) {
-                $this->makethumb($_file_path,$config,$flag);
-            }
-            //上传到远程
-            $_file_path = model('Upload')->api($_file_path,$config);
+        //附件访问地址
+        $_file_path = $_save_path.$file_name;
+        //写入文件
+        $r = mac_write_file($_upload_path.$file_name,$img);
+        if(!$r){
+            return $url;
+        }
+        $file_size = filesize($_upload_path.$file_name);
+        // 水印
+        if ($config['watermark'] == 1) {
+            $this->watermark($_file_path,$config,$flag);
+        }
+        // 缩略图
+        if ($config['thumb'] == 1) {
+            $this->makethumb($_file_path,$config,$flag);
+        }
+        //上传到远程
+        $_file_path = model('Upload')->api($_file_path, $config);
 
-            $tmp = $_file_path;
-            if((substr($tmp,0,7) == "/upload")){
-                $tmp = substr($tmp,1);
-            }
-            if((substr($tmp,0,6) == "upload")){
-                $annex = [];
-                $annex['annex_file'] = $tmp;
-                $annex['annex_type'] = 'image';
-                $annex['annex_size'] = $file_size;
-                model('Annex')->saveData($annex);
-            }
-            return $_file_path;
+        $tmp = $_file_path;
+        if (str_starts_with($tmp, '/upload')) {
+            $tmp = substr($tmp,1);
         }
-        else{
-            return $url;
+        if (str_starts_with($tmp, 'upload')) {
+            $annex = [];
+            $annex['annex_file'] = $tmp;
+            $annex['annex_type'] = 'image';
+            $annex['annex_size'] = $file_size;
+            model('Annex')->saveData($annex);
         }
+        return $_file_path;
     }
 
     public function watermark($file_path,$config,$flag='vod')