mirror of https://github.com/cloudreve/Cloudreve
				
				
				
			Add: model/folder
							parent
							
								
									ceb8ee72d8
								
							
						
					
					
						commit
						4c1ece4b96
					
				| 
						 | 
				
			
			@ -0,0 +1,17 @@
 | 
			
		|||
package model
 | 
			
		||||
 | 
			
		||||
import "github.com/jinzhu/gorm"
 | 
			
		||||
 | 
			
		||||
// Folder 目录
 | 
			
		||||
type Folder struct {
 | 
			
		||||
	// 表字段
 | 
			
		||||
	gorm.Model
 | 
			
		||||
	Name             string
 | 
			
		||||
	ParentID         uint
 | 
			
		||||
	Position         string `gorm:"size:65536"`
 | 
			
		||||
	OwnerID          uint
 | 
			
		||||
	PositionAbsolute string `gorm:"size:65536"`
 | 
			
		||||
 | 
			
		||||
	// 关联模型
 | 
			
		||||
	OptionsSerialized PolicyOption `gorm:"-"`
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -25,7 +25,7 @@ func migration() {
 | 
			
		|||
	util.Log().Info("开始进行数据库自动迁移...")
 | 
			
		||||
 | 
			
		||||
	// 自动迁移模式
 | 
			
		||||
	DB.Set("gorm:table_options", "ENGINE=InnoDB").AutoMigrate(&User{}, &Setting{}, &Group{}, &Policy{})
 | 
			
		||||
	DB.Set("gorm:table_options", "ENGINE=InnoDB").AutoMigrate(&User{}, &Setting{}, &Group{}, &Policy{}, &Folder{})
 | 
			
		||||
 | 
			
		||||
	// 创建初始存储策略
 | 
			
		||||
	addDefaultPolicy()
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -107,6 +107,19 @@ func (user *User) BeforeSave() (err error) {
 | 
			
		|||
	return err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// AfterCreate 创建用户后的钩子
 | 
			
		||||
func (user *User) AfterCreate(tx *gorm.DB) (err error) {
 | 
			
		||||
	// 创建用户的默认根目录
 | 
			
		||||
	defaultFolder := &Folder{
 | 
			
		||||
		Name:             "根目录",
 | 
			
		||||
		Position:         ".",
 | 
			
		||||
		OwnerID:          user.ID,
 | 
			
		||||
		PositionAbsolute: "/",
 | 
			
		||||
	}
 | 
			
		||||
	tx.Create(defaultFolder)
 | 
			
		||||
	return err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
//SerializeOptions 将序列后的Option写入到数据库字段
 | 
			
		||||
func (user *User) SerializeOptions() (err error) {
 | 
			
		||||
	optionsValue, err := json.Marshal(&user.OptionsSerialized)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue