forked from cloudreve/Cloudreve
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: slave policy creating upload session API
- Loading branch information
Showing
21 changed files
with
236 additions
and
106 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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,70 @@ | ||
package remote | ||
|
||
import ( | ||
"encoding/json" | ||
model "github.com/cloudreve/Cloudreve/v3/models" | ||
"github.com/cloudreve/Cloudreve/v3/pkg/auth" | ||
"github.com/cloudreve/Cloudreve/v3/pkg/request" | ||
"github.com/cloudreve/Cloudreve/v3/pkg/serializer" | ||
"net/url" | ||
"strings" | ||
) | ||
|
||
// Client to operate remote slave server | ||
type Client interface { | ||
CreateUploadSession(session *serializer.UploadSession, ttl int64) error | ||
} | ||
|
||
// NewClient creates new Client from given policy | ||
func NewClient(policy *model.Policy) (Client, error) { | ||
authInstance := auth.HMACAuth{[]byte(policy.SecretKey)} | ||
serverURL, err := url.Parse(policy.Server) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
base, _ := url.Parse("/api/v3/slave") | ||
signTTL := model.GetIntSetting("slave_api_timeout", 60) | ||
|
||
return &remoteClient{ | ||
policy: policy, | ||
authInstance: authInstance, | ||
httpClient: request.NewClient( | ||
request.WithEndpoint(serverURL.ResolveReference(base).String()), | ||
request.WithCredential(authInstance, int64(signTTL)), | ||
request.WithMasterMeta(), | ||
), | ||
}, nil | ||
} | ||
|
||
type remoteClient struct { | ||
policy *model.Policy | ||
authInstance auth.Auth | ||
httpClient request.Client | ||
} | ||
|
||
func (c *remoteClient) CreateUploadSession(session *serializer.UploadSession, ttl int64) error { | ||
reqBodyEncoded, err := json.Marshal(map[string]interface{}{ | ||
"session": session, | ||
"ttl": ttl, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
bodyReader := strings.NewReader(string(reqBodyEncoded)) | ||
resp, err := c.httpClient.Request( | ||
"PUT", | ||
"upload", | ||
bodyReader, | ||
).CheckHTTPResponse(200).DecodeResponse() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if resp.Code != 0 { | ||
return serializer.NewErrorFromResponse(resp) | ||
} | ||
|
||
return nil | ||
} |
This file contains 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 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 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 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
Oops, something went wrong.