进行了一些小修复
This commit is contained in:
@@ -65,6 +65,7 @@ class SchemaPermissions {
|
||||
]],
|
||||
['key' => 'content', 'title' => '内容上传限制', 'fields' => [
|
||||
['key' => 'submit_image_max_size', 'label' => '投稿图片大小上限 (MB)', 'type' => 'number', 'default' => 5, 'min' => 1, 'max' => 20],
|
||||
['key' => 'submit_video_max_size', 'label' => '投稿视频大小上限 (MB)', 'type' => 'number', 'default' => 30, 'min' => 1, 'max' => 200],
|
||||
['key' => 'comment_image_max_size', 'label' => '评论图片大小上限 (MB)', 'type' => 'number', 'default' => 2, 'min' => 1, 'max' => 10],
|
||||
]],
|
||||
['key' => 'resource', 'title' => '资源附件限制', 'fields' => [
|
||||
|
||||
@@ -45,14 +45,14 @@ class FrontSubmit {
|
||||
}
|
||||
|
||||
public function register_rest_routes(): void {
|
||||
register_rest_route('aksrt/v1', '/front-submit/upload/(?P<kind>image|resource)', [
|
||||
register_rest_route('aksrt/v1', '/front-submit/upload/(?P<kind>image|video|resource)', [
|
||||
'methods' => 'POST',
|
||||
'callback' => [$this, 'rest_upload_attachment'],
|
||||
'permission_callback' => [$this, 'can_upload_attachment'],
|
||||
'args' => [
|
||||
'kind' => [
|
||||
'validate_callback' => static function ($value): bool {
|
||||
return in_array((string) $value, ['image', 'resource'], true);
|
||||
return in_array((string) $value, ['image', 'video', 'resource'], true);
|
||||
},
|
||||
],
|
||||
],
|
||||
@@ -69,7 +69,15 @@ class FrontSubmit {
|
||||
}
|
||||
|
||||
$kind = (string) $request->get_param('kind');
|
||||
$cap = $kind === 'resource' ? 'upload_resource_attachment' : 'upload_submit_image';
|
||||
$caps = [
|
||||
'image' => 'upload_submit_image',
|
||||
'video' => 'new_post_upload_video',
|
||||
'resource' => 'upload_resource_attachment',
|
||||
];
|
||||
$cap = $caps[$kind] ?? '';
|
||||
if ($cap === '') {
|
||||
return false;
|
||||
}
|
||||
return $this->user_can_cap($cap, $user_id);
|
||||
}
|
||||
|
||||
@@ -84,9 +92,11 @@ class FrontSubmit {
|
||||
}
|
||||
|
||||
$settings = class_exists('\AKSRT\Settings\Settings') ? \AKSRT\Settings\Settings::get_all() : [];
|
||||
$max_mb = $kind === 'resource'
|
||||
? max(1, min(200, (int) ($settings['resource_max_size'] ?? 50)))
|
||||
: max(1, min(20, (int) ($settings['submit_image_max_size'] ?? 5)));
|
||||
$max_mb = match ($kind) {
|
||||
'resource' => max(1, min(200, (int) ($settings['resource_max_size'] ?? 50))),
|
||||
'video' => max(1, min(200, (int) ($settings['submit_video_max_size'] ?? 30))),
|
||||
default => max(1, min(20, (int) ($settings['submit_image_max_size'] ?? 5))),
|
||||
};
|
||||
$max_size = $max_mb * MB_IN_BYTES;
|
||||
$size = (int) ($file['size'] ?? 0);
|
||||
|
||||
@@ -119,6 +129,8 @@ class FrontSubmit {
|
||||
return new \WP_Error('aksrt_upload_image_rejected', __('图片未通过内容审核。', 'aksrt'), ['status' => 422]);
|
||||
}
|
||||
}
|
||||
} elseif ($kind === 'video' && strpos($mime, 'video/') !== 0) {
|
||||
return new \WP_Error('aksrt_upload_invalid_video', __('请上传有效的视频文件。', 'aksrt'), ['status' => 400]);
|
||||
}
|
||||
|
||||
if (!function_exists('media_handle_upload')) {
|
||||
@@ -150,6 +162,7 @@ class FrontSubmit {
|
||||
'attachment_id' => $attachment_id,
|
||||
'url' => $url,
|
||||
'name' => get_the_title($attachment_id),
|
||||
'filename' => wp_basename((string) get_attached_file($attachment_id)),
|
||||
'mime' => $mime,
|
||||
], 201);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user