Skip to content

Commit

Permalink
don't iterate files or folders of list objects if they are nil
Browse files Browse the repository at this point in the history
  • Loading branch information
Abrar-Ahmed7 committed Feb 17, 2023
1 parent 2ca4c94 commit 4b1f1f8
Showing 1 changed file with 28 additions and 24 deletions.
52 changes: 28 additions & 24 deletions internal/dao/buck_obj.go
Expand Up @@ -29,12 +29,12 @@ func (bo *BObj) List(ctx context.Context) ([]Object, error) {
bucketInfo := aws.GetInfoAboutBucket(*sess, bucketName, "/", fn)
folderArrayInfo, fileArrayInfo := getBucLevelInfo(bucketInfo)
var s3Objects []aws.S3Object
if len(folderArrayInfo) != 0 || len(fileArrayInfo) != 0 {
s3Objects = setFoldersAndFiles(bucketInfo.CommonPrefixes, bucketInfo.Contents)
} else {
if len(folderArrayInfo) == 0 && len(fileArrayInfo) == 0 {
s3Objects = append(s3Objects, aws.S3Object{
Name: "No objects found",
})
} else {
s3Objects = setFoldersAndFiles(bucketInfo.CommonPrefixes, bucketInfo.Contents)
}
objs := make([]Object, len(s3Objects))
for i, obj := range s3Objects {
Expand Down Expand Up @@ -62,33 +62,37 @@ func getBucLevelInfo(bucketInfo *s3.ListObjectsV2Output) ([]string, []string) {
return folderArrayInfo, fileArrayInfo
}

func setFoldersAndFiles(Folder []*s3.CommonPrefix, File []*s3.Object) []aws.S3Object {
func setFoldersAndFiles(folders []*s3.CommonPrefix, files []*s3.Object) []aws.S3Object {
var s3Objects []aws.S3Object
indx := 0
for _, bi := range Folder {
keyA := strings.Split(*bi.Prefix, "/")
o := aws.S3Object{
Name: keyA[len(keyA)-2],
ObjectType: "Folder",
LastModified: "-",
Size: "-",
StorageClass: "-",
if len(folders) != 0 {
for _, bi := range folders {
keyA := strings.Split(*bi.Prefix, "/")
o := aws.S3Object{
Name: keyA[len(keyA)-2],
ObjectType: "Folder",
LastModified: "-",
Size: "-",
StorageClass: "-",
}
s3Objects = append(s3Objects, o)
indx++
}
s3Objects = append(s3Objects, o)
indx++
}

for _, fi := range File {
keyA := strings.Split(*fi.Key, "/")
o := aws.S3Object{
Name: keyA[len(keyA)-1],
ObjectType: "File",
LastModified: fi.LastModified.String(),
Size: humanize.Bytes(uint64(*fi.Size)),
StorageClass: *fi.StorageClass,
if len(files) != 0 {
for _, fi := range files {
keyA := strings.Split(*fi.Key, "/")
o := aws.S3Object{
Name: keyA[len(keyA)-1],
ObjectType: "File",
LastModified: fi.LastModified.String(),
Size: humanize.Bytes(uint64(*fi.Size)),
StorageClass: *fi.StorageClass,
}
s3Objects = append(s3Objects, o)
indx++
}
s3Objects = append(s3Objects, o)
indx++
}

return s3Objects
Expand Down

0 comments on commit 4b1f1f8

Please sign in to comment.