mirror of
https://github.com/casdoor/casdoor.git
synced 2025-05-23 02:35:49 +08:00
Improve UploadFile() API.
This commit is contained in:
parent
b7e0a4fe4e
commit
004282f970
@ -15,10 +15,11 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/casbin/casdoor/object"
|
||||
"github.com/casbin/casdoor/original"
|
||||
@ -214,9 +215,10 @@ func (c *ApiController) GetAccount() {
|
||||
// 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"
|
||||
// @Param owner query string true "The owner"
|
||||
// @Param tag query string true "The tag"
|
||||
// @Param fullFilePath query string true "The full file path"
|
||||
// @Param file query string true "The file"
|
||||
// @Success 200 {object} controllers.Response The Response object
|
||||
// @router /upload-file [post]
|
||||
func (c *ApiController) UploadFile() {
|
||||
@ -225,8 +227,23 @@ func (c *ApiController) UploadFile() {
|
||||
return
|
||||
}
|
||||
|
||||
folder := c.Input().Get("folder")
|
||||
subFolder := c.Input().Get("subFolder")
|
||||
//owner := c.Input().Get("owner")
|
||||
tag := c.Input().Get("tag")
|
||||
parent := c.Input().Get("parent")
|
||||
fullFilePath := c.Input().Get("fullFilePath")
|
||||
|
||||
file, _, err := c.GetFile("file")
|
||||
defer file.Close()
|
||||
if err != nil {
|
||||
c.ResponseError(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
fileBuffer := bytes.NewBuffer(nil)
|
||||
if _, err = io.Copy(fileBuffer, file); err != nil {
|
||||
c.ResponseError(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
user := object.GetUser(userId)
|
||||
application := object.GetApplicationByUser(user)
|
||||
@ -235,38 +252,22 @@ func (c *ApiController) UploadFile() {
|
||||
c.ResponseError("No storage provider is found")
|
||||
return
|
||||
}
|
||||
file, header, err := c.GetFile("file")
|
||||
defer file.Close()
|
||||
|
||||
fileUrl, err := object.UploadFile(provider, fullFilePath, fileBuffer)
|
||||
if err != nil {
|
||||
c.ResponseError(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
fileType := header.Header.Get("Content-Type")
|
||||
|
||||
fileSuffix := ""
|
||||
switch fileType {
|
||||
case "image/png":
|
||||
fileSuffix = "png"
|
||||
case "text/html":
|
||||
fileSuffix = "html"
|
||||
}
|
||||
|
||||
fileUrl, err := object.UploadFile(provider, folder, subFolder, file, fileSuffix)
|
||||
if err != nil {
|
||||
c.ResponseError(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
switch folder {
|
||||
switch tag {
|
||||
case "avatar":
|
||||
user.Avatar = fileUrl
|
||||
object.UpdateUser(user.GetId(), user)
|
||||
case "termsofuse":
|
||||
appId := fmt.Sprintf("admin/%s", strings.Split(subFolder, "/")[0])
|
||||
app := object.GetApplication(appId)
|
||||
case "termsOfUse":
|
||||
applicationId := fmt.Sprintf("admin/%s", parent)
|
||||
app := object.GetApplication(applicationId)
|
||||
app.TermsOfUse = fileUrl
|
||||
object.UpdateApplication(appId, app)
|
||||
object.UpdateApplication(applicationId, app)
|
||||
}
|
||||
|
||||
c.ResponseOk(fileUrl)
|
||||
|
@ -17,15 +17,13 @@ package object
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"strings"
|
||||
|
||||
"github.com/casbin/casdoor/storage"
|
||||
"github.com/casbin/casdoor/util"
|
||||
)
|
||||
|
||||
func UploadFile(provider *Provider, folder string, subFolder string, file multipart.File, suffix string) (string, error) {
|
||||
func UploadFile(provider *Provider, fullFilePath string, fileBuffer *bytes.Buffer) (string, error) {
|
||||
storageProvider := storage.GetStorageProvider(provider.Type, provider.ClientId, provider.ClientSecret, provider.RegionId, provider.Bucket, provider.Endpoint)
|
||||
if storageProvider == nil {
|
||||
return "", fmt.Errorf("the provider type: %s is not supported", provider.Type)
|
||||
@ -36,12 +34,8 @@ func UploadFile(provider *Provider, folder string, subFolder string, file multip
|
||||
UpdateProvider(provider.GetId(), provider)
|
||||
}
|
||||
|
||||
path := fmt.Sprintf("%s/%s.%s", util.UrlJoin(util.GetUrlPath(provider.Domain), folder), subFolder, suffix)
|
||||
fileBuf := bytes.NewBuffer(nil)
|
||||
if _, err := io.Copy(fileBuf, file); err != nil {
|
||||
return "", err
|
||||
}
|
||||
_, err := storageProvider.Put(path, fileBuf)
|
||||
path := util.UrlJoin(util.GetUrlPath(provider.Domain), fullFilePath)
|
||||
_, err := storageProvider.Put(path, fileBuffer)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
@ -94,20 +94,21 @@ class ApplicationEditPage extends React.Component {
|
||||
|
||||
handleUpload(info) {
|
||||
if (info.file.type !== "text/html") {
|
||||
Setting.showMessage("error", i18next.t("provider:Please select a HTML file"))
|
||||
return
|
||||
Setting.showMessage("error", i18next.t("application:Please select a HTML file"));
|
||||
return;
|
||||
}
|
||||
this.setState({uploading: true})
|
||||
UserBackend.uploadFile("termsofuse", `${this.state.applicationName}/termsofuse`, info.file)
|
||||
this.setState({uploading: true});
|
||||
const fullFilePath = `termsOfUse/${this.state.application.owner}/${this.state.application.name}.html`;
|
||||
UserBackend.uploadFile(this.state.application.owner, "termsOfUse", this.state.application.name, fullFilePath, info.file)
|
||||
.then(res => {
|
||||
if (res.status === "ok") {
|
||||
Setting.showMessage("success", i18next.t("general:Upload success"))
|
||||
this.updateApplicationField("termsOfUse", res.data)
|
||||
Setting.showMessage("success", i18next.t("application:File uploaded successfully"));
|
||||
this.updateApplicationField("termsOfUse", res.data);
|
||||
} else {
|
||||
Setting.showMessage("error", res.msg)
|
||||
Setting.showMessage("error", res.msg);
|
||||
}
|
||||
}).finally(() => {
|
||||
this.setState({uploading: false})
|
||||
this.setState({uploading: false});
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -55,8 +55,9 @@ export const CropperDiv = (props) => {
|
||||
return false;
|
||||
}
|
||||
// Setting.showMessage("success", "uploading...");
|
||||
const userId = `${user.owner}/${user.name}`;
|
||||
UserBackend.uploadFile("avatar", userId, blob)
|
||||
const extension = image.substring(image.indexOf('/') + 1, image.indexOf(';base64'));
|
||||
const fullFilePath = `avatar/${user.owner}/${user.name}.${extension}`;
|
||||
UserBackend.uploadFile("admin", "avatar", "", fullFilePath, blob)
|
||||
.then((res) => {
|
||||
if (res.status === "ok") {
|
||||
window.location.href = "/account";
|
||||
@ -107,7 +108,7 @@ export const CropperDiv = (props) => {
|
||||
>
|
||||
<Col style={{margin: "0px auto 40px auto", width: 1000, height: 300}}>
|
||||
<Row style={{width: "100%", marginBottom: "20px"}}>
|
||||
<input style={{display: "none"}} ref={input => uploadButton = input} type="file" onChange={onChange}/>
|
||||
<input style={{display: "none"}} ref={input => uploadButton = input} type="file" accept="image/*" onChange={onChange}/>
|
||||
<Button block onClick={selectFile}>{i18next.t("user:Select a photo...")}</Button>
|
||||
</Row>
|
||||
<Cropper
|
||||
|
@ -76,10 +76,10 @@ export function getAffiliationOptions(url, code) {
|
||||
}).then(res => res.json());
|
||||
}
|
||||
|
||||
export function uploadFile(folder, subFolder, file) {
|
||||
export function uploadFile(owner, tag, parent, fullFilePath, file) {
|
||||
let formData = new FormData();
|
||||
formData.append("file", file);
|
||||
return fetch(`${Setting.ServerUrl}/api/upload-file?folder=${encodeURIComponent(folder)}&subFolder=${encodeURIComponent(subFolder)}`, {
|
||||
return fetch(`${Setting.ServerUrl}/api/upload-file?owner=${owner}&tag=${tag}&parent=${parent}&fullFilePath=${encodeURIComponent(fullFilePath)}`, {
|
||||
body: formData,
|
||||
method: 'POST',
|
||||
credentials: 'include',
|
||||
|
Loading…
x
Reference in New Issue
Block a user