github.com/cloudreve/Cloudreve/v3@v3.0.0-20240224133659-3edb00a6484c/service/setting/webdav.go (about) 1 package setting 2 3 import ( 4 model "github.com/cloudreve/Cloudreve/v3/models" 5 "github.com/cloudreve/Cloudreve/v3/pkg/serializer" 6 "github.com/cloudreve/Cloudreve/v3/pkg/util" 7 "github.com/gin-gonic/gin" 8 ) 9 10 // WebDAVListService WebDAV 列表服务 11 type WebDAVListService struct { 12 } 13 14 // WebDAVAccountService WebDAV 账号管理服务 15 type WebDAVAccountService struct { 16 ID uint `uri:"id" binding:"required,min=1"` 17 } 18 19 // WebDAVAccountCreateService WebDAV 账号创建服务 20 type WebDAVAccountCreateService struct { 21 Path string `json:"path" binding:"required,min=1,max=65535"` 22 Name string `json:"name" binding:"required,min=1,max=255"` 23 } 24 25 // WebDAVAccountUpdateService WebDAV 修改只读性和是否使用代理服务 26 type WebDAVAccountUpdateService struct { 27 ID uint `json:"id" binding:"required,min=1"` 28 Readonly *bool `json:"readonly" binding:"required_without=UseProxy"` 29 UseProxy *bool `json:"use_proxy" binding:"required_without=Readonly"` 30 } 31 32 // WebDAVMountCreateService WebDAV 挂载创建服务 33 type WebDAVMountCreateService struct { 34 Path string `json:"path" binding:"required,min=1,max=65535"` 35 Policy string `json:"policy" binding:"required,min=1"` 36 } 37 38 // Create 创建WebDAV账户 39 func (service *WebDAVAccountCreateService) Create(c *gin.Context, user *model.User) serializer.Response { 40 account := model.Webdav{ 41 Name: service.Name, 42 Password: util.RandStringRunes(32), 43 UserID: user.ID, 44 Root: service.Path, 45 } 46 47 if _, err := account.Create(); err != nil { 48 return serializer.Err(serializer.CodeDBError, "创建失败", err) 49 } 50 51 return serializer.Response{ 52 Data: map[string]interface{}{ 53 "id": account.ID, 54 "password": account.Password, 55 "created_at": account.CreatedAt, 56 }, 57 } 58 } 59 60 // Delete 删除WebDAV账户 61 func (service *WebDAVAccountService) Delete(c *gin.Context, user *model.User) serializer.Response { 62 model.DeleteWebDAVAccountByID(service.ID, user.ID) 63 return serializer.Response{} 64 } 65 66 // Update 修改WebDAV账户只读性和是否使用代理服务 67 func (service *WebDAVAccountUpdateService) Update(c *gin.Context, user *model.User) serializer.Response { 68 var updates = make(map[string]interface{}) 69 if service.Readonly != nil { 70 updates["readonly"] = *service.Readonly 71 } 72 if service.UseProxy != nil { 73 updates["use_proxy"] = *service.UseProxy 74 } 75 model.UpdateWebDAVAccountByID(service.ID, user.ID, updates) 76 return serializer.Response{Data: updates} 77 } 78 79 // Accounts 列出WebDAV账号 80 func (service *WebDAVListService) Accounts(c *gin.Context, user *model.User) serializer.Response { 81 accounts := model.ListWebDAVAccounts(user.ID) 82 83 return serializer.Response{Data: map[string]interface{}{ 84 "accounts": accounts, 85 }} 86 }