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