进行了一些小修复

This commit is contained in:
2026-08-26 17:57:29 +08:00
parent 1f84e6aa18
commit fb2bbb64c5
8 changed files with 630 additions and 54 deletions
+19 -6
View File
@@ -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);
}