@@ -28,10 +28,11 @@ import (
28
28
"net/http"
29
29
"net/url"
30
30
"path/filepath"
31
- "strconv"
32
31
"strings"
33
32
"time"
34
33
34
+ "github.com/apache/answer/pkg/checker"
35
+
35
36
"github.com/apache/answer-plugins/storage-tencentyuncos/i18n"
36
37
"github.com/apache/answer-plugins/util"
37
38
"github.com/apache/answer/plugin"
@@ -41,11 +42,6 @@ import (
41
42
//go:embed info.yaml
42
43
var Info embed.FS
43
44
44
- const (
45
- // 10MB
46
- defaultMaxFileSize int64 = 10 * 1024 * 1024
47
- )
48
-
49
45
type Storage struct {
50
46
Config * StorageConfig
51
47
}
@@ -57,7 +53,6 @@ type StorageConfig struct {
57
53
SecretID string `json:"secret_id"`
58
54
SecretKey string `json:"secret_key"`
59
55
VisitUrlPrefix string `json:"visit_url_prefix"`
60
- MaxFileSize string `json:"max_file_size"`
61
56
}
62
57
63
58
func init () {
@@ -80,7 +75,7 @@ func (s *Storage) Info() plugin.Info {
80
75
}
81
76
}
82
77
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 ) {
84
79
resp = plugin.UploadFileResponse {}
85
80
86
81
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)
106
101
return resp
107
102
}
108
103
109
- if ! s . CheckFileType (file .Filename , source ) {
104
+ if s . IsUnsupportedFileType (file .Filename , condition ) {
110
105
resp .OriginalError = fmt .Errorf ("file type not allowed" )
111
106
resp .DisplayErrorMsg = plugin .MakeTranslator (i18n .ErrUnsupportedFileType )
112
107
return resp
113
108
}
114
109
115
- if file .Size > s . maxFileSizeLimit ( ) {
110
+ if s . ExceedFileSizeLimit ( file .Size , condition ) {
116
111
resp .OriginalError = fmt .Errorf ("file size too large" )
117
112
resp .DisplayErrorMsg = plugin .MakeTranslator (i18n .ErrOverFileSizeLimit )
118
113
return resp
@@ -126,7 +121,7 @@ func (s *Storage) UploadFile(ctx *plugin.GinContext, source plugin.UploadSource)
126
121
}
127
122
defer openFile .Close ()
128
123
129
- objectKey := s .createObjectKey (file .Filename , source )
124
+ objectKey := s .createObjectKey (file .Filename , condition . Source )
130
125
_ , err = client .Object .Put (ctx , objectKey , openFile , nil )
131
126
if err != nil {
132
127
resp .OriginalError = fmt .Errorf ("upload file failed: %v" , err )
@@ -137,6 +132,29 @@ func (s *Storage) UploadFile(ctx *plugin.GinContext, source plugin.UploadSource)
137
132
return resp
138
133
}
139
134
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
+
140
158
func (s * Storage ) createObjectKey (originalFilename string , source plugin.UploadSource ) string {
141
159
ext := strings .ToLower (filepath .Ext (originalFilename ))
142
160
randomString := s .randomObjectKey ()
@@ -145,6 +163,8 @@ func (s *Storage) createObjectKey(originalFilename string, source plugin.UploadS
145
163
return s .Config .ObjectKeyPrefix + "avatar/" + randomString + ext
146
164
case plugin .UserPost :
147
165
return s .Config .ObjectKeyPrefix + "post/" + randomString + ext
166
+ case plugin .UserPostAttachment :
167
+ return s .Config .ObjectKeyPrefix + "attachment/" + randomString + ext
148
168
case plugin .AdminBranding :
149
169
return s .Config .ObjectKeyPrefix + "branding/" + randomString + ext
150
170
default :
@@ -166,17 +186,6 @@ func (s *Storage) CheckFileType(originalFilename string, source plugin.UploadSou
166
186
return false
167
187
}
168
188
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
-
180
189
func (s * Storage ) ConfigFields () []plugin.ConfigField {
181
190
return []plugin.ConfigField {
182
191
{
@@ -245,17 +254,6 @@ func (s *Storage) ConfigFields() []plugin.ConfigField {
245
254
},
246
255
Value : s .Config .VisitUrlPrefix ,
247
256
},
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
- },
259
257
}
260
258
}
261
259
0 commit comments