File tree Expand file tree Collapse file tree 3 files changed +19
-0
lines changed Expand file tree Collapse file tree 3 files changed +19
-0
lines changed Original file line number Diff line number Diff line change @@ -391,6 +391,10 @@ SESSION_LIFE_TIME = 86400
391
391
392
392
[picture]
393
393
AVATAR_UPLOAD_PATH = data/avatars
394
+ ; Max Width and Height of uploaded avatars. This is to limit the amount of RAM
395
+ ; used when resizing the image.
396
+ AVATAR_MAX_WIDTH = 4096
397
+ AVATAR_MAX_HEIGHT = 3072
394
398
; Chinese users can choose "duoshuo"
395
399
; or a custom avatar source, like: http://cn.gravatar.com/avatar/
396
400
GRAVATAR_SOURCE = gravatar
Original file line number Diff line number Diff line change @@ -430,6 +430,17 @@ func (u *User) IsPasswordSet() bool {
430
430
// UploadAvatar saves custom avatar for user.
431
431
// FIXME: split uploads to different subdirs in case we have massive users.
432
432
func (u * User ) UploadAvatar (data []byte ) error {
433
+ imgCfg , _ , err := image .DecodeConfig (bytes .NewReader (data ))
434
+ if err != nil {
435
+ return fmt .Errorf ("DecodeConfig: %v" , err )
436
+ }
437
+ if imgCfg .Width > setting .AvatarMaxWidth {
438
+ return fmt .Errorf ("Image width is to large: %d > %d" , imgCfg .Width , setting .AvatarMaxWidth )
439
+ }
440
+ if imgCfg .Height > setting .AvatarMaxHeight {
441
+ return fmt .Errorf ("Image height is to large: %d > %d" , imgCfg .Height , setting .AvatarMaxHeight )
442
+ }
443
+
433
444
img , _ , err := image .Decode (bytes .NewReader (data ))
434
445
if err != nil {
435
446
return fmt .Errorf ("Decode: %v" , err )
Original file line number Diff line number Diff line change @@ -345,6 +345,8 @@ var (
345
345
346
346
// Picture settings
347
347
AvatarUploadPath string
348
+ AvatarMaxWidth int
349
+ AvatarMaxHeight int
348
350
GravatarSource string
349
351
GravatarSourceURL * url.URL
350
352
DisableGravatar bool
@@ -1036,6 +1038,8 @@ func NewContext() {
1036
1038
if ! filepath .IsAbs (AvatarUploadPath ) {
1037
1039
AvatarUploadPath = path .Join (AppWorkPath , AvatarUploadPath )
1038
1040
}
1041
+ AvatarMaxWidth = sec .Key ("AVATAR_MAX_WIDTH" ).MustInt (4096 )
1042
+ AvatarMaxHeight = sec .Key ("AVATAR_MAX_HEIGHT" ).MustInt (3072 )
1039
1043
switch source := sec .Key ("GRAVATAR_SOURCE" ).MustString ("gravatar" ); source {
1040
1044
case "duoshuo" :
1041
1045
GravatarSource = "http://gravatar.duoshuo.com/avatar/"
You can’t perform that action at this time.
0 commit comments