Skip to content

Commit

Permalink
Feat(github_releases): 可选填入 GitHub token 来提高速率限制或访问私有仓库
Browse files Browse the repository at this point in the history
  • Loading branch information
YangRucheng committed Jan 21, 2025
1 parent ecd6f8b commit 2f9f346
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
6 changes: 3 additions & 3 deletions drivers/github_releases/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ func (d *GithubReleases) List(ctx context.Context, dir model.Obj, args model.Lis

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

if d.Addition.ShowReadme {
resp, err := GetGithubOtherFile(repo.RepoName, path, d.Storage.CacheExpiration)
resp, err := GetGithubOtherFile(repo.RepoName, path, d.Storage.CacheExpiration, d.Addition.Token)
if err != nil {
return nil, err
}
Expand All @@ -67,7 +67,7 @@ func (d *GithubReleases) List(ctx context.Context, dir model.Obj, args model.Lis
if nextDir == "" {
continue
}
repo, _ := GetRepoReleaseInfo(repo.RepoName, path, d.Storage.CacheExpiration)
repo, _ := GetRepoReleaseInfo(repo.RepoName, path, d.Storage.CacheExpiration, d.Addition.Token)

hasSameDir := false
for index, file := range files {
Expand Down
1 change: 1 addition & 0 deletions drivers/github_releases/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ 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:"text" required:"false" help:"GitHub token, if you want to access private repositories or increase the rate limit"`
}

var config = driver.Config{
Expand Down
20 changes: 13 additions & 7 deletions drivers/github_releases/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,19 @@ func GetNextDir(wholePath string, basePath string) string {
}

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

res, err := base.RestyClient.R().Get(url)
req := base.RestyClient.R()
if token != "" {
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
}
res, err := req.Get(url)
if err != nil || res.StatusCode() != 200 {
return nil, fmt.Errorf("request fail: %v", err)
}
Expand All @@ -78,10 +82,11 @@ func GetRequest(url string, CacheExpiration int) (*resty.Response, error) {
}

// 获取 README、LICENSE 等文件
func GetGithubOtherFile(repo string, basePath string, CacheExpiration int) (*[]File, error) {
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,
cacheExpiration,
token,
)
body := jsoniter.Get(res.Body())
var files []File
Expand All @@ -108,10 +113,11 @@ func GetGithubOtherFile(repo string, basePath string, CacheExpiration int) (*[]F
}

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

0 comments on commit 2f9f346

Please sign in to comment.