|
| 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) |
0 commit comments