作者 lyh

gx

  1 +<?php
  2 +/**
  3 + * @remark :
  4 + * @name :SyncTimeFiles.php
  5 + * @author :lyh
  6 + * @method :post
  7 + * @time :2024/8/14 14:23
  8 + */
  9 +
  10 +namespace App\Console\Commands\Sync;
  11 +
  12 +use App\Models\File\ErrorFile;
  13 +use App\Models\File\File;
  14 +use Illuminate\Console\Command;
  15 +
  16 +class SyncTimeMinuteFile extends Command
  17 +{
  18 + /**
  19 + * The name and signature of the console command.
  20 + *
  21 + * @var string
  22 + */
  23 + protected $signature = 'sync_minute_file';
  24 +
  25 + /**
  26 + * The console command description.
  27 + *
  28 + * @var string
  29 + */
  30 + protected $description = '按时间同步图片与文件(每分钟执行前面10分钟的数据)';
  31 +
  32 + public function handle()
  33 + {
  34 + $dir = '/www/wwwroot/cos';
  35 + // 获取当前时间的 Unix 时间戳
  36 + $currentTime = time();
  37 + // 获取当前时间前10分钟的 Unix 时间戳
  38 + $time10MinutesAgo = $currentTime - 10 * 60;
  39 + // 将 Unix 时间戳格式化为标准的日期时间格式
  40 + $start = date('Y-m-d H:i:s', $time10MinutesAgo);
  41 + $end = date('Y-m-d H:i:s');
  42 + $fileModel = new File();
  43 + $lists = $fileModel->list(['created_at'=>['between',[$start,$end]]]);
  44 + foreach ($lists as $v){
  45 + $path = $v['path'];
  46 + if (file_exists($dir.$path)) {
  47 + echo date('Y-m-d H:i:s') . ' | file_ok:' . $dir.$path . PHP_EOL;
  48 + continue;
  49 + }
  50 + $this->param['name'] = basename($path);
  51 + $this->param['path'] = str_replace('/'.$this->param['name'],'',$path);
  52 + $file_path = $this->getUrl($this->param['path'].'/'.$this->param['name'], 0,0);
  53 + $cmd = 'curl -F "file_path='.$file_path.'" -F "save_path=/www/wwwroot/cos'.$this->param['path'].'" https://v6-file.globalso.com/upload.php';
  54 + echo date('Y-m-d H:i:s') . ' | ' . $cmd . PHP_EOL;
  55 + $code = shell_exec($cmd);
  56 + if(200 != (int)$code){
  57 + echo date('Y-m-d H:i:s') . ' | 错误状态:' . $code . PHP_EOL;
  58 + $errorFileModel = new ErrorFile();
  59 + $errorFileModel->add(['path'=>$this->param['path'].'/'.$this->param['name']]);
  60 + }
  61 + echo date('Y-m-d H:i:s') . ' | ok:' . $code . PHP_EOL;
  62 + }
  63 + return true;
  64 + }
  65 +
  66 + /**
  67 + * @remark :获取图片文件链接
  68 + * @name :getUrl
  69 + * @author :lyh
  70 + * @method :post
  71 + * @time :2024/5/22 11:53
  72 + */
  73 + public function getUrl($path,$storage_type,$location){
  74 + if(is_array($path)){
  75 + $url =[];
  76 + foreach ($path as $v){
  77 + $url[] = $this->getUrl($v,$storage_type,$location);
  78 + }
  79 + }else{
  80 + if(empty($path)){
  81 + return '';
  82 + }
  83 + if((strpos($path,'https://')!== false) || (strpos($path,'http://') !== false)){
  84 + return $path;
  85 + }
  86 + if(substr($path,0,2) == '//'){
  87 + return 'https:'.$path;
  88 + }
  89 + if($location == 0){
  90 + $cos = config('filesystems.disks.cos');
  91 + $cosCdn = ($storage_type == 0) ? $cos['cdn'] : $cos['cdn1'];
  92 + $url = $cosCdn.$path;
  93 + }else{
  94 + $s3 = config('filesystems.disks.s3');
  95 + $cdn = $s3['cdn'];
  96 + $url = $cdn.$path;
  97 + }
  98 + }
  99 + return $url;
  100 + }
  101 +}