|
|
|
@@ -1,6 +1,7 @@ |
|
|
|
package cdsapi |
|
|
|
|
|
|
|
import ( |
|
|
|
"context" |
|
|
|
"fmt" |
|
|
|
"io" |
|
|
|
"mime" |
|
|
|
@@ -9,6 +10,8 @@ import ( |
|
|
|
"strings" |
|
|
|
"time" |
|
|
|
|
|
|
|
v4 "github.com/aws/aws-sdk-go-v2/aws/signer/v4" |
|
|
|
"github.com/aws/aws-sdk-go-v2/credentials" |
|
|
|
"gitlink.org.cn/cloudream/common/consts/errorcode" |
|
|
|
"gitlink.org.cn/cloudream/common/pkgs/iterator" |
|
|
|
"gitlink.org.cn/cloudream/common/sdks" |
|
|
|
@@ -115,16 +118,15 @@ func (c *ObjectService) Upload(req ObjectUpload) (*ObjectUploadResp, error) { |
|
|
|
return nil, fmt.Errorf("upload info to json: %w", err) |
|
|
|
} |
|
|
|
|
|
|
|
resp, err := http2.PostMultiPart(url, http2.MultiPartRequestParam{ |
|
|
|
Form: map[string]string{"info": string(infoJSON)}, |
|
|
|
Files: iterator.Map(req.Files, func(src *UploadingObject) (*http2.IterMultiPartFile, error) { |
|
|
|
resp, err := PostMultiPart(c.cfg, url, |
|
|
|
map[string]string{"info": string(infoJSON)}, |
|
|
|
iterator.Map(req.Files, func(src *UploadingObject) (*http2.IterMultiPartFile, error) { |
|
|
|
return &http2.IterMultiPartFile{ |
|
|
|
FieldName: "files", |
|
|
|
FileName: src.Path, |
|
|
|
File: src.File, |
|
|
|
}, nil |
|
|
|
}), |
|
|
|
}) |
|
|
|
})) |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
@@ -151,25 +153,42 @@ func (c *ObjectService) Upload(req ObjectUpload) (*ObjectUploadResp, error) { |
|
|
|
const ObjectDownloadPath = "/object/download" |
|
|
|
|
|
|
|
type ObjectDownload struct { |
|
|
|
UserID cdssdk.UserID `form:"userID" json:"userID" binding:"required"` |
|
|
|
ObjectID cdssdk.ObjectID `form:"objectID" json:"objectID" binding:"required"` |
|
|
|
Offset int64 `form:"offset" json:"offset,omitempty"` |
|
|
|
Length *int64 `form:"length" json:"length,omitempty"` |
|
|
|
UserID cdssdk.UserID `form:"userID" url:"userID" binding:"required"` |
|
|
|
ObjectID cdssdk.ObjectID `form:"objectID" url:"objectID" binding:"required"` |
|
|
|
Offset int64 `form:"offset" url:"offset,omitempty"` |
|
|
|
Length *int64 `form:"length" url:"length,omitempty"` |
|
|
|
} |
|
|
|
|
|
|
|
func (r *ObjectDownload) MakeParam() *sdks.RequestParam { |
|
|
|
return sdks.MakeQueryParam(http.MethodGet, ObjectDownloadPath, r) |
|
|
|
} |
|
|
|
|
|
|
|
type DownloadingObject struct { |
|
|
|
Path string |
|
|
|
File io.ReadCloser |
|
|
|
} |
|
|
|
|
|
|
|
func (c *ObjectService) Download(req ObjectDownload) (*DownloadingObject, error) { |
|
|
|
url, err := url.JoinPath(c.cfg.URL, ObjectDownloadPath) |
|
|
|
httpReq, err := req.MakeParam().MakeRequest(c.cfg.URL) |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
|
|
|
|
resp, err := http2.GetJSON(url, http2.RequestParam{ |
|
|
|
Query: req, |
|
|
|
}) |
|
|
|
if c.cfg.AccessKey != "" && c.cfg.SecretKey != "" { |
|
|
|
prod := credentials.NewStaticCredentialsProvider(c.cfg.AccessKey, c.cfg.SecretKey, "") |
|
|
|
cred, err := prod.Retrieve(context.TODO()) |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
|
|
|
|
signer := v4.NewSigner() |
|
|
|
err = signer.SignHTTP(context.Background(), cred, httpReq, "", AuthService, AuthRegion, time.Now()) |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
resp, err := http.DefaultClient.Do(httpReq) |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
@@ -199,22 +218,38 @@ func (c *ObjectService) Download(req ObjectDownload) (*DownloadingObject, error) |
|
|
|
const ObjectDownloadByPathPath = "/object/downloadByPath" |
|
|
|
|
|
|
|
type ObjectDownloadByPath struct { |
|
|
|
UserID cdssdk.UserID `form:"userID" json:"userID" binding:"required"` |
|
|
|
PackageID cdssdk.PackageID `form:"packageID" json:"packageID" binding:"required"` |
|
|
|
Path string `form:"path" json:"path" binding:"required"` |
|
|
|
Offset int64 `form:"offset" json:"offset,omitempty"` |
|
|
|
Length *int64 `form:"length" json:"length,omitempty"` |
|
|
|
UserID cdssdk.UserID `form:"userID" url:"userID" binding:"required"` |
|
|
|
PackageID cdssdk.PackageID `form:"packageID" url:"packageID" binding:"required"` |
|
|
|
Path string `form:"path" url:"path" binding:"required"` |
|
|
|
Offset int64 `form:"offset" url:"offset,omitempty"` |
|
|
|
Length *int64 `form:"length" url:"length,omitempty"` |
|
|
|
} |
|
|
|
|
|
|
|
func (r *ObjectDownloadByPath) MakeParam() *sdks.RequestParam { |
|
|
|
return sdks.MakeQueryParam(http.MethodGet, ObjectDownloadByPathPath, r) |
|
|
|
} |
|
|
|
|
|
|
|
func (c *ObjectService) DownloadByPath(req ObjectDownloadByPath) (*DownloadingObject, error) { |
|
|
|
url, err := url.JoinPath(c.cfg.URL, ObjectDownloadByPathPath) |
|
|
|
httpReq, err := req.MakeParam().MakeRequest(c.cfg.URL) |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
|
|
|
|
resp, err := http2.GetJSON(url, http2.RequestParam{ |
|
|
|
Query: req, |
|
|
|
}) |
|
|
|
if c.cfg.AccessKey != "" && c.cfg.SecretKey != "" { |
|
|
|
prod := credentials.NewStaticCredentialsProvider(c.cfg.AccessKey, c.cfg.SecretKey, "") |
|
|
|
cred, err := prod.Retrieve(context.TODO()) |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
|
|
|
|
signer := v4.NewSigner() |
|
|
|
err = signer.SignHTTP(context.Background(), cred, httpReq, "", AuthService, AuthRegion, time.Now()) |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
resp, err := http.DefaultClient.Do(httpReq) |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
|