-
Notifications
You must be signed in to change notification settings - Fork 114
/
upload_blob.go
73 lines (54 loc) · 1.77 KB
/
upload_blob.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package action
import (
"bytes"
"encoding/base64"
"errors"
boshagentblobstore "github.com/cloudfoundry/bosh-agent/agent/blobstore"
boshcrypto "github.com/cloudfoundry/bosh-utils/crypto"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
)
type UploadBlobSpec struct {
BlobID string `json:"blob_id"`
Checksum boshcrypto.MultipleDigest `json:"checksum"`
Payload string `json:"payload"`
}
type UploadBlobAction struct {
blobManager boshagentblobstore.BlobManagerInterface
}
func NewUploadBlobAction(blobManager boshagentblobstore.BlobManagerInterface) UploadBlobAction {
return UploadBlobAction{blobManager: blobManager}
}
func (a UploadBlobAction) IsAsynchronous(_ ProtocolVersion) bool {
return true
}
func (a UploadBlobAction) IsPersistent() bool {
return false
}
func (a UploadBlobAction) IsLoggable() bool {
return false
}
func (a UploadBlobAction) Run(content UploadBlobSpec) (string, error) {
decodedPayload, err := base64.StdEncoding.DecodeString(content.Payload)
if err != nil {
return content.BlobID, err
}
if err = a.validatePayload(decodedPayload, content.Checksum); err != nil {
return content.BlobID, err
}
reader := bytes.NewReader(decodedPayload)
err = a.blobManager.Write(content.BlobID, reader)
return content.BlobID, err
}
func (a UploadBlobAction) validatePayload(payload []byte, payloadDigest boshcrypto.Digest) error {
err := payloadDigest.Verify(bytes.NewReader(payload))
if err != nil {
return bosherr.WrapErrorf(err, "Payload corrupted. Checksum mismatch. Expected '%s'", payloadDigest.String())
}
return nil
}
func (a UploadBlobAction) Resume() (interface{}, error) {
return nil, errors.New("not supported")
}
func (a UploadBlobAction) Cancel() error {
return errors.New("not supported")
}