summaryrefslogtreecommitdiffstats
path: root/api/user.go
diff options
context:
space:
mode:
authorCorey Hulen <corey@hulen.com>2015-06-19 10:41:56 -0800
committerCorey Hulen <corey@hulen.com>2015-06-19 10:41:56 -0800
commit79a1608a2bbf66646dc8f308c2846824aaa3a2b9 (patch)
treeb9c2c93fe8f7b2f61819c4c1dce18700d7147c23 /api/user.go
parent447f22bbbd8eaacc6005db82aab0dbc69b9f15e1 (diff)
parent0f1771bd7fc5617346d249cf8fef0690e433a0ba (diff)
downloadchat-79a1608a2bbf66646dc8f308c2846824aaa3a2b9.tar.gz
chat-79a1608a2bbf66646dc8f308c2846824aaa3a2b9.tar.bz2
chat-79a1608a2bbf66646dc8f308c2846824aaa3a2b9.zip
Merge pull request #40 from mattermost/image-fix
HELIUM profile image now generates on each call if S3 is not configured
Diffstat (limited to 'api/user.go')
-rw-r--r--api/user.go68
1 files changed, 37 insertions, 31 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 {