shareEntry.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package shareEntry
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "path/filepath"
  6. "sync"
  7. "imuslab.com/arozos/mod/database"
  8. )
  9. /*
  10. Share Entry
  11. This module is designed to isolate the entry operatiosn with the
  12. handle operations so as to reduce the complexity of recursive import
  13. during development
  14. */
  15. type ShareEntryTable struct {
  16. FileToUrlMap *sync.Map
  17. UrlToFileMap *sync.Map
  18. Database *database.Database
  19. }
  20. type ShareOption struct {
  21. UUID string
  22. FileRealPath string
  23. Owner string
  24. Accessibles []string //Use to store username or group names if permission is groups or users
  25. Permission string //Access permission, allow {anyone / signedin / samegroup / groups / users}
  26. AllowLivePreview bool
  27. }
  28. func NewShareEntryTable(db *database.Database) *ShareEntryTable {
  29. //Create the share table if not exists
  30. db.NewTable("share")
  31. FileToUrlMap := sync.Map{}
  32. UrlToFileMap := sync.Map{}
  33. //Load the old share links
  34. entries, _ := db.ListTable("share")
  35. for _, keypairs := range entries {
  36. shareObject := new(ShareOption)
  37. json.Unmarshal(keypairs[1], &shareObject)
  38. if shareObject != nil {
  39. //Append this to the maps
  40. FileToUrlMap.Store(shareObject.FileRealPath, shareObject)
  41. UrlToFileMap.Store(shareObject.UUID, shareObject)
  42. }
  43. }
  44. return &ShareEntryTable{
  45. FileToUrlMap: &FileToUrlMap,
  46. UrlToFileMap: &UrlToFileMap,
  47. Database: db,
  48. }
  49. }
  50. //Delete the share on this vpath
  51. func (s *ShareEntryTable) DeleteShare(rpath string) error {
  52. //Check if the share already exists. If yes, use the previous link
  53. val, ok := s.FileToUrlMap.Load(rpath)
  54. if ok {
  55. //Exists. Send back the old share url
  56. uuid := val.(*ShareOption).UUID
  57. //Remove this from the database
  58. err := s.Database.Delete("share", uuid)
  59. if err != nil {
  60. return err
  61. }
  62. //Remove this form the current sync map
  63. s.UrlToFileMap.Delete(uuid)
  64. s.FileToUrlMap.Delete(rpath)
  65. return nil
  66. } else {
  67. //Already deleted from buffered record.
  68. return nil
  69. }
  70. }
  71. func (s *ShareEntryTable) GetShareUUIDFromPath(rpath string) string {
  72. targetShareObject := s.GetShareObjectFromRealPath(rpath)
  73. if (targetShareObject) != nil {
  74. return targetShareObject.UUID
  75. }
  76. return ""
  77. }
  78. func (s *ShareEntryTable) GetShareObjectFromRealPath(rpath string) *ShareOption {
  79. rpath = filepath.ToSlash(filepath.Clean(rpath))
  80. var targetShareOption *ShareOption
  81. s.FileToUrlMap.Range(func(k, v interface{}) bool {
  82. filePath := k.(string)
  83. shareObject := v.(*ShareOption)
  84. if filepath.ToSlash(filepath.Clean(filePath)) == rpath {
  85. targetShareOption = shareObject
  86. }
  87. return true
  88. })
  89. return targetShareOption
  90. }
  91. func (s *ShareEntryTable) GetShareObjectFromUUID(uuid string) *ShareOption {
  92. var targetShareOption *ShareOption
  93. s.UrlToFileMap.Range(func(k, v interface{}) bool {
  94. thisUuid := k.(string)
  95. shareObject := v.(*ShareOption)
  96. if thisUuid == uuid {
  97. targetShareOption = shareObject
  98. }
  99. return true
  100. })
  101. return targetShareOption
  102. }
  103. func (s *ShareEntryTable) FileIsShared(rpath string) bool {
  104. shareUUID := s.GetShareUUIDFromPath(rpath)
  105. return shareUUID != ""
  106. }
  107. func (s *ShareEntryTable) RemoveShareByRealpath(rpath string) error {
  108. so, ok := s.FileToUrlMap.Load(rpath)
  109. if ok {
  110. shareUUID := so.(*ShareOption).UUID
  111. s.UrlToFileMap.Delete(shareUUID)
  112. s.FileToUrlMap.Delete(rpath)
  113. s.Database.Delete("share", shareUUID)
  114. } else {
  115. return errors.New("Share with given realpath not exists")
  116. }
  117. return nil
  118. }
  119. func (s *ShareEntryTable) RemoveShareByUUID(uuid string) error {
  120. so, ok := s.UrlToFileMap.Load(uuid)
  121. if ok {
  122. s.FileToUrlMap.Delete(so.(*ShareOption).FileRealPath)
  123. s.UrlToFileMap.Delete(uuid)
  124. s.Database.Delete("share", uuid)
  125. } else {
  126. return errors.New("Share with given uuid not exists")
  127. }
  128. return nil
  129. }