4
4
"context"
5
5
"fmt"
6
6
"net/http"
7
- "time"
8
-
9
7
"strings"
10
8
11
9
"github.com/alist-org/alist/v3/internal/driver"
@@ -18,7 +16,7 @@ type GithubReleases struct {
18
16
model.Storage
19
17
Addition
20
18
21
- releases []Release
19
+ points []MountPoint
22
20
}
23
21
24
22
func (d * GithubReleases ) Config () driver.Config {
@@ -30,85 +28,95 @@ func (d *GithubReleases) GetAddition() driver.Additional {
30
28
}
31
29
32
30
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 )
39
32
return nil
40
33
}
41
34
42
35
func (d * GithubReleases ) Drop (ctx context.Context ) error {
43
- ClearCache ()
44
36
return nil
45
37
}
46
38
47
39
func (d * GithubReleases ) List (ctx context.Context , dir model.Obj , args model.ListArgs ) ([]model.Obj , error ) {
48
40
files := make ([]File , 0 )
49
41
path := fmt .Sprintf ("/%s" , strings .Trim (dir .GetPath (), "/" ))
50
42
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 ]
58
45
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
63
58
}
64
- files = append (files , * resp ... )
65
- }
66
59
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
+ }
83
79
}
80
+ } else { // all version
81
+ point .RequestReleases (d .GetRequest , args .Refresh )
84
82
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
99
92
}
100
- }
101
93
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 )... )
112
120
}
113
121
}
114
122
}
@@ -119,35 +127,41 @@ func (d *GithubReleases) List(ctx context.Context, dir model.Obj, args model.Lis
119
127
}
120
128
121
129
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
+
122
137
link := model.Link {
123
- URL : file . GetID () ,
138
+ URL : url ,
124
139
Header : http.Header {},
125
140
}
126
141
return & link , nil
127
142
}
128
143
129
144
func (d * GithubReleases ) MakeDir (ctx context.Context , parentDir model.Obj , dirName string ) (model.Obj , error ) {
145
+ // TODO create folder, optional
130
146
return nil , errs .NotImplement
131
147
}
132
148
133
149
func (d * GithubReleases ) Move (ctx context.Context , srcObj , dstDir model.Obj ) (model.Obj , error ) {
150
+ // TODO move obj, optional
134
151
return nil , errs .NotImplement
135
152
}
136
153
137
154
func (d * GithubReleases ) Rename (ctx context.Context , srcObj model.Obj , newName string ) (model.Obj , error ) {
155
+ // TODO rename obj, optional
138
156
return nil , errs .NotImplement
139
157
}
140
158
141
159
func (d * GithubReleases ) Copy (ctx context.Context , srcObj , dstDir model.Obj ) (model.Obj , error ) {
160
+ // TODO copy obj, optional
142
161
return nil , errs .NotImplement
143
162
}
144
163
145
164
func (d * GithubReleases ) Remove (ctx context.Context , obj model.Obj ) error {
165
+ // TODO remove obj, optional
146
166
return errs .NotImplement
147
167
}
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 )
0 commit comments