mirror of
https://github.com/casdoor/casdoor.git
synced 2025-07-03 12:30:19 +08:00
feat: added avatar tailoring and uploading
Signed-off-by: Kininaru <shiftregister233@outlook.com> fixed type errors Signed-off-by: Kininaru <shiftregister233@outlook.com> fixed the wrong folder Signed-off-by: Kininaru <shiftregister233@outlook.com> rewrite login check logic, added unix time to avatar url Signed-off-by: Kininaru <shiftregister233@outlook.com> fixed a bug about strconv Signed-off-by: Kininaru <shiftregister233@outlook.com> supported oss Signed-off-by: Kininaru <shiftregister233@outlook.com> disabled oss provide qiniu Signed-off-by: Kininaru <shiftregister233@outlook.com> Fixed avatar url error Signed-off-by: Kininaru <shiftregister233@outlook.com> Fixed avatar length bug Signed-off-by: Kininaru <shiftregister233@outlook.com> Fixed avatar length bug Signed-off-by: Kininaru <shiftregister233@outlook.com> fixed oss.conf Signed-off-by: Kininaru <shiftregister233@outlook.com> Put uploading avatar into UserEditPage Signed-off-by: Kininaru <shiftregister233@outlook.com> removed avatar dir Signed-off-by: Kininaru <shiftregister233@outlook.com> removed avatar in main.go Signed-off-by: Kininaru <shiftregister233@outlook.com> Made CropperDiv a reusable component, and updated README for OSS config Signed-off-by: Kininaru <shiftregister233@outlook.com> Convert ts to js Signed-off-by: Kininaru <shiftregister233@outlook.com> removed ts Signed-off-by: Kininaru <shiftregister233@outlook.com> fix: set avatar link to string 255 Signed-off-by: Kininaru <shiftregister233@outlook.com> fix: updated yarn lock Signed-off-by: Kininaru <shiftregister233@outlook.com> add: Casbin license Signed-off-by: Kininaru <shiftregister233@outlook.com>
This commit is contained in:
@ -1,11 +1,28 @@
|
||||
// Copyright 2021 The casbin Authors. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"strconv"
|
||||
"time"
|
||||
"github.com/casdoor/casdoor/object"
|
||||
"github.com/casdoor/casdoor/util"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type RegisterForm struct {
|
||||
@ -145,3 +162,40 @@ func (c *ApiController) GetAccount() {
|
||||
c.Data["json"] = resp
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
func (c *ApiController) UploadAvatar() {
|
||||
var resp Response
|
||||
username := c.GetSessionUser()
|
||||
userObj := object.GetUser(username)
|
||||
|
||||
msg := object.CheckUserLogin(userObj.Owner + "/" + userObj.Name, c.Ctx.Request.Form.Get("password"))
|
||||
if msg != "" {
|
||||
resp = Response{Status: "error", Msg: "Password wrong"}
|
||||
c.Data["json"] = resp
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
avatarBase64 := c.Ctx.Request.Form.Get("avatarfile")
|
||||
index := strings.Index(avatarBase64, ",")
|
||||
if index < 0 || avatarBase64[0: index] != "data:image/png;base64" {
|
||||
resp = Response{Status: "error", Msg: "File encoding error"}
|
||||
c.Data["json"] = resp
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
dist, _ := base64.StdEncoding.DecodeString(avatarBase64[index + 1:])
|
||||
msg = object.UploadAvatar(userObj.Name, dist)
|
||||
if msg != "" {
|
||||
resp = Response{Status: "error", Msg: msg}
|
||||
c.Data["json"] = resp
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
userObj.Avatar = object.GetAvatarPath() + userObj.Name + ".png?time=" + strconv.FormatInt(time.Now().UnixNano(), 10)
|
||||
object.UpdateUser(userObj.Owner + "/" + userObj.Name, userObj)
|
||||
resp = Response{Status: "ok", Msg: "Successfully set avatar"}
|
||||
c.Data["json"] = resp
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
Reference in New Issue
Block a user