casdoor/controllers/resource.go

222 lines
5.6 KiB
Go
Raw Normal View History

2022-02-13 23:39:27 +08:00
// Copyright 2021 The Casdoor Authors. All Rights Reserved.
2021-08-15 00:17:53 +08:00
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package controllers
import (
"bytes"
"encoding/json"
"fmt"
"io"
"mime"
2021-08-15 00:17:53 +08:00
"path/filepath"
2022-09-29 19:44:08 +08:00
"github.com/beego/beego/utils/pagination"
2022-01-20 14:11:46 +08:00
"github.com/casdoor/casdoor/object"
"github.com/casdoor/casdoor/util"
2021-08-15 00:17:53 +08:00
)
2022-08-09 16:50:49 +08:00
// GetResources
// @router /get-resources [get]
// @Tag Resource API
// @Title GetResources
2021-08-15 00:17:53 +08:00
func (c *ApiController) GetResources() {
owner := c.Input().Get("owner")
2021-09-06 00:49:10 +08:00
user := c.Input().Get("user")
limit := c.Input().Get("pageSize")
page := c.Input().Get("p")
field := c.Input().Get("field")
value := c.Input().Get("value")
sortField := c.Input().Get("sortField")
sortOrder := c.Input().Get("sortOrder")
if limit == "" || page == "" {
c.Data["json"] = object.GetResources(owner, user)
c.ServeJSON()
} else {
limit := util.ParseInt(limit)
paginator := pagination.SetPaginator(c.Ctx, limit, int64(object.GetResourceCount(owner, user, field, value)))
resources := object.GetPaginationResources(owner, user, paginator.Offset(), limit, field, value, sortField, sortOrder)
c.ResponseOk(resources, paginator.Nums())
}
2021-08-15 00:17:53 +08:00
}
2022-08-09 16:50:49 +08:00
// GetResource
// @Tag Resource API
// @Title GetResource
// @router /get-resource [get]
2021-08-15 00:17:53 +08:00
func (c *ApiController) GetResource() {
id := c.Input().Get("id")
c.Data["json"] = object.GetResource(id)
c.ServeJSON()
}
2022-08-09 16:50:49 +08:00
// UpdateResource
// @Tag Resource API
// @Title UpdateResource
// @router /update-resource [post]
2021-08-15 00:17:53 +08:00
func (c *ApiController) UpdateResource() {
id := c.Input().Get("id")
var resource object.Resource
err := json.Unmarshal(c.Ctx.Input.RequestBody, &resource)
if err != nil {
c.ResponseError(err.Error())
return
2021-08-15 00:17:53 +08:00
}
c.Data["json"] = wrapActionResponse(object.UpdateResource(id, &resource))
c.ServeJSON()
}
2022-08-09 16:50:49 +08:00
// AddResource
// @Tag Resource API
// @Title AddResource
// @router /add-resource [post]
2021-08-15 00:17:53 +08:00
func (c *ApiController) AddResource() {
var resource object.Resource
err := json.Unmarshal(c.Ctx.Input.RequestBody, &resource)
if err != nil {
c.ResponseError(err.Error())
return
2021-08-15 00:17:53 +08:00
}
c.Data["json"] = wrapActionResponse(object.AddResource(&resource))
c.ServeJSON()
}
2022-08-09 16:50:49 +08:00
// DeleteResource
// @Tag Resource API
// @Title DeleteResource
// @router /delete-resource [post]
2021-08-15 01:09:51 +08:00
func (c *ApiController) DeleteResource() {
2021-08-15 00:17:53 +08:00
var resource object.Resource
err := json.Unmarshal(c.Ctx.Input.RequestBody, &resource)
if err != nil {
c.ResponseError(err.Error())
return
2021-08-15 00:17:53 +08:00
}
2021-09-05 09:44:15 +08:00
provider, _, ok := c.GetProviderFromContext("Storage")
2021-08-15 01:09:51 +08:00
if !ok {
2021-08-15 00:41:51 +08:00
return
}
err = object.DeleteFile(provider, resource.Name, c.GetAcceptLanguage())
2021-08-15 00:41:51 +08:00
if err != nil {
c.ResponseError(err.Error())
return
}
2021-08-15 00:17:53 +08:00
c.Data["json"] = wrapActionResponse(object.DeleteResource(&resource))
c.ServeJSON()
}
2022-08-09 16:50:49 +08:00
// UploadResource
// @Tag Resource API
// @Title UploadResource
// @router /upload-resource [post]
2021-08-15 00:17:53 +08:00
func (c *ApiController) UploadResource() {
owner := c.Input().Get("owner")
2021-09-05 23:46:38 +08:00
username := c.Input().Get("user")
2021-09-04 16:50:26 +08:00
application := c.Input().Get("application")
2021-08-15 00:17:53 +08:00
tag := c.Input().Get("tag")
parent := c.Input().Get("parent")
fullFilePath := c.Input().Get("fullFilePath")
2021-11-21 16:21:35 +08:00
createdTime := c.Input().Get("createdTime")
description := c.Input().Get("description")
2021-08-15 00:17:53 +08:00
file, header, err := c.GetFile("file")
if err != nil {
c.ResponseError(err.Error())
return
}
2021-09-05 00:22:08 +08:00
defer file.Close()
2021-08-15 00:17:53 +08:00
if username == "" || fullFilePath == "" {
c.ResponseError(fmt.Sprintf(c.T("ResourceErr.UsernameOrFilePathEmpty"), username, fullFilePath))
return
}
2021-08-15 00:17:53 +08:00
filename := filepath.Base(fullFilePath)
fileBuffer := bytes.NewBuffer(nil)
if _, err = io.Copy(fileBuffer, file); err != nil {
c.ResponseError(err.Error())
return
}
provider, _, ok := c.GetProviderFromContext("Storage")
2021-08-15 01:09:51 +08:00
if !ok {
2021-08-15 00:17:53 +08:00
return
}
2021-08-15 00:41:51 +08:00
fileType := "unknown"
contentType := header.Header.Get("Content-Type")
fileType, _ = util.GetOwnerAndNameFromId(contentType)
if fileType != "image" && fileType != "video" {
ext := filepath.Ext(filename)
mimeType := mime.TypeByExtension(ext)
fileType, _ = util.GetOwnerAndNameFromId(mimeType)
2021-08-15 00:41:51 +08:00
}
2021-11-20 16:21:15 +08:00
fileUrl, objectKey, err := object.UploadFileSafe(provider, fullFilePath, fileBuffer)
2021-08-15 00:17:53 +08:00
if err != nil {
c.ResponseError(err.Error())
return
}
2021-11-21 16:21:35 +08:00
if createdTime == "" {
createdTime = util.GetCurrentTime()
}
2021-08-15 00:17:53 +08:00
fileFormat := filepath.Ext(fullFilePath)
fileSize := int(header.Size)
resource := &object.Resource{
Owner: owner,
2021-09-04 15:02:11 +08:00
Name: objectKey,
2021-11-21 16:21:35 +08:00
CreatedTime: createdTime,
2021-09-05 23:46:38 +08:00
User: username,
2021-08-15 01:09:51 +08:00
Provider: provider.Name,
2021-09-04 16:50:26 +08:00
Application: application,
2021-08-15 00:17:53 +08:00
Tag: tag,
Parent: parent,
2021-09-04 15:02:11 +08:00
FileName: filename,
2021-08-15 00:17:53 +08:00
FileType: fileType,
FileFormat: fileFormat,
FileSize: fileSize,
Url: fileUrl,
2021-11-21 16:21:35 +08:00
Description: description,
2021-08-15 00:17:53 +08:00
}
object.AddOrUpdateResource(resource)
switch tag {
case "avatar":
user := object.GetUserNoCheck(util.GetId(owner, username))
if user == nil {
c.ResponseError(c.T("ResourceErr.UserIsNil"))
return
}
2021-08-15 00:17:53 +08:00
user.Avatar = fileUrl
2022-01-13 23:19:36 +08:00
object.UpdateUser(user.GetId(), user, []string{"avatar"}, false)
2021-08-15 00:17:53 +08:00
case "termsOfUse":
applicationId := fmt.Sprintf("admin/%s", parent)
app := object.GetApplication(applicationId)
app.TermsOfUse = fileUrl
object.UpdateApplication(applicationId, app)
}
2021-09-04 15:02:11 +08:00
c.ResponseOk(fileUrl, objectKey)
2021-08-15 00:17:53 +08:00
}