/code/rest/matter_model.go

https://github.com/eyebluecn/tank · Go · 158 lines · 111 code · 30 blank · 17 comment · 12 complexity · 8b5ba9abb024b8335743672e401a8b34 MD5 · raw file

  1. package rest
  2. import (
  3. "fmt"
  4. "github.com/eyebluecn/tank/code/core"
  5. "github.com/eyebluecn/tank/code/tool/i18n"
  6. "github.com/eyebluecn/tank/code/tool/result"
  7. "github.com/eyebluecn/tank/code/tool/util"
  8. jsoniter "github.com/json-iterator/go"
  9. "net/http"
  10. "regexp"
  11. "strings"
  12. "time"
  13. )
  14. const (
  15. //root matter's uuid
  16. MATTER_ROOT = "root"
  17. //cache directory name.
  18. MATTER_CACHE = "cache"
  19. //zip file temp directory.
  20. MATTER_ZIP = "zip"
  21. MATTER_NAME_MAX_LENGTH = 200
  22. MATTER_NAME_MAX_DEPTH = 32
  23. //matter name pattern
  24. MATTER_NAME_PATTERN = `[\\/:*?"<>|]`
  25. )
  26. /**
  27. * file is too common. so we use matter as file.
  28. */
  29. type Matter struct {
  30. Base
  31. Puuid string `json:"puuid" gorm:"type:char(36);index:idx_puuid"`
  32. UserUuid string `json:"userUuid" gorm:"type:char(36);index:idx_uu"`
  33. Username string `json:"username" gorm:"type:varchar(45) not null"`
  34. Dir bool `json:"dir" gorm:"type:tinyint(1) not null;default:0"`
  35. Name string `json:"name" gorm:"type:varchar(255) not null"`
  36. Md5 string `json:"md5" gorm:"type:varchar(45)"`
  37. Size int64 `json:"size" gorm:"type:bigint(20) not null;default:0"`
  38. Privacy bool `json:"privacy" gorm:"type:tinyint(1) not null;default:0"`
  39. Path string `json:"path" gorm:"type:varchar(1024)"`
  40. Times int64 `json:"times" gorm:"type:bigint(20) not null;default:0"`
  41. Parent *Matter `json:"parent" gorm:"-"`
  42. Children []*Matter `json:"-" gorm:"-"`
  43. Prop string `json:"prop" gorm:"type:varchar(1024) not null;default:'{}'"`
  44. VisitTime time.Time `json:"visitTime" gorm:"type:timestamp not null;default:'2018-01-01 00:00:00'"`
  45. Deleted bool `json:"deleted" gorm:"type:tinyint(1) not null;index:idx_del;default:0"`
  46. DeleteTime time.Time `json:"deleteTime" gorm:"type:timestamp not null;index:idx_delt;default:'2018-01-01 00:00:00'"`
  47. }
  48. // set File's table name to be `profiles`
  49. func (Matter) TableName() string {
  50. return core.TABLE_PREFIX + "matter"
  51. }
  52. // get matter's absolute path. the Path property is relative path in db.
  53. func (this *Matter) AbsolutePath() string {
  54. return GetUserMatterRootDir(this.Username) + this.Path
  55. }
  56. func (this *Matter) MimeType() string {
  57. return util.GetMimeType(util.GetExtension(this.Name))
  58. }
  59. //Create a root matter. It's convenient for copy and move
  60. func NewRootMatter(user *User) *Matter {
  61. matter := &Matter{}
  62. matter.Uuid = MATTER_ROOT
  63. matter.UserUuid = user.Uuid
  64. matter.Username = user.Username
  65. matter.Dir = true
  66. matter.Path = ""
  67. matter.CreateTime = user.CreateTime
  68. matter.UpdateTime = user.UpdateTime
  69. matter.VisitTime = user.UpdateTime
  70. return matter
  71. }
  72. //get user's space absolute path
  73. func GetUserSpaceRootDir(username string) (rootDirPath string) {
  74. rootDirPath = fmt.Sprintf("%s/%s", core.CONFIG.MatterPath(), username)
  75. return rootDirPath
  76. }
  77. //get user's root absolute path
  78. func GetUserMatterRootDir(username string) (rootDirPath string) {
  79. rootDirPath = fmt.Sprintf("%s/%s/%s", core.CONFIG.MatterPath(), username, MATTER_ROOT)
  80. return rootDirPath
  81. }
  82. //get user's cache absolute path
  83. func GetUserCacheRootDir(username string) (rootDirPath string) {
  84. rootDirPath = fmt.Sprintf("%s/%s/%s", core.CONFIG.MatterPath(), username, MATTER_CACHE)
  85. return rootDirPath
  86. }
  87. //get user's zip absolute path
  88. func GetUserZipRootDir(username string) (rootDirPath string) {
  89. rootDirPath = fmt.Sprintf("%s/%s/%s", core.CONFIG.MatterPath(), username, MATTER_ZIP)
  90. return rootDirPath
  91. }
  92. //check matter's name. If error, panic.
  93. func CheckMatterName(request *http.Request, name string) string {
  94. if name == "" {
  95. panic(result.BadRequest("name cannot be null"))
  96. }
  97. if strings.HasPrefix(name, " ") || strings.HasSuffix(name, " ") {
  98. panic(result.BadRequest("name cannot start with or end with space"))
  99. }
  100. if m, _ := regexp.MatchString(MATTER_NAME_PATTERN, name); m {
  101. panic(result.BadRequestI18n(request, i18n.MatterNameContainSpecialChars))
  102. }
  103. if len(name) > MATTER_NAME_MAX_LENGTH {
  104. panic(result.BadRequestI18n(request, i18n.MatterNameLengthExceedLimit, len(name), MATTER_NAME_MAX_LENGTH))
  105. }
  106. return name
  107. }
  108. //fetch the props
  109. func (this *Matter) FetchPropMap() map[string]string {
  110. m := make(map[string]string)
  111. json := this.Prop
  112. if json == "" {
  113. json = EMPTY_JSON_MAP
  114. }
  115. err := jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal([]byte(json), &m)
  116. if err != nil {
  117. panic(err)
  118. }
  119. return m
  120. }
  121. //fetch the props
  122. func (this *Matter) SetPropMap(propMap map[string]string) {
  123. b, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(propMap)
  124. if err != nil {
  125. panic(err)
  126. }
  127. this.Prop = string(b)
  128. }