-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
feat(driver): add PDS storage driver #2663
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
zymooll
wants to merge
12
commits into
OpenListTeam:main
Choose a base branch
from
zymooll:main
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
12 commits
Select commit
Hold shift + click to select a range
7bcc897
feat(driver): add PDS storage driver
zymooll dfd9241
Merge pull request #1 from zymooll/feat/pds-driver
zymooll 8fd5580
feat(pds): support direct upload completion
zymooll 3e47477
fix(pds): stabilize direct upload completion
zymooll 805c087
Merge branch 'main' into main
PIKACHUIM 7f01259
refactor(pds): remove direct upload functionality and restructure rel…
zymooll 3d5de0f
Merge branch 'main' of https://github.com/zymooll/OpenList
zymooll 4a202fa
Merge branch 'main' into main
PIKACHUIM 50bef46
Merge branch 'main' into main
KawakazeNotFound 7f9388b
refactor(pds): move helpers into util
zymooll 49293e2
chore(pds): remove local readme from tracked tree
zymooll 0f9038b
feat(pds): support thumbnails
zymooll 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,214 @@ | ||
| package pds | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "io" | ||
| "net/http" | ||
| "net/url" | ||
| "strings" | ||
| "time" | ||
| ) | ||
|
|
||
| const ( | ||
| defaultClientID = "lMNVp25Sd1MfqZDQ" | ||
| apiEndpoint = "https://%s.api.aliyunfile.com" | ||
| authEndpoint = "https://%s.auth.aliyunfile.com" | ||
| ) | ||
|
|
||
| type client struct { | ||
| addition *Addition | ||
| http *http.Client | ||
| onSave func() | ||
| } | ||
|
|
||
| func newClient(addition *Addition, onSave func()) *client { | ||
| if addition.ClientID == "" { | ||
| addition.ClientID = defaultClientID | ||
| } | ||
| if addition.TokenType == "" { | ||
| addition.TokenType = "Bearer" | ||
| } | ||
| return &client{ | ||
| addition: addition, | ||
| http: &http.Client{Timeout: 5 * time.Minute}, | ||
| onSave: onSave, | ||
| } | ||
| } | ||
|
|
||
| func (c *client) apiURL(path string) string { | ||
| return fmt.Sprintf(apiEndpoint, c.addition.DomainID) + path | ||
| } | ||
|
|
||
| func (c *client) authURL(path string) string { | ||
| return fmt.Sprintf(authEndpoint, c.addition.DomainID) + path | ||
| } | ||
|
|
||
| func (c *client) refreshToken(ctx context.Context) error { | ||
| if c.addition.RefreshToken == "" { | ||
| return nil | ||
| } | ||
| form := url.Values{} | ||
| form.Set("grant_type", "refresh_token") | ||
| form.Set("refresh_token", c.addition.RefreshToken) | ||
| form.Set("client_id", c.addition.ClientID) | ||
|
|
||
| req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.authURL("/v2/oauth/token"), bytes.NewBufferString(form.Encode())) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| req.Header.Set("Content-Type", "application/x-www-form-urlencoded") | ||
|
|
||
| resp, err := c.http.Do(req) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer resp.Body.Close() | ||
|
|
||
| data, err := io.ReadAll(resp.Body) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if resp.StatusCode >= 400 { | ||
| return fmt.Errorf("refresh token failed: %s: %s", resp.Status, string(data)) | ||
| } | ||
|
|
||
| var token struct { | ||
| AccessToken string `json:"access_token"` | ||
| TokenType string `json:"token_type"` | ||
| ExpiresIn int64 `json:"expires_in"` | ||
| RefreshToken string `json:"refresh_token"` | ||
| } | ||
| if err := json.Unmarshal(data, &token); err != nil { | ||
| return err | ||
| } | ||
| if token.AccessToken == "" { | ||
| return fmt.Errorf("refresh token failed: access_token is empty") | ||
| } | ||
| c.addition.AccessToken = token.AccessToken | ||
| if token.TokenType != "" { | ||
| c.addition.TokenType = token.TokenType | ||
| } | ||
| if token.RefreshToken != "" { | ||
| c.addition.RefreshToken = token.RefreshToken | ||
| } | ||
| c.addition.ExpiresAt = 0 | ||
| if c.onSave != nil { | ||
| c.onSave() | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (c *client) ensureToken(ctx context.Context) error { | ||
| if c.addition.RefreshToken == "" { | ||
| return nil | ||
| } | ||
| if c.addition.AccessToken == "" { | ||
| return c.refreshToken(ctx) | ||
| } | ||
| if c.addition.ExpiresAt > 0 && time.Now().Unix() >= c.addition.ExpiresAt-300 { | ||
| return c.refreshToken(ctx) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (c *client) post(ctx context.Context, path string, body any, out any) error { | ||
| if err := c.ensureToken(ctx); err != nil { | ||
| return err | ||
| } | ||
| payload, err := json.Marshal(body) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| data, statusCode, status, err := c.postPayload(ctx, path, payload) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if statusCode >= 400 && isAccessTokenExpiredError(statusCode, data) && c.addition.RefreshToken != "" { | ||
| if err := c.refreshToken(ctx); err != nil { | ||
| return err | ||
| } | ||
| data, statusCode, status, err = c.postPayload(ctx, path, payload) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } | ||
| if statusCode >= 400 { | ||
| return fmt.Errorf("pds api %s failed: %s: %s", path, status, string(data)) | ||
| } | ||
| if out == nil || len(data) == 0 { | ||
| return nil | ||
| } | ||
| return json.Unmarshal(data, out) | ||
| } | ||
|
|
||
| func (c *client) postPayload(ctx context.Context, path string, payload []byte) ([]byte, int, string, error) { | ||
| req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.apiURL(path), bytes.NewReader(payload)) | ||
| if err != nil { | ||
| return nil, 0, "", err | ||
| } | ||
| req.Header.Set("Authorization", c.addition.TokenType+" "+c.addition.AccessToken) | ||
| req.Header.Set("Content-Type", "application/json") | ||
|
|
||
| resp, err := c.http.Do(req) | ||
| if err != nil { | ||
| return nil, 0, "", err | ||
| } | ||
| defer resp.Body.Close() | ||
|
|
||
| data, err := io.ReadAll(resp.Body) | ||
| if err != nil { | ||
| return nil, 0, "", err | ||
| } | ||
| return data, resp.StatusCode, resp.Status, nil | ||
| } | ||
|
|
||
| func isAccessTokenExpiredError(statusCode int, data []byte) bool { | ||
| if statusCode < http.StatusBadRequest { | ||
| return false | ||
| } | ||
| var apiErr struct { | ||
| Code string `json:"code"` | ||
| Message string `json:"message"` | ||
| Error string `json:"error"` | ||
| } | ||
| text := string(data) | ||
| if len(data) > 0 && json.Unmarshal(data, &apiErr) == nil { | ||
| text = apiErr.Code + " " + apiErr.Message + " " + apiErr.Error | ||
| } | ||
| text = strings.ToLower(text) | ||
| for _, marker := range []string{ | ||
| "accesstokenexpired", | ||
| "access token expired", | ||
| "accesstokeninvalid", | ||
| "access token invalid", | ||
| "invalidaccesstoken", | ||
| "invalid access token", | ||
| "token expired", | ||
| "expiredtoken", | ||
| } { | ||
| if strings.Contains(text, marker) { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| func (c *client) putRaw(ctx context.Context, uploadURL string, r io.Reader) error { | ||
| req, err := http.NewRequestWithContext(ctx, http.MethodPut, uploadURL, r) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| resp, err := c.http.Do(req) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| defer resp.Body.Close() | ||
| if resp.StatusCode >= 400 { | ||
| data, _ := io.ReadAll(resp.Body) | ||
| return fmt.Errorf("pds upload failed: %s: %s", resp.Status, string(data)) | ||
| } | ||
| return nil | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里不应该内置吧