mirror of
https://github.com/casdoor/casdoor.git
synced 2025-05-23 02:35:49 +08:00
Fix avatar upload.
This commit is contained in:
parent
8ea906a132
commit
af3def97bf
@ -241,6 +241,10 @@ func (c *ApiController) UploadAvatar() {
|
||||
user := object.GetUser(userId)
|
||||
application := object.GetApplicationByUser(user)
|
||||
provider := application.GetStorageProvider()
|
||||
if provider == nil {
|
||||
c.ResponseError("No storage provider is found")
|
||||
return
|
||||
}
|
||||
|
||||
avatarBase64 := c.Ctx.Request.Form.Get("avatarfile")
|
||||
index := strings.Index(avatarBase64, ",")
|
||||
@ -259,8 +263,10 @@ func (c *ApiController) UploadAvatar() {
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
user.Avatar = fmt.Sprintf("%s%s.png?time=%s", object.GetAvatarPath(provider), user.GetId(), util.GetCurrentUnixTime())
|
||||
|
||||
user.Avatar = fmt.Sprintf("%s/%s.png?time=%s", util.UrlJoin(provider.Domain, "/avatar"), user.GetId(), util.GetCurrentUnixTime())
|
||||
object.UpdateUser(user.GetId(), user)
|
||||
|
||||
resp = Response{Status: "ok", Msg: ""}
|
||||
c.Data["json"] = resp
|
||||
c.ServeJSON()
|
||||
|
@ -43,7 +43,7 @@ func (c *ApiController) SendEmail() {
|
||||
|
||||
provider := app.GetEmailProvider()
|
||||
if provider == nil {
|
||||
c.ResponseError("No Email provider for this application.")
|
||||
c.ResponseError("No Email provider is found")
|
||||
return
|
||||
}
|
||||
|
||||
@ -112,7 +112,7 @@ func (c *ApiController) SendSms() {
|
||||
|
||||
provider := app.GetSmsProvider()
|
||||
if provider == nil {
|
||||
c.ResponseError("No SMS provider for this application.")
|
||||
c.ResponseError("No SMS provider is found")
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -52,23 +52,23 @@ func getAliyunClient(provider *Provider) oss.StorageInterface {
|
||||
func getQiniuClient(provider *Provider) oss.StorageInterface {
|
||||
fmt.Println("Casdoor does not support Qiniu now.")
|
||||
return nil
|
||||
// endpoint := section.Key("endpoint").String()
|
||||
// accessId := section.Key("accessId").String()
|
||||
// accessKey := section.Key("accessKey").String()
|
||||
// domain = section.Key("domain").String()
|
||||
// bucket := section.Key("bucket").String()
|
||||
// region := section.Key("region").String()
|
||||
// if accessId == "" || accessKey == "" || bucket == "" || endpoint == "" || region == "" {
|
||||
// return "Config oss.conf wrong"
|
||||
// }
|
||||
// storage = qiniu.New(&qiniu.Config{
|
||||
// AccessID: accessId,
|
||||
// AccessKey: accessKey,
|
||||
// Bucket: bucket,
|
||||
// Region: region,
|
||||
// Endpoint: endpoint,
|
||||
// })
|
||||
// return ""
|
||||
// endpoint := section.Key("endpoint").String()
|
||||
// accessId := section.Key("accessId").String()
|
||||
// accessKey := section.Key("accessKey").String()
|
||||
// domain = section.Key("domain").String()
|
||||
// bucket := section.Key("bucket").String()
|
||||
// region := section.Key("region").String()
|
||||
// if accessId == "" || accessKey == "" || bucket == "" || endpoint == "" || region == "" {
|
||||
// return "Config oss.conf wrong"
|
||||
// }
|
||||
// storage = qiniu.New(&qiniu.Config{
|
||||
// AccessID: accessId,
|
||||
// AccessKey: accessKey,
|
||||
// Bucket: bucket,
|
||||
// Region: region,
|
||||
// Endpoint: endpoint,
|
||||
// })
|
||||
// return ""
|
||||
}
|
||||
|
||||
func getAwss3Client(provider *Provider) oss.StorageInterface {
|
||||
@ -103,11 +103,11 @@ func getStorageClient(provider *Provider) oss.StorageInterface {
|
||||
}
|
||||
|
||||
switch provider.Type {
|
||||
case "Aliyun":
|
||||
case "Aliyun OSS":
|
||||
return getAliyunClient(provider)
|
||||
case "Qiniu":
|
||||
return getQiniuClient(provider)
|
||||
case "AWSS3":
|
||||
case "AWS S3":
|
||||
return getAwss3Client(provider)
|
||||
}
|
||||
|
||||
@ -115,22 +115,15 @@ func getStorageClient(provider *Provider) oss.StorageInterface {
|
||||
}
|
||||
|
||||
func UploadAvatar(provider *Provider, username string, avatar []byte) string {
|
||||
if provider == nil {
|
||||
return "invalid Storage provider"
|
||||
}
|
||||
storage := getStorageClient(provider)
|
||||
if storage == nil {
|
||||
return "oss provider not exists"
|
||||
return fmt.Sprintf("Provider type: %s is not supported", provider.Type)
|
||||
}
|
||||
|
||||
path := fmt.Sprintf("/casdoor/avatar/%s.png", username)
|
||||
path := fmt.Sprintf("%s/%s.png", util.UrlJoin(util.GetUrlPath(provider.Domain), "/avatar"), username)
|
||||
_, err := storage.Put(path, bytes.NewReader(avatar))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return err.Error()
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func GetAvatarPath(provider *Provider) string {
|
||||
return fmt.Sprintf("https://%s/casdoor/avatar/", provider.Domain)
|
||||
}
|
||||
|
21
util/path.go
21
util/path.go
@ -14,7 +14,12 @@
|
||||
|
||||
package util
|
||||
|
||||
import "os"
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func FileExist(path string) bool {
|
||||
if _, err := os.Stat(path); os.IsNotExist(err) {
|
||||
@ -22,3 +27,17 @@ func FileExist(path string) bool {
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func UrlJoin(base string, path string) string {
|
||||
if !strings.HasPrefix(base, "http://") && !strings.HasPrefix(base, "https://") {
|
||||
base = fmt.Sprintf("https://%s", base)
|
||||
}
|
||||
|
||||
res := fmt.Sprintf("%s/%s", strings.TrimRight(base, "/"), strings.TrimLeft(path, "/"))
|
||||
return res
|
||||
}
|
||||
|
||||
func GetUrlPath(urlString string) string {
|
||||
u, _ := url.Parse(urlString)
|
||||
return u.Path
|
||||
}
|
||||
|
@ -53,7 +53,7 @@ export const CropperDiv = (props) => {
|
||||
Setting.showMessage("error", "You must select a picture first!");
|
||||
return false;
|
||||
}
|
||||
Setting.showMessage("success", "uploading...");
|
||||
// Setting.showMessage("success", "uploading...");
|
||||
targetFunction(canvas.toDataURL());
|
||||
return true;
|
||||
}
|
||||
|
@ -77,19 +77,19 @@ export function getAffiliationOptions(url, code) {
|
||||
}
|
||||
|
||||
export function uploadAvatar(avatar) {
|
||||
let account;
|
||||
AuthBackend.getAccount("").then((res) => {
|
||||
account = res.data;
|
||||
let formData = new FormData();
|
||||
formData.append("avatarfile", avatar);
|
||||
formData.append("password", account.password);
|
||||
fetch(`${Setting.ServerUrl}/api/upload-avatar`, {
|
||||
body: formData,
|
||||
method: 'POST',
|
||||
credentials: 'include',
|
||||
}).then((res) => {
|
||||
let formData = new FormData();
|
||||
formData.append("avatarfile", avatar);
|
||||
fetch(`${Setting.ServerUrl}/api/upload-avatar`, {
|
||||
body: formData,
|
||||
method: 'POST',
|
||||
credentials: 'include',
|
||||
}).then(res => res.json())
|
||||
.then((res) => {
|
||||
if (res.status === "ok") {
|
||||
window.location.href = "/account";
|
||||
});
|
||||
} else {
|
||||
Setting.showMessage("error", res.msg);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user