mirror of
https://github.com/casdoor/casdoor.git
synced 2025-05-23 02:35:49 +08:00
Improve UploadAvatar API.
This commit is contained in:
parent
ee8f2a6046
commit
ea9bdbf45e
@ -17,9 +17,8 @@ package controllers
|
|||||||
import (
|
import (
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"strconv"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/casdoor/casdoor/object"
|
"github.com/casdoor/casdoor/object"
|
||||||
"github.com/casdoor/casdoor/util"
|
"github.com/casdoor/casdoor/util"
|
||||||
@ -150,17 +149,17 @@ func (c *ApiController) GetAccount() {
|
|||||||
// @router /upload-avatar [post]
|
// @router /upload-avatar [post]
|
||||||
func (c *ApiController) UploadAvatar() {
|
func (c *ApiController) UploadAvatar() {
|
||||||
var resp Response
|
var resp Response
|
||||||
username := c.GetSessionUser()
|
|
||||||
user := object.GetUser(username)
|
|
||||||
|
|
||||||
msg := object.CheckUserLogin(user.Owner+"/"+user.Name, c.Ctx.Request.Form.Get("password"))
|
username := c.GetSessionUser()
|
||||||
if msg != "" {
|
if c.GetSessionUser() == "" {
|
||||||
resp = Response{Status: "error", Msg: "Wrong password"}
|
resp = Response{Status: "error", Msg: "Please sign in first", Data: c.GetSessionUser()}
|
||||||
c.Data["json"] = resp
|
c.Data["json"] = resp
|
||||||
c.ServeJSON()
|
c.ServeJSON()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
user := object.GetUser(username)
|
||||||
|
|
||||||
avatarBase64 := c.Ctx.Request.Form.Get("avatarfile")
|
avatarBase64 := c.Ctx.Request.Form.Get("avatarfile")
|
||||||
index := strings.Index(avatarBase64, ",")
|
index := strings.Index(avatarBase64, ",")
|
||||||
if index < 0 || avatarBase64[0:index] != "data:image/png;base64" {
|
if index < 0 || avatarBase64[0:index] != "data:image/png;base64" {
|
||||||
@ -171,16 +170,16 @@ func (c *ApiController) UploadAvatar() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dist, _ := base64.StdEncoding.DecodeString(avatarBase64[index+1:])
|
dist, _ := base64.StdEncoding.DecodeString(avatarBase64[index+1:])
|
||||||
msg = object.UploadAvatar(user.Name, dist)
|
msg := object.UploadAvatar(user.Name, dist)
|
||||||
if msg != "" {
|
if msg != "" {
|
||||||
resp = Response{Status: "error", Msg: msg}
|
resp = Response{Status: "error", Msg: msg}
|
||||||
c.Data["json"] = resp
|
c.Data["json"] = resp
|
||||||
c.ServeJSON()
|
c.ServeJSON()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
user.Avatar = object.GetAvatarPath() + user.Name + ".png?time=" + strconv.FormatInt(time.Now().UnixNano(), 10)
|
user.Avatar = fmt.Sprintf("%s%s.png?time=%s", object.GetAvatarPath(), user.Name, util.GetCurrentUnixTime())
|
||||||
object.UpdateUser(user.Owner+"/"+user.Name, user)
|
object.UpdateUser(user.Owner+"/"+user.Name, user)
|
||||||
resp = Response{Status: "ok", Msg: "Successfully set avatar"}
|
resp = Response{Status: "ok", Msg: ""}
|
||||||
c.Data["json"] = resp
|
c.Data["json"] = resp
|
||||||
c.ServeJSON()
|
c.ServeJSON()
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
package util
|
package util
|
||||||
|
|
||||||
import "time"
|
import (
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
func GetCurrentTime() string {
|
func GetCurrentTime() string {
|
||||||
timestamp := time.Now().Unix()
|
timestamp := time.Now().Unix()
|
||||||
@ -8,3 +11,6 @@ func GetCurrentTime() string {
|
|||||||
return tm.Format(time.RFC3339)
|
return tm.Format(time.RFC3339)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetCurrentUnixTime() string {
|
||||||
|
return strconv.FormatInt(time.Now().UnixNano(), 10)
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user