Skip to content

Commit

Permalink
feat(github_releases): 支持显示所有版本,开启后不显示文件夹大小
Browse files Browse the repository at this point in the history
  • Loading branch information
YangRucheng committed Jan 23, 2025
1 parent c2a4e34 commit 2b39e34
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 38 deletions.
28 changes: 21 additions & 7 deletions drivers/github_releases/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type GithubReleases struct {
model.Storage
Addition

repoList []Repo
releases []Release
}

func (d *GithubReleases) Config() driver.Config {
Expand All @@ -30,11 +30,12 @@ func (d *GithubReleases) GetAddition() driver.Additional {
}

func (d *GithubReleases) Init(ctx context.Context) error {
repos, err := ParseRepos(d.Addition.RepoStructure)
SetHeader(d.Addition.Token)
repos, err := ParseRepos(d.Addition.RepoStructure, d.Addition.ShowAllVersion)
if err != nil {
return err
}
d.repoList = repos
d.releases = repos
return nil
}

Expand All @@ -47,16 +48,16 @@ func (d *GithubReleases) List(ctx context.Context, dir model.Obj, args model.Lis
files := make([]File, 0)
path := fmt.Sprintf("/%s", strings.Trim(dir.GetPath(), "/"))

for _, repo := range d.repoList {
for _, repo := range d.releases {
if repo.Path == path { // 与仓库路径相同
resp, err := GetRepoReleaseInfo(repo.RepoName, path, d.Storage.CacheExpiration, d.Addition.Token)
resp, err := GetRepoReleaseInfo(repo.RepoName, repo.ID, path, d.Storage.CacheExpiration)
if err != nil {
return nil, err
}
files = append(files, resp.Files...)

if d.Addition.ShowReadme {
resp, err := GetGithubOtherFile(repo.RepoName, path, d.Storage.CacheExpiration, d.Addition.Token)
resp, err := GetGithubOtherFile(repo.RepoName, path, d.Storage.CacheExpiration)
if err != nil {
return nil, err
}
Expand All @@ -68,7 +69,20 @@ func (d *GithubReleases) List(ctx context.Context, dir model.Obj, args model.Lis
if nextDir == "" {
continue
}
repo, _ := GetRepoReleaseInfo(repo.RepoName, path, d.Storage.CacheExpiration, d.Addition.Token)
if d.Addition.ShowAllVersion {
files = append(files, File{
FileName: nextDir,
Size: 0,
CreateAt: time.Time{},
UpdateAt: time.Time{},
Url: "",
Type: "dir",
Path: fmt.Sprintf("%s/%s", path, nextDir),
})
continue
}

repo, _ := GetRepoReleaseInfo(repo.RepoName, repo.Version, path, d.Storage.CacheExpiration)

hasSameDir := false
for index, file := range files {
Expand Down
7 changes: 4 additions & 3 deletions drivers/github_releases/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import (

type Addition struct {
driver.RootID
RepoStructure string `json:"repo_structure" type:"text" required:"true" default:"/path/to/alist-gh:alistGo/alist\n/path/to2/alist-web-gh:AlistGo/alist-web" help:"structure:[path:]org/repo"`
ShowReadme bool `json:"show_readme" type:"bool" default:"true" help:"show README、LICENSE file"`
Token string `json:"token" type:"string" required:"false" help:"GitHub token, if you want to access private repositories or increase the rate limit"`
RepoStructure string `json:"repo_structure" type:"text" required:"true" default:"/path/to/alist-gh:alistGo/alist\n/path/to2/alist-web-gh:AlistGo/alist-web" help:"structure:[path:]org/repo"`
ShowReadme bool `json:"show_readme" type:"bool" default:"true" help:"show README、LICENSE file"`
Token string `json:"token" type:"string" required:"false" help:"GitHub token, if you want to access private repositories or increase the rate limit"`
ShowAllVersion bool `json:"show_all_version" type:"bool" default:"false" help:"show all versions"`
}

var config = driver.Config{
Expand Down
10 changes: 6 additions & 4 deletions drivers/github_releases/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,17 @@ func (f File) Thumb() string {
return ""
}

type GithubReleasesData struct {
type ReleasesData struct {
Files []File `json:"files"`
Size int64 `json:"size"`
UpdateAt time.Time `json:"chtime"`
CreateAt time.Time `json:"time"`
Url string `json:"url"`
}

type Repo struct {
Path string
RepoName string
type Release struct {
Path string // 挂载路径
RepoName string // 仓库名称
Version string // 版本号, tag
ID string // 版本ID
}
78 changes: 54 additions & 24 deletions drivers/github_releases/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ var (
cache = make(map[string]*resty.Response)
created = make(map[string]time.Time)
mu sync.Mutex
req *resty.Request
)

// 解析仓库列表
func ParseRepos(text string) ([]Repo, error) {
func ParseRepos(text string, allVersion bool) ([]Release, error) {
lines := strings.Split(text, "\n")
var repos []Repo
var repos []Release
for _, line := range lines {
line = strings.TrimSpace(line)
if line == "" {
Expand All @@ -32,10 +33,21 @@ func ParseRepos(text string) ([]Repo, error) {
if len(parts) != 2 {
return nil, fmt.Errorf("invalid format: %s", line)
}
repos = append(repos, Repo{
Path: fmt.Sprintf("/%s", strings.Trim(parts[0], "/")),
RepoName: parts[1],
})

path := fmt.Sprintf("/%s", strings.Trim(parts[0], "/"))

if allVersion {
releases, _ := GetAllVersion(parts[1], path)
repos = append(repos, *releases...)
} else {
repos = append(repos, Release{
Path: path,
RepoName: parts[1],
Version: "latest",
ID: "latest",
})
}

}
return repos, nil
}
Expand All @@ -57,20 +69,14 @@ func GetNextDir(wholePath string, basePath string) string {
}

// 发送 GET 请求
func GetRequest(url string, cacheExpiration int, token string) (*resty.Response, error) {
func GetRequest(url string, cacheExpiration int) (*resty.Response, error) {
mu.Lock()
if res, ok := cache[url]; ok && time.Now().Before(created[url].Add(time.Duration(cacheExpiration)*time.Minute)) {
mu.Unlock()
return res, nil
}
mu.Unlock()

req := base.RestyClient.R()
if token != "" {
req.SetHeader("Authorization", fmt.Sprintf("Bearer %s", token))
}
req.SetHeader("Accept", "application/vnd.github+json")
req.SetHeader("X-GitHub-Api-Version", "2022-11-28")
res, err := req.Get(url)
if err != nil {
return nil, err
Expand All @@ -88,12 +94,9 @@ func GetRequest(url string, cacheExpiration int, token string) (*resty.Response,
}

// 获取 README、LICENSE 等文件
func GetGithubOtherFile(repo string, basePath string, cacheExpiration int, token string) (*[]File, error) {
res, _ := GetRequest(
fmt.Sprintf("https://api.github.com/repos/%s/contents/", strings.Trim(repo, "/")),
cacheExpiration,
token,
)
func GetGithubOtherFile(repo string, basePath string, cacheExpiration int) (*[]File, error) {
url := fmt.Sprintf("https://api.github.com/repos/%s/contents/", strings.Trim(repo, "/"))
res, _ := GetRequest(url, cacheExpiration)
body := jsoniter.Get(res.Body())
var files []File
for i := 0; i < body.Size(); i++ {
Expand All @@ -119,13 +122,13 @@ func GetGithubOtherFile(repo string, basePath string, cacheExpiration int, token
}

// 获取 GitHub Release 详细信息
func GetRepoReleaseInfo(repo string, basePath string, cacheExpiration int, token string) (*GithubReleasesData, error) {
url := fmt.Sprintf("https://api.github.com/repos/%s/releases/latest", strings.Trim(repo, "/"))
res, _ := GetRequest(url, cacheExpiration, token)
func GetRepoReleaseInfo(repo string, version string, basePath string, cacheExpiration int) (*ReleasesData, error) {
url := fmt.Sprintf("https://api.github.com/repos/%s/releases/%s", strings.Trim(repo, "/"), version)
res, _ := GetRequest(url, cacheExpiration)
body := res.Body()

if jsoniter.Get(res.Body(), "status").ToInt64() != 0 {
return &GithubReleasesData{}, fmt.Errorf("%s", res.String())
return &ReleasesData{}, fmt.Errorf("%s", res.String())
}

assets := jsoniter.Get(res.Body(), "assets")
Expand All @@ -152,7 +155,7 @@ func GetRepoReleaseInfo(repo string, basePath string, cacheExpiration int, token
})
}

return &GithubReleasesData{
return &ReleasesData{
Files: files,
Url: jsoniter.Get(body, "html_url").ToString(),

Expand All @@ -174,9 +177,36 @@ func GetRepoReleaseInfo(repo string, basePath string, cacheExpiration int, token
}, nil
}

// 获取所有的版本号
func GetAllVersion(repo string, path string) (*[]Release, error) {
url := fmt.Sprintf("https://api.github.com/repos/%s/releases", strings.Trim(repo, "/"))
res, _ := GetRequest(url, 0)
body := jsoniter.Get(res.Body())
releases := make([]Release, 0)
for i := 0; i < body.Size(); i++ {
version := body.Get(i, "tag_name").ToString()
releases = append(releases, Release{
Path: fmt.Sprintf("%s/%s", path, version),
Version: version,
RepoName: repo,
ID: body.Get(i, "id").ToString(),
})
}
return &releases, nil
}

func ClearCache() {
mu.Lock()
cache = make(map[string]*resty.Response)
created = make(map[string]time.Time)
mu.Unlock()
}

func SetHeader(token string) {
req = base.RestyClient.R()
if token != "" {
req.SetHeader("Authorization", fmt.Sprintf("Bearer %s", token))
}
req.SetHeader("Accept", "application/vnd.github+json")
req.SetHeader("X-GitHub-Api-Version", "2022-11-28")
}

0 comments on commit 2b39e34

Please sign in to comment.