-
-
Notifications
You must be signed in to change notification settings - Fork 2k
feat(driver/bunny_storage): add Bunny Storage driver #2472
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
demogest
wants to merge
2
commits into
OpenListTeam:main
Choose a base branch
from
demogest:bunny-storage-driver
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,226 @@ | ||
| package bunny_storage | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "fmt" | ||
| "net/http" | ||
| "net/url" | ||
| stdpath "path" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/OpenListTeam/OpenList/v4/drivers/base" | ||
| "github.com/OpenListTeam/OpenList/v4/internal/driver" | ||
| "github.com/OpenListTeam/OpenList/v4/internal/errs" | ||
| "github.com/OpenListTeam/OpenList/v4/internal/model" | ||
| "github.com/go-resty/resty/v2" | ||
| ) | ||
|
|
||
| type BunnyStorage struct { | ||
| model.Storage | ||
| Addition | ||
| client *resty.Client | ||
| endpoint *url.URL | ||
| cdnBase *url.URL | ||
| } | ||
|
|
||
| func (d *BunnyStorage) Config() driver.Config { | ||
| cfg := config | ||
| if d.StorageZoneName != "" && d.CDNBaseURL == "" { | ||
| cfg.OnlyProxy = true | ||
| cfg.PreferProxy = true | ||
| } | ||
| if d.CDNTokenKey != "" && d.CDNTokenIncludeIP { | ||
| cfg.LinkCacheMode = driver.LinkCacheIP | ||
| } | ||
| return cfg | ||
| } | ||
|
|
||
| func (d *BunnyStorage) GetAddition() driver.Additional { | ||
| return &d.Addition | ||
| } | ||
|
|
||
| func (d *BunnyStorage) Init(ctx context.Context) error { | ||
| if d.RootFolderPath == "" { | ||
| d.RootFolderPath = "/" | ||
| } | ||
| if d.Endpoint == "" { | ||
| d.Endpoint = defaultEndpoint | ||
| } | ||
| if d.SignURLExpire <= 0 { | ||
| d.SignURLExpire = 4 | ||
| } | ||
| if d.CDNTokenMethod == "" { | ||
| d.CDNTokenMethod = cdnTokenMethodSHA256 | ||
| } | ||
| endpoint, err := normalizeBaseURL(d.Endpoint, defaultEndpoint) | ||
| if err != nil { | ||
| return fmt.Errorf("invalid endpoint: %w", err) | ||
| } | ||
| d.endpoint = endpoint | ||
| if d.CDNBaseURL != "" { | ||
| cdnBase, err := normalizeBaseURL(d.CDNBaseURL, "") | ||
| if err != nil { | ||
| return fmt.Errorf("invalid cdn_base_url: %w", err) | ||
| } | ||
| d.cdnBase = cdnBase | ||
| } | ||
| d.client = base.RestyClient | ||
| if d.client == nil { | ||
| d.client = base.NewRestyClient() | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (d *BunnyStorage) Drop(ctx context.Context) error { | ||
| return nil | ||
| } | ||
|
|
||
| func (d *BunnyStorage) List(ctx context.Context, dir model.Obj, args model.ListArgs) ([]model.Obj, error) { | ||
| var items []bunnyObject | ||
| resp, err := d.authRequest(). | ||
| SetContext(ctx). | ||
| SetResult(&items). | ||
| Get(d.storageURL(dir.GetPath(), true)) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if err := d.handleResponseError(resp); err != nil { | ||
| return nil, err | ||
| } | ||
| result := make([]model.Obj, 0, len(items)) | ||
| placeholder := d.placeholderName() | ||
| for _, item := range items { | ||
| if item.ObjectName == "" { | ||
| continue | ||
| } | ||
| if !args.S3ShowPlaceholder && !item.IsDirectory && item.ObjectName == placeholder { | ||
| continue | ||
| } | ||
| result = append(result, d.toObj(dir.GetPath(), item)) | ||
| } | ||
| return result, nil | ||
| } | ||
|
|
||
| func (d *BunnyStorage) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (*model.Link, error) { | ||
| if file.IsDir() { | ||
| return nil, errs.NotFile | ||
| } | ||
| cacheTTL := time.Duration(0) | ||
| if d.cdnBase != nil { | ||
| linkURL := d.cdnURL(d.cdnObjectPath(file.GetPath())) | ||
| link := &model.Link{ | ||
| URL: linkURL, | ||
| ContentLength: file.GetSize(), | ||
| Expiration: &cacheTTL, | ||
| } | ||
| if d.CDNTokenKey != "" { | ||
| signedURL, _, err := d.signCDNURL(linkURL, args.IP) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| link.URL = signedURL | ||
| } | ||
| return link, nil | ||
| } | ||
| return &model.Link{ | ||
| URL: d.storageURL(file.GetPath(), false), | ||
| Header: http.Header{"AccessKey": []string{d.AccessKey}}, | ||
| ContentLength: file.GetSize(), | ||
| Expiration: &cacheTTL, | ||
| }, nil | ||
| } | ||
|
|
||
| func (d *BunnyStorage) MakeDir(ctx context.Context, parentDir model.Obj, dirName string) (model.Obj, error) { | ||
| dirPath := stdpath.Join(parentDir.GetPath(), dirName) | ||
| placeholderPath := stdpath.Join(dirPath, d.placeholderName()) | ||
| if err := d.putReader(ctx, placeholderPath, bytes.NewReader(nil), 0, "application/octet-stream", nil); err != nil { | ||
| return nil, err | ||
| } | ||
| now := time.Now() | ||
| return &model.Object{ | ||
| Path: dirPath, | ||
| Name: dirName, | ||
| Modified: now, | ||
| Ctime: now, | ||
| IsFolder: true, | ||
| }, nil | ||
| } | ||
|
|
||
| func (d *BunnyStorage) Remove(ctx context.Context, obj model.Obj) error { | ||
| resp, err := d.authRequest(). | ||
| SetContext(ctx). | ||
| Delete(d.storageURL(obj.GetPath(), obj.IsDir())) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| return d.handleResponseError(resp) | ||
| } | ||
|
|
||
| func (d *BunnyStorage) Put(ctx context.Context, dstDir model.Obj, file model.FileStreamer, up driver.UpdateProgress) (model.Obj, error) { | ||
| if up == nil { | ||
| up = func(float64) {} | ||
| } | ||
| dstPath := stdpath.Join(dstDir.GetPath(), file.GetName()) | ||
| err := d.putReader(ctx, dstPath, driver.NewLimitedUploadStream(ctx, &driver.ReaderUpdatingProgress{ | ||
| Reader: file, | ||
| UpdateProgress: up, | ||
| }), file.GetSize(), file.GetMimetype(), nil) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| now := time.Now() | ||
| return &model.Object{ | ||
| Path: dstPath, | ||
| Name: file.GetName(), | ||
| Size: file.GetSize(), | ||
| Modified: now, | ||
| Ctime: now, | ||
| }, nil | ||
| } | ||
|
|
||
| func (d *BunnyStorage) putReader(ctx context.Context, path string, body any, size int64, contentType string, extraHeaders http.Header) error { | ||
| if contentType == "" { | ||
| contentType = "application/octet-stream" | ||
| } | ||
| req := d.authRequest(). | ||
| SetContext(ctx). | ||
| SetBody(body). | ||
| SetHeader("Content-Type", contentType) | ||
| if size >= 0 { | ||
| req.SetHeader("Content-Length", fmt.Sprint(size)) | ||
| } | ||
| for key, values := range extraHeaders { | ||
| for _, value := range values { | ||
| req.SetHeader(key, value) | ||
| } | ||
| } | ||
| resp, err := req.Put(d.storageURL(path, false)) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| return d.handleResponseError(resp) | ||
| } | ||
|
|
||
| func (d *BunnyStorage) Get(ctx context.Context, path string) (model.Obj, error) { | ||
| fullPath := stdpath.Join(d.GetRootPath(), path) | ||
| parentPath, name := stdpath.Split(fullPath) | ||
| parentPath = strings.TrimSuffix(parentPath, "/") | ||
| if parentPath == "" { | ||
| parentPath = "/" | ||
| } | ||
| objs, err := d.List(ctx, &model.Object{Path: parentPath, IsFolder: true}, model.ListArgs{S3ShowPlaceholder: true}) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| for _, obj := range objs { | ||
| if obj.GetName() == name { | ||
| return obj, nil | ||
| } | ||
| } | ||
| return nil, errs.ObjectNotFound | ||
| } | ||
|
|
||
| var _ driver.Driver = (*BunnyStorage)(nil) | ||
| var _ driver.Getter = (*BunnyStorage)(nil) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package bunny_storage | ||
|
|
||
| import ( | ||
| "github.com/OpenListTeam/OpenList/v4/internal/driver" | ||
| "github.com/OpenListTeam/OpenList/v4/internal/op" | ||
| ) | ||
|
|
||
| type Addition struct { | ||
| driver.RootPath | ||
| StorageZoneName string `json:"storage_zone_name" required:"true"` | ||
| AccessKey string `json:"access_key" required:"true"` | ||
| Endpoint string `json:"endpoint" required:"true" default:"storage.bunnycdn.com"` | ||
| CDNBaseURL string `json:"cdn_base_url"` | ||
| CDNTokenKey string `json:"cdn_token_key"` | ||
| CDNTokenMethod string `json:"cdn_token_method" type:"select" options:"sha256,hmac_sha256" default:"sha256"` | ||
| CDNTokenIncludeIP bool `json:"cdn_token_include_ip" default:"false"` | ||
| SignURLExpire int `json:"sign_url_expire" type:"number" default:"4"` | ||
| Placeholder string `json:"placeholder" default:".openlist"` | ||
| } | ||
|
|
||
| var config = driver.Config{ | ||
| Name: "Bunny Storage", | ||
| LocalSort: true, | ||
| DefaultRoot: "/", | ||
| CheckStatus: true, | ||
| } | ||
|
|
||
| func init() { | ||
| op.RegisterDriver(func() driver.Driver { | ||
| return &BunnyStorage{} | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package bunny_storage | ||
|
|
||
| import "time" | ||
|
|
||
| type bunnyObject struct { | ||
| Guid string `json:"Guid"` | ||
| StorageZoneName string `json:"StorageZoneName"` | ||
| Path string `json:"Path"` | ||
| ObjectName string `json:"ObjectName"` | ||
| Length int64 `json:"Length"` | ||
| LastChanged string `json:"LastChanged"` | ||
| IsDirectory bool `json:"IsDirectory"` | ||
| ServerID int `json:"ServerId"` | ||
| UserID string `json:"UserId"` | ||
| DateCreated string `json:"DateCreated"` | ||
| StorageZoneID int64 `json:"StorageZoneId"` | ||
| } | ||
|
|
||
| type apiError struct { | ||
| HttpCode int `json:"HttpCode"` | ||
| Message string `json:"Message"` | ||
| } | ||
|
|
||
| type parsedTimes struct { | ||
| modified time.Time | ||
| created time.Time | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.