8000 feat(github_releases): support dir size for show all version (#7938) · AlistGo/alist@f795807 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit f795807

Browse files
authored
feat(github_releases): support dir size for show all version (#7938)
* refactor * 修改默认 RepoStructure * feat: 支持使用 gh-proxy
1 parent 6164e45 commit f795807

File tree

5 files changed

+385
-271
lines changed

5 files changed

+385
-271
lines changed

drivers/github_releases/driver.go

+85-71
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"context"
55
"fmt"
66
"net/http"
7-
"time"
8-
97
"strings"
108

119
"github.com/alist-org/alist/v3/internal/driver"
@@ -18,7 +16,7 @@ type GithubReleases struct {
1816
model.Storage
1917
Addition
2018

21-
releases []Release
19+
points []MountPoint
2220
}
2321

2422
func (d *GithubReleases) Config() driver.Config {
@@ -30,85 +28,95 @@ func (d *GithubReleases) GetAddition() driver.Additional {
3028
}
3129

3230
func (d *GithubReleases) Init(ctx context.Context) error {
33-
SetHeader(d.Addition.Token)
34-
repos, err := ParseRepos(d.Addition.RepoStructure, d.Addition.ShowAllVersion)
35-
if err != nil {
36-
return err
37-
}
38-
d.releases = repos
31+
d.ParseRepos(d.Addition.RepoStructure)
3932
return nil
4033
}
4134

4235
func (d *GithubReleases) Drop(ctx context.Context) error {
43-
ClearCache()
4436
return nil
4537
}
4638

4739
func (d *GithubReleases) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([]model.Obj, error) {
4840
files := make([]File, 0)
4941
path := fmt.Sprintf("/%s", strings.Trim(dir.GetPath(), "/"))
5042

51-
for _, repo := range d.releases {
52-
if repo.Path == path { // 与仓库路径相同
53-
resp, err := GetRepoReleaseInfo(repo.RepoName, repo.ID, path, d.Storage.CacheExpiration)
54-
if err != nil {
55-
return nil, err
56-
}
57-
files = append(files, resp.Files...)
43+
for i := range d.points {
44+
point := &d.points[i]
5845

59-
if d.Addition.ShowReadme {
60-
resp, err := GetGithubOtherFile(repo.RepoName, path, d.Storage.CacheExpiration)
61-
if err != nil {
62-
return nil, err
46+
if !d.Addition.ShowAllVersion { // latest
47+
point.RequestRelease(d.GetRequest, args.Refresh)
48+
49+
if point.Point == path { // 与仓库路径相同
50+
files = append(files, point.GetLatestRelease()...)
51+
if d.Addition.ShowReadme {
52+
files = append(files, point.GetOtherFile(d.GetRequest, args.Refresh)...)
53+
}
54+
} else if strings.HasPrefix(point.Point, path) { // 仓库目录的父目录
1E0A 55+
nextDir := GetNextDir(point.Point, path)
56+
if nextDir == "" {
57+
continue
6358
}
64-
files = append(files, *resp...)
65-
}
6659

67-
} else if strings.HasPrefix(repo.Path, path) { // 仓库路径是目录的子目录
68-
nextDir := GetNextDir(repo.Path, path)
69-
if nextDir == "" {
70-
continue
71-
}
72-
if d.Addition.ShowAllVersion {
73-
files = append(files, File{
74-
FileName: nextDir,
75-
Size: 0,
76-
CreateAt: time.Time{},
77-
UpdateAt: time.Time{},
78-
Url: "",
79-
Type: "dir",
80-
Path: fmt.Sprintf("%s/%s", path, nextDir),
81-
})
82-
continue
60+
hasSameDir := false
61+
for index := range files {
62+
if files[index].GetName() == nextDir {
63+
hasSameDir = true
64+
files[index].Size += point.GetLatestSize()
65+
break
66+
}
67+
}
68+
if !hasSameDir {
69+
files = append(files, File{
70+
Path: path + "/" + nextDir,
71+
FileName: nextDir,
72+
Size: point.GetLatestSize(),
73+
UpdateAt: point.Release.PublishedAt,
74+
CreateAt: point.Release.CreatedAt,
75+
Type: "dir",
76+
Url: "",
77+
})
78+
}
8379
}
80+
} else { // all version
81+
point.RequestReleases(d.GetRequest, args.Refresh)
8482

85-
repo, _ := GetRepoReleaseInfo(repo.RepoName, repo.Version, path, d.Storage.CacheExpiration)
86-
87-
hasSameDir := false
88-
for index, file := range files {
89-
if file.FileName == nextDir {
90-
hasSameDir = true
91-
files[index].Size += repo.Size
92-
files[index].UpdateAt = func(a time.Time, b time.Time) time.Time {
93-
if a.After(b) {
94-
return a
95-
}
96-
return b
97-
}(files[index].UpdateAt, repo.UpdateAt)
98-
break
83+
if point.Point == path { // 与仓库路径相同
84+
files = append(files, point.GetAllVersion()...)
85+
if d.Addition.ShowReadme {
86+
files = append(files, point.GetOtherFile(d.GetRequest, args.Refresh)...)
87+
}
88+
} else if strings.HasPrefix(point.Point, path) { // 仓库目录的父目录
89+
nextDir := GetNextDir(point.Point, path)
90+
if nextDir == "" {
91+
continue
9992
}
100-
}
10193

102-
if !hasSameDir {
103-
files = append(files, File{
104-
FileName: nextDir,
105-
Size: repo.Size,
106-
CreateAt: repo.CreateAt,
107-
UpdateAt: repo.UpdateAt,
108-
Url: repo.Url,
109-
Type: "dir",
110-
Path: fmt.Sprintf("%s/%s", path, nextDir),
111-
})
94+
hasSameDir := false
95+
for index := range files {
96+
if files[index].GetName() == nextDir {
97+
hasSameDir = true
98+
files[index].Size += point.GetAllVersionSize()
99+
break
100+
}
101+
}
102+
if !hasSameDir {
103+
files = append(files, File{
104+
FileName: nextDir,
105+
Path: path + "/" + nextDir,
106+
Size: point.GetAllVersionSize(),
107+
UpdateAt: (*point.Releases)[0].PublishedAt,
108+
CreateAt: (*point.Releases)[0].CreatedAt,
109+
Type: "dir",
110+
Url: "",
111+
})
112+
}
113+
} else if strings.HasPrefix(path, point.Point) { // 仓库目录的子目录
114+
tagName := GetNextDir(path, point.Point)
115+
if tagName == "" {
116+
continue
117+
}
118+
119+
files = append(files, point.GetReleaseByTagName(tagName)...)
112120
}
113121
}
114122
}
@@ -119,35 +127,41 @@ func (d *GithubReleases) List(ctx context.Context, dir model.Obj, args model.Lis
119127
}
120128

121129
func (d *GithubReleases) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (*model.Link, error) {
130+
url := file.GetID()
131+
gh_proxy := strings.TrimSpace(d.Addition.GitHubProxy)
132+
133+
if gh_proxy != "" {
134+
url = strings.Replace(url, "https://github.com", gh_proxy, 1)
135+
}
136+
122137
link := model.Link{
123-
URL: file.GetID(),
138+
URL: url,
124139
Header: http.Header{},
125140
}
126141
return &link, nil
127142
}
128143

129144
func (d *GithubReleases) MakeDir(ctx context.Context, parentDir model.Obj, dirName string) (model.Obj, error) {
145+
// TODO create folder, optional
130146
return nil, errs.NotImplement
131147
}
132148

133149
func (d *GithubReleases) Move(ctx context.Context, srcObj, dstDir model.Obj) (model.Obj, error) {
150+
// TODO move obj, optional
134151
return nil, errs.NotImplement
135152
}
136153

137154
func (d *GithubReleases) Rename(ctx context.Context, srcObj model.Obj, newName string) (model.Obj, error) {
155+
// TODO rename obj, optional
138156
return nil, errs.NotImplement
139157
}
140158

141159
func (d *GithubReleases) Copy(ctx context.Context, srcObj, dstDir model.Obj) (model.Obj, error) {
160+
// TODO copy obj, optional
142161
return nil, errs.NotImplement
143162
}
144163

145164
func (d *GithubReleases) Remove(ctx context.Context, obj model.Obj) error {
165+
// TODO remove obj, optional
146166
return errs.NotImplement
147167
}
148-
149-
func (d *GithubReleases) Put(ctx context.Context, dstDir model.Obj, stream model.FileStreamer, up driver.UpdateProgress) (model.Obj, error) {
150-
return nil, errs.NotImplement
151-
}
152-
153-
var _ driver.Driver = (*GithubReleases)(nil)

drivers/github_releases/meta.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ import (
77

88
type Addition struct {
99
driver.RootID
10-
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"`
10+
RepoStructure string `json:"repo_structure" type:"text" required:"true" default:"alistGo/alist" help:"structure:[path:]org/repo"`
1111
ShowReadme bool `json:"show_readme" type:"bool" default:"true" help:"show README、LICENSE file"`
1212
Token string `json:"token" type:"string" required:"false" help:"GitHub token, if you want to access private repositories or increase the rate limit"`
1313
ShowAllVersion bool `json:"show_all_version" type:"bool" default:"false" help:"show all versions"`
14+
GitHubProxy string `json:"gh_proxy" type:"string" default:"" help:"GitHub proxy, e.g. https://ghproxy.net/github.com or https://gh-proxy.com/github.com "`
1415
}
1516

1617
var config = driver.Config{

drivers/github_releases/models.go

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package github_releases
2+
3+
type Release struct {
4+
Url string `json:"url"`
5+
AssetsUrl string `json:"assets_url"`
6+
UploadUrl string `json:"upload_url"`
7+
HtmlUrl string `json:"html_url"`
8+
Id int `json:"id"`
9+
Author User `json:"author"`
10+
NodeId string `json:"node_id"`
11+
TagName string `json:"tag_name"`
12+
TargetCommitish string `json:"target_commitish"`
13+
Name string `json:"name"`
14+
Draft bool `json:"draft"`
15+
Prerelease bool `json:"prerelease"`
16+
CreatedAt string `json:"created_at"`
17+
PublishedAt string `json:"published_at"`
18+
Assets []Asset `json:"assets"`
19+
TarballUrl string `json:"tarball_url"`
20+
ZipballUrl string `json:"zipball_url"`
21+
Body string `json:"body"`
22+
Reactions Reactions `json:"reactions"`
23+
}
24+
25+
type User struct {
26+
Login string `json:"login"`
27+
Id int `json:"id"`
28+
NodeId string `json:"node_id"`
29+
AvatarUrl string `json:"avatar_url"`
30+
GravatarId string `json:"gravatar_id"`
31+
Url string `json:"url"`
32+
HtmlUrl string `json:"html_url"`
33+
FollowersUrl string `json:"followers_url"`
34+
FollowingUrl string `json:"following_url"`
35+
GistsUrl string `json:"gists_url"`
36+
StarredUrl string `json:"starred_url"`
37+
SubscriptionsUrl string `json:"subscriptions_url"`
38+
OrganizationsUrl string `json:"organizations_url"`
39+
ReposUrl string `json:"repos_url"`
40+
EventsUrl string `json:"events_url"`
41+
ReceivedEventsUrl string `json:"received_events_url"`
42+
Type string `json:"type"`
43+
UserViewType string `json:"user_view_type"`
44+
SiteAdmin bool `json:"site_admin"`
45+
}
46+
47+
type Asset struct {
48+
Url string `json:"url"`
49+
Id int `json:"id"`
50+
NodeId string `json:"node_id"`
51+
Name string `json:"name"`
52+
Label string `json:"label"`
53+
Uploader User `json:"uploader"`
54+
ContentType string `json:"content_type"`
55+
State string `json:"state"`
56+
Size int64 `json:"size"`
57+
DownloadCount int `json:"download_count"`
58+
CreatedAt string `json:"created_at"`
59+
UpdatedAt string `json:"updated_at"`
60+
BrowserDownloadUrl string `json:"browser_download_url"`
61+
}
62+
63+
type Reactions struct {
64+
Url string `json:"url"`
65+
TotalCount int `json:"total_count"`
66+
PlusOne int `json:"+1"`
67+
MinusOne int `json:"-1"`
68+
Laugh int `json:"laugh"`
69+
Hooray int `json:"hooray"`
70+
Confused int `json:"confused"`
71+
Heart int `json:"heart"`
72+
Rocket int `json:"rocket"`
73+
Eyes int `json:"eyes"`
74+
}
75+
76+
type FileInfo struct {
77+
Name string `json:"name"`
78+
Path string `json:"path"`
79+
Sha string `json:"sha"`
80+
Size int64 `json:"size"`
81+
Url string `json:"url"`
82+
HtmlUrl string `json:"html_url"`
83+
GitUrl string `json:"git_url"`
84+
DownloadUrl string `json:"download_url"`
85+
Type string `json:"type"`
86+
}

0 commit comments

Comments
 (0)
0