Merge pull request #1143 from jigargandhi/content-type

Added content type to SetRequest struct
This commit is contained in:
Long Dai 2021-10-27 10:58:30 +08:00 committed by GitHub
commit e373b95489
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 55 additions and 27 deletions

View File

@ -228,7 +228,38 @@ func (r *StateStore) writeFile(req *state.SetRequest) error {
blobURL := r.containerURL.NewBlockBlobURL(getFileName(req.Key))
// this is for backward compatibility where it might have come from http request
blobHTTPHeaders, err := createBlobHTTPHeadersFromRequest(req)
if err != nil {
return err
}
_, err = azblob.UploadBufferToBlockBlob(context.Background(), r.marshal(req), blobURL, azblob.UploadToBlockBlobOptions{
Parallelism: 16,
Metadata: req.Metadata,
AccessConditions: accessConditions,
BlobHTTPHeaders: blobHTTPHeaders,
})
if err != nil {
r.logger.Debugf("write file %s, err %s", req.Key, err)
if req.ETag != nil {
return state.NewETagError(state.ETagMismatch, err)
}
return err
}
return nil
}
func createBlobHTTPHeadersFromRequest(req *state.SetRequest) (azblob.BlobHTTPHeaders, error) {
var blobHTTPHeaders azblob.BlobHTTPHeaders
if req.ContentType != "" {
blobHTTPHeaders.ContentType = req.ContentType
}
if val, ok := req.Metadata[contentType]; ok && val != "" {
blobHTTPHeaders.ContentType = val
delete(req.Metadata, contentType)
@ -236,7 +267,7 @@ func (r *StateStore) writeFile(req *state.SetRequest) error {
if val, ok := req.Metadata[contentMD5]; ok && val != "" {
sDec, err := b64.StdEncoding.DecodeString(val)
if err != nil || len(sDec) != 16 {
return fmt.Errorf("the MD5 value specified in Content MD5 is invalid, MD5 value must be 128 bits and base64 encoded")
return azblob.BlobHTTPHeaders{}, fmt.Errorf("the MD5 value specified in Content MD5 is invalid, MD5 value must be 128 bits and base64 encoded")
}
blobHTTPHeaders.ContentMD5 = sDec
delete(req.Metadata, contentMD5)
@ -257,24 +288,7 @@ func (r *StateStore) writeFile(req *state.SetRequest) error {
blobHTTPHeaders.CacheControl = val
delete(req.Metadata, cacheControl)
}
_, err := azblob.UploadBufferToBlockBlob(context.Background(), r.marshal(req), blobURL, azblob.UploadToBlockBlobOptions{
Parallelism: 16,
Metadata: req.Metadata,
AccessConditions: accessConditions,
BlobHTTPHeaders: blobHTTPHeaders,
})
if err != nil {
r.logger.Debugf("write file %s, err %s", req.Key, err)
if req.ETag != nil {
return state.NewETagError(state.ETagMismatch, err)
}
return err
}
return nil
return blobHTTPHeaders, nil
}
func (r *StateStore) deleteFile(req *state.DeleteRequest) error {

View File

@ -71,3 +71,15 @@ func TestFileName(t *testing.T) {
assert.Equal(t, "key", key)
})
}
func TestBlobHTTPHeaderGeneration(t *testing.T) {
t.Run("Content type is set from request", func(t *testing.T) {
req := &state.SetRequest{
ContentType: "application/json",
}
blobHeaders, err := createBlobHTTPHeadersFromRequest(req)
assert.Nil(t, err)
assert.Equal(t, "application/json", blobHeaders.ContentType)
})
}

View File

@ -45,11 +45,12 @@ type DeleteStateOption struct {
// SetRequest is the object describing an upsert request.
type SetRequest struct {
Key string `json:"key"`
Value interface{} `json:"value"`
ETag *string `json:"etag,omitempty"`
Metadata map[string]string `json:"metadata,omitempty"`
Options SetStateOption `json:"options,omitempty"`
Key string `json:"key"`
Value interface{} `json:"value"`
ETag *string `json:"etag,omitempty"`
Metadata map[string]string `json:"metadata,omitempty"`
Options SetStateOption `json:"options,omitempty"`
ContentType string `json:"contentType,omitempty"`
}
// GetKey gets the Key on a SetRequest.

View File

@ -7,9 +7,10 @@ package state
// GetResponse is the response object for getting state.
type GetResponse struct {
Data []byte `json:"data"`
ETag *string `json:"etag,omitempty"`
Metadata map[string]string `json:"metadata"`
Data []byte `json:"data"`
ETag *string `json:"etag,omitempty"`
Metadata map[string]string `json:"metadata"`
ContentType string `json:"contentType,omitempty"`
}
// BulkGetResponse is the response object for bulk get response.