diff --git a/models/group.go b/models/group.go index 1d8acba..d1b7730 100644 --- a/models/group.go +++ b/models/group.go @@ -15,6 +15,7 @@ type Group struct { WebDAVEnabled bool Aria2Option string Color string + SpeedLimit int // 数据库忽略字段 PolicyList []uint `gorm:"-"` diff --git a/models/policy.go b/models/policy.go index ed4ea4e..de82f15 100644 --- a/models/policy.go +++ b/models/policy.go @@ -38,7 +38,6 @@ type PolicyOption struct { OPPassword string `json:"op_pwd"` FileType []string `json:"file_type"` MimeType string `json:"mimetype"` - SpeedLimit int `json:"speed_limit"` RangeTransferEnabled bool `json:"range_transfer_enabled"` } @@ -53,6 +52,9 @@ func GetPolicyByID(ID interface{}) (Policy, error) { func (policy *Policy) AfterFind() (err error) { // 解析上传策略设置到OptionsSerialized err = json.Unmarshal([]byte(policy.Options), &policy.OptionsSerialized) + if policy.OptionsSerialized.FileType == nil { + policy.OptionsSerialized.FileType = []string{} + } return err } diff --git a/pkg/serializer/setting_test.go b/pkg/serializer/setting_test.go index b465e68..09c9797 100644 --- a/pkg/serializer/setting_test.go +++ b/pkg/serializer/setting_test.go @@ -1,6 +1,8 @@ package serializer import ( + model "github.com/HFO4/cloudreve/models" + "github.com/jinzhu/gorm" "github.com/stretchr/testify/assert" "testing" ) @@ -23,4 +25,13 @@ func TestBuildSiteConfig(t *testing.T) { res = BuildSiteConfig(map[string]string{"qq_login": "1"}, nil) asserts.Equal(true, res.Data.(SiteConfig).QQLogin) + asserts.Equal(uint(0), res.Data.(SiteConfig).User.ID) + + // 非空用户 + res = BuildSiteConfig(map[string]string{"qq_login": "1"}, &model.User{ + Model: gorm.Model{ + ID: 5, + }, + }) + asserts.Equal(uint(5), res.Data.(SiteConfig).User.ID) } diff --git a/pkg/serializer/user.go b/pkg/serializer/user.go index 22aea59..c9678ba 100644 --- a/pkg/serializer/user.go +++ b/pkg/serializer/user.go @@ -22,8 +22,21 @@ type User struct { Avatar string `json:"avatar"` CreatedAt int64 `json:"created_at"` PreferredTheme string `json:"preferred_theme"` - Policy struct { - } `json:"policy"` + Policy Policy `json:"policy"` + Group Group `json:"group"` +} + +type Policy struct { + SaveType string `json:"saveType"` + MaxSize string `json:"maxSize"` + AllowedType []string `json:"allowedType"` + UploadURL string `json:"upUrl"` +} + +type Group struct { + AllowShare bool `json:"allowShare"` + AllowRemoteDownload bool `json:"allowRemoteDownload"` + AllowTorrentDownload bool `json:"allowTorrentDownload"` } // BuildUser 序列化用户 @@ -37,6 +50,17 @@ func BuildUser(user model.User) User { Avatar: user.Avatar, CreatedAt: user.CreatedAt.Unix(), PreferredTheme: user.OptionsSerialized.PreferredTheme, + Policy: Policy{ + SaveType: user.Policy.Type, + MaxSize: fmt.Sprintf("%.2fmb", float64(user.Policy.MaxSize)/1024*1024), + AllowedType: user.Policy.OptionsSerialized.FileType, + UploadURL: user.Policy.Server, + }, + Group: Group{ + AllowShare: user.Group.ShareEnabled, + AllowRemoteDownload: user.Group.Aria2Option[0] == '1', + AllowTorrentDownload: user.Group.Aria2Option[2] == '1', + }, } } diff --git a/routers/controllers/file.go b/routers/controllers/file.go index b9f87a1..6455f4c 100644 --- a/routers/controllers/file.go +++ b/routers/controllers/file.go @@ -8,6 +8,7 @@ import ( "github.com/HFO4/cloudreve/pkg/serializer" "github.com/HFO4/cloudreve/pkg/util" "github.com/gin-gonic/gin" + "net/url" "strconv" ) @@ -30,12 +31,20 @@ func FileUploadStream(c *gin.Context) { return } + // 解码文件名和路径 + fileName, err := url.QueryUnescape(c.Request.Header.Get("X-FileName")) + filePath, err := url.QueryUnescape(c.Request.Header.Get("X-Path")) + if err != nil { + c.JSON(200, ErrorResponse(err)) + return + } + fileData := local.FileStream{ MIMEType: c.Request.Header.Get("Content-Type"), File: c.Request.Body, Size: fileSize, - Name: c.Request.Header.Get("X-FileName"), - VirtualPath: util.DotPathToStandardPath(c.Request.Header.Get("X-Path")), + Name: fileName, + VirtualPath: util.DotPathToStandardPath(filePath), } // 创建文件系统 diff --git a/routers/router.go b/routers/router.go index 50237db..0a6a939 100644 --- a/routers/router.go +++ b/routers/router.go @@ -21,7 +21,7 @@ func InitRouter() *gin.Engine { r.Use(cors.New(cors.Config{ AllowOrigins: []string{"http://localhost:3000"}, AllowMethods: []string{"PUT", "POST", "GET", "OPTIONS"}, - AllowHeaders: []string{"X-PINGOTHER", "Content-Type"}, + AllowHeaders: []string{"Content-Length", "Content-Type", "X-Path", "X-FileName"}, AllowCredentials: true, }))