github.com/cloudreve/Cloudreve/v3@v3.0.0-20240224133659-3edb00a6484c/models/group.go (about)

     1  package model
     2  
     3  import (
     4  	"encoding/json"
     5  	"github.com/jinzhu/gorm"
     6  )
     7  
     8  // Group 用户组模型
     9  type Group struct {
    10  	gorm.Model
    11  	Name          string
    12  	Policies      string
    13  	MaxStorage    uint64
    14  	ShareEnabled  bool
    15  	WebDAVEnabled bool
    16  	SpeedLimit    int
    17  	Options       string `json:"-" gorm:"size:4294967295"`
    18  
    19  	// 数据库忽略字段
    20  	PolicyList        []uint      `gorm:"-"`
    21  	OptionsSerialized GroupOption `gorm:"-"`
    22  }
    23  
    24  // GroupOption 用户组其他配置
    25  type GroupOption struct {
    26  	ArchiveDownload  bool                   `json:"archive_download,omitempty"` // 打包下载
    27  	ArchiveTask      bool                   `json:"archive_task,omitempty"`     // 在线压缩
    28  	CompressSize     uint64                 `json:"compress_size,omitempty"`    // 可压缩大小
    29  	DecompressSize   uint64                 `json:"decompress_size,omitempty"`
    30  	OneTimeDownload  bool                   `json:"one_time_download,omitempty"`
    31  	ShareDownload    bool                   `json:"share_download,omitempty"`
    32  	Aria2            bool                   `json:"aria2,omitempty"`         // 离线下载
    33  	Aria2Options     map[string]interface{} `json:"aria2_options,omitempty"` // 离线下载用户组配置
    34  	SourceBatchSize  int                    `json:"source_batch,omitempty"`
    35  	RedirectedSource bool                   `json:"redirected_source,omitempty"`
    36  	Aria2BatchSize   int                    `json:"aria2_batch,omitempty"`
    37  	AdvanceDelete    bool                   `json:"advance_delete,omitempty"`
    38  	WebDAVProxy      bool                   `json:"webdav_proxy,omitempty"`
    39  }
    40  
    41  // GetGroupByID 用ID获取用户组
    42  func GetGroupByID(ID interface{}) (Group, error) {
    43  	var group Group
    44  	result := DB.First(&group, ID)
    45  	return group, result.Error
    46  }
    47  
    48  // AfterFind 找到用户组后的钩子,处理Policy列表
    49  func (group *Group) AfterFind() (err error) {
    50  	// 解析用户组策略列表
    51  	if group.Policies != "" {
    52  		err = json.Unmarshal([]byte(group.Policies), &group.PolicyList)
    53  	}
    54  	if err != nil {
    55  		return err
    56  	}
    57  
    58  	// 解析用户组设置
    59  	if group.Options != "" {
    60  		err = json.Unmarshal([]byte(group.Options), &group.OptionsSerialized)
    61  	}
    62  
    63  	return err
    64  }
    65  
    66  // BeforeSave Save用户前的钩子
    67  func (group *Group) BeforeSave() (err error) {
    68  	err = group.SerializePolicyList()
    69  	return err
    70  }
    71  
    72  // SerializePolicyList 将序列后的可选策略列表、配置写入数据库字段
    73  // TODO 完善测试
    74  func (group *Group) SerializePolicyList() (err error) {
    75  	policies, err := json.Marshal(&group.PolicyList)
    76  	group.Policies = string(policies)
    77  	if err != nil {
    78  		return err
    79  	}
    80  
    81  	optionsValue, err := json.Marshal(&group.OptionsSerialized)
    82  	group.Options = string(optionsValue)
    83  	return err
    84  }