summaryrefslogtreecommitdiffstats
path: root/api
diff options
context:
space:
mode:
Diffstat (limited to 'api')
-rw-r--r--api/auto_constants.go2
-rw-r--r--api/context.go4
-rw-r--r--api/post.go2
-rw-r--r--api/user.go99
-rw-r--r--api/user_test.go15
5 files changed, 55 insertions, 67 deletions
diff --git a/api/auto_constants.go b/api/auto_constants.go
index 7af90a5f1..3f8831055 100644
--- a/api/auto_constants.go
+++ b/api/auto_constants.go
@@ -32,5 +32,5 @@ var (
POST_MESSAGE_LEN = utils.Range{100, 400}
POST_HASHTAGS_NUM = utils.Range{5, 10}
POST_MENTIONS_NUM = utils.Range{0, 3}
- TEST_IMAGE_FILENAMES = []string{"test.png", "salamander.jpg", "toothless.gif"}
+ TEST_IMAGE_FILENAMES = []string{"test.png", "testjpg.jpg", "testgif.gif"}
)
diff --git a/api/context.go b/api/context.go
index 16105c8af..501e4e77f 100644
--- a/api/context.go
+++ b/api/context.go
@@ -303,6 +303,10 @@ func IsTestDomain(r *http.Request) bool {
return true
}
+ if strings.Index(r.Host, "dockerhost") == 0 {
+ return true
+ }
+
if strings.Index(r.Host, "test") == 0 {
return true
}
diff --git a/api/post.go b/api/post.go
index 36607c231..3acc95551 100644
--- a/api/post.go
+++ b/api/post.go
@@ -302,7 +302,7 @@ func fireAndForgetNotifications(post *model.Post, teamId, teamUrl string) {
// Build a map as a list of unique user_ids that are mentioned in this post
splitF := func(c rune) bool {
- return c == ',' || c == ' ' || c == '.' || c == '!' || c == '?' || c == ':' || c == '<' || c == '>'
+ return model.SplitRunes[c]
}
splitMessage := strings.FieldsFunc(strings.Replace(post.Message, "<br>", " ", -1), splitF)
for _, word := range splitMessage {
diff --git a/api/user.go b/api/user.go
index 83e29b28e..79d4bb32c 100644
--- a/api/user.go
+++ b/api/user.go
@@ -5,7 +5,6 @@ package api
import (
"bytes"
- "code.google.com/p/draw2d/draw2d"
l4g "code.google.com/p/log4go"
"fmt"
"github.com/goamz/goamz/aws"
@@ -19,6 +18,7 @@ import (
"hash/fnv"
"image"
"image/color"
+ "image/draw"
_ "image/gif"
_ "image/jpeg"
"image/png"
@@ -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},
@@ -602,48 +602,20 @@ func createProfileImage(username string, userId string) *image.RGBA {
h.Write([]byte(userId))
seed := h.Sum32()
- initials := ""
- parts := strings.Split(username, " ")
+ color := colors[int(seed)%len(colors)]
+ img := image.NewRGBA(image.Rect(0, 0, int(utils.Cfg.ImageSettings.ProfileWidth), int(utils.Cfg.ImageSettings.ProfileHeight)))
+ draw.Draw(img, img.Bounds(), &image.Uniform{color}, image.ZP, draw.Src)
- for _, v := range parts {
-
- if len(v) > 0 {
- initials += string(strings.ToUpper(v)[0])
- }
- }
-
- if len(initials) == 0 {
- initials = "^"
- }
+ buf := new(bytes.Buffer)
- if len(initials) > 2 {
- initials = initials[0:2]
+ if imgErr := png.Encode(buf, img); imgErr != nil {
+ return nil, model.NewAppError("getProfileImage", "Could not encode default profile image", imgErr.Error())
+ } else {
+ return buf.Bytes(), nil
}
-
- draw2d.SetFontFolder(utils.FindDir("web/static/fonts"))
- i := image.NewRGBA(image.Rect(0, 0, 128, 128))
- gc := draw2d.NewGraphicContext(i)
- draw2d.Rect(gc, 0, 0, 128, 128)
- gc.SetFillColor(colors[int(seed)%len(colors)])
- gc.Fill()
- gc.SetFontSize(50)
- gc.SetFontData(draw2d.FontData{"luxi", draw2d.FontFamilyMono, draw2d.FontStyleBold | draw2d.FontStyleItalic})
- left, top, right, bottom := gc.GetStringBounds("CH")
- width := (128 - (right - left + 10)) / 2
- height := (128 - (top - bottom + 6)) / 2
- 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
- }
-
params := mux.Vars(r)
id := params["id"]
@@ -651,36 +623,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
- s := s3.New(auth, aws.Regions[utils.Cfg.AWSSettings.S3Region])
- bucket := s.Bucket(utils.Cfg.AWSSettings.S3Bucket)
+ 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
- path := "teams/" + c.Session.TeamId + "/users/" + id + "/profile.png"
+ s := s3.New(auth, aws.Regions[utils.Cfg.AWSSettings.S3Region])
+ bucket := s.Bucket(utils.Cfg.AWSSettings.S3Bucket)
- var img []byte
+ path := "teams/" + c.Session.TeamId + "/users/" + id + "/profile.png"
- if data, getErr := bucket.Get(path); getErr != nil {
- rawImg := createProfileImage(result.Data.(*model.User).Username, id)
- buf := new(bytes.Buffer)
+ 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 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, img, "image", s3.Private, options); err != nil {
+ c.Err = model.NewAppError("getImage", "Couldn't upload default profile image", err.Error())
+ return
+ }
- 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
+ } else {
+ 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")
}