Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alist-org
GitHub Repository: alist-org/alist
Path: blob/main/drivers/github_releases/util.go
1987 views
1
package github_releases
2
3
import (
4
"fmt"
5
"path/filepath"
6
"strings"
7
8
"github.com/alist-org/alist/v3/drivers/base"
9
"github.com/alist-org/alist/v3/pkg/utils"
10
"github.com/go-resty/resty/v2"
11
)
12
13
// 发送 GET 请求
14
func (d *GithubReleases) GetRequest(url string) (*resty.Response, error) {
15
req := base.RestyClient.R()
16
req.SetHeader("Accept", "application/vnd.github+json")
17
req.SetHeader("X-GitHub-Api-Version", "2022-11-28")
18
if d.Addition.Token != "" {
19
req.SetHeader("Authorization", fmt.Sprintf("Bearer %s", d.Addition.Token))
20
}
21
res, err := req.Get(url)
22
if err != nil {
23
return nil, err
24
}
25
if res.StatusCode() != 200 {
26
utils.Log.Warnf("failed to get request: %s %d %s", url, res.StatusCode(), res.String())
27
}
28
return res, nil
29
}
30
31
// 解析挂载结构
32
func (d *GithubReleases) ParseRepos(text string) ([]MountPoint, error) {
33
lines := strings.Split(text, "\n")
34
points := make([]MountPoint, 0)
35
for _, line := range lines {
36
line = strings.TrimSpace(line)
37
if line == "" {
38
continue
39
}
40
parts := strings.Split(line, ":")
41
path, repo := "", ""
42
if len(parts) == 1 {
43
path = "/"
44
repo = parts[0]
45
} else if len(parts) == 2 {
46
path = fmt.Sprintf("/%s", strings.Trim(parts[0], "/"))
47
repo = parts[1]
48
} else {
49
return nil, fmt.Errorf("invalid format: %s", line)
50
}
51
52
points = append(points, MountPoint{
53
Point: path,
54
Repo: repo,
55
Release: nil,
56
Releases: nil,
57
})
58
}
59
d.points = points
60
return points, nil
61
}
62
63
// 获取下一级目录
64
func GetNextDir(wholePath string, basePath string) string {
65
basePath = fmt.Sprintf("%s/", strings.TrimRight(basePath, "/"))
66
if !strings.HasPrefix(wholePath, basePath) {
67
return ""
68
}
69
remainingPath := strings.TrimLeft(strings.TrimPrefix(wholePath, basePath), "/")
70
if remainingPath != "" {
71
parts := strings.Split(remainingPath, "/")
72
nextDir := parts[0]
73
if strings.HasPrefix(wholePath, strings.TrimRight(basePath, "/")+"/"+nextDir) {
74
return nextDir
75
}
76
}
77
return ""
78
}
79
80
// 判断当前目录是否是目标目录的祖先目录
81
func IsAncestorDir(parentDir string, targetDir string) bool {
82
absTargetDir, _ := filepath.Abs(targetDir)
83
absParentDir, _ := filepath.Abs(parentDir)
84
return strings.HasPrefix(absTargetDir, absParentDir)
85
}
86
87