bucket.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package service
  2. import (
  3. "context"
  4. "go-common/app/admin/main/upload/model"
  5. "go-common/library/log"
  6. )
  7. // ListBucket .
  8. func (s *Service) ListBucket(c context.Context, lbp *model.ListBucketParam) (bucketPage *model.BucketListPage, err error) {
  9. var (
  10. buckets []*model.Bucket
  11. count int
  12. )
  13. if err = s.orm.Table("bucket").Order("id desc").Limit(lbp.PS).Offset((lbp.PN - 1) * lbp.PS).Find(&buckets).Error; err != nil {
  14. log.Error("read bucket error(%v)", err)
  15. return
  16. }
  17. if err = s.orm.Table("bucket").Count(&count).Error; err != nil {
  18. log.Error("bucket count error(%v)", err)
  19. err = nil
  20. }
  21. bucketPage = &model.BucketListPage{
  22. Items: buckets,
  23. Page: &model.Page{
  24. PS: lbp.PS,
  25. PN: lbp.PN,
  26. Total: count,
  27. },
  28. }
  29. return
  30. }
  31. // ListPublicBucket .
  32. func (s *Service) ListPublicBucket(c context.Context, lbp *model.ListBucketParam) (bucketPage *model.BucketListPage, err error) {
  33. var (
  34. buckets []*model.Bucket
  35. count int
  36. )
  37. if err = s.orm.Table("bucket").Order("id desc").Limit(lbp.PS).Offset((lbp.PN-1)*lbp.PS).Where("property != ? AND property != ?", model.PrivateRead, model.PrivateReadWrite).Find(&buckets).Error; err != nil {
  38. log.Error("read bucket error(%v)", err)
  39. return
  40. }
  41. if err = s.orm.Table("bucket").Count(&count).Error; err != nil {
  42. log.Error("bucket count error(%v)", err)
  43. err = nil
  44. }
  45. bucketPage = &model.BucketListPage{
  46. Items: buckets,
  47. Page: &model.Page{
  48. PS: lbp.PS,
  49. PN: lbp.PN,
  50. Total: count,
  51. },
  52. }
  53. return
  54. }
  55. // AddBucket .
  56. func (s *Service) AddBucket(c context.Context, abp *model.AddBucketParam) (err error) {
  57. b := &model.Bucket{BucketName: abp.Name}
  58. if err = s.orm.Table("bucket").Where("bucket_name=?", abp.Name).
  59. Assign(map[string]interface{}{
  60. "key_id": abp.KeyID,
  61. "key_secret": abp.KeySecret,
  62. "purge_cdn": abp.PurgeCDN,
  63. "property": abp.Property,
  64. "cache_control": abp.CacheControl,
  65. "domain": abp.Domain,
  66. }).FirstOrCreate(b).Error; err != nil {
  67. log.Error("Failed to add bucket (%+v): %v", b, err)
  68. return
  69. }
  70. return s.dao.CreateTable(context.Background(), abp.Name)
  71. }
  72. // DetailBucket .
  73. func (s *Service) DetailBucket(c context.Context, bucketName string) (bucket *model.Bucket, err error) {
  74. var (
  75. limits []*model.DirLimit
  76. )
  77. bucket = &model.Bucket{}
  78. if err = s.orm.Table("bucket").Where("bucket_name = ?", bucketName).Find(bucket).Error; err != nil {
  79. return
  80. }
  81. if err = s.orm.Table("dir_limit").Where("bucket_name = ?", bucket.BucketName).Find(&limits).Error; err != nil {
  82. return
  83. }
  84. if len(limits) == 0 {
  85. return
  86. }
  87. bucket.DirLimit = limits
  88. return
  89. }