Feat: mount storage policy for folders in WebDAV

This commit is contained in:
HFO4
2020-02-13 15:27:40 +08:00
parent a0a9686959
commit 44d6ca487c
13 changed files with 76 additions and 12 deletions

View File

@@ -15,6 +15,7 @@ type Folder struct {
Name string `gorm:"unique_index:idx_only_one_name"`
ParentID *uint `gorm:"index:parent_id;unique_index:idx_only_one_name"`
OwnerID uint `gorm:"index:owner_id"`
PolicyID uint // Webdav下挂载的存储策略ID
// 数据库忽略字段
Position string `gorm:"-"`

View File

@@ -30,7 +30,7 @@ func migration() {
DB = DB.Set("gorm:table_options", "ENGINE=InnoDB")
}
DB.AutoMigrate(&User{}, &Setting{}, &Group{}, &Policy{}, &Folder{}, &File{}, &StoragePack{}, &Share{},
&Task{}, &Download{}, &Tag{})
&Task{}, &Download{}, &Tag{}, &Webdav{})
// 创建初始存储策略
addDefaultPolicy()

View File

@@ -220,6 +220,11 @@ func (share *Share) Delete() error {
return DB.Model(share).Delete(share).Error
}
// DeleteShareBySourceIDs 根据原始资源类型和ID删除文件
func DeleteShareBySourceIDs(sources []uint, isDir bool) error {
return DB.Where("source_id in (?) and is_dir = ?", sources, isDir).Delete(&Share{}).Error
}
// ListShares 列出UID下的分享
func ListShares(uid uint, page, pageSize int, order string, publicOnly bool) ([]Share, int) {
var (

View File

@@ -135,17 +135,20 @@ func (user *User) GetRemainingCapacity() uint64 {
}
// GetPolicyID 获取用户当前的存储策略ID
func (user *User) GetPolicyID() uint {
func (user *User) GetPolicyID(prefer uint) uint {
if prefer == 0 {
prefer = user.OptionsSerialized.PreferredPolicy
}
// 用户未指定时,返回可用的第一个
if user.OptionsSerialized.PreferredPolicy == 0 {
if prefer == 0 {
if len(user.Group.PolicyList) != 0 {
return user.Group.PolicyList[0]
}
return 1
}
// 用户指定时,先检查是否为可用策略列表中的值
if util.ContainsUint(user.Group.PolicyList, user.OptionsSerialized.PreferredPolicy) {
return user.OptionsSerialized.PreferredPolicy
if util.ContainsUint(user.Group.PolicyList, prefer) {
return prefer
}
// 不可用时,返回第一个
if len(user.Group.PolicyList) != 0 {
@@ -205,7 +208,7 @@ func (user *User) AfterFind() (err error) {
}
// 预加载存储策略
user.Policy, _ = GetPolicyByID(user.GetPolicyID())
user.Policy, _ = GetPolicyByID(user.GetPolicyID(0))
return err
}

View File

@@ -166,7 +166,7 @@ func TestUser_GetPolicyID(t *testing.T) {
for key, testCase := range testCases {
newUser.OptionsSerialized.PreferredPolicy = testCase.preferred
newUser.Group.PolicyList = testCase.available
asserts.Equal(testCase.expected, newUser.GetPolicyID(), "测试用例 #%d 未通过", key)
asserts.Equal(testCase.expected, newUser.GetPolicyID(0), "测试用例 #%d 未通过", key)
}
}

19
models/webdav.go Normal file
View File

@@ -0,0 +1,19 @@
package model
import "github.com/jinzhu/gorm"
// Webdav 应用账户
type Webdav struct {
gorm.Model
Name string // 应用名称
Password string `gorm:"unique_index:password_only_on"` // 应用密码
UserID uint `gorm:"unique_index:password_only_on"` // 用户ID
Root string `gorm:"type:text"` // 根目录
}
// GetWebdavByPassword 根据密码和用户查找Webdav应用
func GetWebdavByPassword(password string, uid uint) (*Webdav, error) {
webdav := &Webdav{}
res := DB.Where("user_id = ? and password = ?", uid, password).First(webdav)
return webdav, res.Error
}