mirror of
https://github.com/casdoor/casdoor.git
synced 2025-05-23 02:35:49 +08:00
Add UploadFile() API.
This commit is contained in:
parent
40fbca7db4
commit
5c2f96bda0
@ -212,19 +212,23 @@ func (c *ApiController) GetAccount() {
|
||||
c.ResponseOk(user, organization)
|
||||
}
|
||||
|
||||
// UploadAvatar
|
||||
// @Title UploadAvatar
|
||||
// @Description upload avatar
|
||||
// @Param avatarfile formData string true "The base64 encode of avatarfile"
|
||||
// @Param password formData string true "The password"
|
||||
// UploadFile
|
||||
// @Title UploadFile
|
||||
// @Description upload file
|
||||
// @Param folder query string true "The folder"
|
||||
// @Param subFolder query string true "The sub folder"
|
||||
// @Param file formData string true "The file"
|
||||
// @Success 200 {object} controllers.Response The Response object
|
||||
// @router /upload-avatar [post]
|
||||
func (c *ApiController) UploadAvatar() {
|
||||
// @router /upload-file [post]
|
||||
func (c *ApiController) UploadFile() {
|
||||
userId, ok := c.RequireSignedIn()
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
folder := c.Input().Get("folder")
|
||||
subFolder := c.Input().Get("subFolder")
|
||||
|
||||
user := object.GetUser(userId)
|
||||
application := object.GetApplicationByUser(user)
|
||||
provider := application.GetStorageProvider()
|
||||
@ -233,24 +237,28 @@ func (c *ApiController) UploadAvatar() {
|
||||
return
|
||||
}
|
||||
|
||||
avatarBase64 := c.Ctx.Request.Form.Get("avatarfile")
|
||||
index := strings.Index(avatarBase64, ",")
|
||||
if index < 0 || avatarBase64[0:index] != "data:image/png;base64" {
|
||||
c.ResponseError("File encoding error")
|
||||
return
|
||||
fileString := c.Ctx.Request.Form.Get("file")
|
||||
|
||||
var fileBytes []byte
|
||||
pngHeader := "data:image/png;base64,"
|
||||
if strings.HasPrefix(fileString, pngHeader) {
|
||||
fileBytes, _ = base64.StdEncoding.DecodeString(fileString[len(pngHeader):])
|
||||
} else {
|
||||
fileBytes = []byte(fileString)
|
||||
}
|
||||
|
||||
dist, _ := base64.StdEncoding.DecodeString(avatarBase64[index+1:])
|
||||
fileUrl, err := object.UploadFile(provider, "avatar", user.GetId(), dist)
|
||||
fileUrl, err := object.UploadFile(provider, folder, subFolder, fileBytes)
|
||||
if err != nil {
|
||||
c.ResponseError(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
user.Avatar = fileUrl
|
||||
object.UpdateUser(user.GetId(), user)
|
||||
if folder == "avatar" {
|
||||
user.Avatar = fileUrl
|
||||
object.UpdateUser(user.GetId(), user)
|
||||
}
|
||||
|
||||
c.ResponseOk()
|
||||
c.ResponseOk(fileUrl)
|
||||
}
|
||||
|
||||
// GetHumanCheck ...
|
||||
|
@ -59,7 +59,7 @@ func initAPI() {
|
||||
beego.Router("/api/update-user", &controllers.ApiController{}, "POST:UpdateUser")
|
||||
beego.Router("/api/add-user", &controllers.ApiController{}, "POST:AddUser")
|
||||
beego.Router("/api/delete-user", &controllers.ApiController{}, "POST:DeleteUser")
|
||||
beego.Router("/api/upload-avatar", &controllers.ApiController{}, "POST:UploadAvatar")
|
||||
beego.Router("/api/upload-file", &controllers.ApiController{}, "POST:UploadFile")
|
||||
beego.Router("/api/set-password", &controllers.ApiController{}, "POST:SetPassword")
|
||||
beego.Router("/api/get-email-and-phone", &controllers.ApiController{}, "POST:GetEmailAndPhone")
|
||||
beego.Router("/api/send-verification-code", &controllers.ApiController{}, "POST:SendVerificationCode")
|
||||
|
@ -18,6 +18,7 @@ import "cropperjs/dist/cropper.css";
|
||||
import * as Setting from "./Setting";
|
||||
import {Button, Row, Col, Modal} from 'antd';
|
||||
import i18next from "i18next";
|
||||
import * as UserBackend from "./backend/UserBackend";
|
||||
|
||||
export const CropperDiv = (props) => {
|
||||
const [image, setImage] = useState("");
|
||||
@ -25,7 +26,7 @@ export const CropperDiv = (props) => {
|
||||
const [visible, setVisible] = React.useState(false);
|
||||
const [confirmLoading, setConfirmLoading] = React.useState(false);
|
||||
const {title} = props;
|
||||
const {targetFunction} = props;
|
||||
const {user} = props;
|
||||
const {buttonText} = props;
|
||||
let uploadButton;
|
||||
|
||||
@ -54,7 +55,8 @@ export const CropperDiv = (props) => {
|
||||
return false;
|
||||
}
|
||||
// Setting.showMessage("success", "uploading...");
|
||||
targetFunction(canvas.toDataURL());
|
||||
const userId = `${user.owner}/${user.name}`;
|
||||
UserBackend.uploadFile("avatar", userId, canvas.toDataURL());
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -76,10 +76,10 @@ export function getAffiliationOptions(url, code) {
|
||||
}).then(res => res.json());
|
||||
}
|
||||
|
||||
export function uploadAvatar(avatar) {
|
||||
export function uploadFile(folder, subFolder, file) {
|
||||
let formData = new FormData();
|
||||
formData.append("avatarfile", avatar);
|
||||
fetch(`${Setting.ServerUrl}/api/upload-avatar`, {
|
||||
formData.append("file", file);
|
||||
fetch(`${Setting.ServerUrl}/api/upload-file?folder=${encodeURIComponent(folder)}&subFolder=${encodeURIComponent(subFolder)}`, {
|
||||
body: formData,
|
||||
method: 'POST',
|
||||
credentials: 'include',
|
||||
|
Loading…
x
Reference in New Issue
Block a user