Skip to content

Commit

Permalink
Support setting cache-control of object (#1730)
Browse files Browse the repository at this point in the history
* Update .gitignore

* Support to set object's CacheControl

* Revert "Update .gitignore"

This reverts commit 35d70c3.

* fakestorage: set `Cache-Control` in the `downloadObject` endpoint

---------

Co-authored-by: francisco souza <[email protected]>
  • Loading branch information
j1apeng and fsouza authored Sep 22, 2024
1 parent c523dad commit 88d8d3d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
7 changes: 7 additions & 0 deletions fakestorage/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type ObjectAttrs struct {
ContentType string
ContentEncoding string
ContentDisposition string
CacheControl string
// Crc32c checksum of Content. calculated by server when it's upload methods are used.
Crc32c string
Md5Hash string
Expand Down Expand Up @@ -396,6 +397,7 @@ func toBackendObjects(objects []StreamingObject) []backend.StreamingObject {
ContentType: o.ContentType,
ContentEncoding: o.ContentEncoding,
ContentDisposition: o.ContentDisposition,
CacheControl: o.CacheControl,
ACL: o.ACL,
Created: getCurrentIfZero(o.Created).Format(timestampFormat),
Deleted: o.Deleted.Format(timestampFormat),
Expand Down Expand Up @@ -450,6 +452,7 @@ func fromBackendObjects(objects []backend.StreamingObject) []StreamingObject {
ContentType: o.ContentType,
ContentEncoding: o.ContentEncoding,
ContentDisposition: o.ContentDisposition,
CacheControl: o.CacheControl,
Crc32c: o.Crc32c,
Md5Hash: o.Md5Hash,
Etag: o.Etag,
Expand Down Expand Up @@ -477,6 +480,7 @@ func fromBackendObjectsAttrs(objectAttrs []backend.ObjectAttrs) []ObjectAttrs {
ContentType: o.ContentType,
ContentEncoding: o.ContentEncoding,
ContentDisposition: o.ContentDisposition,
CacheControl: o.CacheControl,
Crc32c: o.Crc32c,
Md5Hash: o.Md5Hash,
Etag: o.Etag,
Expand Down Expand Up @@ -898,6 +902,9 @@ func (s *Server) downloadObject(w http.ResponseWriter, r *http.Request) {
if obj.ContentType != "" {
w.Header().Set(contentTypeHeader, obj.ContentType)
}
if obj.CacheControl != "" {
w.Header().Set(cacheControlHeader, obj.CacheControl)
}
// If content was transcoded, the underlying encoding was removed so we shouldn't report it.
if obj.ContentEncoding != "" && !transcoded {
w.Header().Set("Content-Encoding", obj.ContentEncoding)
Expand Down
9 changes: 8 additions & 1 deletion fakestorage/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ import (
"github.com/gorilla/mux"
)

const contentTypeHeader = "Content-Type"
const (
contentTypeHeader = "Content-Type"
cacheControlHeader = "Cache-Control"
)

const (
uploadTypeMedia = "media"
Expand All @@ -51,6 +54,7 @@ type multipartMetadata struct {
ContentType string `json:"contentType"`
ContentEncoding string `json:"contentEncoding"`
ContentDisposition string `json:"contentDisposition"`
CacheControl string `json:"cacheControl"`
CustomTime time.Time `json:"customTime,omitempty"`
Name string `json:"name"`
Metadata map[string]string `json:"metadata"`
Expand Down Expand Up @@ -239,6 +243,7 @@ func (s *Server) simpleUpload(bucketName string, r *http.Request) jsonResponse {
BucketName: bucketName,
Name: name,
ContentType: r.Header.Get(contentTypeHeader),
CacheControl: r.Header.Get(cacheControlHeader),
ContentEncoding: contentEncoding,
CustomTime: convertTimeWithoutError(customTime),
ACL: getObjectACL(predefinedACL),
Expand Down Expand Up @@ -374,6 +379,7 @@ func (s *Server) multipartUpload(bucketName string, r *http.Request) jsonRespons
BucketName: bucketName,
Name: objName,
ContentType: contentType,
CacheControl: metadata.CacheControl,
ContentEncoding: metadata.ContentEncoding,
ContentDisposition: metadata.ContentDisposition,
CustomTime: metadata.CustomTime,
Expand Down Expand Up @@ -423,6 +429,7 @@ func (s *Server) resumableUpload(bucketName string, r *http.Request) jsonRespons
BucketName: bucketName,
Name: objName,
ContentType: metadata.ContentType,
CacheControl: metadata.CacheControl,
ContentEncoding: contentEncoding,
CustomTime: metadata.CustomTime,
ACL: getObjectACL(predefinedACL),
Expand Down
10 changes: 10 additions & 0 deletions fakestorage/upload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func TestServerClientObjectWriter(t *testing.T) {
test := test
t.Run(test.testCase, func(t *testing.T) {
const contentType = "text/plain; charset=utf-8"
cacheControl := "public, max-age=3600"
server.CreateBucketWithOpts(CreateBucketOpts{Name: test.bucketName})
client := server.Client()

Expand All @@ -82,6 +83,7 @@ func TestServerClientObjectWriter(t *testing.T) {
w.Metadata = map[string]string{
"foo": "bar",
}
w.CacheControl = cacheControl
w.Write([]byte(content))
err := w.Close()
if err != nil {
Expand Down Expand Up @@ -123,6 +125,9 @@ func TestServerClientObjectWriter(t *testing.T) {
if !customTime.Equal(obj.CustomTime) {
t.Errorf("wrong custom time\nwant %q\ngot %q", customTime.String(), obj.CustomTime.String())
}
if obj.CacheControl != cacheControl {
t.Errorf("wrong cache control\nwant %q\ngot %q", cacheControl, obj.CacheControl)
}

reader, err := client.Bucket(test.bucketName).Object(test.objectName).NewReader(context.Background())
if err != nil {
Expand Down Expand Up @@ -154,6 +159,7 @@ func TestServerClientObjectWriterOverwrite(t *testing.T) {
runServersTest(t, runServersOptions{}, func(t *testing.T, server *Server) {
const content = "other content"
const contentType = "text/plain"
const cacheControl = "no-cache"
server.CreateObject(Object{
ObjectAttrs: ObjectAttrs{
BucketName: "some-bucket",
Expand All @@ -165,6 +171,7 @@ func TestServerClientObjectWriterOverwrite(t *testing.T) {
objHandle := server.Client().Bucket("some-bucket").Object("some-object.txt")
w := objHandle.NewWriter(context.Background())
w.ContentType = contentType
w.CacheControl = cacheControl
w.Write([]byte(content))
err := w.Close()
if err != nil {
Expand All @@ -181,6 +188,9 @@ func TestServerClientObjectWriterOverwrite(t *testing.T) {
if obj.ContentType != contentType {
t.Errorf("wrong content-type\nwsant %q\ngot %q", contentType, obj.ContentType)
}
if obj.CacheControl != cacheControl {
t.Errorf("wrong cache control\nwant %q\ngot %q", cacheControl, obj.CacheControl)
}
})
}

Expand Down
1 change: 1 addition & 0 deletions internal/backend/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type ObjectAttrs struct {
ContentType string
ContentEncoding string
ContentDisposition string
CacheControl string
Crc32c string
Md5Hash string
Etag string
Expand Down

0 comments on commit 88d8d3d

Please sign in to comment.