@@ -26,23 +26,18 @@ import (
26
26
"encoding/json"
27
27
"fmt"
28
28
"path/filepath"
29
- "strconv"
30
29
"strings"
31
30
"time"
32
31
33
32
"github.com/apache/answer-plugins/storage-s3/i18n"
34
33
"github.com/apache/answer-plugins/util"
34
+ "github.com/apache/answer/pkg/checker"
35
35
"github.com/apache/answer/plugin"
36
36
)
37
37
38
38
//go:embed info.yaml
39
39
var Info embed.FS
40
40
41
- const (
42
- // 10MB
43
- defaultMaxFileSize int64 = 10 * 1024 * 1024
44
- )
45
-
46
41
type Storage struct {
47
42
Config * StorageConfig
48
43
Client * Client
@@ -56,7 +51,6 @@ type StorageConfig struct {
56
51
AccessKeySecret string `json:"access_key_secret"`
57
52
AccessToken string `json:"access_token"`
58
53
VisitUrlPrefix string `json:"visit_url_prefix"`
59
- MaxFileSize string `json:"max_file_size"`
60
54
Region string `json:"region"`
61
55
DisableSSL bool `json:"disable_ssl"`
62
56
}
@@ -81,7 +75,7 @@ func (s *Storage) Info() plugin.Info {
81
75
}
82
76
}
83
77
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 ) {
85
79
resp = plugin.UploadFileResponse {}
86
80
87
81
file , err := ctx .FormFile ("file" )
@@ -91,13 +85,13 @@ func (s *Storage) UploadFile(ctx *plugin.GinContext, source plugin.UploadSource)
91
85
return resp
92
86
}
93
87
94
- if ! s . checkFileType (file .Filename , source ) {
88
+ if s . IsUnsupportedFileType (file .Filename , condition ) {
95
89
resp .OriginalError = fmt .Errorf ("file type not allowed" )
96
90
resp .DisplayErrorMsg = plugin .MakeTranslator (i18n .ErrUnsupportedFileType )
97
91
return resp
98
92
}
99
93
100
- if file .Size > s . maxFileSizeLimit ( ) {
94
+ if s . ExceedFileSizeLimit ( file .Size , condition ) {
101
95
resp .OriginalError = fmt .Errorf ("file size too large" )
102
96
resp .DisplayErrorMsg = plugin .MakeTranslator (i18n .ErrOverFileSizeLimit )
103
97
return resp
@@ -111,7 +105,7 @@ func (s *Storage) UploadFile(ctx *plugin.GinContext, source plugin.UploadSource)
111
105
}
112
106
defer openFile .Close ()
113
107
114
- objectKey := s .createObjectKey (file .Filename , source )
108
+ objectKey := s .createObjectKey (file .Filename , condition . Source )
115
109
err = s .Client .PutObject (objectKey , strings .ToLower (filepath .Ext (file .Filename )), openFile )
116
110
if err != nil {
117
111
resp .OriginalError = fmt .Errorf ("upload file failed: %v" , err )
@@ -122,6 +116,29 @@ func (s *Storage) UploadFile(ctx *plugin.GinContext, source plugin.UploadSource)
122
116
return resp
123
117
}
124
118
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
+
125
142
func (s * Storage ) createObjectKey (originalFilename string , source plugin.UploadSource ) string {
126
143
ext := strings .ToLower (filepath .Ext (originalFilename ))
127
144
randomString := s .randomObjectKey ()
@@ -130,6 +147,8 @@ func (s *Storage) createObjectKey(originalFilename string, source plugin.UploadS
130
147
return s .Config .ObjectKeyPrefix + "avatar/" + randomString + ext
131
148
case plugin .UserPost :
132
149
return s .Config .ObjectKeyPrefix + "post/" + randomString + ext
150
+ case plugin .UserPostAttachment :
151
+ return s .Config .ObjectKeyPrefix + "attachment/" + randomString + ext
133
152
case plugin .AdminBranding :
134
153
return s .Config .ObjectKeyPrefix + "branding/" + randomString + ext
135
154
default :
@@ -151,17 +170,6 @@ func (s *Storage) checkFileType(originalFilename string, source plugin.UploadSou
151
170
return false
152
171
}
153
172
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
-
165
173
func (s * Storage ) ConfigFields () []plugin.ConfigField {
166
174
return []plugin.ConfigField {
167
175
{
@@ -241,17 +249,6 @@ func (s *Storage) ConfigFields() []plugin.ConfigField {
241
249
},
242
250
Value : s .Config .VisitUrlPrefix ,
243
251
},
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
- },
255
252
{
256
253
Name : "region" ,
257
254
Type : plugin .ConfigTypeInput ,
0 commit comments