Enable overwrite for non-first chunk uploading request
This commit is contained in:
@@ -1,11 +1,14 @@
|
||||
package cluster
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
model "github.com/cloudreve/Cloudreve/v3/models"
|
||||
"github.com/cloudreve/Cloudreve/v3/pkg/aria2/common"
|
||||
"github.com/cloudreve/Cloudreve/v3/pkg/aria2/rpc"
|
||||
"github.com/cloudreve/Cloudreve/v3/pkg/auth"
|
||||
"github.com/cloudreve/Cloudreve/v3/pkg/conf"
|
||||
"github.com/cloudreve/Cloudreve/v3/pkg/request"
|
||||
"github.com/cloudreve/Cloudreve/v3/pkg/serializer"
|
||||
"github.com/cloudreve/Cloudreve/v3/pkg/util"
|
||||
@@ -408,3 +411,43 @@ func getAria2RequestBody(body *serializer.SlaveAria2Call) (io.Reader, error) {
|
||||
|
||||
return strings.NewReader(string(reqBodyEncoded)), nil
|
||||
}
|
||||
|
||||
// TODO: move to slave pkg
|
||||
// RemoteCallback 发送远程存储策略上传回调请求
|
||||
func RemoteCallback(url string, body serializer.UploadCallback) error {
|
||||
callbackBody, err := json.Marshal(struct {
|
||||
Data serializer.UploadCallback `json:"data"`
|
||||
}{
|
||||
Data: body,
|
||||
})
|
||||
if err != nil {
|
||||
return serializer.NewError(serializer.CodeCallbackError, "无法编码回调正文", err)
|
||||
}
|
||||
|
||||
resp := request.GeneralClient.Request(
|
||||
"POST",
|
||||
url,
|
||||
bytes.NewReader(callbackBody),
|
||||
request.WithTimeout(time.Duration(conf.SlaveConfig.CallbackTimeout)*time.Second),
|
||||
request.WithCredential(auth.General, int64(conf.SlaveConfig.SignatureTTL)),
|
||||
)
|
||||
|
||||
if resp.Err != nil {
|
||||
return serializer.NewError(serializer.CodeCallbackError, "从机无法发起回调请求", resp.Err)
|
||||
}
|
||||
|
||||
// 解析回调服务端响应
|
||||
resp = resp.CheckHTTPResponse(200)
|
||||
if resp.Err != nil {
|
||||
return serializer.NewError(serializer.CodeCallbackError, "主机服务器返回异常响应", resp.Err)
|
||||
}
|
||||
response, err := resp.DecodeResponse()
|
||||
if err != nil {
|
||||
return serializer.NewError(serializer.CodeCallbackError, "从机无法解析主机返回的响应", err)
|
||||
}
|
||||
if response.Code != 0 {
|
||||
return serializer.NewError(response.Code, response.Msg, errors.New(response.Error))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -441,3 +441,125 @@ func TestSlaveCaller_DeleteTempFile(t *testing.T) {
|
||||
a.NoError(err)
|
||||
}
|
||||
}
|
||||
|
||||
//func TestRemoteCallback(t *testing.T) {
|
||||
// asserts := assert.New(t)
|
||||
//
|
||||
// // 回调成功
|
||||
// {
|
||||
// clientMock := request.ClientMock{}
|
||||
// mockResp, _ := json.Marshal(serializer.Response{Code: 0})
|
||||
// clientMock.On(
|
||||
// "Request",
|
||||
// "POST",
|
||||
// "http://test/test/url",
|
||||
// testMock.Anything,
|
||||
// testMock.Anything,
|
||||
// ).Return(&request.Response{
|
||||
// Err: nil,
|
||||
// Response: &http.Response{
|
||||
// StatusCode: 200,
|
||||
// Body: ioutil.NopCloser(bytes.NewReader(mockResp)),
|
||||
// },
|
||||
// })
|
||||
// request.GeneralClient = clientMock
|
||||
// resp := RemoteCallback("http://test/test/url", serializer.UploadCallback{
|
||||
// SourceName: "source",
|
||||
// })
|
||||
// asserts.NoError(resp)
|
||||
// clientMock.AssertExpectations(t)
|
||||
// }
|
||||
//
|
||||
// // 服务端返回业务错误
|
||||
// {
|
||||
// clientMock := request.ClientMock{}
|
||||
// mockResp, _ := json.Marshal(serializer.Response{Code: 401})
|
||||
// clientMock.On(
|
||||
// "Request",
|
||||
// "POST",
|
||||
// "http://test/test/url",
|
||||
// testMock.Anything,
|
||||
// testMock.Anything,
|
||||
// ).Return(&request.Response{
|
||||
// Err: nil,
|
||||
// Response: &http.Response{
|
||||
// StatusCode: 200,
|
||||
// Body: ioutil.NopCloser(bytes.NewReader(mockResp)),
|
||||
// },
|
||||
// })
|
||||
// request.GeneralClient = clientMock
|
||||
// resp := RemoteCallback("http://test/test/url", serializer.UploadCallback{
|
||||
// SourceName: "source",
|
||||
// })
|
||||
// asserts.EqualValues(401, resp.(serializer.AppError).Code)
|
||||
// clientMock.AssertExpectations(t)
|
||||
// }
|
||||
//
|
||||
// // 无法解析回调响应
|
||||
// {
|
||||
// clientMock := request.ClientMock{}
|
||||
// clientMock.On(
|
||||
// "Request",
|
||||
// "POST",
|
||||
// "http://test/test/url",
|
||||
// testMock.Anything,
|
||||
// testMock.Anything,
|
||||
// ).Return(&request.Response{
|
||||
// Err: nil,
|
||||
// Response: &http.Response{
|
||||
// StatusCode: 200,
|
||||
// Body: ioutil.NopCloser(strings.NewReader("mockResp")),
|
||||
// },
|
||||
// })
|
||||
// request.GeneralClient = clientMock
|
||||
// resp := RemoteCallback("http://test/test/url", serializer.UploadCallback{
|
||||
// SourceName: "source",
|
||||
// })
|
||||
// asserts.Error(resp)
|
||||
// clientMock.AssertExpectations(t)
|
||||
// }
|
||||
//
|
||||
// // HTTP状态码非200
|
||||
// {
|
||||
// clientMock := request.ClientMock{}
|
||||
// clientMock.On(
|
||||
// "Request",
|
||||
// "POST",
|
||||
// "http://test/test/url",
|
||||
// testMock.Anything,
|
||||
// testMock.Anything,
|
||||
// ).Return(&request.Response{
|
||||
// Err: nil,
|
||||
// Response: &http.Response{
|
||||
// StatusCode: 404,
|
||||
// Body: ioutil.NopCloser(strings.NewReader("mockResp")),
|
||||
// },
|
||||
// })
|
||||
// request.GeneralClient = clientMock
|
||||
// resp := RemoteCallback("http://test/test/url", serializer.UploadCallback{
|
||||
// SourceName: "source",
|
||||
// })
|
||||
// asserts.Error(resp)
|
||||
// clientMock.AssertExpectations(t)
|
||||
// }
|
||||
//
|
||||
// // 无法发起回调
|
||||
// {
|
||||
// clientMock := request.ClientMock{}
|
||||
// clientMock.On(
|
||||
// "Request",
|
||||
// "POST",
|
||||
// "http://test/test/url",
|
||||
// testMock.Anything,
|
||||
// testMock.Anything,
|
||||
// ).Return(&request.Response{
|
||||
// Err: errors.New("error"),
|
||||
// })
|
||||
// request.GeneralClient = clientMock
|
||||
// resp := RemoteCallback("http://test/test/url", serializer.UploadCallback{
|
||||
// SourceName: "source",
|
||||
// })
|
||||
// asserts.Error(resp)
|
||||
// clientMock.AssertExpectations(t)
|
||||
// }
|
||||
//}
|
||||
|
||||
Reference in New Issue
Block a user