Skip to content

Commit e5a92e1

Browse files
committed
Merge remote-tracking branch 'origin/main' into dev
2 parents 8c14d6a + 6266dc0 commit e5a92e1

File tree

19 files changed

+93
-148
lines changed

19 files changed

+93
-148
lines changed

.github/workflows/sync-info.yml

-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,5 @@ jobs:
4040
title: "chore: Sync Plugin Info"
4141
body: "Sync Plugin Info"
4242
branch: "chore/sync-info"
43-
base: "dev"
4443

4544

storage-aliyunoss/README.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Aliyun OSS Storage (preview)
1+
# Aliyun OSS Storage
22
> This plugin can be used to store attachments and avatars to Aliyun OSS.
33
44
## How to use
@@ -14,5 +14,4 @@
1414
- `Object Key Prefix` - Prefix of the object key like 'answer/data/' that ending with '/'
1515
- `Access Key Id` - AccessKeyID of the AliCloud OSS storage
1616
- `Access Key Secret` - AccessKeySecret of the AliCloud OSS storage
17-
- `Visit Url Prefix` - Prefix of access address for the uploaded file, ending with '/' such as https://example.com/xxx/
18-
- `Max File Size` - Max file size in MB, default is 10MB
17+
- `Visit Url Prefix` - Prefix of access address for the uploaded file, ending with '/' such as https://example.com/xxx/

storage-aliyunoss/aliyunoss.go

