作者 刘锟

update

@@ -102,7 +102,7 @@ class HtmlCollect extends Command @@ -102,7 +102,7 @@ class HtmlCollect extends Command
102 $source_list = $this->html_preg($new_html, $project_id, $collect_info->domain, $old_info['web_url_domain'], $old_info['home_url']); 102 $source_list = $this->html_preg($new_html, $project_id, $collect_info->domain, $old_info['web_url_domain'], $old_info['home_url']);
103 103
104 if ($source_list) { 104 if ($source_list) {
105 - $html = $this->upload_source($html, $source_list, $project_id); 105 + $html = $this->upload_source($html, $source_list, $project_id, $collect_info->domain, $old_info['web_url_domain'], $old_info['home_url']);
106 } 106 }
107 } catch (\Exception $e) { 107 } catch (\Exception $e) {
108 $collect_info->status = CollectTask::STATUS_FAIL; 108 $collect_info->status = CollectTask::STATUS_FAIL;
@@ -321,7 +321,7 @@ class HtmlCollect extends Command @@ -321,7 +321,7 @@ class HtmlCollect extends Command
321 } 321 }
322 322
323 //下载并替换资源 323 //下载并替换资源
324 - protected function upload_source($html, $source, $project_id) 324 + protected function upload_source($html, $source, $project_id, $domain, $web_url_domain, $home_url)
325 { 325 {
326 foreach ($source as $vs) { 326 foreach ($source as $vs) {
327 327
@@ -337,21 +337,17 @@ class HtmlCollect extends Command @@ -337,21 +337,17 @@ class HtmlCollect extends Command
337 ]); 337 ]);
338 $html = str_replace($vs['url'], getImageUrl($new_source), $html); 338 $html = str_replace($vs['url'], getImageUrl($new_source), $html);
339 339
340 - if (substr($new_source, -3, 3) == 'css' || substr($new_source, -2, 2) == 'js') {  
341 - $source_html = curl_c($vs['url_complete'], false);  
342 - if (substr($new_source, -3, 3) == 'css') {  
343 - preg_match_all("/url\(['\"](\s*[^>]+?)['\"]\)/i", $source_html, $result_source);  
344 - } else {  
345 - preg_match_all("/[large|thumb]+URL:['\"]+(\s*[^>]+?)['\"]+,/i", $source_html, $result_source);  
346 - }  
347 - $source_list = $result_source[1] ?? []; 340 + if (substr($new_source, -3, 3) == 'css') {
  341 + // 下载css文件中的资源
  342 + $css_html = curl_c($vs['url_complete'], false);
  343 + preg_match_all("/url\(['\"](\s*[^>]+?)['\"]\)/i", $css_html, $result_css_source);
  344 + $css_source = $result_css_source[1] ?? [];
348 345
349 $url_arr = explode('/', $vs['url_complete']); 346 $url_arr = explode('/', $vs['url_complete']);
350 $target_arr = explode('/', $new_source); 347 $target_arr = explode('/', $new_source);
351 - foreach ($source_list as $vcs) { 348 + foreach ($css_source as $vcs) {
352 $vcs = str_replace('"', '', $vcs); 349 $vcs = str_replace('"', '', $vcs);
353 $vcs_arr = parse_url($vcs); 350 $vcs_arr = parse_url($vcs);
354 -  
355 if (isset($vcs_arr['domain'])) { 351 if (isset($vcs_arr['domain'])) {
356 //不是相对路径,不下载 352 //不是相对路径,不下载
357 continue; 353 continue;
@@ -391,6 +387,34 @@ class HtmlCollect extends Command @@ -391,6 +387,34 @@ class HtmlCollect extends Command
391 ]); 387 ]);
392 } 388 }
393 } 389 }
  390 + } elseif (substr($new_source, -2, 2) == 'js') {
  391 + $js_html = curl_c(getImageUrl($new_source), false);
  392 + preg_match_all("/[large|thumb]+URL:['\"]+(\s*[^>]+?)['\"]+,/i", $js_html, $result_js_source);
  393 + $js_source = $result_js_source[1] ?? [];
  394 + foreach ($js_source as $vjs) {
  395 + $vjs_result = $this->url_check($vjs, $project_id, $domain, $web_url_domain, $home_url);
  396 + if (!$vjs_result) {
  397 + continue;
  398 + }
  399 +
  400 + if ($vjs_result['download']) {
  401 + $new_vjs = CosService::uploadRemote($project_id, 'source', $vjs_result['url_complete']);
  402 + if ($new_vjs) {
  403 + CollectSource::insert([
  404 + 'project_id' => $project_id,
  405 + 'origin' => $vjs_result['url'],
  406 + 'target' => $new_vjs,
  407 + 'created_at' => date('Y-m-d H:i:s'),
  408 + 'updated_at' => date('Y-m-d H:i:s'),
  409 + ]);
  410 + $js_html = str_replace($vjs, getImageUrl($new_vjs), $js_html);
  411 + }
  412 + } else {
  413 + $js_html = str_replace($vjs, getImageUrl($vjs_result['url_complete']), $js_html);
  414 + }
  415 + }
  416 +
  417 + CosService::uploadRemote($project_id, 'source', $new_source, $new_source, $js_html);
394 } 418 }
395 } 419 }
396 } else { 420 } else {
@@ -69,11 +69,12 @@ class CosService @@ -69,11 +69,12 @@ class CosService
69 * @param $image_type 69 * @param $image_type
70 * @param $file_url 70 * @param $file_url
71 * @param $key 71 * @param $key
  72 + * @param $body_str
72 * @return string 73 * @return string
73 * @author Akun 74 * @author Akun
74 * @date 2023/09/21 9:39 75 * @date 2023/09/21 9:39
75 */ 76 */
76 - public static function uploadRemote($project_id,$image_type,$file_url,$key='') 77 + public static function uploadRemote($project_id,$image_type,$file_url,$key='',$body_str='')
77 { 78 {
78 if(!$key){ 79 if(!$key){
79 $url_arr = parse_url($file_url); 80 $url_arr = parse_url($file_url);
@@ -96,7 +97,7 @@ class CosService @@ -96,7 +97,7 @@ class CosService
96 ]); 97 ]);
97 98
98 try { 99 try {
99 - $body = curl_c($file_url,false); 100 + $body = $body_str ?:curl_c($file_url,false);
100 }catch (\Exception $e){ 101 }catch (\Exception $e){
101 return ''; 102 return '';
102 } 103 }