feat: apply UI/storage/share optimizations and quota improvements
This commit is contained in:
@@ -37,6 +37,8 @@ console.log(`[数据库] 路径: ${dbPath}`);
|
||||
|
||||
// 创建或连接数据库
|
||||
const db = new Database(dbPath);
|
||||
const DEFAULT_LOCAL_STORAGE_QUOTA_BYTES = 1024 * 1024 * 1024; // 1GB
|
||||
const DEFAULT_OSS_STORAGE_QUOTA_BYTES = 1024 * 1024 * 1024; // 1GB
|
||||
|
||||
// ===== 性能优化配置(P0 优先级修复) =====
|
||||
|
||||
@@ -525,7 +527,8 @@ const UserDB = {
|
||||
'has_oss_config': 'number',
|
||||
'is_verified': 'number',
|
||||
'local_storage_quota': 'number',
|
||||
'local_storage_used': 'number'
|
||||
'local_storage_used': 'number',
|
||||
'oss_storage_quota': 'number'
|
||||
};
|
||||
|
||||
const expectedType = FIELD_TYPES[fieldName];
|
||||
@@ -585,6 +588,7 @@ const UserDB = {
|
||||
'current_storage_type': 'current_storage_type',
|
||||
'local_storage_quota': 'local_storage_quota',
|
||||
'local_storage_used': 'local_storage_used',
|
||||
'oss_storage_quota': 'oss_storage_quota',
|
||||
|
||||
// 偏好设置
|
||||
'theme_preference': 'theme_preference'
|
||||
@@ -665,6 +669,7 @@ const UserDB = {
|
||||
'current_storage_type': 'current_storage_type',
|
||||
'local_storage_quota': 'local_storage_quota',
|
||||
'local_storage_used': 'local_storage_used',
|
||||
'oss_storage_quota': 'oss_storage_quota',
|
||||
|
||||
// 偏好设置
|
||||
'theme_preference': 'theme_preference'
|
||||
@@ -717,7 +722,8 @@ const UserDB = {
|
||||
'current_storage_type': 'string', 'theme_preference': 'string',
|
||||
'is_admin': 'number', 'is_active': 'number', 'is_banned': 'number',
|
||||
'has_oss_config': 'number', 'is_verified': 'number',
|
||||
'local_storage_quota': 'number', 'local_storage_used': 'number'
|
||||
'local_storage_quota': 'number', 'local_storage_used': 'number',
|
||||
'oss_storage_quota': 'number'
|
||||
}[key];
|
||||
|
||||
console.warn(`[类型检查] 字段 ${key} 值类型不符: 期望 ${expectedType}, 实际 ${typeof value}, 值: ${JSON.stringify(value)}`);
|
||||
@@ -883,11 +889,14 @@ const ShareDB = {
|
||||
// 根据分享码查找
|
||||
// 增强: 检查分享者是否被封禁(被封禁用户的分享不可访问)
|
||||
// ===== 性能优化(P0 优先级修复):只查询必要字段,避免 N+1 查询 =====
|
||||
// 移除了敏感字段:oss_access_key_id, oss_access_key_secret(不需要传递给分享访问者)
|
||||
// 不返回 oss_access_key_id / oss_access_key_secret;
|
||||
// share_password 仅用于服务端验证,不会透传给前端。
|
||||
findByCode(shareCode) {
|
||||
const result = db.prepare(`
|
||||
SELECT
|
||||
s.id, s.user_id, s.share_code, s.share_path, s.share_type,
|
||||
s.storage_type,
|
||||
s.share_password,
|
||||
s.view_count, s.download_count, s.created_at, s.expires_at,
|
||||
u.username,
|
||||
-- OSS 配置(访问分享文件所需)
|
||||
@@ -1172,8 +1181,9 @@ function migrateToV2() {
|
||||
db.exec(`
|
||||
ALTER TABLE users ADD COLUMN storage_permission TEXT DEFAULT 'sftp_only';
|
||||
ALTER TABLE users ADD COLUMN current_storage_type TEXT DEFAULT 'sftp';
|
||||
ALTER TABLE users ADD COLUMN local_storage_quota INTEGER DEFAULT 1073741824;
|
||||
ALTER TABLE users ADD COLUMN local_storage_quota INTEGER DEFAULT ${DEFAULT_LOCAL_STORAGE_QUOTA_BYTES};
|
||||
ALTER TABLE users ADD COLUMN local_storage_used INTEGER DEFAULT 0;
|
||||
ALTER TABLE users ADD COLUMN oss_storage_quota INTEGER DEFAULT ${DEFAULT_OSS_STORAGE_QUOTA_BYTES};
|
||||
`);
|
||||
|
||||
console.log('[数据库迁移] ✓ 用户表已升级');
|
||||
@@ -1247,6 +1257,34 @@ function migrateToOss() {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 数据库迁移 - OSS 存储配额字段
|
||||
function migrateOssQuotaField() {
|
||||
try {
|
||||
const columns = db.prepare("PRAGMA table_info(users)").all();
|
||||
const hasOssStorageQuota = columns.some(col => col.name === 'oss_storage_quota');
|
||||
|
||||
if (!hasOssStorageQuota) {
|
||||
console.log('[数据库迁移] 添加 oss_storage_quota 字段...');
|
||||
db.exec(`ALTER TABLE users ADD COLUMN oss_storage_quota INTEGER DEFAULT ${DEFAULT_OSS_STORAGE_QUOTA_BYTES}`);
|
||||
console.log('[数据库迁移] ✓ oss_storage_quota 字段已添加');
|
||||
}
|
||||
|
||||
// 统一策略:未配置或无效值默认 1GB
|
||||
const backfillResult = db.prepare(`
|
||||
UPDATE users
|
||||
SET oss_storage_quota = ?
|
||||
WHERE oss_storage_quota IS NULL OR oss_storage_quota <= 0
|
||||
`).run(DEFAULT_OSS_STORAGE_QUOTA_BYTES);
|
||||
|
||||
if (backfillResult.changes > 0) {
|
||||
console.log(`[数据库迁移] ✓ OSS 配额默认值已回填: ${backfillResult.changes} 条记录`);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('[数据库迁移] oss_storage_quota 迁移失败:', error);
|
||||
}
|
||||
}
|
||||
|
||||
// 系统日志操作
|
||||
const SystemLogDB = {
|
||||
// 日志级别常量
|
||||
@@ -1432,6 +1470,7 @@ initDefaultSettings();
|
||||
migrateToV2(); // 执行数据库迁移
|
||||
migrateThemePreference(); // 主题偏好迁移
|
||||
migrateToOss(); // SFTP → OSS 迁移
|
||||
migrateOssQuotaField(); // OSS 配额字段迁移
|
||||
|
||||
module.exports = {
|
||||
db,
|
||||
|
||||
Reference in New Issue
Block a user