Skip to content

Commit c68284a

Browse files
authored
Expose Upsert/Content-Type/Cache opts (nedpals#42)
1 parent 218ba1e commit c68284a

File tree

1 file changed

+31
-8
lines changed

1 file changed

+31
-8
lines changed

storage.go

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,30 @@ const (
248248
defaultSortOrder = "asc"
249249
)
250250

251-
func (f *file) UploadOrUpdate(path string, data io.Reader, update bool) FileResponse {
251+
type FileUploadOptions struct {
252+
CacheControl string
253+
ContentType string
254+
Upsert bool
255+
}
256+
257+
func (f *file) UploadOrUpdate(path string, data io.Reader, update bool, opts *FileUploadOptions) FileResponse {
258+
// use default options, then override with whatever is passed in opts
259+
mergedOpts := FileUploadOptions{
260+
CacheControl: defaultFileCacheControl,
261+
ContentType: defaultFileContent,
262+
Upsert: defaultFileUpsert,
263+
}
264+
265+
if opts != nil {
266+
if opts.CacheControl != "" {
267+
mergedOpts.CacheControl = opts.CacheControl
268+
}
269+
if opts.ContentType != "" {
270+
mergedOpts.ContentType = opts.ContentType
271+
}
272+
mergedOpts.Upsert = opts.Upsert
273+
}
274+
252275
body := bufio.NewReader(data)
253276
_path := removeEmptyFolder(f.BucketId + "/" + path)
254277
client := &http.Client{}
@@ -273,9 +296,9 @@ func (f *file) UploadOrUpdate(path string, data io.Reader, update bool) FileResp
273296
}
274297

275298
injectAuthorizationHeader(req, f.storage.client.apiKey)
276-
req.Header.Set("cache-control", defaultFileCacheControl)
277-
req.Header.Set("content-type", defaultFileContent)
278-
req.Header.Set("x-upsert", strconv.FormatBool(defaultFileUpsert))
299+
req.Header.Set("cache-control", mergedOpts.CacheControl)
300+
req.Header.Set("content-type", mergedOpts.ContentType)
301+
req.Header.Set("x-upsert", strconv.FormatBool(mergedOpts.Upsert))
279302
if !update {
280303
req.Header.Set("content-type", defaultFileContent)
281304
}
@@ -299,13 +322,13 @@ func (f *file) UploadOrUpdate(path string, data io.Reader, update bool) FileResp
299322
}
300323

301324
// Update updates a file object in a storage bucket
302-
func (f *file) Update(path string, data io.Reader) FileResponse {
303-
return f.UploadOrUpdate(path, data, true)
325+
func (f *file) Update(path string, data io.Reader, opts *FileUploadOptions) FileResponse {
326+
return f.UploadOrUpdate(path, data, true, opts)
304327
}
305328

306329
// Upload uploads a file object to a storage bucket
307-
func (f *file) Upload(path string, data io.Reader) FileResponse {
308-
return f.UploadOrUpdate(path, data, false)
330+
func (f *file) Upload(path string, data io.Reader, opts *FileUploadOptions) FileResponse {
331+
return f.UploadOrUpdate(path, data, false, opts)
309332
}
310333

311334
// Move moves a file object

0 commit comments

Comments
 (0)