diff options
-rw-r--r-- | api/user.go | 68 | ||||
-rw-r--r-- | api/user_test.go | 15 |
2 files changed, 48 insertions, 35 deletions
diff --git a/api/user.go b/api/user.go index 83e29b28e..6af737df3 100644 --- a/api/user.go +++ b/api/user.go @@ -567,7 +567,7 @@ func getAudits(c *Context, w http.ResponseWriter, r *http.Request) { } } -func createProfileImage(username string, userId string) *image.RGBA { +func createProfileImage(username string, userId string) ([]byte, *model.AppError) { colors := []color.NRGBA{ {197, 8, 126, 255}, @@ -634,16 +634,17 @@ func createProfileImage(username string, userId string) *image.RGBA { gc.Translate(width, height) gc.SetFillColor(image.White) gc.FillString(initials) - return i -} -func getProfileImage(c *Context, w http.ResponseWriter, r *http.Request) { - if !utils.IsS3Configured() { - c.Err = model.NewAppError("getProfileImage", "Unable to get image. Amazon S3 not configured. ", "") - c.Err.StatusCode = http.StatusNotImplemented - return + buf := new(bytes.Buffer) + + if imgErr := png.Encode(buf, i); imgErr != nil { + return nil, model.NewAppError("getProfileImage", "Could not encode default profile image", imgErr.Error()) + } else { + return buf.Bytes(), nil } +} +func getProfileImage(c *Context, w http.ResponseWriter, r *http.Request) { params := mux.Vars(r) id := params["id"] @@ -651,36 +652,41 @@ func getProfileImage(c *Context, w http.ResponseWriter, r *http.Request) { c.Err = result.Err return } else { - var auth aws.Auth - auth.AccessKey = utils.Cfg.AWSSettings.S3AccessKeyId - auth.SecretKey = utils.Cfg.AWSSettings.S3SecretAccessKey + var img []byte + var err *model.AppError + + if !utils.IsS3Configured() { + img, err = createProfileImage(result.Data.(*model.User).Username, id) + if err != nil { + c.Err = err + return + } + } else { + var auth aws.Auth + auth.AccessKey = utils.Cfg.AWSSettings.S3AccessKeyId + auth.SecretKey = utils.Cfg.AWSSettings.S3SecretAccessKey - s := s3.New(auth, aws.Regions[utils.Cfg.AWSSettings.S3Region]) - bucket := s.Bucket(utils.Cfg.AWSSettings.S3Bucket) + s := s3.New(auth, aws.Regions[utils.Cfg.AWSSettings.S3Region]) + bucket := s.Bucket(utils.Cfg.AWSSettings.S3Bucket) - path := "teams/" + c.Session.TeamId + "/users/" + id + "/profile.png" + path := "teams/" + c.Session.TeamId + "/users/" + id + "/profile.png" - var img []byte + if data, getErr := bucket.Get(path); getErr != nil { + img, err = createProfileImage(result.Data.(*model.User).Username, id) + if err != nil { + c.Err = err + return + } - if data, getErr := bucket.Get(path); getErr != nil { - rawImg := createProfileImage(result.Data.(*model.User).Username, id) - buf := new(bytes.Buffer) + options := s3.Options{} + if err := bucket.Put(path, img, "image", s3.Private, options); err != nil { + c.Err = model.NewAppError("getImage", "Couldn't upload default profile image", err.Error()) + return + } - if imgErr := png.Encode(buf, rawImg); imgErr != nil { - c.Err = model.NewAppError("getProfileImage", "Could not encode default profile image", imgErr.Error()) - return } else { - img = buf.Bytes() - } - - options := s3.Options{} - if err := bucket.Put(path, buf.Bytes(), "image", s3.Private, options); err != nil { - c.Err = model.NewAppError("getImage", "Couldn't upload default profile image", err.Error()) - return + img = data } - - } else { - img = data } if c.Session.UserId == id { diff --git a/api/user_test.go b/api/user_test.go index 4d5d2b3f0..92ab216aa 100644 --- a/api/user_test.go +++ b/api/user_test.go @@ -10,6 +10,7 @@ import ( "github.com/goamz/goamz/s3" "github.com/mattermost/platform/model" "github.com/mattermost/platform/utils" + "image" "image/color" "io" "mime/multipart" @@ -324,14 +325,20 @@ func TestGetAudits(t *testing.T) { func TestUserCreateImage(t *testing.T) { Setup() - i := createProfileImage("Corey Hulen", "eo1zkdr96pdj98pjmq8zy35wba") - if i == nil { - t.Fatal("Failed to gen image") + b, err := createProfileImage("Corey Hulen", "eo1zkdr96pdj98pjmq8zy35wba") + if err != nil { + t.Fatal(err) + } + + rdr := bytes.NewReader(b) + img, _, err2 := image.Decode(rdr) + if err2 != nil { + t.Fatal(err) } colorful := color.RGBA{116, 49, 196, 255} - if i.RGBAAt(1, 1) != colorful { + if img.At(1, 1) != colorful { t.Fatal("Failed to create correct color") } |