-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcos-commands.php
123 lines (109 loc) · 2.92 KB
/
cos-commands.php
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
if (!class_exists('WP_CLI')) {
return;
}
class COS_CLI_Commands
{
/**
* 同步文件夹到 COS
*
* ## OPTIONS
*
* <path>
* : 要同步的文件夹
*
* ## EXAMPLES
*
* wp cos upload wp-content/uploads
*
* @when after_wp_load
*/
public function upload($args, $assoc_args)
{
[$path] = $args;
$dir = ABSPATH . $path;
if (!is_dir($dir)) {
WP_CLI::error("Directory not found: [{$dir}]");
}
WP_CLI::line("Uploading files from [{$dir}] to COS...");
$files = cos_read_dir_queue(ABSPATH, $path);
if (empty($files)) {
WP_CLI::success('No files to upload.');
return;
}
foreach ($files as $file) {
$status = cos_file_upload($file['key'], $file['filepath']);
if ($status) {
WP_CLI::line("Uploaded: {$file['key']}");
} else {
WP_CLI::line("Failed: {$file['key']}");
}
}
$total = count($files);
WP_CLI::success("Uploaded {$total} files.");
}
/**
* 同步文件到 COS
*
* ## OPTIONS
*
* <path>
* : 要同步的文件
*
* [--delete]
* : 如果设置,上传后会删除本地文件
* [--key=<key>]
* : 指定上传到 COS 的 key,默认和文件路径一致
*
* ## EXAMPLES
*
* wp cos upload-file wp-content/uploads/2021/01/1.jpg
* wp cos upload-file wp-content/uploads/2021/01/1.jpg --delete
* wp cos upload-file wp-content/uploads/2021/01/1.jpg --key=2021/01/1.jpg
*
* @when after_wp_load
* @subcommand upload-file
*/
public function upload_file($args, $assoc_args)
{
[$path] = $args;
$file = ABSPATH . $path;
if (!is_file($file)) {
WP_CLI::error("File not found: {$file}");
}
$delete = false;
if (isset($assoc_args['delete'])) {
$delete = true;
}
$key = isset($assoc_args['key']) ? $assoc_args['key'] : $path;
WP_CLI::line("Uploading file [{$file}] to COS with key [$key]...");
$status = cos_file_upload("/{$key}", $file, $delete);
if ($status) {
WP_CLI::success("Uploaded: {$path}");
} else {
WP_CLI::error("Failed: {$path}");
}
}
/**
* 删除 COS 中的文件
*
* ## OPTIONS
*
* <key>
* : 需要删除 COS 中的文件 key
*
* ## EXAMPLES
*
* wp cos delete-file 2021/01/1.jpg
*
* @when after_wp_load
* @subcommand delete-file
*/
public function delete_file($args, $assoc_args)
{
[$key] = $args;
WP_CLI::line("Deleting file [{$key}] from COS...");
cos_delete_cos_file($key);
}
}
WP_CLI::add_command('cos', 'COS_CLI_Commands', ['shortdesc' => 'Commands used to operate COS.']);