+24-34
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@ import (
2626
"encoding/json"
2727
"fmt"
2828
"path/filepath"
29-
"strconv"
3029
"strings"
3130
"time"
3231

32+
"github.com/apache/answer/pkg/checker"
33+
3334
"github.com/aliyun/aliyun-oss-go-sdk/oss"
3435
"github.com/apache/answer-plugins/storage-aliyunoss/i18n"
3536
"github.com/apache/answer-plugins/util"
@@ -39,11 +40,6 @@ import (
3940
//go:embed info.yaml
4041
var Info embed.FS
4142

42-
const (
43-
// 10MB
44-
defaultMaxFileSize int64 = 10 * 1024 * 1024
45-
)
46-
4743
type Storage struct {
4844
Config *StorageConfig
4945
}
@@ -55,7 +51,6 @@ type StorageConfig struct {
5551
AccessKeyID string `json:"access_key_id"`
5652
AccessKeySecret string `json:"access_key_secret"`
5753
VisitUrlPrefix string `json:"visit_url_prefix"`
58-
MaxFileSize string `json:"max_file_size"`
5954
}
6055

6156
func init() {
@@ -78,7 +73,7 @@ func (s *Storage) Info() plugin.Info {
7873
}
7974
}
8075

81-
func (s *Storage) UploadFile(ctx *plugin.GinContext, source plugin.UploadSource) (resp plugin.UploadFileResponse) {
76+
func (s *Storage) UploadFile(ctx *plugin.GinContext, condition plugin.UploadFileCondition) (resp plugin.UploadFileResponse) {
8277
resp = plugin.UploadFileResponse{}
8378
client, err := oss.New(s.Config.Endpoint, s.Config.AccessKeyID, s.Config.AccessKeySecret)
8479
if err != nil {
@@ -101,13 +96,13 @@ func (s *Storage) UploadFile(ctx *plugin.GinContext, source plugin.UploadSource)
10196
return resp
10297
}
10398

104-
if !s.CheckFileType(file.Filename, source) {
99+
if s.IsUnsupportedFileType(file.Filename, condition) {
105100
resp.OriginalError = fmt.Errorf("file type not allowed")
106101
resp.DisplayErrorMsg = plugin.MakeTranslator(i18n.ErrUnsupportedFileType)
107102
return resp
108103
}
109104

110-
if file.Size > s.maxFileSizeLimit() {
105+
if s.ExceedFileSizeLimit(file.Size, condition) {
111106
resp.OriginalError = fmt.Errorf("file size too large")
112107
resp.DisplayErrorMsg = plugin.MakeTranslator(i18n.ErrOverFileSizeLimit)
113108
return resp
@@ -121,7 +116,7 @@ func (s *Storage) UploadFile(ctx *plugin.GinContext, source plugin.UploadSource)
121116
}
122117
defer open.Close()
123118

124-
objectKey := s.createObjectKey(file.Filename, source)
119+
objectKey := s.createObjectKey(file.Filename, condition.Source)
125120
request := &oss.PutObjectRequest{
126121
ObjectKey: objectKey,
127122
Reader: open,
@@ -145,6 +140,8 @@ func (s *Storage) createObjectKey(originalFilename string, source plugin.UploadS
145140
return s.Config.ObjectKeyPrefix + "avatar/" + randomString + ext
146141
case plugin.UserPost:
147142
return s.Config.ObjectKeyPrefix + "post/" + randomString + ext
143+
case plugin.UserPostAttachment:
144+
return s.Config.ObjectKeyPrefix + "attachment/" + randomString + ext
148145
case plugin.AdminBranding:
149146
return s.Config.ObjectKeyPrefix + "branding/" + randomString + ext
150147
default:
@@ -158,23 +155,27 @@ func (s *Storage) randomObjectKey() string {
158155
return fmt.Sprintf("%d", time.Now().UnixNano()) + hex.EncodeToString(bytes)
159156
}
160157

161-
func (s *Storage) CheckFileType(originalFilename string, source plugin.UploadSource) bool {
162-
ext := strings.ToLower(filepath.Ext(originalFilename))
163-
if _, ok := plugin.DefaultFileTypeCheckMapping[source][ext]; ok {
158+
func (s *Storage) IsUnsupportedFileType(originalFilename string, condition plugin.UploadFileCondition) bool {
159+
if condition.Source == plugin.AdminBranding || condition.Source == plugin.UserAvatar {
160+
ext := strings.ToLower(filepath.Ext(originalFilename))
161+
if _, ok := plugin.DefaultFileTypeCheckMapping[condition.Source][ext]; ok {
162+
return false
163+
}
164164
return true
165165
}
166-
return false
167-
}
168166

169-
func (s *Storage) maxFileSizeLimit() int64 {
170-
if len(s.Config.MaxFileSize) == 0 {
171-
return defaultMaxFileSize
167+
// check the post image and attachment file type check
168+
if condition.Source == plugin.UserPost {
169+
return checker.IsUnAuthorizedExtension(originalFilename, condition.AuthorizedImageExtensions)
172170
}
173-
limit, _ := strconv.Atoi(s.Config.MaxFileSize)
174-
if limit <= 0 {
175-
return defaultMaxFileSize
171+
return checker.IsUnAuthorizedExtension(originalFilename, condition.AuthorizedAttachmentExtensions)
172+
}
173+
174+
func (s *Storage) ExceedFileSizeLimit(fileSize int64, condition plugin.UploadFileCondition) bool {
175+
if condition.Source == plugin.UserPostAttachment {
176+
return fileSize > int64(condition.MaxAttachmentSize)*1024*1024
176177
}
177-
return int64(limit) * 1024 * 1024
178+
return fileSize > int64(condition.MaxImageSize)*1024*1024
178179
}
179180

180181
func (s *Storage) ConfigFields() []plugin.ConfigField {
@@ -245,17 +246,6 @@ func (s *Storage) ConfigFields() []plugin.ConfigField {
245246
},
246247
Value: s.Config.VisitUrlPrefix,
247248
},
248-
{
249-
Name: "max_file_size",
250-
Type: plugin.ConfigTypeInput,
251-
Title: plugin.MakeTranslator(i18n.ConfigMaxFileSizeTitle),
252-
Description: plugin.MakeTranslator(i18n.ConfigMaxFileSizeDescription),
253-
Required: false,
254-
UIOptions: plugin.ConfigFieldUIOptions{
255-
InputType: plugin.InputTypeNumber,
256-
},
257-
Value: s.Config.MaxFileSize,
258-
},
259249
}
260250
}
261251

storage-aliyunoss/i18n/en_US.yaml

-5
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,6 @@ plugin:
5454
other: Access URL prefix
5555
description:
5656
other: prefix of the final access address of the uploaded file, ending with '/' https://example.com/xxx/
57-
max_file_size:
58-
title:
59-
other: Maximum file size(MB)
60-
description:
61-
other: Limit the maximum size of uploaded files, in MB, default is 10MB
6257
err:
6358
mis_storage_config:
6459
other: Wrong storage configuration causes upload failure.

storage-aliyunoss/i18n/translation.go

-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ const (
3535
ConfigAccessKeySecretDescription = "plugin.aliyunoss_storage.backend.config.access_key_secret.description"
3636
ConfigVisitUrlPrefixTitle = "plugin.aliyunoss_storage.backend.config.visit_url_prefix.title"
3737
ConfigVisitUrlPrefixDescription = "plugin.aliyunoss_storage.backend.config.visit_url_prefix.description"
38-
ConfigMaxFileSizeTitle = "plugin.aliyunoss_storage.backend.config.max_file_size.title"
39-
ConfigMaxFileSizeDescription = "plugin.aliyunoss_storage.backend.config.max_file_size.description"
4038

4139
ErrMisStorageConfig = "plugin.aliyunoss_storage.backend.err.mis_storage_config"
4240
ErrFileNotFound = "plugin.aliyunoss_storage.backend.err.file_not_found"

storage-aliyunoss/i18n/zh_CN.yaml

-5
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,6 @@ plugin:
5454
other: 访问URL前缀
5555
description:
5656
other: 上传文件最终访问地址的前缀,以 '/' 结尾 https://example.com/xxx/
57-
max_file_size:
58-
title:
59-
other: 最大文件大小(MB)
60-
description:
61-
other: 限制上传文件的最大大小,单位为MB,默认为 10MB
6257
err:
6358
mis_storage_config:
6459
other: 错误的存储配置导致上传失败

storage-aliyunoss/info.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@
1717

1818
slug_name: aliyunoss_storage
1919
type: storage
20-
version: 1.2.10
20+
version: 1.2.12
2121
author: answerdev
2222
link: https://github.com/apache/answer-plugins/tree/main/storage-aliyunoss

storage-s3/README.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# S3 Storage (preview)
1+
# S3 Storage
22
> This plugin can be used to store attachments and avatars to AWS S3.
33
44
## How to use
@@ -15,5 +15,4 @@
1515
- `Access Key Id` - AccessKeyId of the S3
1616
- `Access Key Secret` - AccessKeySecret of the S3
1717
- `Access Token` - AccessToken of the S3
18-
- `Visit Url Prefix` - Prefix of access address for the uploaded file, ending with '/' such as https://example.com/xxx/
19-
- `Max File Size` - Max file size in MB, default is 10MB
18+
- `Visit Url Prefix` - Prefix of access address for the uploaded file, ending with '/' such as https://example.com/xxx/

storage-s3/i18n/en_US.yaml

-5
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,6 @@ plugin:
5959
other: Access URL prefix
6060
description:
6161
other: prefix of the final access address of the uploaded file, ending with '/' https://example.com/xxx/
62-
max_file_size:
63-
title:
64-
other: Maximum file size(MB)
65-
description:
66-
other: Limit the maximum size of uploaded files, in MB, default is 10MB
6762
region:
6863
title:
6964
other: Region

storage-s3/i18n/translation.go

-2
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ const (
3737
ConfigAccessTokenDescription = "plugin.s3_storage.backend.config.access_token.description"
3838
ConfigVisitUrlPrefixTitle = "plugin.s3_storage.backend.config.visit_url_prefix.title"
3939
ConfigVisitUrlPrefixDescription = "plugin.s3_storage.backend.config.visit_url_prefix.description"
40-
ConfigMaxFileSizeTitle = "plugin.s3_storage.backend.config.max_file_size.title"
41-
ConfigMaxFileSizeDescription = "plugin.s3_storage.backend.config.max_file_size.description"
4240
ConfigRegionTitle = "plugin.s3_storage.backend.config.region.title"
4341
ConfigRegionDescription = "plugin.s3_storage.backend.config.region.description"
4442
ConfigDisableSSLTitle = "plugin.s3_storage.backend.config.disable_ssl.title"

storage-s3/i18n/zh_CN.yaml

-5
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,6 @@ plugin:
5959
other: 访问URL前缀
6060
description:
6161
other: 上传文件最终访问地址的前缀,以 '/' 结尾 https://example.com/xxx/
62-
max_file_size:
63-
title:
64-
other: 文件最大大小(MB)
65-
description:
66-
other: 限制上传文件的最大大小,单位MB,默认为10MB
6762
region:
6863
title:
6964
other: 区域(Region)

storage-s3/info.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@
1717

1818
slug_name: s3_storage
1919
type: storage
20-
version: 1.2.10
20+
version: 1.2.12
2121
author: answerdev
2222
link: https://github.com/apache/answer-plugins/tree/main/storage-s3

storage-s3/s3.go

+30-33
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,18 @@ import (
2626
"encoding/json"
2727
"fmt"
2828
"path/filepath"
29-
"strconv"
3029
"strings"
3130
"time"
3231

3332
"github.com/apache/answer-plugins/storage-s3/i18n"
3433
"github.com/apache/answer-plugins/util"
34+
"github.com/apache/answer/pkg/checker"
3535
"github.com/apache/answer/plugin"
3636
)
3737

3838
//go:embed info.yaml
3939
var Info embed.FS
4040

41-
const (
42-
// 10MB
43-
defaultMaxFileSize int64 = 10 * 1024 * 1024
44-
)
45-
4641
type Storage struct {
4742
Config *StorageConfig
4843
Client *Client
@@ -56,7 +51,6 @@ type StorageConfig struct {
5651
AccessKeySecret string `json:"access_key_secret"`
5752
AccessToken string `json:"access_token"`
5853
VisitUrlPrefix string `json:"visit_url_prefix"`
59-
MaxFileSize string `json:"max_file_size"`
6054
Region string `json:"region"`
6155
DisableSSL bool `json:"disable_ssl"`
6256
}
@@ -81,7 +75,7 @@ func (s *Storage) Info() plugin.Info {
8175
}
8276
}
8377

84-
func (s *Storage) UploadFile(ctx *plugin.GinContext, source plugin.UploadSource) (resp plugin.UploadFileResponse) {
78+
func (s *Storage) UploadFile(ctx *plugin.GinContext, condition plugin.UploadFileCondition) (resp plugin.UploadFileResponse) {
8579
resp = plugin.UploadFileResponse{}
8680

8781
file, err := ctx.FormFile("file")
@@ -91,13 +85,13 @@ func (s *Storage) UploadFile(ctx *plugin.GinContext, source plugin.UploadSource)
9185
return resp
9286
}
9387

94-
if !s.checkFileType(file.Filename, source) {
88+
if s.IsUnsupportedFileType(file.Filename, condition) {
9589
resp.OriginalError = fmt.Errorf("file type not allowed")
9690
resp.DisplayErrorMsg = plugin.MakeTranslator(i18n.ErrUnsupportedFileType)
9791
return resp
9892
}
9993

100-
if file.Size > s.maxFileSizeLimit() {
94+
if s.ExceedFileSizeLimit(file.Size, condition) {
10195
resp.OriginalError = fmt.Errorf("file size too large")
10296
resp.DisplayErrorMsg = plugin.MakeTranslator(i18n.ErrOverFileSizeLimit)
10397
return resp
@@ -111,7 +105,7 @@ func (s *Storage) UploadFile(ctx *plugin.GinContext, source plugin.UploadSource)
111105
}
112106
defer openFile.Close()
113107

114-
objectKey := s.createObjectKey(file.Filename, source)
108+
objectKey := s.createObjectKey(file.Filename, condition.Source)
115109
err = s.Client.PutObject(objectKey, strings.ToLower(filepath.Ext(file.Filename)), openFile)
116110
if err != nil {
117111
resp.OriginalError = fmt.Errorf("upload file failed: %v", err)
@@ -122,6 +116,29 @@ func (s *Storage) UploadFile(ctx *plugin.GinContext, source plugin.UploadSource)
122116
return resp
123117
}
124118

119+
func (s *Storage) IsUnsupportedFileType(originalFilename string, condition plugin.UploadFileCondition) bool {
120+
if condition.Source == plugin.AdminBranding || condition.Source == plugin.UserAvatar {
121+
ext := strings.ToLower(filepath.Ext(originalFilename))
122+
if _, ok := plugin.DefaultFileTypeCheckMapping[condition.Source][ext]; ok {
123+
return false
124+
}
125+
return true
126+
}
127+
128+
// check the post image and attachment file type check
129+
if condition.Source == plugin.UserPost {
130+
return checker.IsUnAuthorizedExtension(originalFilename, condition.AuthorizedImageExtensions)
131+
}
132+
return checker.IsUnAuthorizedExtension(originalFilename, condition.AuthorizedAttachmentExtensions)
133+
}
134+
135+
func (s *Storage) ExceedFileSizeLimit(fileSize int64, condition plugin.UploadFileCondition) bool {
136+
if condition.Source == plugin.UserPostAttachment {
137+
return fileSize > int64(condition.MaxAttachmentSize)*1024*1024
138+
}
139+
return fileSize > int64(condition.MaxImageSize)*1024*1024
140+
}
141+
125142
func (s *Storage) createObjectKey(originalFilename string, source plugin.UploadSource) string {
126143
ext := strings.ToLower(filepath.Ext(originalFilename))
127144
randomString := s.randomObjectKey()
@@ -130,6 +147,8 @@ func (s *Storage) createObjectKey(originalFilename string, source plugin.UploadS
130147
return s.Config.ObjectKeyPrefix + "avatar/" + randomString + ext
131148
case plugin.UserPost:
132149
return s.Config.ObjectKeyPrefix + "post/" + randomString + ext
150+
case plugin.UserPostAttachment:
151+
return s.Config.ObjectKeyPrefix + "attachment/" + randomString + ext
133152
case plugin.AdminBranding:
134153
return s.Config.ObjectKeyPrefix + "branding/" + randomString + ext
135154
default:
@@ -151,17 +170,6 @@ func (s *Storage) checkFileType(originalFilename string, source plugin.UploadSou
151170
return false
152171
}
153172

154-
func (s *Storage) maxFileSizeLimit() int64 {
155-
if len(s.Config.MaxFileSize) == 0 {
156-
return defaultMaxFileSize
157-
}
158-
limit, _ := strconv.Atoi(s.Config.MaxFileSize)
159-
if limit <= 0 {
160-
return defaultMaxFileSize
161-
}
162-
return int64(limit) * 1024 * 1024
163-
}
164-
165173
func (s *Storage) ConfigFields() []plugin.ConfigField {
166174
return []plugin.ConfigField{
167175
{
@@ -241,17 +249,6 @@ func (s *Storage) ConfigFields() []plugin.ConfigField {
241249
},
242250
Value: s.Config.VisitUrlPrefix,
243251
},
244-
{
245-
Name: "max_file_size",
246-
Type: plugin.ConfigTypeInput,
247-
Title: plugin.MakeTranslator(i18n.ConfigMaxFileSizeTitle),
248-
Description: plugin.MakeTranslator(i18n.ConfigMaxFileSizeDescription),
249-
Required: false,
250-
UIOptions: plugin.ConfigFieldUIOptions{
251-
InputType: plugin.InputTypeNumber,
252-
},
253-
Value: s.Config.MaxFileSize,
254-
},
255252
{
256253
Name: "region",
257254
Type: plugin.ConfigTypeInput,

0 commit comments

Comments
 (0)