Skip to content

Commit 68d6b78

Browse files
sy-recordsLinkinStars
authored andcommitted
refactor(storage): Add support for attachment uploads
1 parent 3c8ae67 commit 68d6b78

File tree

6 files changed

+33
-48
lines changed

6 files changed

+33
-48
lines changed

storage-tencentyuncos/README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Tencent COS Storage (preview)
1+
# Tencent COS Storage
22

33
> This plugin can be used to store attachments and avatars to Tencent COS.
44
@@ -18,4 +18,3 @@
1818
- `Secret ID` - Secret ID of the Tencent COS storage
1919
- `Secret Key` - Secret Key of the Tencent COS storage
2020
- `Visit Url Prefix` - Prefix of access address for the uploaded file, ending with '/' such as https://example.com/xxx/
21-
- `Max File Size` - Max file size in MB, default is 10MB

storage-tencentyuncos/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-tencentyuncos/i18n/translation.go

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

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

storage-tencentyuncos/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-tencentyuncos/info.yaml

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

1818
slug_name: tencentyuncos_storage
1919
type: storage
20-
version: 1.0.2
20+
version: 1.0.3
2121
author: Luffy
2222
link: https://github.com/apache/answer-plugins/tree/main/storage-tencentyuncos

storage-tencentyuncos/tencentyuncos.go

+31-33
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ import (
2828
"net/http"
2929
"net/url"
3030
"path/filepath"
31-
"strconv"
3231
"strings"
3332
"time"
3433

34+
"github.com/apache/answer/pkg/checker"
35+
3536
"github.com/apache/answer-plugins/storage-tencentyuncos/i18n"
3637
"github.com/apache/answer-plugins/util"
3738
"github.com/apache/answer/plugin"
@@ -41,11 +42,6 @@ import (
4142
//go:embed info.yaml
4243
var Info embed.FS
4344

44-
const (
45-
// 10MB
46-
defaultMaxFileSize int64 = 10 * 1024 * 1024
47-
)
48-
4945
type Storage struct {
5046
Config *StorageConfig
5147
}
@@ -57,7 +53,6 @@ type StorageConfig struct {
5753
SecretID string `json:"secret_id"`
5854
SecretKey string `json:"secret_key"`
5955
VisitUrlPrefix string `json:"visit_url_prefix"`
60-
MaxFileSize string `json:"max_file_size"`
6156
}
6257

6358
func init() {
@@ -80,7 +75,7 @@ func (s *Storage) Info() plugin.Info {
8075
}
8176
}
8277

83-
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) {
8479
resp = plugin.UploadFileResponse{}
8580

8681
BucketURL, _ := url.Parse(fmt.Sprintf("https://%s.cos.%s.myqcloud.com", s.Config.BucketName, s.Config.Region))
@@ -106,13 +101,13 @@ func (s *Storage) UploadFile(ctx *plugin.GinContext, source plugin.UploadSource)
106101
return resp
107102
}
108103

109-
if !s.CheckFileType(file.Filename, source) {
104+
if s.IsUnsupportedFileType(file.Filename, condition) {
110105
resp.OriginalError = fmt.Errorf("file type not allowed")
111106
resp.DisplayErrorMsg = plugin.MakeTranslator(i18n.ErrUnsupportedFileType)
112107
return resp
113108
}
114109

115-
if file.Size > s.maxFileSizeLimit() {
110+
if s.ExceedFileSizeLimit(file.Size, condition) {
116111
resp.OriginalError = fmt.Errorf("file size too large")
117112
resp.DisplayErrorMsg = plugin.MakeTranslator(i18n.ErrOverFileSizeLimit)
118113
return resp
@@ -126,7 +121,7 @@ func (s *Storage) UploadFile(ctx *plugin.GinContext, source plugin.UploadSource)
126121
}
127122
defer openFile.Close()
128123

129-
objectKey := s.createObjectKey(file.Filename, source)
124+
objectKey := s.createObjectKey(file.Filename, condition.Source)
130125
_, err = client.Object.Put(ctx, objectKey, openFile, nil)
131126
if err != nil {
132127
resp.OriginalError = fmt.Errorf("upload file failed: %v", err)
@@ -137,6 +132,29 @@ func (s *Storage) UploadFile(ctx *plugin.GinContext, source plugin.UploadSource)
137132
return resp
138133
}
139134

135+
func (s *Storage) IsUnsupportedFileType(originalFilename string, condition plugin.UploadFileCondition) bool {
136+
if condition.Source == plugin.AdminBranding || condition.Source == plugin.UserAvatar {
137+
ext := strings.ToLower(filepath.Ext(originalFilename))
138+
if _, ok := plugin.DefaultFileTypeCheckMapping[condition.Source][ext]; ok {
139+
return false
140+
}
141+
return true
142+
}
143+
144+
// check the post image and attachment file type check
145+
if condition.Source == plugin.UserPost {
146+
return checker.IsUnAuthorizedExtension(originalFilename, condition.AuthorizedImageExtensions)
147+
}
148+
return checker.IsUnAuthorizedExtension(originalFilename, condition.AuthorizedAttachmentExtensions)
149+
}
150+
151+
func (s *Storage) ExceedFileSizeLimit(fileSize int64, condition plugin.UploadFileCondition) bool {
152+
if condition.Source == plugin.UserPostAttachment {
153+
return fileSize > int64(condition.MaxAttachmentSize)*1024*1024
154+
}
155+
return fileSize > int64(condition.MaxImageSize)*1024*1024
156+
}
157+
140158
func (s *Storage) createObjectKey(originalFilename string, source plugin.UploadSource) string {
141159
ext := strings.ToLower(filepath.Ext(originalFilename))
142160
randomString := s.randomObjectKey()
@@ -145,6 +163,8 @@ func (s *Storage) createObjectKey(originalFilename string, source plugin.UploadS
145163
return s.Config.ObjectKeyPrefix + "avatar/" + randomString + ext
146164
case plugin.UserPost:
147165
return s.Config.ObjectKeyPrefix + "post/" + randomString + ext
166+
case plugin.UserPostAttachment:
167+
return s.Config.ObjectKeyPrefix + "attachment/" + randomString + ext
148168
case plugin.AdminBranding:
149169
return s.Config.ObjectKeyPrefix + "branding/" + randomString + ext
150170
default:
@@ -166,17 +186,6 @@ func (s *Storage) CheckFileType(originalFilename string, source plugin.UploadSou
166186
return false
167187
}
168188

169-
func (s *Storage) maxFileSizeLimit() int64 {
170-
if len(s.Config.MaxFileSize) == 0 {
171-
return defaultMaxFileSize
172-
}
173-
limit, _ := strconv.Atoi(s.Config.MaxFileSize)
174-
if limit <= 0 {
175-
return defaultMaxFileSize
176-
}
177-
return int64(limit) * 1024 * 1024
178-
}
179-
180189
func (s *Storage) ConfigFields() []plugin.ConfigField {
181190
return []plugin.ConfigField{
182191
{
@@ -245,17 +254,6 @@ func (s *Storage) ConfigFields() []plugin.ConfigField {
245254
},
246255
Value: s.Config.VisitUrlPrefix,
247256
},
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-
},
259257
}
260258
}
261259

0 commit comments

Comments
 (0)