split site adapter helpers

This commit is contained in:
ShukeBta
2026-06-27 11:51:39 +08:00
parent f25873e1aa
commit c9cc4a6ee9
3 changed files with 91 additions and 79 deletions
-79
View File
@@ -6,8 +6,6 @@ import (
"fmt"
"io"
"net/http"
"regexp"
"strconv"
"strings"
"time"
@@ -227,80 +225,3 @@ func doRequestJSON(ctx context.Context, client *http.Client, method, rawURL stri
}
return data, resp.StatusCode, nil
}
func mteamCodeOK(code any) bool {
codeStr := mteamCodeString(code)
return codeStr == "0" || codeStr == "200"
}
func mteamCodeString(code any) string {
switch v := code.(type) {
case string:
return strings.TrimSpace(v)
case float64:
return strconv.Itoa(int(v))
case int:
return strconv.Itoa(v)
default:
return ""
}
}
// parseSizeString 将带单位的字符串转换为字节数。
func parseSizeString(value string, unit string) int64 {
v, err := strconv.ParseFloat(value, 64)
if err != nil {
return 0
}
switch strings.ToLower(unit) {
case "kb":
return int64(v * 1024)
case "mb":
return int64(v * 1024 * 1024)
case "gb":
return int64(v * 1024 * 1024 * 1024)
case "tb":
return int64(v * 1024 * 1024 * 1024 * 1024)
default:
return int64(v)
}
}
// stripHTML 移除 HTML 标签。
func stripHTML(s string) string {
re := regexp.MustCompile(`<[^>]*>`)
return re.ReplaceAllString(s, "")
}
// GetAdapterForType 根据站点类型返回对应的适配器实例。
func GetAdapterForType(siteType string) SiteAdapter {
switch strings.ToLower(siteType) {
case "nexusphp":
return NewNexusPHPAdapter()
case "gazelle":
return NewGazelleAdapter()
case "unit3d":
return NewUNIT3DAdapter()
case "mteam":
return NewMTeamAdapter()
case "yemapt":
return NewYemaPTAdapter()
case "discuz":
return NewDiscuzAdapter()
case "custom_rss":
return NewCustomRSSAdapter()
default:
return NewNexusPHPAdapter()
}
}
// NewSiteAdapter 根据站点模型创建对应的适配器。
func NewSiteAdapter(site *model.Site) SiteAdapter {
if site == nil {
return nil
}
if isYemaPTURL(site.URL) {
return NewYemaPTAdapter()
}
return GetAdapterForType(site.Type)
}
+40
View File
@@ -0,0 +1,40 @@
package service
import (
"strings"
"github.com/ShukeBta/MediaStationGo/internal/model"
)
// GetAdapterForType 根据站点类型返回对应的适配器实例。
func GetAdapterForType(siteType string) SiteAdapter {
switch strings.ToLower(siteType) {
case "nexusphp":
return NewNexusPHPAdapter()
case "gazelle":
return NewGazelleAdapter()
case "unit3d":
return NewUNIT3DAdapter()
case "mteam":
return NewMTeamAdapter()
case "yemapt":
return NewYemaPTAdapter()
case "discuz":
return NewDiscuzAdapter()
case "custom_rss":
return NewCustomRSSAdapter()
default:
return NewNexusPHPAdapter()
}
}
// NewSiteAdapter 根据站点模型创建对应的适配器。
func NewSiteAdapter(site *model.Site) SiteAdapter {
if site == nil {
return nil
}
if isYemaPTURL(site.URL) {
return NewYemaPTAdapter()
}
return GetAdapterForType(site.Type)
}
+51
View File
@@ -0,0 +1,51 @@
package service
import (
"regexp"
"strconv"
"strings"
)
func mteamCodeOK(code any) bool {
codeStr := mteamCodeString(code)
return codeStr == "0" || codeStr == "200"
}
func mteamCodeString(code any) string {
switch v := code.(type) {
case string:
return strings.TrimSpace(v)
case float64:
return strconv.Itoa(int(v))
case int:
return strconv.Itoa(v)
default:
return ""
}
}
// parseSizeString 将带单位的字符串转换为字节数。
func parseSizeString(value string, unit string) int64 {
v, err := strconv.ParseFloat(value, 64)
if err != nil {
return 0
}
switch strings.ToLower(unit) {
case "kb":
return int64(v * 1024)
case "mb":
return int64(v * 1024 * 1024)
case "gb":
return int64(v * 1024 * 1024 * 1024)
case "tb":
return int64(v * 1024 * 1024 * 1024 * 1024)
default:
return int64(v)
}
}
// stripHTML 移除 HTML 标签。
func stripHTML(s string) string {
re := regexp.MustCompile(`<[^>]*>`)
return re.ReplaceAllString(s, "")
}