Skip to content

Commit ee34e75

Browse files
committed
fix: addressing PR comments
Signed-off-by: Tom Plowman <7210552+alsenz@users.noreply.github.com>
1 parent b0814c7 commit ee34e75

3 files changed

Lines changed: 28 additions & 29 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ type Bucket interface {
6060
// Upload the contents of the reader as an object into the bucket.
6161
// Upload should be idempotent.
6262
Upload(ctx context.Context, name string, r io.Reader, options ...ObjectUploadOption) error
63-
64-
// SupportedObjectUploadOptions returns a list of ObjectUploadOptions supported by the underlying provider.
63+
64+
// SupportedObjectUploadOptions returns a list of ObjectUploadOptions supported by the underlying provider.
6565
SupportedObjectUploadOptions() []ObjectUploadOptionType
6666

6767
// Delete removes the object with the given name.

objstore.go

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,8 @@ const (
275275

276276
// ObjectUploadOption configures UploadObjectParams.
277277
type ObjectUploadOption struct {
278-
Type ObjectUploadOptionType
279-
Apply func(params *UploadObjectParams)
278+
optType ObjectUploadOptionType
279+
apply func(params *UploadObjectParams)
280280
}
281281

282282
// UploadObjectParams hold content-type and conditional write attribute metadata for upload operations that are
@@ -291,8 +291,8 @@ type UploadObjectParams struct {
291291
// WithContentType sets the content type of the object upload operation.
292292
func WithContentType(contentType string) ObjectUploadOption {
293293
return ObjectUploadOption{
294-
Type: ContentType,
295-
Apply: func(params *UploadObjectParams) {
294+
optType: ContentType,
295+
apply: func(params *UploadObjectParams) {
296296
params.ContentType = contentType
297297
},
298298
}
@@ -302,8 +302,8 @@ func WithContentType(contentType string) ObjectUploadOption {
302302
// When supported by providers this operation is usually atomic, however this is dependent on the provider.
303303
func WithIfNotExists() ObjectUploadOption {
304304
return ObjectUploadOption{
305-
Type: IfNotExists,
306-
Apply: func(params *UploadObjectParams) {
305+
optType: IfNotExists,
306+
apply: func(params *UploadObjectParams) {
307307
params.IfNotExists = true
308308
},
309309
}
@@ -313,8 +313,8 @@ func WithIfNotExists() ObjectUploadOption {
313313
// otherwise, the operation fails.
314314
func WithIfMatch(ver *ObjectVersion) ObjectUploadOption {
315315
return ObjectUploadOption{
316-
Type: IfMatch,
317-
Apply: func(params *UploadObjectParams) {
316+
optType: IfMatch,
317+
apply: func(params *UploadObjectParams) {
318318
params.Condition = ver
319319
},
320320
}
@@ -324,8 +324,8 @@ func WithIfMatch(ver *ObjectVersion) ObjectUploadOption {
324324
// otherwise, the operation fails.
325325
func WithIfNotMatch(ver *ObjectVersion) ObjectUploadOption {
326326
return ObjectUploadOption{
327-
Type: IfNotMatch,
328-
Apply: func(params *UploadObjectParams) {
327+
optType: IfNotMatch,
328+
apply: func(params *UploadObjectParams) {
329329
params.Condition = ver
330330
params.IfNotMatch = true
331331
},
@@ -334,22 +334,21 @@ func WithIfNotMatch(ver *ObjectVersion) ObjectUploadOption {
334334

335335
// ValidateUploadOptions ensures that only supported options are passed as options, and that options used simultaneously are valid.
336336
func ValidateUploadOptions(supportedOptions []ObjectUploadOptionType, opts ...ObjectUploadOption) error {
337-
338337
for _, opt := range opts {
339-
if !slices.Contains(supportedOptions, opt.Type) {
340-
return fmt.Errorf("%w: %d", ErrUploadOptionNotSupported, opt.Type)
338+
if !slices.Contains(supportedOptions, opt.optType) {
339+
return fmt.Errorf("%w: %d", ErrUploadOptionNotSupported, opt.optType)
341340
}
342-
if opt.Type == IfMatch || opt.Type == IfNotMatch {
341+
if opt.optType == IfMatch || opt.optType == IfNotMatch {
343342
candidate := &UploadObjectParams{}
344-
opt.Apply(candidate)
343+
opt.apply(candidate)
345344
if candidate.Condition == nil {
346345
return fmt.Errorf("%w: Condition nil", ErrUploadOptionInvalid)
347346
}
348347
}
349-
if opt.Type == IfNotExists {
348+
if opt.optType == IfNotExists {
350349
// If IfNotExists provided alongside IfMatch or IfNotMatch.
351350
if slices.ContainsFunc(opts, func(opt ObjectUploadOption) bool {
352-
return opt.Type == IfMatch || opt.Type == IfNotMatch
351+
return opt.optType == IfMatch || opt.optType == IfNotMatch
353352
}) {
354353
return fmt.Errorf("%w: IfNotExists not valid with IfMatch or IfNotMatch", ErrUploadOptionInvalid)
355354
}
@@ -362,7 +361,7 @@ func ValidateUploadOptions(supportedOptions []ObjectUploadOptionType, opts ...Ob
362361
func ApplyObjectUploadOptions(opts ...ObjectUploadOption) UploadObjectParams {
363362
out := UploadObjectParams{}
364363
for _, opt := range opts {
365-
opt.Apply(&out)
364+
opt.apply(&out)
366365
}
367366
return out
368367
}

providers/filesystem/filesystem.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,6 @@ func openSwap(name string) (swf *os.File, err error) {
289289
}
290290

291291
func tryOpenFile(name string, ifNotExists bool) (exists bool, err error) {
292-
// First try to open the file with exclusive create, then truncate if permitted
293292
flags := os.O_RDWR | os.O_CREATE | os.O_EXCL
294293
var f *os.File
295294
f, err = os.OpenFile(name, flags, 0666)
@@ -305,9 +304,8 @@ func tryOpenFile(name string, ifNotExists bool) (exists bool, err error) {
305304
return
306305
}
307306

308-
// Upload writes the file specified in src to into the memory.
307+
// Upload writes the contents of r to the object with the given name.
309308
func (b *Bucket) Upload(ctx context.Context, name string, r io.Reader, opts ...objstore.ObjectUploadOption) (err error) {
310-
311309
if err := objstore.ValidateUploadOptions(b.SupportedObjectUploadOptions(), opts...); err != nil {
312310
return err
313311
}
@@ -348,13 +346,15 @@ func (b *Bucket) Upload(ctx context.Context, name string, r io.Reader, opts ...o
348346
return err
349347
}
350348

349+
var writer io.Writer = swf
350+
h := sha256.New()
351+
if xattr.XATTR_SUPPORTED {
352+
writer = io.MultiWriter(swf, h)
353+
}
354+
if _, err := io.Copy(writer, r); err != nil {
355+
return errors.Wrapf(err, "copy to %s", swap)
356+
}
351357
if xattr.XATTR_SUPPORTED {
352-
h := sha256.New()
353-
writer := io.MultiWriter(swf, h)
354-
if _, err := io.Copy(writer, r); err != nil {
355-
return errors.Wrapf(err, "copy to %s", swap)
356-
}
357-
// Write the checksum into an xattr
358358
if err := xattr.Set(swap, xAttrKey, h.Sum(nil)); err != nil {
359359
return err
360360
}

0 commit comments

Comments
 (0)