Support wp-cli commands to upload files (#38)

pull/39/head v1.4.13
Luffy 2024-06-07 08:12:25 +08:00 committed by GitHub
parent b192c22585
commit 53934e9d4b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 134 additions and 26 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 97 KiB

View File

@ -24,6 +24,7 @@
- [x] 支持上传文件自动重命名
- [x] 支持使用 RAM 操作
- [x] 支持原图保护
- [x] 支持 `wp-cli` 命令上传文件
## 安装

View File

@ -3,7 +3,7 @@
Plugin Name: OSS Aliyun
Plugin URI: https://github.com/sy-records/aliyun-oss-wordpress
Description: 使用阿里云对象存储 OSS 作为附件存储空间。This is a plugin that uses Aliyun Object Storage Service for attachments remote saving.
Version: 1.4.12
Version: 1.4.13
Author: 沈唁
Author URI: https://qq52o.me
License: Apache2.0
@ -19,11 +19,15 @@ use OSS\Credentials\CredentialsProvider;
use AlibabaCloud\Credentials\Credential;
use OSS\Credentials\StaticCredentialsProvider;
define('OSS_VERSION', '1.4.12');
define('OSS_VERSION', '1.4.13');
define('OSS_BASEFOLDER', plugin_basename(dirname(__FILE__)));
if (!function_exists('get_home_path')) {
require_once(ABSPATH . 'wp-admin/includes/file.php');
require_once ABSPATH . 'wp-admin/includes/file.php';
}
if (defined('WP_CLI') && WP_CLI) {
require_once plugin_dir_path(__FILE__) . 'oss-commands.php';
}
class OSSCredentialsWrapper implements CredentialsProvider
@ -121,9 +125,7 @@ function oss_get_file_meta($object)
$bucket = oss_get_bucket_name();
return $ossClient->getObjectMeta($bucket, $object);
} catch (\Throwable $e) {
if (WP_DEBUG) {
echo 'Error Message: ', $e->getMessage(), PHP_EOL, 'Error Code: ', $e->getCode();
}
error_log($e->getMessage());
return ['content-length' => 0];
}
}
@ -132,6 +134,7 @@ function oss_get_file_meta($object)
* @param string $object
* @param string $file
* @param bool $no_local_file
* @return bool
*/
function oss_file_upload($object, $file, $no_local_file = false)
{
@ -143,14 +146,15 @@ function oss_file_upload($object, $file, $no_local_file = false)
$ossClient = oss_get_client();
try {
$ossClient->uploadFile($bucket, ltrim($object, '/'), $file);
} catch (\Throwable $e) {
if (WP_DEBUG) {
echo 'Error Message: ', $e->getMessage(), PHP_EOL, 'Error Code: ', $e->getCode();
}
}
if ($no_local_file) {
oss_delete_local_file($file);
}
return true;
} catch (\Throwable $e) {
error_log($e->getMessage());
return false;
}
}
/**
@ -167,7 +171,7 @@ function oss_is_delete_local_file()
/**
* 删除本地文件
*
* @param $file
* @param string $file
* @return bool
*/
function oss_delete_local_file($file)
@ -201,9 +205,7 @@ function oss_delete_oss_file($file)
$ossClient = oss_get_client();
$ossClient->deleteObject($bucket, $file);
} catch (\Throwable $e) {
if (WP_DEBUG) {
echo 'Error Message: ', $e->getMessage(), PHP_EOL, 'Error Code: ', $e->getCode();
}
error_log($e->getMessage());
}
}
@ -223,9 +225,7 @@ function oss_delete_oss_files(array $files)
$ossClient = oss_get_client();
$ossClient->deleteObjects($bucket, $deleteObjects);
} catch (\Throwable $e) {
if (WP_DEBUG) {
echo 'Error Message: ', $e->getMessage(), PHP_EOL, 'Error Code: ', $e->getCode();
}
error_log($e->getMessage());
}
}
@ -457,7 +457,7 @@ function oss_read_dir_queue($homePath, $uploadPath)
$foundFiles = [];
while (!$dirsToProcess->isEmpty()) {
list($currentDir, $relativeDir) = $dirsToProcess->dequeue();
[$currentDir, $relativeDir] = $dirsToProcess->dequeue();
foreach (new DirectoryIterator($currentDir) as $fileInfo) {
if ($fileInfo->isDot()) continue;

101
oss-commands.php Normal file
View File

@ -0,0 +1,101 @@
<?php
if (!class_exists('WP_CLI')) {
return;
}
class OSS_CLI_Commands
{
/**
* 同步文件夹到 OSS
*
* ## OPTIONS
*
* <path>
* : 要同步的文件夹
*
* ## EXAMPLES
*
* wp oss 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 OSS...");
$files = oss_read_dir_queue(ABSPATH, $path);
if (empty($files)) {
WP_CLI::success('No files to upload.');
return;
}
foreach ($files as $file) {
$status = oss_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.");
}
/**
* 同步文件到 OSS
*
* ## OPTIONS
*
* <path>
* : 要同步的文件
*
* [--delete]
* : 如果设置,上传后会删除本地文件
* [--key=<key>]
* : 指定上传到 OSS key默认和文件路径一致
*
* ## EXAMPLES
*
* wp oss upload-file wp-content/uploads/2021/01/1.jpg
* wp oss upload-file wp-content/uploads/2021/01/1.jpg --delete
* wp oss 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 OSS with key [$key]...");
$status = oss_file_upload("/{$key}", $file, $delete);
if ($status) {
WP_CLI::success("Uploaded: {$path}");
} else {
WP_CLI::error("Failed: {$path}");
}
}
}
WP_CLI::add_command('oss', 'OSS_CLI_Commands', ['shortdesc' => 'Commands used to operate OSS.']);

View File

@ -4,8 +4,8 @@ Donate link: https://qq52o.me/sponsor.html
Tags: oss, 阿里云, 对象存储, aliyun
Requires at least: 4.6
Tested up to: 6.5
Requires PHP: 7.0
Stable tag: 1.4.12
Requires PHP: 7.1
Stable tag: 1.4.13
License: Apache2.0
License URI: http://www.apache.org/licenses/LICENSE-2.0.html
@ -29,7 +29,8 @@ License URI: http://www.apache.org/licenses/LICENSE-2.0.html
8. 支持上传文件自动重命名
9. 支持使用 RAM 操作
10. 支持原图保护
11. 插件更多详细介绍和安装:[https://github.com/sy-records/aliyun-oss-wordpress](https://github.com/sy-records/aliyun-oss-wordpress)
11. 支持 `wp-cli` 命令上传文件
12. 插件更多详细介绍和安装:[https://github.com/sy-records/aliyun-oss-wordpress](https://github.com/sy-records/aliyun-oss-wordpress)
## 其他插件
@ -54,6 +55,7 @@ License URI: http://www.apache.org/licenses/LICENSE-2.0.html
1. 设置页面
2. 数据库同步
3. 内置的 wp-cli 命令
== Frequently Asked Questions ==
@ -74,6 +76,10 @@ License URI: http://www.apache.org/licenses/LICENSE-2.0.html
== Changelog ==
= 1.4.13 =
- 支持 `wp-cli` 命令上传文件
= 1.4.12 =
- 支持原图保护

View File

@ -1,7 +1,7 @@
<?php
if (!defined('WP_UNINSTALL_PLUGIN')) {
exit();
exit;
}
$options = get_option('oss_options', true);