shareEntry.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. package shareEntry
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "path/filepath"
  6. "strings"
  7. "sync"
  8. uuid "github.com/satori/go.uuid"
  9. "imuslab.com/arozos/mod/database"
  10. "imuslab.com/arozos/mod/filesystem"
  11. )
  12. /*
  13. Share Entry
  14. This module is designed to isolate the entry operatiosn with the
  15. handle operations so as to reduce the complexity of recursive import
  16. during development
  17. */
  18. type ShareEntryTable struct {
  19. FileToUrlMap *sync.Map
  20. UrlToFileMap *sync.Map
  21. Database *database.Database
  22. }
  23. type ShareOption struct {
  24. UUID string
  25. PathHash string //Path Hash, the key for loading a share from vpath and fsh specific config
  26. FileVirtualPath string
  27. FileRealPath string
  28. Owner string
  29. Accessibles []string //Use to store username or group names if permission is groups or users
  30. Permission string //Access permission, allow {anyone / signedin / samegroup / groups / users}
  31. IsFolder bool
  32. }
  33. func NewShareEntryTable(db *database.Database) *ShareEntryTable {
  34. //Create the share table if not exists
  35. db.NewTable("share")
  36. FileToUrlMap := sync.Map{}
  37. UrlToFileMap := sync.Map{}
  38. //Load the old share links
  39. entries, _ := db.ListTable("share")
  40. for _, keypairs := range entries {
  41. shareObject := new(ShareOption)
  42. json.Unmarshal(keypairs[1], &shareObject)
  43. if shareObject != nil {
  44. //Append this to the maps
  45. FileToUrlMap.Store(shareObject.PathHash, shareObject)
  46. UrlToFileMap.Store(shareObject.UUID, shareObject)
  47. }
  48. }
  49. return &ShareEntryTable{
  50. FileToUrlMap: &FileToUrlMap,
  51. UrlToFileMap: &UrlToFileMap,
  52. Database: db,
  53. }
  54. }
  55. func (s *ShareEntryTable) CreateNewShare(srcFsh *filesystem.FileSystemHandler, vpath string, username string, usergroups []string) (*ShareOption, error) {
  56. rpath, err := srcFsh.FileSystemAbstraction.VirtualPathToRealPath(vpath, username)
  57. if err != nil {
  58. return nil, errors.New("Unable to translate path given")
  59. }
  60. rpath = filepath.ToSlash(filepath.Clean(rpath))
  61. //Check if source file exists
  62. if !srcFsh.FileSystemAbstraction.FileExists(rpath) {
  63. return nil, errors.New("Unable to find the file on disk")
  64. }
  65. sharePathHash := GetPathHash(srcFsh, vpath, username)
  66. //Check if the share already exists. If yes, use the previous link
  67. val, ok := s.FileToUrlMap.Load(sharePathHash)
  68. if ok {
  69. //Exists. Send back the old share url
  70. ShareOption := val.(*ShareOption)
  71. return ShareOption, nil
  72. } else {
  73. //Create new link for this file
  74. shareUUID := uuid.NewV4().String()
  75. //Create a share object
  76. shareOption := ShareOption{
  77. UUID: shareUUID,
  78. PathHash: sharePathHash,
  79. FileVirtualPath: vpath,
  80. FileRealPath: rpath,
  81. Owner: username,
  82. Accessibles: usergroups,
  83. Permission: "anyone",
  84. IsFolder: srcFsh.FileSystemAbstraction.IsDir(rpath),
  85. }
  86. //Store results on two map to make sure O(1) Lookup time
  87. s.FileToUrlMap.Store(sharePathHash, &shareOption)
  88. s.UrlToFileMap.Store(shareUUID, &shareOption)
  89. //Write object to database
  90. s.Database.Write("share", shareUUID, shareOption)
  91. return &shareOption, nil
  92. }
  93. }
  94. //Delete the share on this vpath
  95. func (s *ShareEntryTable) DeleteShareByPathHash(pathhash string) error {
  96. //Check if the share already exists. If yes, use the previous link
  97. val, ok := s.FileToUrlMap.Load(pathhash)
  98. if ok {
  99. //Exists. Send back the old share url
  100. uuid := val.(*ShareOption).UUID
  101. //Remove this from the database
  102. err := s.Database.Delete("share", uuid)
  103. if err != nil {
  104. return err
  105. }
  106. //Remove this form the current sync map
  107. s.UrlToFileMap.Delete(uuid)
  108. s.FileToUrlMap.Delete(pathhash)
  109. return nil
  110. } else {
  111. //Already deleted from buffered record.
  112. return nil
  113. }
  114. }
  115. func (s *ShareEntryTable) DeleteShareByUUID(uuid string) error {
  116. //Check if the share already exists. If yes, use the previous link
  117. val, ok := s.UrlToFileMap.Load(uuid)
  118. if ok {
  119. //Exists. Send back the old share url
  120. so := val.(*ShareOption)
  121. //Remove this from the database
  122. err := s.Database.Delete("share", so.UUID)
  123. if err != nil {
  124. return err
  125. }
  126. //Remove this form the current sync map
  127. s.FileToUrlMap.Delete(so.PathHash)
  128. s.UrlToFileMap.Delete(uuid)
  129. return nil
  130. } else {
  131. //Already deleted from buffered record.
  132. return nil
  133. }
  134. }
  135. func (s *ShareEntryTable) GetShareUUIDFromPathHash(pathhash string) string {
  136. shareObject := s.GetShareObjectFromPathHash(pathhash)
  137. if shareObject == nil {
  138. return ""
  139. } else {
  140. return shareObject.UUID
  141. }
  142. }
  143. func (s *ShareEntryTable) GetShareObjectFromPathHash(pathhash string) *ShareOption {
  144. var targetShareOption *ShareOption = nil
  145. s.FileToUrlMap.Range(func(k, v interface{}) bool {
  146. thisPathhash := k.(string)
  147. shareObject := v.(*ShareOption)
  148. if thisPathhash == pathhash {
  149. targetShareOption = shareObject
  150. }
  151. return true
  152. })
  153. return targetShareOption
  154. }
  155. func (s *ShareEntryTable) GetShareObjectFromUUID(uuid string) *ShareOption {
  156. var targetShareOption *ShareOption
  157. s.UrlToFileMap.Range(func(k, v interface{}) bool {
  158. thisUuid := k.(string)
  159. shareObject := v.(*ShareOption)
  160. if thisUuid == uuid {
  161. targetShareOption = shareObject
  162. }
  163. return true
  164. })
  165. return targetShareOption
  166. }
  167. func (s *ShareEntryTable) FileIsShared(pathhash string) bool {
  168. shareUUID := s.GetShareUUIDFromPathHash(pathhash)
  169. return shareUUID != ""
  170. }
  171. func (s *ShareEntryTable) RemoveShareByPathHash(pathhash string) error {
  172. so, ok := s.FileToUrlMap.Load(pathhash)
  173. if ok {
  174. shareUUID := so.(*ShareOption).UUID
  175. s.UrlToFileMap.Delete(shareUUID)
  176. s.FileToUrlMap.Delete(pathhash)
  177. s.Database.Delete("share", shareUUID)
  178. } else {
  179. return errors.New("Share with given realpath not exists")
  180. }
  181. return nil
  182. }
  183. func (s *ShareEntryTable) RemoveShareByUUID(uuid string) error {
  184. so, ok := s.UrlToFileMap.Load(uuid)
  185. if ok {
  186. shareOption := so.(*ShareOption)
  187. s.FileToUrlMap.Delete(shareOption.PathHash)
  188. s.UrlToFileMap.Delete(uuid)
  189. s.Database.Delete("share", uuid)
  190. } else {
  191. return errors.New("Share with given uuid not exists")
  192. }
  193. return nil
  194. }
  195. func (s *ShareEntryTable) ResolveShareOptionFromShareSubpath(subpath string) (*ShareOption, error) {
  196. subpathElements := strings.Split(filepath.ToSlash(filepath.Clean(subpath))[1:], "/")
  197. if len(subpathElements) >= 1 {
  198. shareObject := s.GetShareObjectFromUUID(subpathElements[0])
  199. if shareObject == nil {
  200. return nil, errors.New("Invalid subpath")
  201. } else {
  202. return shareObject, nil
  203. }
  204. } else {
  205. return nil, errors.New("Invalid subpath")
  206. }
  207. }
  208. func GetPathHash(fsh *filesystem.FileSystemHandler, vpath string, username string) string {
  209. return fsh.GetUniquePathHash(vpath, username)
  210. }