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"
|
2021-09-05 01:03:29 +08:00
|
|
|
"mime"
|
2021-08-15 00:17:53 +08:00
|
|
|
"path/filepath"
|
2023-05-10 23:57:03 +08:00
|
|
|
"strings"
|
2021-08-15 00:17:53 +08:00
|
|
|
|
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
|
2021-12-03 20:42:36 +08:00
|
|
|
// @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")
|
2021-11-06 11:32:22 +08:00
|
|
|
limit := c.Input().Get("pageSize")
|
|
|
|
page := c.Input().Get("p")
|
2021-12-25 10:55:10 +08:00
|
|
|
field := c.Input().Get("field")
|
|
|
|
value := c.Input().Get("value")
|
|
|
|
sortField := c.Input().Get("sortField")
|
|
|
|
sortOrder := c.Input().Get("sortOrder")
|
2022-12-03 00:57:11 +08:00
|
|
|
|
|
|
|
userObj, ok := c.RequireSignedInUser()
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if userObj.IsAdmin {
|
|
|
|
user = ""
|
|
|
|
}
|
|
|
|
|
2021-11-06 11:32:22 +08:00
|
|
|
if limit == "" || page == "" {
|
2023-05-30 15:49:39 +08:00
|
|
|
resources, err := object.GetResources(owner, user)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Data["json"] = resources
|
2021-11-06 11:32:22 +08:00
|
|
|
c.ServeJSON()
|
|
|
|
} else {
|
|
|
|
limit := util.ParseInt(limit)
|
2023-05-30 15:49:39 +08:00
|
|
|
count, err := object.GetResourceCount(owner, user, field, value)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
paginator := pagination.SetPaginator(c.Ctx, limit, count)
|
|
|
|
resources, err := object.GetPaginationResources(owner, user, paginator.Offset(), limit, field, value, sortField, sortOrder)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-06 11:32:22 +08:00
|
|
|
c.ResponseOk(resources, paginator.Nums())
|
|
|
|
}
|
2021-08-15 00:17:53 +08:00
|
|
|
}
|
|
|
|
|
2022-08-09 16:50:49 +08:00
|
|
|
// GetResource
|
2021-12-03 20:42:36 +08:00
|
|
|
// @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")
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
resource, err := object.GetResource(id)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Data["json"] = resource
|
2021-08-15 00:17:53 +08:00
|
|
|
c.ServeJSON()
|
|
|
|
}
|
|
|
|
|
2022-08-09 16:50:49 +08:00
|
|
|
// UpdateResource
|
2021-12-03 20:42:36 +08:00
|
|
|
// @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 {
|
2022-08-20 21:09:32 +08:00
|
|
|
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
|
2021-12-03 20:42:36 +08:00
|
|
|
// @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 {
|
2022-08-20 21:09:32 +08:00
|
|
|
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
|
2021-12-03 20:42:36 +08:00
|
|
|
// @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 {
|
2022-08-20 21:09:32 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-10-23 15:16:24 +08:00
|
|
|
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
|
2021-12-03 20:42:36 +08:00
|
|
|
// @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
|
|
|
|
2021-11-20 15:35:33 +08:00
|
|
|
if username == "" || fullFilePath == "" {
|
2022-12-07 13:13:23 +08:00
|
|
|
c.ResponseError(fmt.Sprintf(c.T("resource:Username or fullFilePath is empty: username = %s, fullFilePath = %s"), username, fullFilePath))
|
2021-11-20 15:35:33 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-11-25 09:36:47 +08:00
|
|
|
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")
|
2021-09-05 01:03:29 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-02-10 20:27:20 +08:00
|
|
|
fullFilePath = object.GetTruncatedPath(provider, fullFilePath, 175)
|
2022-11-27 17:32:15 +08:00
|
|
|
if tag != "avatar" && tag != "termsOfUse" {
|
|
|
|
ext := filepath.Ext(filepath.Base(fullFilePath))
|
|
|
|
index := len(fullFilePath) - len(ext)
|
|
|
|
for i := 1; ; i++ {
|
|
|
|
_, objectKey := object.GetUploadFileUrl(provider, fullFilePath, true)
|
2023-05-30 15:49:39 +08:00
|
|
|
if count, err := object.GetResourceCount(owner, username, "name", objectKey); err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
} else if count == 0 {
|
2022-11-27 17:32:15 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// duplicated fullFilePath found, change it
|
|
|
|
fullFilePath = fullFilePath[:index] + fmt.Sprintf("-%d", i) + ext
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-19 21:47:49 +08:00
|
|
|
fileUrl, objectKey, err := object.UploadFileSafe(provider, fullFilePath, fileBuffer, c.GetAcceptLanguage())
|
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
|
|
|
}
|
2023-05-30 15:49:39 +08:00
|
|
|
_, err = object.AddOrUpdateResource(resource)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
2021-08-15 00:17:53 +08:00
|
|
|
|
|
|
|
switch tag {
|
|
|
|
case "avatar":
|
2023-05-30 15:49:39 +08:00
|
|
|
user, err := object.GetUserNoCheck(util.GetId(owner, username))
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-05 01:03:29 +08:00
|
|
|
if user == nil {
|
2022-12-07 13:13:23 +08:00
|
|
|
c.ResponseError(c.T("resource:User is nil for tag: avatar"))
|
2022-11-25 09:36:47 +08:00
|
|
|
return
|
2021-09-05 01:03:29 +08:00
|
|
|
}
|
|
|
|
|
2021-08-15 00:17:53 +08:00
|
|
|
user.Avatar = fileUrl
|
2023-05-30 15:49:39 +08:00
|
|
|
_, err = object.UpdateUser(user.GetId(), user, []string{"avatar"}, false)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-15 00:17:53 +08:00
|
|
|
case "termsOfUse":
|
2023-05-30 15:49:39 +08:00
|
|
|
user, err := object.GetUserNoCheck(util.GetId(owner, username))
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-12 01:00:06 +08:00
|
|
|
if user == nil {
|
|
|
|
c.ResponseError(fmt.Sprintf(c.T("general:The user: %s doesn't exist"), util.GetId(owner, username)))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !user.IsAdminUser() {
|
|
|
|
c.ResponseError(c.T("auth:Unauthorized operation"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-10 23:57:03 +08:00
|
|
|
_, applicationId := util.GetOwnerAndNameFromIdNoCheck(strings.TrimRight(fullFilePath, ".html"))
|
2023-05-30 15:49:39 +08:00
|
|
|
applicationObj, err := object.GetApplication(applicationId)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-10 23:57:03 +08:00
|
|
|
applicationObj.TermsOfUse = fileUrl
|
2023-05-30 15:49:39 +08:00
|
|
|
_, err = object.UpdateApplication(applicationId, applicationObj)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
2021-08-15 00:17:53 +08:00
|
|
|
}
|
|
|
|
|
2021-09-04 15:02:11 +08:00
|
|
|
c.ResponseOk(fileUrl, objectKey)
|
2021-08-15 00:17:53 +08:00
|
|
|
}
|