Skip to content

Commit

Permalink
mod: option handling
Browse files Browse the repository at this point in the history
  • Loading branch information
bushiyama committed Feb 11, 2023
1 parent e5630f3 commit 64faa77
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 70 deletions.
35 changes: 10 additions & 25 deletions aws/awss3/awss3.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,7 @@ func Presign(ctx context.Context, region awsconfig.Region, bucketName BucketName
Key: key.AWSString(),
}
if c.PresignFileName != "" {
input.ResponseContentDisposition = aws.String(ResponseContentDisposition(c.PresignFileName))
// memo: If type is specified, it is overridden as the behavior in the new version.
if c.PresignFileType != nil {
input.ResponseContentType = aws.String(ResponseContentDispositionMulti(*c.PresignFileType, c.PresignFileName))
}
input.ResponseContentDisposition = aws.String(ResponseContentDisposition(c.ContentDispositionType, c.PresignFileName))
}
ps := s3.NewPresignClient(client)
resp, err := ps.PresignGetObject(ctx, input, func(o *s3.PresignOptions) {
Expand All @@ -273,26 +269,15 @@ func Presign(ctx context.Context, region awsconfig.Region, bucketName BucketName
return resp.URL, nil
}

// ResponseContentDisposition
// Setting ResponseContentDisposition to support file names with multibyte characters
func ResponseContentDisposition(fileName string) string {
return fmt.Sprintf(`attachment; filename*=UTF-8''%s`, url.PathEscape(fileName))
}

// ResponseContentDispositionMulti
// Setting ResponseContentDisposition to support file names with multibyte characters
func ResponseContentDispositionMulti(tp s3presigned.ContentDispositionType, fileName string) string {
switch {
case tp == s3presigned.PresignFileTypeInline && fileName == "":
return string(s3presigned.PresignFileTypeInline)
case tp == s3presigned.PresignFileTypeInline && fileName != "":
return fmt.Sprintf(`inline; filename*=UTF-8''%s`, url.PathEscape(fileName))
case tp == s3presigned.PresignFileTypeAttachment && fileName == "":
return string(s3presigned.PresignFileTypeAttachment)
case tp == s3presigned.PresignFileTypeAttachment && fileName != "":
return fmt.Sprintf(`attachment; filename*=UTF-8''%s`, url.PathEscape(fileName))
}
return ""
func ResponseContentDisposition(tp s3presigned.ContentDispositionType, fileName string) string {
var dispositionType string
switch tp {
case s3presigned.ContentDispositionTypeAttachment:
dispositionType = "attachment"
case s3presigned.ContentDispositionTypeInline:
dispositionType = "inline"
}
return fmt.Sprintf(`%s; filename*=UTF-8''%s`, dispositionType, url.PathEscape(fileName))
}

// Copy copies an Amazon S3 object from one bucket to same.
Expand Down
46 changes: 1 addition & 45 deletions aws/awss3/awss3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,55 +289,11 @@ func TestPresign(t *testing.T) {
func TestResponseContentDisposition(t *testing.T) {
const fileName = ",あいうえお 牡蠣喰家来 サシスセソ@+$_-^|+{}"
t.Run("success", func(t *testing.T) {
actual := awss3.ResponseContentDisposition(fileName)
actual := awss3.ResponseContentDisposition(s3presigned.ContentDispositionTypeAttachment, fileName)
assert.NotEmpty(t, actual)
})
}

func TestResponseContentDispositionMulti(t *testing.T) {
const fileName = ",あいうえお 牡蠣喰家来 サシスセソ@+$_-^|+{}"

cases := map[string]struct {
tp s3presigned.ContentDispositionType
fileName string
emptyFLG bool
}{
"inline": {
fileName: "",
tp: s3presigned.PresignFileTypeInline,
},
"inline&filename": {
tp: s3presigned.PresignFileTypeInline,
fileName: fileName,
},
"attachment": {
tp: s3presigned.PresignFileTypeAttachment,
fileName: "",
},
"attachment&filename": {
tp: s3presigned.PresignFileTypeAttachment,
fileName: fileName,
},
"empty": {
tp: "",
fileName: "",
emptyFLG: true,
},
}

for name, c := range cases {
t.Run(name, func(t *testing.T) {
actual := awss3.ResponseContentDispositionMulti(c.tp, c.fileName)
switch c.emptyFLG {
case true:
assert.Empty(t, actual)
default:
assert.NotEmpty(t, actual)
}
})
}
}

func TestCopy(t *testing.T) {
createFixture := func(ctx context.Context) awss3.Key {
s3Client, err := awss3.GetClient(ctx, TestRegion)
Expand Down

0 comments on commit 64faa77

Please sign in to comment.