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