objs.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package local_objs
  4. import (
  5. "git.getensh.com/common/gopkgs/database"
  6. "gorm.io/gorm"
  7. "os"
  8. "strings"
  9. "time"
  10. "xingjia-management-gateway/errors"
  11. dbmodel "xingjia-management-gateway/model"
  12. "xingjia-management-gateway/parser"
  13. )
  14. func parseObjUrls(objUrls []string) []string {
  15. ret := []string{}
  16. for _, v := range objUrls {
  17. array := strings.Split(v, "=")
  18. if len(array) != 2 {
  19. continue
  20. }
  21. name := strings.TrimSpace(array[1])
  22. ret = append(ret, name)
  23. }
  24. return ret
  25. }
  26. func AddToDb(db *gorm.DB, name string) error {
  27. p := dbmodel.TLocalObjs{
  28. Name: name,
  29. Used: 0,
  30. Timestamp: time.Now().Unix(),
  31. }
  32. err := p.Insert(db)
  33. if err != nil {
  34. return errors.DataBaseError
  35. }
  36. return nil
  37. }
  38. func prepareInAndOut(in []string, out []string) ([]string, []string) {
  39. in = parseObjUrls(in)
  40. out = parseObjUrls(out)
  41. inM := map[string]bool{}
  42. for _, v := range in {
  43. if v == "" {
  44. continue
  45. }
  46. inM[v] = true
  47. }
  48. outM := map[string]bool{}
  49. for _, v := range out {
  50. if v == "" {
  51. continue
  52. }
  53. outM[v] = true
  54. }
  55. tmp := []string{}
  56. for k, _ := range inM {
  57. if _, ok := outM[k]; ok {
  58. tmp = append(tmp, k)
  59. }
  60. }
  61. for _, v := range tmp {
  62. delete(inM, v)
  63. delete(outM, v)
  64. }
  65. inRet := []string{}
  66. outRet := []string{}
  67. for v, _ := range inM {
  68. inRet = append(inRet, v)
  69. }
  70. for v, _ := range outM {
  71. outRet = append(outRet, v)
  72. }
  73. return inRet, outRet
  74. }
  75. func InAndOut(db *gorm.DB, in []string, out []string) error {
  76. in, out = prepareInAndOut(in, out)
  77. p := dbmodel.TLocalObjs{}
  78. if len(in) > 0 {
  79. where := map[string]interface{}{}
  80. values := map[string]interface{}{}
  81. where["name in"] = in
  82. values["used"] = 1
  83. err := p.Update(db, where, values)
  84. if err != nil {
  85. return errors.DataBaseError
  86. }
  87. }
  88. if len(out) == 0 {
  89. return nil
  90. }
  91. where := map[string]interface{}{}
  92. where["name in"] = out
  93. err := p.Delete(db, where)
  94. if err != nil {
  95. return errors.DataBaseError
  96. }
  97. for _, v := range out {
  98. path := parser.Conf.ObjPath + v
  99. os.Remove(path)
  100. }
  101. return nil
  102. }
  103. func DelTask() {
  104. t := time.NewTicker(24 * time.Hour)
  105. p := dbmodel.TLocalObjs{}
  106. where := map[string]interface{}{
  107. "timestamp <": time.Now().Unix() - 24*60*60,
  108. "used": 0,
  109. }
  110. filter := map[string]interface{}{}
  111. for {
  112. select {
  113. case <-t.C:
  114. list, _ := p.List(database.DB(), where, nil, -1, -1)
  115. for _, v := range list {
  116. path := parser.Conf.ObjPath + v.Name
  117. os.Remove(path)
  118. filter["id"] = v.ID
  119. p.Delete(database.DB(), where)
  120. }
  121. }
  122. }
  123. }