|
| 1 | +package bunny_storage |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + "net/url" |
| 9 | + stdpath "path" |
| 10 | + "strings" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/OpenListTeam/OpenList/v4/drivers/base" |
| 14 | + "github.com/OpenListTeam/OpenList/v4/internal/driver" |
| 15 | + "github.com/OpenListTeam/OpenList/v4/internal/errs" |
| 16 | + "github.com/OpenListTeam/OpenList/v4/internal/model" |
| 17 | + "github.com/go-resty/resty/v2" |
| 18 | +) |
| 19 | + |
| 20 | +type BunnyStorage struct { |
| 21 | + model.Storage |
| 22 | + Addition |
| 23 | + client *resty.Client |
| 24 | + endpoint *url.URL |
| 25 | + cdnBase *url.URL |
| 26 | +} |
| 27 | + |
| 28 | +func (d *BunnyStorage) Config() driver.Config { |
| 29 | + cfg := config |
| 30 | + if d.StorageZoneName != "" && d.CDNBaseURL == "" { |
| 31 | + cfg.OnlyProxy = true |
| 32 | + cfg.PreferProxy = true |
| 33 | + } |
| 34 | + if d.CDNTokenKey != "" && d.CDNTokenIncludeIP { |
| 35 | + cfg.LinkCacheMode = driver.LinkCacheIP |
| 36 | + } |
| 37 | + return cfg |
| 38 | +} |
| 39 | + |
| 40 | +func (d *BunnyStorage) GetAddition() driver.Additional { |
| 41 | + return &d.Addition |
| 42 | +} |
| 43 | + |
| 44 | +func (d *BunnyStorage) Init(ctx context.Context) error { |
| 45 | + if d.RootFolderPath == "" { |
| 46 | + d.RootFolderPath = "/" |
| 47 | + } |
| 48 | + if d.Endpoint == "" { |
| 49 | + d.Endpoint = defaultEndpoint |
| 50 | + } |
| 51 | + if d.SignURLExpire <= 0 { |
| 52 | + d.SignURLExpire = 4 |
| 53 | + } |
| 54 | + if d.CDNTokenMethod == "" { |
| 55 | + d.CDNTokenMethod = cdnTokenMethodSHA256 |
| 56 | + } |
| 57 | + endpoint, err := normalizeBaseURL(d.Endpoint, defaultEndpoint) |
| 58 | + if err != nil { |
| 59 | + return fmt.Errorf("invalid endpoint: %w", err) |
| 60 | + } |
| 61 | + d.endpoint = endpoint |
| 62 | + if d.CDNBaseURL != "" { |
| 63 | + cdnBase, err := normalizeBaseURL(d.CDNBaseURL, "") |
| 64 | + if err != nil { |
| 65 | + return fmt.Errorf("invalid cdn_base_url: %w", err) |
| 66 | + } |
| 67 | + d.cdnBase = cdnBase |
| 68 | + } |
| 69 | + d.client = base.RestyClient |
| 70 | + if d.client == nil { |
| 71 | + d.client = base.NewRestyClient() |
| 72 | + } |
| 73 | + return nil |
| 74 | +} |
| 75 | + |
| 76 | +func (d *BunnyStorage) Drop(ctx context.Context) error { |
| 77 | + return nil |
| 78 | +} |
| 79 | + |
| 80 | +func (d *BunnyStorage) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([]model.Obj, error) { |
| 81 | + var items []bunnyObject |
| 82 | + resp, err := d.authRequest(). |
| 83 | + SetContext(ctx). |
| 84 | + SetResult(&items). |
| 85 | + Get(d.storageURL(dir.GetPath(), true)) |
| 86 | + if err != nil { |
| 87 | + return nil, err |
| 88 | + } |
| 89 | + if err := d.handleResponseError(resp); err != nil { |
| 90 | + return nil, err |
| 91 | + } |
| 92 | + result := make([]model.Obj, 0, len(items)) |
| 93 | + placeholder := d.placeholderName() |
| 94 | + for _, item := range items { |
| 95 | + if item.ObjectName == "" { |
| 96 | + continue |
| 97 | + } |
| 98 | + if !args.S3ShowPlaceholder && !item.IsDirectory && item.ObjectName == placeholder { |
| 99 | + continue |
| 100 | + } |
| 101 | + result = append(result, d.toObj(dir.GetPath(), item)) |
| 102 | + } |
| 103 | + return result, nil |
| 104 | +} |
| 105 | + |
| 106 | +func (d *BunnyStorage) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (*model.Link, error) { |
| 107 | + if file.IsDir() { |
| 108 | + return nil, errs.NotFile |
| 109 | + } |
| 110 | + cacheTTL := time.Duration(0) |
| 111 | + if d.cdnBase != nil { |
| 112 | + linkURL := d.cdnURL(d.cdnObjectPath(file.GetPath())) |
| 113 | + link := &model.Link{ |
| 114 | + URL: linkURL, |
| 115 | + ContentLength: file.GetSize(), |
| 116 | + Expiration: &cacheTTL, |
| 117 | + } |
| 118 | + if d.CDNTokenKey != "" { |
| 119 | + signedURL, _, err := d.signCDNURL(linkURL, args.IP) |
| 120 | + if err != nil { |
| 121 | + return nil, err |
| 122 | + } |
| 123 | + link.URL = signedURL |
| 124 | + } |
| 125 | + return link, nil |
| 126 | + } |
| 127 | + return &model.Link{ |
| 128 | + URL: d.storageURL(file.GetPath(), false), |
| 129 | + Header: http.Header{"AccessKey": []string{d.AccessKey}}, |
| 130 | + ContentLength: file.GetSize(), |
| 131 | + Expiration: &cacheTTL, |
| 132 | + }, nil |
| 133 | +} |
| 134 | + |
| 135 | +func (d *BunnyStorage) MakeDir(ctx context.Context, parentDir model.Obj, dirName string) (model.Obj, error) { |
| 136 | + dirPath := stdpath.Join(parentDir.GetPath(), dirName) |
| 137 | + placeholderPath := stdpath.Join(dirPath, d.placeholderName()) |
| 138 | + if err := d.putReader(ctx, placeholderPath, bytes.NewReader(nil), 0, "application/octet-stream", nil); err != nil { |
| 139 | + return nil, err |
| 140 | + } |
| 141 | + now := time.Now() |
| 142 | + return &model.Object{ |
| 143 | + Path: dirPath, |
| 144 | + Name: dirName, |
| 145 | + Modified: now, |
| 146 | + Ctime: now, |
| 147 | + IsFolder: true, |
| 148 | + }, nil |
| 149 | +} |
| 150 | + |
| 151 | +func (d *BunnyStorage) Remove(ctx context.Context, obj model.Obj) error { |
| 152 | + resp, err := d.authRequest(). |
| 153 | + SetContext(ctx). |
| 154 | + Delete(d.storageURL(obj.GetPath(), obj.IsDir())) |
| 155 | + if err != nil { |
| 156 | + return err |
| 157 | + } |
| 158 | + return d.handleResponseError(resp) |
| 159 | +} |
| 160 | + |
| 161 | +func (d *BunnyStorage) Put(ctx context.Context, dstDir model.Obj, file model.FileStreamer, up driver.UpdateProgress) (model.Obj, error) { |
| 162 | + if up == nil { |
| 163 | + up = func(float64) {} |
| 164 | + } |
| 165 | + dstPath := stdpath.Join(dstDir.GetPath(), file.GetName()) |
| 166 | + err := d.putReader(ctx, dstPath, driver.NewLimitedUploadStream(ctx, &driver.ReaderUpdatingProgress{ |
| 167 | + Reader: file, |
| 168 | + UpdateProgress: up, |
| 169 | + }), file.GetSize(), file.GetMimetype(), nil) |
| 170 | + if err != nil { |
| 171 | + return nil, err |
| 172 | + } |
| 173 | + now := time.Now() |
| 174 | + return &model.Object{ |
| 175 | + Path: dstPath, |
| 176 | + Name: file.GetName(), |
| 177 | + Size: file.GetSize(), |
| 178 | + Modified: now, |
| 179 | + Ctime: now, |
| 180 | + }, nil |
| 181 | +} |
| 182 | + |
| 183 | +func (d *BunnyStorage) putReader(ctx context.Context, path string, body any, size int64, contentType string, extraHeaders http.Header) error { |
| 184 | + if contentType == "" { |
| 185 | + contentType = "application/octet-stream" |
| 186 | + } |
| 187 | + req := d.authRequest(). |
| 188 | + SetContext(ctx). |
| 189 | + SetBody(body). |
| 190 | + SetHeader("Content-Type", contentType) |
| 191 | + if size >= 0 { |
| 192 | + req.SetHeader("Content-Length", fmt.Sprint(size)) |
| 193 | + } |
| 194 | + for key, values := range extraHeaders { |
| 195 | + for _, value := range values { |
| 196 | + req.SetHeader(key, value) |
| 197 | + } |
| 198 | + } |
| 199 | + resp, err := req.Put(d.storageURL(path, false)) |
| 200 | + if err != nil { |
| 201 | + return err |
| 202 | + } |
| 203 | + return d.handleResponseError(resp) |
| 204 | +} |
| 205 | + |
| 206 | +func (d *BunnyStorage) Get(ctx context.Context, path string) (model.Obj, error) { |
| 207 | + fullPath := stdpath.Join(d.GetRootPath(), path) |
| 208 | + parentPath, name := stdpath.Split(fullPath) |
| 209 | + parentPath = strings.TrimSuffix(parentPath, "/") |
| 210 | + if parentPath == "" { |
| 211 | + parentPath = "/" |
| 212 | + } |
| 213 | + objs, err := d.List(ctx, &model.Object{Path: parentPath, IsFolder: true}, model.ListArgs{S3ShowPlaceholder: true}) |
| 214 | + if err != nil { |
| 215 | + return nil, err |
| 216 | + } |
| 217 | + for _, obj := range objs { |
| 218 | + if obj.GetName() == name { |
| 219 | + return obj, nil |
| 220 | + } |
| 221 | + } |
| 222 | + return nil, errs.ObjectNotFound |
| 223 | +} |
| 224 | + |
| 225 | +var _ driver.Driver = (*BunnyStorage)(nil) |
| 226 | +var _ driver.Getter = (*BunnyStorage)(nil) |
0 commit comments