From b1cda2a7147dcb0ee27f3c00c777b168fc35b030 Mon Sep 17 00:00:00 2001 From: Angira Kekteeva Date: Mon, 19 Jul 2021 12:10:28 +0300 Subject: [PATCH] [#154] api: refactor EncodeV1 and EncodeV2 Move common parts of Encode to separate functions. Signed-off-by: Angira Kekteeva --- api/handler/object_list.go | 75 +++++++++++++++++--------------------- 1 file changed, 33 insertions(+), 42 deletions(-) diff --git a/api/handler/object_list.go b/api/handler/object_list.go index 0e7bbb3a5..63eb69994 100644 --- a/api/handler/object_list.go +++ b/api/handler/object_list.go @@ -42,28 +42,9 @@ func encodeV1(p *layer.ListObjectsParamsV1, list *layer.ListObjectsInfoV1) *List NextMarker: list.NextMarker, } - // fill common prefixes - for i := range list.Prefixes { - res.CommonPrefixes = append(res.CommonPrefixes, CommonPrefix{ - Prefix: s3PathEncode(list.Prefixes[i], p.Encode), - }) - } + res.CommonPrefixes = fillPrefixes(list.Prefixes, p.Encode) - // fill contents - for _, obj := range list.Objects { - res.Contents = append(res.Contents, Object{ - Key: s3PathEncode(obj.Name, p.Encode), - Size: obj.Size, - LastModified: obj.Created.Format(time.RFC3339), - - Owner: Owner{ - ID: obj.Owner.String(), - DisplayName: obj.Owner.String(), - }, - - ETag: obj.HashSum, - }) - } + res.Contents = fillContents(list.Objects, p.Encode) return res } @@ -104,28 +85,9 @@ func encodeV2(p *layer.ListObjectsParamsV2, list *layer.ListObjectsInfoV2) *List NextContinuationToken: list.NextContinuationToken, } - // fill common prefixes - for i := range list.Prefixes { - res.CommonPrefixes = append(res.CommonPrefixes, CommonPrefix{ - Prefix: s3PathEncode(list.Prefixes[i], p.Encode), - }) - } + res.CommonPrefixes = fillPrefixes(list.Prefixes, p.Encode) - // fill contents - for _, obj := range list.Objects { - res.Contents = append(res.Contents, Object{ - Key: s3PathEncode(obj.Name, p.Encode), - Size: obj.Size, - LastModified: obj.Created.Format(time.RFC3339), - - Owner: Owner{ - ID: obj.Owner.String(), - DisplayName: obj.Owner.String(), - }, - - ETag: obj.HashSum, - }) - } + res.Contents = fillContents(list.Objects, p.Encode) return res } @@ -188,6 +150,35 @@ func parseListObjectArgs(r *http.Request) (*layer.ListObjectsParamsCommon, error return &res, nil } +func fillPrefixes(src []string, encode string) []CommonPrefix { + var dst []CommonPrefix + for _, obj := range src { + dst = append(dst, CommonPrefix{ + Prefix: s3PathEncode(obj, encode), + }) + } + return dst +} + +func fillContents(src []*layer.ObjectInfo, encode string) []Object { + var dst []Object + for _, obj := range src { + dst = append(dst, Object{ + Key: s3PathEncode(obj.Name, encode), + Size: obj.Size, + LastModified: obj.Created.Format(time.RFC3339), + + Owner: Owner{ + ID: obj.Owner.String(), + DisplayName: obj.Owner.String(), + }, + + ETag: obj.HashSum, + }) + } + return dst +} + func (h *handler) ListBucketObjectVersionsHandler(w http.ResponseWriter, r *http.Request) { p, err := parseListObjectVersionsRequest(r) if err != nil {