Init V4 community edition (#2265)
* Init V4 community edition * Init V4 community edition
This commit is contained in:
44
service/setting/response.go
Normal file
44
service/setting/response.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package setting
|
||||
|
||||
import (
|
||||
"github.com/cloudreve/Cloudreve/v4/ent"
|
||||
"github.com/cloudreve/Cloudreve/v4/inventory"
|
||||
"github.com/cloudreve/Cloudreve/v4/pkg/boolset"
|
||||
"github.com/cloudreve/Cloudreve/v4/pkg/hashid"
|
||||
"github.com/samber/lo"
|
||||
"time"
|
||||
)
|
||||
|
||||
type ListDavAccountResponse struct {
|
||||
Accounts []DavAccount `json:"accounts"`
|
||||
Pagination *inventory.PaginationResults `json:"pagination"`
|
||||
}
|
||||
|
||||
func BuildListDavAccountResponse(res *inventory.ListDavAccountResult, hasher hashid.Encoder) *ListDavAccountResponse {
|
||||
return &ListDavAccountResponse{
|
||||
Accounts: lo.Map(res.Accounts, func(item *ent.DavAccount, index int) DavAccount {
|
||||
return BuildDavAccount(item, hasher)
|
||||
}),
|
||||
Pagination: res.PaginationResults,
|
||||
}
|
||||
}
|
||||
|
||||
type DavAccount struct {
|
||||
ID string `json:"id"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
Name string `json:"name"`
|
||||
Uri string `json:"uri"`
|
||||
Password string `json:"password"`
|
||||
Options *boolset.BooleanSet `json:"options"`
|
||||
}
|
||||
|
||||
func BuildDavAccount(account *ent.DavAccount, hasher hashid.Encoder) DavAccount {
|
||||
return DavAccount{
|
||||
ID: hashid.EncodeDavAccountID(hasher, account.ID),
|
||||
CreatedAt: account.CreatedAt,
|
||||
Name: account.Name,
|
||||
Uri: account.URI,
|
||||
Password: account.Password,
|
||||
Options: account.Options,
|
||||
}
|
||||
}
|
||||
@@ -1,16 +1,19 @@
|
||||
package setting
|
||||
|
||||
import (
|
||||
model "github.com/cloudreve/Cloudreve/v3/models"
|
||||
"github.com/cloudreve/Cloudreve/v3/pkg/serializer"
|
||||
"github.com/cloudreve/Cloudreve/v3/pkg/util"
|
||||
"github.com/cloudreve/Cloudreve/v4/application/constants"
|
||||
"github.com/cloudreve/Cloudreve/v4/application/dependency"
|
||||
"github.com/cloudreve/Cloudreve/v4/ent"
|
||||
"github.com/cloudreve/Cloudreve/v4/inventory"
|
||||
"github.com/cloudreve/Cloudreve/v4/inventory/types"
|
||||
"github.com/cloudreve/Cloudreve/v4/pkg/boolset"
|
||||
"github.com/cloudreve/Cloudreve/v4/pkg/filemanager/fs"
|
||||
"github.com/cloudreve/Cloudreve/v4/pkg/hashid"
|
||||
"github.com/cloudreve/Cloudreve/v4/pkg/serializer"
|
||||
"github.com/cloudreve/Cloudreve/v4/pkg/util"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// WebDAVListService WebDAV 列表服务
|
||||
type WebDAVListService struct {
|
||||
}
|
||||
|
||||
// WebDAVAccountService WebDAV 账号管理服务
|
||||
type WebDAVAccountService struct {
|
||||
ID uint `uri:"id" binding:"required,min=1"`
|
||||
@@ -22,11 +25,10 @@ type WebDAVAccountCreateService struct {
|
||||
Name string `json:"name" binding:"required,min=1,max=255"`
|
||||
}
|
||||
|
||||
// WebDAVAccountUpdateService WebDAV 修改只读性和是否使用代理服务
|
||||
type WebDAVAccountUpdateService struct {
|
||||
ID uint `json:"id" binding:"required,min=1"`
|
||||
Readonly *bool `json:"readonly" binding:"required_without=UseProxy"`
|
||||
UseProxy *bool `json:"use_proxy" binding:"required_without=Readonly"`
|
||||
// WebDAVAccountUpdateReadonlyService WebDAV 修改只读性服务
|
||||
type WebDAVAccountUpdateReadonlyService struct {
|
||||
ID uint `json:"id" binding:"required,min=1"`
|
||||
Readonly bool `json:"readonly"`
|
||||
}
|
||||
|
||||
// WebDAVMountCreateService WebDAV 挂载创建服务
|
||||
@@ -35,52 +37,163 @@ type WebDAVMountCreateService struct {
|
||||
Policy string `json:"policy" binding:"required,min=1"`
|
||||
}
|
||||
|
||||
// Create 创建WebDAV账户
|
||||
func (service *WebDAVAccountCreateService) Create(c *gin.Context, user *model.User) serializer.Response {
|
||||
account := model.Webdav{
|
||||
Name: service.Name,
|
||||
Password: util.RandStringRunes(32),
|
||||
UserID: user.ID,
|
||||
Root: service.Path,
|
||||
}
|
||||
//// Unmount 取消目录挂载
|
||||
//func (service *WebDAVListService) Unmount(c *gin.Context, user *model.User) serializer.Response {
|
||||
// folderID, _ := c.Get("object_id")
|
||||
// folder, err := model.GetFoldersByIDs([]uint{folderID.(uint)}, user.ID)
|
||||
// if err != nil || len(folder) == 0 {
|
||||
// return serializer.ErrDeprecated(serializer.CodeParentNotExist, "", err)
|
||||
// }
|
||||
//
|
||||
// if err := folder[0].Mount(0); err != nil {
|
||||
// return serializer.DBErrDeprecated("Failed to update folder record", err)
|
||||
// }
|
||||
//
|
||||
// return serializer.Response{}
|
||||
//}
|
||||
|
||||
if _, err := account.Create(); err != nil {
|
||||
return serializer.Err(serializer.CodeDBError, "创建失败", err)
|
||||
type (
|
||||
ListDavAccountsService struct {
|
||||
PageSize int `form:"page_size" binding:"required,min=10,max=100"`
|
||||
NextPageToken string `form:"next_page_token"`
|
||||
}
|
||||
|
||||
return serializer.Response{
|
||||
Data: map[string]interface{}{
|
||||
"id": account.ID,
|
||||
"password": account.Password,
|
||||
"created_at": account.CreatedAt,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Delete 删除WebDAV账户
|
||||
func (service *WebDAVAccountService) Delete(c *gin.Context, user *model.User) serializer.Response {
|
||||
model.DeleteWebDAVAccountByID(service.ID, user.ID)
|
||||
return serializer.Response{}
|
||||
}
|
||||
|
||||
// Update 修改WebDAV账户只读性和是否使用代理服务
|
||||
func (service *WebDAVAccountUpdateService) Update(c *gin.Context, user *model.User) serializer.Response {
|
||||
var updates = make(map[string]interface{})
|
||||
if service.Readonly != nil {
|
||||
updates["readonly"] = *service.Readonly
|
||||
}
|
||||
if service.UseProxy != nil {
|
||||
updates["use_proxy"] = *service.UseProxy
|
||||
}
|
||||
model.UpdateWebDAVAccountByID(service.ID, user.ID, updates)
|
||||
return serializer.Response{Data: updates}
|
||||
}
|
||||
ListDavAccountParamCtx struct{}
|
||||
)
|
||||
|
||||
// Accounts 列出WebDAV账号
|
||||
func (service *WebDAVListService) Accounts(c *gin.Context, user *model.User) serializer.Response {
|
||||
accounts := model.ListWebDAVAccounts(user.ID)
|
||||
func (service *ListDavAccountsService) List(c *gin.Context) (*ListDavAccountResponse, error) {
|
||||
dep := dependency.FromContext(c)
|
||||
user := inventory.UserFromContext(c)
|
||||
hasher := dep.HashIDEncoder()
|
||||
davAccountClient := dep.DavAccountClient()
|
||||
|
||||
return serializer.Response{Data: map[string]interface{}{
|
||||
"accounts": accounts,
|
||||
}}
|
||||
args := &inventory.ListDavAccountArgs{
|
||||
UserID: user.ID,
|
||||
PaginationArgs: &inventory.PaginationArgs{
|
||||
UseCursorPagination: true,
|
||||
PageSize: service.PageSize,
|
||||
PageToken: service.NextPageToken,
|
||||
},
|
||||
}
|
||||
|
||||
res, err := davAccountClient.List(c, args)
|
||||
if err != nil {
|
||||
return nil, serializer.NewError(serializer.CodeDBError, "Failed to list dav accounts", err)
|
||||
}
|
||||
|
||||
return BuildListDavAccountResponse(res, hasher), nil
|
||||
}
|
||||
|
||||
type (
|
||||
CreateDavAccountService struct {
|
||||
Uri string `json:"uri" binding:"required"`
|
||||
Name string `json:"name" binding:"required,min=1,max=255"`
|
||||
Readonly bool `json:"readonly"`
|
||||
Proxy bool `json:"proxy"`
|
||||
}
|
||||
CreateDavAccountParamCtx struct{}
|
||||
)
|
||||
|
||||
// Create 创建WebDAV账号
|
||||
func (service *CreateDavAccountService) Create(c *gin.Context) (*DavAccount, error) {
|
||||
dep := dependency.FromContext(c)
|
||||
user := inventory.UserFromContext(c)
|
||||
|
||||
bs, err := service.validateAndGetBs(user)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
davAccountClient := dep.DavAccountClient()
|
||||
account, err := davAccountClient.Create(c, &inventory.CreateDavAccountParams{
|
||||
UserID: user.ID,
|
||||
Name: service.Name,
|
||||
URI: service.Uri,
|
||||
Password: util.RandString(32, util.RandomLowerCases),
|
||||
Options: bs,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, serializer.NewError(serializer.CodeDBError, "Failed to create dav account", err)
|
||||
}
|
||||
|
||||
accountRes := BuildDavAccount(account, dep.HashIDEncoder())
|
||||
return &accountRes, nil
|
||||
}
|
||||
|
||||
// Update updates an existing account
|
||||
func (service *CreateDavAccountService) Update(c *gin.Context) (*DavAccount, error) {
|
||||
dep := dependency.FromContext(c)
|
||||
user := inventory.UserFromContext(c)
|
||||
accountId := hashid.FromContext(c)
|
||||
|
||||
// Find existing account
|
||||
davAccountClient := dep.DavAccountClient()
|
||||
account, err := davAccountClient.GetByIDAndUserID(c, accountId, user.ID)
|
||||
if err != nil {
|
||||
return nil, serializer.NewError(serializer.CodeNotFound, "Account not exist", err)
|
||||
}
|
||||
|
||||
bs, err := service.validateAndGetBs(user)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Update account
|
||||
account, err = davAccountClient.Update(c, accountId, &inventory.CreateDavAccountParams{
|
||||
Name: service.Name,
|
||||
URI: service.Uri,
|
||||
Options: bs,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, serializer.NewError(serializer.CodeDBError, "Failed to update dav account", err)
|
||||
}
|
||||
|
||||
accountRes := BuildDavAccount(account, dep.HashIDEncoder())
|
||||
return &accountRes, nil
|
||||
}
|
||||
|
||||
func (service *CreateDavAccountService) validateAndGetBs(user *ent.User) (*boolset.BooleanSet, error) {
|
||||
if !user.Edges.Group.Permissions.Enabled(int(types.GroupPermissionWebDAV)) {
|
||||
return nil, serializer.NewError(serializer.CodeGroupNotAllowed, "WebDAV is not enabled for this user group", nil)
|
||||
}
|
||||
|
||||
uri, err := fs.NewUriFromString(service.Uri)
|
||||
if err != nil {
|
||||
return nil, serializer.NewError(serializer.CodeParamErr, "Invalid URI", err)
|
||||
}
|
||||
|
||||
// Only "my" and "share" fs is allowed in WebDAV
|
||||
if uriFs := uri.FileSystem(); uri.SearchParameters() != nil ||
|
||||
(uriFs != constants.FileSystemMy && uriFs != constants.FileSystemShare) {
|
||||
return nil, serializer.NewError(serializer.CodeParamErr, "Invalid URI", nil)
|
||||
}
|
||||
|
||||
bs := boolset.BooleanSet{}
|
||||
if service.Readonly {
|
||||
boolset.Set(types.DavAccountReadOnly, true, &bs)
|
||||
}
|
||||
|
||||
if service.Proxy && user.Edges.Group.Permissions.Enabled(int(types.GroupPermissionWebDAVProxy)) {
|
||||
boolset.Set(types.DavAccountProxy, true, &bs)
|
||||
}
|
||||
return &bs, nil
|
||||
}
|
||||
|
||||
func DeleteDavAccount(c *gin.Context) error {
|
||||
dep := dependency.FromContext(c)
|
||||
user := inventory.UserFromContext(c)
|
||||
accountId := hashid.FromContext(c)
|
||||
|
||||
// Find existing account
|
||||
davAccountClient := dep.DavAccountClient()
|
||||
_, err := davAccountClient.GetByIDAndUserID(c, accountId, user.ID)
|
||||
if err != nil {
|
||||
return serializer.NewError(serializer.CodeNotFound, "Account not exist", err)
|
||||
}
|
||||
|
||||
if err := davAccountClient.Delete(c, accountId); err != nil {
|
||||
return serializer.NewError(serializer.CodeDBError, "Failed to delete dav account", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user