Worktree/attachment upload config #35

Merged
kate522 merged 2 commits from worktree/attachment-upload-config into main 2026-09-24 03:20:55 +00:00
5 changed files with 28 additions and 39 deletions
@@ -24,7 +24,7 @@ const uploadMimeUserTypeOptions = [
const uploadSizeFields = [
{ key: 'type', label: '用户类型', type: 'select', default: 'logged', options: uploadUserTypeOptions },
{ key: 'val', label: '可上传', type: 'number', default: 30, min: 1, max: 2048 },
{ key: 'val', label: '可上传大小', type: 'number', default: 30, min: 1, max: 2048, unit: 'M' },
]
export const permissionsSchemas = {
@@ -218,9 +218,8 @@ export const permissionsSchemas = {
key: 'permissions.attachment',
title: '附件配置',
icon: 'fa fas fa-paperclip',
description: '统一管理头像、上传大小、上传格式与上传行为;准入权限请在“基本权限”中配置。',
description: '统一管理上传大小、上传格式与上传行为;准入权限请在“基本权限”中配置。',
defaults: {
avatar_max_size: 2,
upload_img_size: [
{ type: 'default', val: 5 },
{ type: 'admin', val: 20 },
@@ -243,14 +242,6 @@ export const permissionsSchemas = {
close_attachment_page: true,
},
groups: [
{
key: 'profile',
title: '用户头像与资料卡图片',
icon: 'fa fas fa-id-card',
fields: [
{ key: 'avatar_max_size', label: '头像/用户资料卡背景图大小上限 (MB)', type: 'number', min: 1, max: 10 },
],
},
{
key: 'size-limits',
title: '上传大小限制',
@@ -263,7 +254,7 @@ export const permissionsSchemas = {
type: 'repeater',
min: 1,
addText: '添加细分用户权限',
help: '投稿、评论等前端场景允许上传的最大图片大小(单位 MB)。同一个用户满足多个条件则取最大值,「最小默认值」为所有人兜底。',
help: '投稿、评论、头像与资料卡背景图等前端场景允许上传的最大图片大小(单位 MB)。同一个用户满足多个条件则取最大值,「最小默认值」为所有人兜底。',
fields: uploadSizeFields.map((field) => ({ ...field, default: field.key === 'val' ? 5 : field.default })),
},
{ key: 'image_upload_multiple', label: '图片批量上传最大数量', type: 'number', min: 0, max: 100, unit: '张', help: '前端投稿、评论上传图片允许的单次批量上传数量(为 0 则不限制,为 1 则不能批量上传)。' },
+6 -6
View File
@@ -346,11 +346,11 @@ add_filter('user_profile_update_errors', static function (WP_Error $errors, bool
}
$file = $_FILES['aksrt_avatar_file'];
$settings = class_exists('\\AKSRT\\Settings\\Settings') ? \AKSRT\Settings\Settings::get_all() : [];
$max_size_mb = max(1, min(10, (int) ($settings['avatar_max_size'] ?? 2)));
// 头像上限并入「前端图片上传限制」统一治理(参考主题同源)。
$max_size = class_exists('\AKSRT\Attachment\UploadLimits') ? \AKSRT\Attachment\UploadLimits::limit_bytes('image') : (int) wp_max_upload_size();
$actual_size = !empty($file['tmp_name']) && is_readable($file['tmp_name']) ? (int) filesize($file['tmp_name']) : 0;
if ((int) ($file['error'] ?? UPLOAD_ERR_NO_FILE) !== UPLOAD_ERR_OK || $actual_size <= 0 || $actual_size > $max_size_mb * MB_IN_BYTES) {
$errors->add('aksrt_avatar_size', sprintf(__('头像文件无效或超过 %dMB', 'aksrt'), $max_size_mb));
if ((int) ($file['error'] ?? UPLOAD_ERR_NO_FILE) !== UPLOAD_ERR_OK || $actual_size <= 0 || $actual_size > $max_size) {
$errors->add('aksrt_avatar_size', sprintf(__('头像文件无效或超过 %s', 'aksrt'), size_format($max_size)));
return;
}
@@ -395,8 +395,8 @@ if (!function_exists('aksrt_save_admin_avatar_field')) {
return;
}
$settings = class_exists('\\AKSRT\\Settings\\Settings') ? \AKSRT\Settings\Settings::get_all() : [];
$max_size = max(1, min(10, (int) ($settings['avatar_max_size'] ?? 2))) * MB_IN_BYTES;
// 头像上限并入「前端图片上传限制」统一治理(参考主题同源)。
$max_size = class_exists('\AKSRT\Attachment\UploadLimits') ? \AKSRT\Attachment\UploadLimits::limit_bytes('image') : (int) wp_max_upload_size();
$file = $_FILES['aksrt_avatar_file'];
$actual_size = !empty($file['tmp_name']) && is_readable($file['tmp_name']) ? (int) filesize($file['tmp_name']) : 0;
if ((int) ($file['error'] ?? UPLOAD_ERR_NO_FILE) !== UPLOAD_ERR_OK || $actual_size <= 0 || $actual_size > $max_size) {
+6 -6
View File
@@ -383,8 +383,8 @@ if (!function_exists('aksrt_user_center_avatar_upload_config')) {
if (!$enabled) {
return [];
}
$settings = class_exists('\AKSRT\Settings\Settings') ? \AKSRT\Settings\Settings::get_all() : [];
$max_size = max(1, min(10, (int) ($settings['avatar_max_size'] ?? 2))) * MB_IN_BYTES;
// 头像上限并入「前端图片上传限制」统一治理(参考主题同源)。
$max_size = class_exists('\AKSRT\Attachment\UploadLimits') ? \AKSRT\Attachment\UploadLimits::limit_bytes('image') : (int) wp_max_upload_size();
return [
'enabled' => true,
'restUrl' => rest_url('aksrt/v1/user/avatar'),
@@ -396,7 +396,7 @@ if (!function_exists('aksrt_user_center_avatar_upload_config')) {
'maxSize' => $max_size,
'idleText' => __('更换头像', 'aksrt'),
'loadingText' => __('上传中…', 'aksrt'),
'tooLargeText' => sprintf(__('图片大小不能超过 %dMB', 'aksrt'), (int) ($max_size / MB_IN_BYTES)),
'tooLargeText' => sprintf(__('图片大小不能超过 %s', 'aksrt'), size_format($max_size)),
'successText' => __('头像已更新', 'aksrt'),
'errorText' => __('头像上传失败', 'aksrt'),
];
@@ -411,8 +411,8 @@ if (!function_exists('aksrt_user_center_cover_upload_config')) {
if (!$allow_bg_upload) {
return [];
}
$settings = class_exists('\\AKSRT\\Settings\\Settings') ? \AKSRT\Settings\Settings::get_all() : [];
$max_size = max(1, min(10, (int) ($settings['avatar_max_size'] ?? 2))) * MB_IN_BYTES;
// 背景图上限并入「前端图片上传限制」统一治理(参考主题同源)。
$max_size = class_exists('\AKSRT\Attachment\UploadLimits') ? \AKSRT\Attachment\UploadLimits::limit_bytes('image') : (int) wp_max_upload_size();
return [
'enabled' => true,
'restUrl' => rest_url('aksrt/v1/user/background'),
@@ -425,7 +425,7 @@ if (!function_exists('aksrt_user_center_cover_upload_config')) {
'fallbackImage' => function_exists('aksrt_get_user_cover_url') ? aksrt_get_user_cover_url(get_current_user_id()) : get_theme_file_uri('assets/img/user_t.jpg'),
'idleText' => __('修改背景图', 'aksrt'),
'loadingText' => __('上传中…', 'aksrt'),
'tooLargeText' => sprintf(__('图片大小不能超过 %dMB', 'aksrt'), (int) ($max_size / MB_IN_BYTES)),
'tooLargeText' => sprintf(__('图片大小不能超过 %s', 'aksrt'), size_format($max_size)),
'successText' => __('背景图已更新', 'aksrt'),
'errorText' => __('上传失败', 'aksrt'),
'networkErrorText' => __('网络错误,请重试', 'aksrt'),
+4 -7
View File
@@ -91,17 +91,14 @@ class SchemaPermissions {
'version' => '1.2.0',
'option_key' => 'aksrt_settings',
'groups' => [
['key' => 'profile', 'title' => '用户头像与资料卡图片', 'fields' => [
['key' => 'avatar_max_size', 'label' => '头像/用户资料卡背景图大小上限 (MB)', 'type' => 'number', 'default' => 2, 'min' => 1, 'max' => 10],
]],
['key' => 'size-limits', 'title' => '上传大小限制', 'fields' => [
['key' => 'size_limit_notice', 'label' => '服务器环境提示', 'type' => 'notice', 'alertType' => 'warning', 'alertContent' => '设置上传大小,请考虑服务器负荷、硬盘,以及服务器最大能支持的范围。实际上传大小取「主题限制」与「PHP 环境限制」的较小值,超过 PHP 环境限制时请调整 php.ini 的 upload_max_filesize 与 post_max_size。'],
['key' => 'upload_img_size', 'label' => '前端图片上传限制', 'type' => 'repeater', 'default' => [
['type' => 'default', 'val' => 5],
['type' => 'admin', 'val' => 20],
], 'min' => 1, 'button_title' => '添加细分用户权限', 'help' => '投稿、评论等前端场景允许上传的最大图片大小(单位 MB)。同一个用户满足多个条件则取最大值,「最小默认值」为所有人兜底。', 'fields' => [
], 'min' => 1, 'button_title' => '添加细分用户权限', 'help' => '投稿、评论、头像与资料卡背景图等前端场景允许上传的最大图片大小(单位 MB)。同一个用户满足多个条件则取最大值,「最小默认值」为所有人兜底。', 'fields' => [
['key' => 'type', 'label' => '用户类型', 'type' => 'select', 'default' => 'logged', 'options' => self::upload_user_type_options()],
['key' => 'val', 'label' => '可上传', 'type' => 'number', 'default' => 5, 'min' => 1, 'max' => 2048, 'unit' => 'M'],
['key' => 'val', 'label' => '可上传大小', 'type' => 'number', 'default' => 5, 'min' => 1, 'max' => 2048, 'unit' => 'M'],
]],
['key' => 'image_upload_multiple', 'label' => '图片批量上传最大数量', 'type' => 'number', 'default' => 6, 'min' => 0, 'max' => 100, 'unit' => '张', 'help' => '前端投稿、评论上传图片允许的单次批量上传数量(为 0 则不限制,为 1 则不能批量上传)。'],
['key' => 'upload_video_size', 'label' => '前端视频上传限制', 'type' => 'repeater', 'default' => [
@@ -109,14 +106,14 @@ class SchemaPermissions {
['type' => 'admin', 'val' => 2048],
], 'min' => 1, 'button_title' => '添加细分用户权限', 'help' => '前端允许上传的最大视频大小(单位 MB)。同一个用户满足多个条件则取最大值,必须包含最低默认值。', 'fields' => [
['key' => 'type', 'label' => '用户类型', 'type' => 'select', 'default' => 'logged', 'options' => self::upload_user_type_options()],
['key' => 'val', 'label' => '可上传', 'type' => 'number', 'default' => 30, 'min' => 1, 'max' => 2048, 'unit' => 'M'],
['key' => 'val', 'label' => '可上传大小', 'type' => 'number', 'default' => 30, 'min' => 1, 'max' => 2048, 'unit' => 'M'],
]],
['key' => 'upload_file_size', 'label' => '前端文件上传大小限制', 'type' => 'repeater', 'default' => [
['type' => 'default', 'val' => 30],
['type' => 'admin', 'val' => 2048],
], 'min' => 1, 'button_title' => '添加细分用户权限', 'help' => '前端允许上传的其他文件(不含视频和图片,建议大于视频图片)最大大小(单位 MB)。同一个用户满足多个条件则取最大值,必须包含最低默认值。', 'fields' => [
['key' => 'type', 'label' => '用户类型', 'type' => 'select', 'default' => 'logged', 'options' => self::upload_user_type_options()],
['key' => 'val', 'label' => '可上传', 'type' => 'number', 'default' => 30, 'min' => 1, 'max' => 2048, 'unit' => 'M'],
['key' => 'val', 'label' => '可上传大小', 'type' => 'number', 'default' => 30, 'min' => 1, 'max' => 2048, 'unit' => 'M'],
]],
]],
['key' => 'mime-limits', 'title' => '上传格式限制', 'fields' => [
@@ -84,12 +84,13 @@ trait UserControllerProfileMediaOperations {
return $this->error(__('头像文件上传失败,请重新选择', 'aksrt'), 400);
}
$settings = class_exists('\AKSRT\Settings\Settings') ? \AKSRT\Settings\Settings::get_all() : [];
$max_size_mb = max(1, min(10, (int) ($settings['avatar_max_size'] ?? 2)));
$max_size = $max_size_mb * MB_IN_BYTES;
// 头像与资料卡背景图并入「前端图片上传限制」统一治理(参考主题同源)。
$max_size = class_exists('\AKSRT\Attachment\UploadLimits')
? \AKSRT\Attachment\UploadLimits::limit_bytes('image', $user_id)
: (int) wp_max_upload_size();
$actual_size = !empty($file['tmp_name']) && is_readable($file['tmp_name']) ? (int) filesize($file['tmp_name']) : 0;
if ($actual_size <= 0 || $actual_size > $max_size) {
return $this->error(sprintf(__('图片大小必须在 1 字节至 %dMB 之间', 'aksrt'), $max_size_mb), 413);
return $this->error(sprintf(__('图片大小不能超过 %s。', 'aksrt'), size_format($max_size)), 413);
}
$checked = wp_check_filetype_and_ext($file['tmp_name'], $file['name'], [