feat: make zero download quota block downloads and use -1 for unlimited

This commit is contained in:
2026-02-17 19:25:39 +08:00
parent 53e77ebf4e
commit 978ae545e1
4 changed files with 58 additions and 34 deletions

View File

@@ -650,8 +650,11 @@ function normalizeOssQuota(rawQuota) {
function normalizeDownloadTrafficQuota(rawQuota) {
const parsedQuota = Number(rawQuota);
if (!Number.isFinite(parsedQuota) || parsedQuota <= 0) {
return 0; // 0 表示不限流量
if (!Number.isFinite(parsedQuota)) {
return 0; // 0 表示禁止下载
}
if (parsedQuota < 0) {
return -1; // -1 表示不限流量
}
return Math.min(MAX_DOWNLOAD_TRAFFIC_BYTES, Math.floor(parsedQuota));
}
@@ -661,7 +664,7 @@ function normalizeDownloadTrafficUsed(rawUsed, quota = 0) {
const normalizedUsed = Number.isFinite(parsedUsed) && parsedUsed > 0
? Math.floor(parsedUsed)
: 0;
if (quota > 0) {
if (quota >= 0) {
return Math.min(normalizedUsed, quota);
}
return normalizedUsed;
@@ -670,11 +673,12 @@ function normalizeDownloadTrafficUsed(rawUsed, quota = 0) {
function getDownloadTrafficState(user) {
const quota = normalizeDownloadTrafficQuota(user?.download_traffic_quota);
const used = normalizeDownloadTrafficUsed(user?.download_traffic_used, quota);
const isUnlimited = quota < 0;
return {
quota,
used,
isUnlimited: quota <= 0,
remaining: quota > 0 ? Math.max(0, quota - used) : Number.POSITIVE_INFINITY
isUnlimited,
remaining: isUnlimited ? Number.POSITIVE_INFINITY : Math.max(0, quota - used)
};
}
@@ -3077,11 +3081,13 @@ app.get('/api/user/download-traffic-report', authMiddleware, (req, res) => {
return peak;
}, null);
const safeQuota = trafficState.quota > 0 ? trafficState.quota : 0;
const remainingBytes = safeQuota > 0 ? Math.max(0, safeQuota - trafficState.used) : null;
const usagePercentage = safeQuota > 0
? Math.min(100, Math.round((trafficState.used / safeQuota) * 100))
: null;
const safeQuota = trafficState.isUnlimited ? 0 : Math.max(0, trafficState.quota);
const remainingBytes = trafficState.isUnlimited ? null : Math.max(0, safeQuota - trafficState.used);
const usagePercentage = trafficState.isUnlimited
? null
: (safeQuota > 0
? Math.min(100, Math.round((trafficState.used / safeQuota) * 100))
: 100);
res.json({
success: true,
@@ -7602,7 +7608,7 @@ app.post('/api/admin/users/:id/storage-permission',
body('storage_permission').isIn(['local_only', 'oss_only', 'user_choice']).withMessage('无效的存储权限'),
body('local_storage_quota').optional({ nullable: true }).isInt({ min: 1048576, max: 10995116277760 }).withMessage('本地配额必须在 1MB 到 10TB 之间'),
body('oss_storage_quota').optional({ nullable: true }).isInt({ min: 1048576, max: 10995116277760 }).withMessage('OSS配额必须在 1MB 到 10TB 之间'),
body('download_traffic_quota').optional({ nullable: true }).isInt({ min: 0, max: MAX_DOWNLOAD_TRAFFIC_BYTES }).withMessage('下载流量配额必须在 0 到 10TB 之间(0表示不限)'),
body('download_traffic_quota').optional({ nullable: true }).isInt({ min: -1, max: MAX_DOWNLOAD_TRAFFIC_BYTES }).withMessage('下载流量配额必须在 -1 到 10TB 之间(-1表示不限0表示禁止下载'),
body('download_traffic_delta').optional({ nullable: true }).isInt({ min: -MAX_DOWNLOAD_TRAFFIC_BYTES, max: MAX_DOWNLOAD_TRAFFIC_BYTES }).withMessage('下载流量增减值必须在 -10TB 到 10TB 之间'),
body('download_traffic_reset_cycle').optional({ nullable: true }).isIn(['none', 'daily', 'weekly', 'monthly']).withMessage('下载流量重置周期无效'),
body('download_traffic_quota_expires_at').optional({ nullable: true }).custom((value) => {