From 284a560ea63f95132ee11154d0c00f24eb4f37ec Mon Sep 17 00:00:00 2001 From: Angira Kekteeva Date: Sat, 13 Nov 2021 23:35:50 +0300 Subject: [PATCH] [#186] Refactor objectSearch, fix objectPut Fixed panic in objectPut() if input reader is nil Made objectSearch searching by any attribute Signed-off-by: Angira Kekteeva --- api/layer/layer.go | 2 +- api/layer/object.go | 48 ++++++++++++++++++++++++-------------- api/layer/system_object.go | 12 ++++++++-- 3 files changed, 42 insertions(+), 20 deletions(-) diff --git a/api/layer/layer.go b/api/layer/layer.go index 5de984735..e7e200ed8 100644 --- a/api/layer/layer.go +++ b/api/layer/layer.go @@ -587,7 +587,7 @@ func (n *layer) deleteObject(ctx context.Context, bkt *data.BucketInfo, obj *Ver obj.DeleteMarkVersion = objInfo.Version() } } else { - ids, err = n.objectSearch(ctx, &findParams{cid: bkt.CID, val: obj.Name}) + ids, err = n.objectSearchByName(ctx, bkt.CID, obj.Name) if err != nil { obj.Error = err return obj diff --git a/api/layer/object.go b/api/layer/object.go index 7fd6d141d..f5fa8d724 100644 --- a/api/layer/object.go +++ b/api/layer/object.go @@ -22,10 +22,14 @@ import ( type ( findParams struct { - attr string - val string - cid *cid.ID - prefix string + filters []filter + cid *cid.ID + prefix string + } + + filter struct { + attr string + val string } getParams struct { @@ -68,21 +72,29 @@ type ( } ) +func (n *layer) objectSearchByName(ctx context.Context, cid *cid.ID, filename string) ([]*object.ID, error) { + f := &findParams{ + filters: []filter{{attr: object.AttributeFileName, val: filename}}, + cid: cid, + prefix: "", + } + return n.objectSearch(ctx, f) +} + // objectSearch returns all available objects by search params. func (n *layer) objectSearch(ctx context.Context, p *findParams) ([]*object.ID, error) { var opts object.SearchFilters opts.AddRootFilter() - if filename, err := url.QueryUnescape(p.val); err != nil { - return nil, err - } else if filename != "" { - if p.attr == "" { - opts.AddFilter(object.AttributeFileName, filename, object.MatchStringEqual) - } else { - opts.AddFilter(p.attr, filename, object.MatchStringEqual) + for _, filter := range p.filters { + if val, err := url.QueryUnescape(filter.val); err != nil { + return nil, err + } else if val != "" { + opts.AddFilter(filter.attr, val, object.MatchStringEqual) } } + if prefix, err := url.QueryUnescape(p.prefix); err != nil { return nil, err } else if prefix != "" { @@ -142,12 +154,14 @@ func (n *layer) objectPut(ctx context.Context, bkt *data.BucketInfo, p *PutObjec idsToDeleteArr := updateCRDT2PSetHeaders(p.Header, versions, versioningEnabled) r := p.Reader - if len(p.Header[api.ContentType]) == 0 { - d := newDetector(r) - if contentType, err := d.Detect(); err == nil { - p.Header[api.ContentType] = contentType + if r != nil { + if len(p.Header[api.ContentType]) == 0 { + d := newDetector(r) + if contentType, err := d.Detect(); err == nil { + p.Header[api.ContentType] = contentType + } + r = d.MultiReader() } - r = d.MultiReader() } rawObject := formRawObject(p, bkt.CID, own, obj) @@ -307,7 +321,7 @@ func (n *layer) headLastVersionIfNotDeleted(ctx context.Context, bkt *data.Bucke } func (n *layer) headVersions(ctx context.Context, bkt *data.BucketInfo, objectName string) (*objectVersions, error) { - ids, err := n.objectSearch(ctx, &findParams{cid: bkt.CID, val: objectName}) + ids, err := n.objectSearchByName(ctx, bkt.CID, objectName) if err != nil { return nil, err } diff --git a/api/layer/system_object.go b/api/layer/system_object.go index 72536db07..6b347562f 100644 --- a/api/layer/system_object.go +++ b/api/layer/system_object.go @@ -44,7 +44,11 @@ func (n *layer) headSystemObject(ctx context.Context, bkt *data.BucketInfo, objN } func (n *layer) deleteSystemObject(ctx context.Context, bktInfo *data.BucketInfo, name string) error { - ids, err := n.objectSearch(ctx, &findParams{cid: bktInfo.CID, attr: objectSystemAttributeName, val: name}) + f := &findParams{ + filters: []filter{{attr: objectSystemAttributeName, val: name}}, + cid: bktInfo.CID, + } + ids, err := n.objectSearch(ctx, f) if err != nil { return err } @@ -166,7 +170,11 @@ func (n *layer) getCORS(ctx context.Context, bkt *data.BucketInfo, sysName strin } func (n *layer) headSystemVersions(ctx context.Context, bkt *data.BucketInfo, sysName string) (*objectVersions, error) { - ids, err := n.objectSearch(ctx, &findParams{cid: bkt.CID, attr: objectSystemAttributeName, val: sysName}) + f := &findParams{ + filters: []filter{{attr: objectSystemAttributeName, val: sysName}}, + cid: bkt.CID, + } + ids, err := n.objectSearch(ctx, f) if err != nil { return nil, err }