(c *gin.Context)
| 1339 | } |
| 1340 | |
| 1341 | func UpdateUserSetting(c *gin.Context) { |
| 1342 | var req UpdateUserSettingRequest |
| 1343 | if err := c.ShouldBindJSON(&req); err != nil { |
| 1344 | common.ApiErrorI18n(c, i18n.MsgInvalidParams) |
| 1345 | return |
| 1346 | } |
| 1347 | |
| 1348 | // 验证预警类型 |
| 1349 | if req.QuotaWarningType != dto.NotifyTypeEmail && req.QuotaWarningType != dto.NotifyTypeWebhook && req.QuotaWarningType != dto.NotifyTypeBark && req.QuotaWarningType != dto.NotifyTypeGotify { |
| 1350 | common.ApiErrorI18n(c, i18n.MsgSettingInvalidType) |
| 1351 | return |
| 1352 | } |
| 1353 | |
| 1354 | // 验证预警阈值 |
| 1355 | if req.QuotaWarningThreshold <= 0 { |
| 1356 | common.ApiErrorI18n(c, i18n.MsgQuotaThresholdGtZero) |
| 1357 | return |
| 1358 | } |
| 1359 | |
| 1360 | // 如果是webhook类型,验证webhook地址 |
| 1361 | if req.QuotaWarningType == dto.NotifyTypeWebhook { |
| 1362 | if req.WebhookUrl == "" { |
| 1363 | common.ApiErrorI18n(c, i18n.MsgSettingWebhookEmpty) |
| 1364 | return |
| 1365 | } |
| 1366 | // 验证URL格式 |
| 1367 | if _, err := url.ParseRequestURI(req.WebhookUrl); err != nil { |
| 1368 | common.ApiErrorI18n(c, i18n.MsgSettingWebhookInvalid) |
| 1369 | return |
| 1370 | } |
| 1371 | } |
| 1372 | |
| 1373 | // 如果是邮件类型,验证邮箱地址 |
| 1374 | if req.QuotaWarningType == dto.NotifyTypeEmail && req.NotificationEmail != "" { |
| 1375 | // 验证邮箱格式 |
| 1376 | if !strings.Contains(req.NotificationEmail, "@") { |
| 1377 | common.ApiErrorI18n(c, i18n.MsgSettingEmailInvalid) |
| 1378 | return |
| 1379 | } |
| 1380 | } |
| 1381 | |
| 1382 | // 如果是Bark类型,验证Bark URL |
| 1383 | if req.QuotaWarningType == dto.NotifyTypeBark { |
| 1384 | if req.BarkUrl == "" { |
| 1385 | common.ApiErrorI18n(c, i18n.MsgSettingBarkUrlEmpty) |
| 1386 | return |
| 1387 | } |
| 1388 | // 验证URL格式 |
| 1389 | if _, err := url.ParseRequestURI(req.BarkUrl); err != nil { |
| 1390 | common.ApiErrorI18n(c, i18n.MsgSettingBarkUrlInvalid) |
| 1391 | return |
| 1392 | } |
| 1393 | // 检查是否是HTTP或HTTPS |
| 1394 | if !strings.HasPrefix(req.BarkUrl, "https://") && !strings.HasPrefix(req.BarkUrl, "http://") { |
| 1395 | common.ApiErrorI18n(c, i18n.MsgSettingUrlMustHttp) |
| 1396 | return |
| 1397 | } |
| 1398 | } |
nothing calls this directly
no test coverage detected