Skip to content

Commit 80bec46

Browse files
committed
feat(github_releases):添加对 github_releases 驱动程序的支持 (AlistGo#7844) 关闭 AlistGo#7842)
1 parent 9064860 commit 80bec46

File tree

5 files changed

+473
-0
lines changed

5 files changed

+473
-0
lines changed

drivers/all.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
_ "github.com/alist-org/alist/v3/drivers/febbox"
2626
_ "github.com/alist-org/alist/v3/drivers/ftp"
2727
_ "github.com/alist-org/alist/v3/drivers/github"
28+
_ "github.com/alist-org/alist/v3/drivers/github_releases"
2829
_ "github.com/alist-org/alist/v3/drivers/google_drive"
2930
_ "github.com/alist-org/alist/v3/drivers/google_photo"
3031
_ "github.com/alist-org/alist/v3/drivers/halalcloud"

drivers/github_releases/driver.go

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
package github_releases
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/http"
7+
"time"
8+
9+
"strings"
10+
11+
"github.com/alist-org/alist/v3/internal/driver"
12+
"github.com/alist-org/alist/v3/internal/errs"
13+
"github.com/alist-org/alist/v3/internal/model"
14+
"github.com/alist-org/alist/v3/pkg/utils"
15+
)
16+
17+
type GithubReleases struct {
18+
model.Storage
19+
Addition
20+
21+
releases []Release
22+
}
23+
24+
func (d *GithubReleases) Config() driver.Config {
25+
return config
26+
}
27+
28+
func (d *GithubReleases) GetAddition() driver.Additional {
29+
return &d.Addition
30+
}
31+
32+
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
39+
return nil
40+
}
41+
42+
func (d *GithubReleases) Drop(ctx context.Context) error {
43+
ClearCache()
44+
return nil
45+
}
46+
47+
func (d *GithubReleases) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([]model.Obj, error) {
48+
files := make([]File, 0)
49+
path := fmt.Sprintf("/%s", strings.Trim(dir.GetPath(), "/"))
50+
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...)
58+
59+
if d.Addition.ShowReadme {
60+
resp, err := GetGithubOtherFile(repo.RepoName, path, d.Storage.CacheExpiration)
61+
if err != nil {
62+
return nil, err
63+
}
64+
files = append(files, *resp...)
65+
}
66+
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
83+
}
84+
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
99+
}
100+
}
101+
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+
})
112+
}
113+
}
114+
}
115+
116+
return utils.SliceConvert(files, func(src File) (model.Obj, error) {
117+
return src, nil
118+
})
119+
}
120+
121+
func (d *GithubReleases) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (*model.Link, error) {
122+
link := model.Link{
123+
URL: file.GetID(),
124+
Header: http.Header{},
125+
}
126+
return &link, nil
127+
}
128+
129+
func (d *GithubReleases) MakeDir(ctx context.Context, parentDir model.Obj, dirName string) (model.Obj, error) {
130+
return nil, errs.NotImplement
131+
}
132+
133+
func (d *GithubReleases) Move(ctx context.Context, srcObj, dstDir model.Obj) (model.Obj, error) {
134+
return nil, errs.NotImplement
135+
}
136+
137+
func (d *GithubReleases) Rename(ctx context.Context, srcObj model.Obj, newName string) (model.Obj, error) {
138+
return nil, errs.NotImplement
139+
}
140+
141+
func (d *GithubReleases) Copy(ctx context.Context, srcObj, dstDir model.Obj) (model.Obj, error) {
142+
return nil, errs.NotImplement
143+
}
144+
145+
func (d *GithubReleases) Remove(ctx context.Context, obj model.Obj) error {
146+
return errs.NotImplement
147+
}
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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package github_releases
2+
3+
import (
4+
"github.com/alist-org/alist/v3/internal/driver"
5+
"github.com/alist-org/alist/v3/internal/op"
6+
)
7+
8+
type Addition struct {
9+
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"`
11+
ShowReadme bool `json:"show_readme" type:"bool" default:"true" help:"show README、LICENSE file"`
12+
Token string `json:"token" type:"string" required:"false" help:"GitHub token, if you want to access private repositories or increase the rate limit"`
13+
ShowAllVersion bool `json:"show_all_version" type:"bool" default:"false" help:"show all versions"`
14+
}
15+
16+
var config = driver.Config{
17+
Name: "GitHub Releases",
18+
LocalSort: false,
19+
OnlyLocal: false,
20+
OnlyProxy: false,
21+
NoCache: false,
22+
NoUpload: false,
23+
NeedMs: false,
24+
DefaultRoot: "",
25+
CheckStatus: false,
26+
Alert: "",
27+
NoOverwriteUpload: false,
28+
}
29+
30+
func init() {
31+
op.RegisterDriver(func() driver.Driver {
32+
return &GithubReleases{}
33+
})
34+
}

drivers/github_releases/types.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package github_releases
2+
3+
import (
4+
"time"
5+
6+
"github.com/alist-org/alist/v3/pkg/utils"
7+
)
8+
9+
type File struct {
10+
FileName string `json:"name"`
11+
Size int64 `json:"size"`
12+
CreateAt time.Time `json:"time"`
13+
UpdateAt time.Time `json:"chtime"`
14+
Url string `json:"url"`
15+
Type string `json:"type"`
16+
Path string `json:"path"`
17+
}
18+
19+
func (f File) GetHash() utils.HashInfo {
20+
return utils.HashInfo{}
21+
}
22+
23+
func (f File) GetPath() string {
24+
return f.Path
25+
}
26+
27+
func (f File) GetSize() int64 {
28+
return f.Size
29+
}
30+
31+
func (f File) GetName() string {
32+
return f.FileName
33+
}
34+
35+
func (f File) ModTime() time.Time {
36+
return f.UpdateAt
37+
}
38+
39+
func (f File) CreateTime() time.Time {
40+
return f.CreateAt
41+
}
42+
43+
func (f File) IsDir() bool {
44+
return f.Type == "dir"
45+
}
46+
47+
func (f File) GetID() string {
48+
return f.Url
49+
}
50+
51+
func (f File) Thumb() string {
52+
return ""
53+
}
54+
55+
type ReleasesData struct {
56+
Files []File `json:"files"`
57+
Size int64 `json:"size"`
58+
UpdateAt time.Time `json:"chtime"`
59+
CreateAt time.Time `json:"time"`
60+
Url string `json:"url"`
61+
}
62+
63+
type Release struct {
64+
Path string // 挂载路径
65+
RepoName string // 仓库名称
66+
Version string // 版本号, tag
67+
ID string // 版本ID
68+
}

0 commit comments

Comments
 (0)