|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- package api
-
- import (
- "net/http"
- "net/url"
- "time"
-
- "github.com/google/go-querystring/query"
- "gitlink.org.cn/cloudream/jcs-pub/client/sdk/signer"
- jcstypes "gitlink.org.cn/cloudream/jcs-pub/common/types"
- )
-
- type PresignedService struct {
- *Client
- }
-
- func (c *Client) Presigned() *PresignedService {
- return &PresignedService{
- Client: c,
- }
- }
-
- const PresignedObjectListByPathPath = "/presigned/object/listByPath"
-
- type PresignedObjectListByPath struct {
- ObjectListByPath
- }
-
- func (c *PresignedService) ObjectListByPath(req PresignedObjectListByPath, expireIn int) (string, error) {
- return c.presign(req, PresignedObjectListByPathPath, http.MethodGet, expireIn)
- }
-
- const PresignedObjectDownloadByPathPath = "/presigned/object/downloadByPath"
-
- type PresignedObjectDownloadByPath struct {
- PackageID jcstypes.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 (c *PresignedService) ObjectDownloadByPath(req PresignedObjectDownloadByPath, expireIn int) (string, error) {
- return c.presign(req, PresignedObjectDownloadByPathPath, http.MethodGet, expireIn)
- }
-
- const PresignedObjectDownloadPath = "/presigned/object/download"
-
- type PresignedObjectDownload struct {
- ObjectID jcstypes.ObjectID `form:"objectID" url:"objectID" binding:"required"`
- Offset int64 `form:"offset" url:"offset,omitempty"`
- Length *int64 `form:"length" url:"length,omitempty"`
- }
-
- func (c *PresignedService) ObjectDownload(req PresignedObjectDownload, expireIn int) (string, error) {
- return c.presign(req, PresignedObjectDownloadPath, http.MethodGet, expireIn)
- }
-
- const PresignedObjectUploadPath = "/presigned/object/upload"
-
- type PresignedObjectUpload struct {
- PackageID jcstypes.PackageID `form:"packageID" binding:"required" url:"packageID"`
- Path string `form:"path" binding:"required" url:"path"`
- Affinity jcstypes.UserSpaceID `form:"affinity" url:"affinity,omitempty"`
- CopyTo []jcstypes.UserSpaceID `form:"copyTo" url:"copyTo,omitempty"`
- CopyToPath []string `form:"copyToPath" url:"copyToPath,omitempty"`
- }
-
- type PresignedObjectUploadResp struct {
- Object jcstypes.Object `json:"object"`
- }
-
- func (c *PresignedService) ObjectUpload(req PresignedObjectUpload, expireIn int) (string, error) {
- return c.presign(req, PresignedObjectUploadPath, http.MethodPost, expireIn)
- }
-
- const PresignedObjectNewMultipartUploadPath = "/presigned/object/newMultipartUpload"
-
- type PresignedObjectNewMultipartUpload struct {
- PackageID jcstypes.PackageID `form:"packageID" binding:"required" url:"packageID"`
- Path string `form:"path" binding:"required" url:"path"`
- }
-
- type PresignedObjectNewMultipartUploadResp struct {
- Object jcstypes.Object `json:"object"`
- }
-
- func (c *PresignedService) ObjectNewMultipartUpload(req PresignedObjectNewMultipartUpload, expireIn int) (string, error) {
- return c.presign(req, PresignedObjectNewMultipartUploadPath, http.MethodPost, expireIn)
- }
-
- const PresignedObjectUploadPartPath = "/presigned/object/uploadPart"
-
- type PresignedObjectUploadPart struct {
- ObjectID jcstypes.ObjectID `form:"objectID" binding:"required" url:"objectID"`
- Index int `form:"index" binding:"required" url:"index"`
- }
-
- type PresignedUploadPartResp struct{}
-
- func (c *PresignedService) ObjectUploadPart(req PresignedObjectUploadPart, expireIn int) (string, error) {
- return c.presign(req, PresignedObjectUploadPartPath, http.MethodPost, expireIn)
- }
-
- const PresignedObjectCompleteMultipartUploadPath = "/presigned/object/completeMultipartUpload"
-
- type PresignedObjectCompleteMultipartUpload struct {
- ObjectID jcstypes.ObjectID `form:"objectID" binding:"required" url:"objectID"`
- Indexes []int `form:"indexes" binding:"required" url:"indexes"`
- }
-
- type PresignedObjectCompleteMultipartUploadResp struct {
- Object jcstypes.Object `json:"object"`
- }
-
- func (c *PresignedService) ObjectCompleteMultipartUpload(req PresignedObjectCompleteMultipartUpload, expireIn int) (string, error) {
- return c.presign(req, PresignedObjectCompleteMultipartUploadPath, http.MethodPost, expireIn)
- }
-
- func (c *PresignedService) presign(req any, path string, method string, expireIn int) (string, error) {
- u, err := url.Parse(c.cfg.EndPoint)
- if err != nil {
- return "", err
- }
- u = u.JoinPath("/v1", path)
-
- us, err := query.Values(req)
- if err != nil {
- return "", err
- }
-
- u.RawQuery = us.Encode()
-
- err = signer.PresignURL(c.signKey, method, u, time.Now(), expireIn)
- if err != nil {
- return "", err
- }
-
- return u.String(), nil
- }
|