[#154] api: refactor EncodeV1 and EncodeV2

Move common parts of Encode to separate functions.

Signed-off-by: Angira Kekteeva <kira@nspcc.ru>
This commit is contained in:
Angira Kekteeva 2021-07-19 12:10:28 +03:00
parent ee84062154
commit b1cda2a714

View file

@ -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 {