@@ -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
4243var Info embed.FS
4344
44- const (
45- // 10MB
46- defaultMaxFileSize int64 = 10 * 1024 * 1024
47- )
48-
4945type 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
6358func 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+
140158func (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-
180189func (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