Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 38 additions & 6 deletions block.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package proton

import (
"context"
"fmt"
"io"
"net/http"

"github.com/go-resty/resty/v2"
)
Expand Down Expand Up @@ -33,10 +35,40 @@ func (c *Client) RequestBlockUpload(ctx context.Context, req BlockUploadReq) ([]
}

func (c *Client) UploadBlock(ctx context.Context, bareURL, token string, block io.Reader) error {
return c.do(ctx, func(r *resty.Request) (*resty.Response, error) {
return r.
SetHeader("pm-storage-token", token).
SetMultipartField("Block", "blob", "application/octet-stream", block).
Post(bareURL)
})
// Storage servers require only pm-storage-token, not API auth headers.
// Use a plain resty request instead of going through c.do() which adds
// x-pm-uid and Authorization headers.
req := resty.New().R().SetContext(ctx)
req.SetHeader("pm-storage-token", token)
req.SetMultipartField("Block", "blob", "application/octet-stream", block)

resp, err := req.Post(bareURL)
if err != nil {
return err
}

if resp.StatusCode() != http.StatusOK {
return fmt.Errorf("%v POST %s: %s", resp.StatusCode(), bareURL, resp.String())
}

return nil
}

func (c *Client) GetUploadVerification(ctx context.Context, volumeID, linkID, revisionID string) (UploadVerification, error) {
var res struct {
VerificationCode string
ContentKeyPacket string
}

route := fmt.Sprintf("/drive/v2/volumes/%s/links/%s/revisions/%s/verification", volumeID, linkID, revisionID)
if err := c.do(ctx, func(r *resty.Request) (*resty.Response, error) {
return r.SetResult(&res).Get(route)
}); err != nil {
return UploadVerification{}, err
}

return UploadVerification{
VerificationCode: res.VerificationCode,
ContentKeyPacket: res.ContentKeyPacket,
}, nil
}
11 changes: 11 additions & 0 deletions block_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type BlockUploadReq struct {
ShareID string
LinkID string
RevisionID string
VolumeID string

BlockList []BlockUploadInfo
}
Expand All @@ -28,6 +29,16 @@ type BlockUploadInfo struct {
Size int64
EncSignature string
Hash string
Verifier *BlockVerifier `json:"Verifier,omitempty"`
}

type BlockVerifier struct {
Token string
}

type UploadVerification struct {
VerificationCode string
ContentKeyPacket string
}

type BlockUploadLink struct {
Expand Down