Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
trim21 committed Oct 10, 2024
1 parent c24c165 commit 82ef472
Showing 1 changed file with 29 additions and 24 deletions.
53 changes: 29 additions & 24 deletions canal/on_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ import (
"encoding/json"
"fmt"
"strings"
"time"

"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/aws-sdk-go-v2/service/s3/types"
"github.com/samber/lo"
"github.com/trim21/errgo"
"go.uber.org/zap"
Expand Down Expand Up @@ -91,34 +93,37 @@ func (e *eventHandler) clearImageCache(avatar string) {

e.log.Debug("clear image for prefix", zap.String("avatar", avatar), zap.String("prefix", p))

err := e.s3.ListObjectsV2(context.Background(),
&s3.ListObjectsV2Input{Bucket: &e.config.S3ImageResizeBucket, Prefix: &p},
func(output *s3.ListObjectsV2Output, b bool) bool {
if len(output.Contents) == 0 {
return false
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

Check warning on line 97 in canal/on_user.go

View check run for this annotation

Codecov / codecov/patch

canal/on_user.go#L96-L97

Added lines #L96 - L97 were not covered by tests

_, err := e.s3.DeleteObjects(&s3.DeleteObjectsInput{
Bucket: &e.config.S3ImageResizeBucket,
Delete: &s3.Delete{
Objects: lo.Map(output.Contents, func(item *s3.Object, index int) *s3.ObjectIdentifier {
return &s3.ObjectIdentifier{
Key: item.Key,
}
}),
},
})
pages := s3.NewListObjectsV2Paginator(
e.s3,
&s3.ListObjectsV2Input{Bucket: &e.config.S3ImageResizeBucket, Prefix: &p},
)

Check warning on line 102 in canal/on_user.go

View check run for this annotation

Codecov / codecov/patch

canal/on_user.go#L99-L102

Added lines #L99 - L102 were not covered by tests

if err != nil {
e.log.Error("failed to clear s3 cached image", zap.Error(err))
}
for pages.HasMorePages() {
output, err := pages.NextPage(ctx)
if err != nil {
break

Check warning on line 107 in canal/on_user.go

View check run for this annotation

Codecov / codecov/patch

canal/on_user.go#L104-L107

Added lines #L104 - L107 were not covered by tests
}

return true
},
)
if len(output.Contents) == 0 {
break

Check warning on line 111 in canal/on_user.go

View check run for this annotation

Codecov / codecov/patch

canal/on_user.go#L110-L111

Added lines #L110 - L111 were not covered by tests
}

if err != nil {
e.log.Error("failed to clear s3 cached image", zap.Error(err))
_, err = e.s3.DeleteObjects(ctx, &s3.DeleteObjectsInput{
Bucket: &e.config.S3ImageResizeBucket,
Delete: &types.Delete{
Objects: lo.Map(output.Contents, func(item types.Object, index int) types.ObjectIdentifier {
return types.ObjectIdentifier{
Key: item.Key,

Check warning on line 119 in canal/on_user.go

View check run for this annotation

Codecov / codecov/patch

canal/on_user.go#L114-L119

Added lines #L114 - L119 were not covered by tests
}
}),

Check warning on line 121 in canal/on_user.go

View check run for this annotation

Codecov / codecov/patch

canal/on_user.go#L121

Added line #L121 was not covered by tests
},
})
if err != nil {
e.log.Error("failed to clear s3 cached image", zap.Error(err))

Check warning on line 125 in canal/on_user.go

View check run for this annotation

Codecov / codecov/patch

canal/on_user.go#L124-L125

Added lines #L124 - L125 were not covered by tests
}
}
}

Expand Down

0 comments on commit 82ef472

Please sign in to comment.