bucket.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package dao
  2. import (
  3. "encoding/json"
  4. "strings"
  5. "go-common/app/interface/main/upload/model"
  6. "go-common/library/log"
  7. )
  8. // Buckets all bucket info from database.
  9. func (d *Dao) Buckets() (bucketMap map[string]*model.Bucket, err error) {
  10. var (
  11. buckets []*model.Bucket
  12. limitMap map[string]map[string]*model.DirConfig
  13. )
  14. if err = d.orm.Table("bucket").Find(&buckets).Error; err != nil {
  15. log.Error("orm.Table(bucket) error(%v)", err)
  16. return
  17. }
  18. if limitMap, err = d.dirLimits(); err != nil {
  19. return
  20. }
  21. bucketMap = make(map[string]*model.Bucket)
  22. for _, b := range buckets {
  23. v, ok := limitMap[b.Name]
  24. if ok {
  25. b.DirLimit = v
  26. }
  27. bucketMap[b.Name] = b
  28. }
  29. return
  30. }
  31. // dirLimits directory limit from database.
  32. func (d *Dao) dirLimits() (limitMap map[string]map[string]*model.DirConfig, err error) {
  33. limits := make([]*model.DirLimit, 0)
  34. if err = d.orm.Table("dir_limit").Find(&limits).Error; err != nil {
  35. return
  36. }
  37. limitMap = make(map[string]map[string]*model.DirConfig)
  38. for _, l := range limits {
  39. var (
  40. pic model.DirPicConfig
  41. rate model.DirRateConfig
  42. )
  43. if err = json.Unmarshal([]byte(l.DirPicConfig), &pic); err != nil {
  44. log.Error("json.Unmarshal(%s) error(%v)", l.DirPicConfig, err)
  45. err = nil
  46. continue
  47. }
  48. if pic.AllowType != "" {
  49. pic.AllowTypeSlice = strings.Split(pic.AllowType, ",")
  50. }
  51. if err = json.Unmarshal([]byte(l.DirRateConfig), &rate); err != nil {
  52. log.Error("json.Unmarshal(%s) error(%v)", l.DirRateConfig, err)
  53. err = nil
  54. continue
  55. }
  56. if _, ok := limitMap[l.BucketName]; !ok {
  57. limitMap[l.BucketName] = make(map[string]*model.DirConfig)
  58. }
  59. // NOTE empty dir is also in limit map
  60. l.Dir = strings.Trim(l.Dir, "/")
  61. limitMap[l.BucketName][l.Dir] = &model.DirConfig{Pic: pic, Rate: rate}
  62. }
  63. return
  64. }