aboutsummaryrefslogtreecommitdiff
path: root/internal/userutil/userutil.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/userutil/userutil.go')
-rw-r--r--internal/userutil/userutil.go29
1 files changed, 29 insertions, 0 deletions
diff --git a/internal/userutil/userutil.go b/internal/userutil/userutil.go
index 8063aef0..d8a8b031 100644
--- a/internal/userutil/userutil.go
+++ b/internal/userutil/userutil.go
@@ -5,16 +5,19 @@
package userutil
import (
+ "bytes"
"crypto/sha256"
"crypto/subtle"
"encoding/hex"
"fmt"
+ "image"
"image/png"
"os"
"path/filepath"
"strconv"
"strings"
+ "github.com/nfnt/resize"
"github.com/pkg/errors"
"golang.org/x/crypto/pbkdf2"
@@ -81,6 +84,32 @@ func GenerateRandomAvatar(userID int64, name, email string) error {
return nil
}
+// SaveAvatar saves the given avatar for the user.
+func SaveAvatar(userID int64, data []byte) error {
+ img, _, err := image.Decode(bytes.NewReader(data))
+ if err != nil {
+ return errors.Wrap(err, "decode 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() }()
+
+ m := resize.Resize(avatar.DefaultSize, avatar.DefaultSize, img, resize.NearestNeighbor)
+ if err = png.Encode(f, m); err != nil {
+ return errors.Wrap(err, "encode avatar image to file")
+ }
+ return nil
+}
+
// EncodePassword encodes password using PBKDF2 SHA256 with given salt.
func EncodePassword(password, salt string) string {
newPasswd := pbkdf2.Key([]byte(password), []byte(salt), 10000, 50, sha256.New)