aboutsummaryrefslogtreecommitdiff
path: root/internal/userutil
diff options
context:
space:
mode:
Diffstat (limited to 'internal/userutil')
-rw-r--r--internal/userutil/userutil.go47
-rw-r--r--internal/userutil/userutil_test.go39
2 files changed, 84 insertions, 2 deletions
diff --git a/internal/userutil/userutil.go b/internal/userutil/userutil.go
index 87b8f15b..d5c74325 100644
--- a/internal/userutil/userutil.go
+++ b/internal/userutil/userutil.go
@@ -7,8 +7,15 @@ package userutil
import (
"encoding/hex"
"fmt"
+ "image/png"
+ "os"
+ "path/filepath"
+ "strconv"
"strings"
+ "github.com/pkg/errors"
+
+ "gogs.io/gogs/internal/avatar"
"gogs.io/gogs/internal/conf"
"gogs.io/gogs/internal/tool"
)
@@ -23,9 +30,9 @@ func DashboardURLPath(name string, isOrganization bool) string {
// GenerateActivateCode generates an activate code based on user information and
// the given email.
-func GenerateActivateCode(id int64, email, name, password, rands string) string {
+func GenerateActivateCode(userID int64, email, name, password, rands string) string {
code := tool.CreateTimeLimitCode(
- fmt.Sprintf("%d%s%s%s%s", id, email, strings.ToLower(name), password, rands),
+ fmt.Sprintf("%d%s%s%s%s", userID, email, strings.ToLower(name), password, rands),
conf.Auth.ActivateCodeLives,
nil,
)
@@ -34,3 +41,39 @@ func GenerateActivateCode(id int64, email, name, password, rands string) string
code += hex.EncodeToString([]byte(strings.ToLower(name)))
return code
}
+
+// CustomAvatarPath returns the absolute path of the user custom avatar file.
+func CustomAvatarPath(userID int64) string {
+ return filepath.Join(conf.Picture.AvatarUploadPath, strconv.FormatInt(userID, 10))
+}
+
+// GenerateRandomAvatar generates a random avatar and stores to local file
+// system using given user information.
+func GenerateRandomAvatar(userID int64, name, email string) error {
+ seed := email
+ if seed == "" {
+ seed = name
+ }
+
+ img, err := avatar.RandomImage([]byte(seed))
+ if err != nil {
+ return errors.Wrap(err, "generate random image")
+ }
+
+ avatarPath := CustomAvatarPath(userID)
+ err = os.MkdirAll(filepath.Dir(avatarPath), os.ModePerm)
+ if err != nil {
+ return errors.Wrap(err, "create avatar directory")
+ }
+
+ f, err := os.Create(avatarPath)
+ if err != nil {
+ return errors.Wrap(err, "create avatar file")
+ }
+ defer func() { _ = f.Close() }()
+
+ if err = png.Encode(f, img); err != nil {
+ return errors.Wrap(err, "encode avatar image to file")
+ }
+ return nil
+}
diff --git a/internal/userutil/userutil_test.go b/internal/userutil/userutil_test.go
index a62363a5..e90c9235 100644
--- a/internal/userutil/userutil_test.go
+++ b/internal/userutil/userutil_test.go
@@ -5,11 +5,15 @@
package userutil
import (
+ "os"
+ "runtime"
"testing"
"github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
"gogs.io/gogs/internal/conf"
+ "gogs.io/gogs/internal/osutil"
"gogs.io/gogs/internal/tool"
)
@@ -38,3 +42,38 @@ func TestGenerateActivateCode(t *testing.T) {
got := tool.VerifyTimeLimitCode("1alice@example.comalice123456rands", conf.Auth.ActivateCodeLives, code[:tool.TIME_LIMIT_CODE_LENGTH])
assert.True(t, got)
}
+
+func TestCustomAvatarPath(t *testing.T) {
+ if runtime.GOOS == "windows" {
+ t.Skip("Skipping testing on Windows")
+ return
+ }
+
+ conf.SetMockPicture(t,
+ conf.PictureOpts{
+ AvatarUploadPath: "data/avatars",
+ },
+ )
+
+ got := CustomAvatarPath(1)
+ want := "data/avatars/1"
+ assert.Equal(t, want, got)
+}
+
+func TestGenerateRandomAvatar(t *testing.T) {
+ if runtime.GOOS == "windows" {
+ t.Skip("Skipping testing on Windows")
+ return
+ }
+
+ conf.SetMockPicture(t,
+ conf.PictureOpts{
+ AvatarUploadPath: os.TempDir(),
+ },
+ )
+
+ err := GenerateRandomAvatar(1, "alice", "alice@example.com")
+ require.NoError(t, err)
+ got := osutil.IsFile(CustomAvatarPath(1))
+ assert.True(t, got)
+}