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