shareEntry.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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, err := GetPathHash(srcFsh, vpath, username)
  66. if err != nil {
  67. return nil, err
  68. }
  69. //Check if the share already exists. If yes, use the previous link
  70. val, ok := s.FileToUrlMap.Load(sharePathHash)
  71. if ok {
  72. //Exists. Send back the old share url
  73. ShareOption := val.(*ShareOption)
  74. return ShareOption, nil
  75. } else {
  76. //Create new link for this file
  77. shareUUID := uuid.NewV4().String()
  78. //Create a share object
  79. shareOption := ShareOption{
  80. UUID: shareUUID,
  81. PathHash: sharePathHash,
  82. FileVirtualPath: vpath,
  83. FileRealPath: rpath,
  84. Owner: username,
  85. Accessibles: usergroups,
  86. Permission: "anyone",
  87. IsFolder: srcFsh.FileSystemAbstraction.IsDir(rpath),
  88. }
  89. //Store results on two map to make sure O(1) Lookup time
  90. s.FileToUrlMap.Store(sharePathHash, &shareOption)
  91. s.UrlToFileMap.Store(shareUUID, &shareOption)
  92. //Write object to database
  93. s.Database.Write("share", shareUUID, shareOption)
  94. return &shareOption, nil
  95. }
  96. }
  97. //Delete the share on this vpath
  98. func (s *ShareEntryTable) DeleteShareByPathHash(pathhash string) error {
  99. //Check if the share already exists. If yes, use the previous link
  100. val, ok := s.FileToUrlMap.Load(pathhash)
  101. if ok {
  102. //Exists. Send back the old share url
  103. uuid := val.(*ShareOption).UUID
  104. //Remove this from the database
  105. err := s.Database.Delete("share", uuid)
  106. if err != nil {
  107. return err
  108. }
  109. //Remove this form the current sync map
  110. s.UrlToFileMap.Delete(uuid)
  111. s.FileToUrlMap.Delete(pathhash)
  112. return nil
  113. } else {
  114. //Already deleted from buffered record.
  115. return nil
  116. }
  117. }
  118. func (s *ShareEntryTable) DeleteShareByUUID(uuid string) error {
  119. //Check if the share already exists. If yes, use the previous link
  120. val, ok := s.UrlToFileMap.Load(uuid)
  121. if ok {
  122. //Exists. Send back the old share url
  123. so := val.(*ShareOption)
  124. //Remove this from the database
  125. err := s.Database.Delete("share", so.UUID)
  126. if err != nil {
  127. return err
  128. }
  129. //Remove this form the current sync map
  130. s.FileToUrlMap.Delete(so.PathHash)
  131. s.UrlToFileMap.Delete(uuid)
  132. return nil
  133. } else {
  134. //Already deleted from buffered record.
  135. return nil
  136. }
  137. }
  138. func (s *ShareEntryTable) GetShareUUIDFromPathHash(pathhash string) string {
  139. shareObject := s.GetShareObjectFromPathHash(pathhash)
  140. if shareObject == nil {
  141. return ""
  142. } else {
  143. return shareObject.UUID
  144. }
  145. }
  146. func (s *ShareEntryTable) GetShareObjectFromPathHash(pathhash string) *ShareOption {
  147. var targetShareOption *ShareOption = nil
  148. s.FileToUrlMap.Range(func(k, v interface{}) bool {
  149. thisPathhash := k.(string)
  150. shareObject := v.(*ShareOption)
  151. if thisPathhash == pathhash {
  152. targetShareOption = shareObject
  153. }
  154. return true
  155. })
  156. return targetShareOption
  157. }
  158. func (s *ShareEntryTable) GetShareObjectFromUUID(uuid string) *ShareOption {
  159. var targetShareOption *ShareOption
  160. s.UrlToFileMap.Range(func(k, v interface{}) bool {
  161. thisUuid := k.(string)
  162. shareObject := v.(*ShareOption)
  163. if thisUuid == uuid {
  164. targetShareOption = shareObject
  165. }
  166. return true
  167. })
  168. return targetShareOption
  169. }
  170. func (s *ShareEntryTable) FileIsShared(pathhash string) bool {
  171. shareUUID := s.GetShareUUIDFromPathHash(pathhash)
  172. return shareUUID != ""
  173. }
  174. func (s *ShareEntryTable) RemoveShareByPathHash(pathhash string) error {
  175. so, ok := s.FileToUrlMap.Load(pathhash)
  176. if ok {
  177. shareUUID := so.(*ShareOption).UUID
  178. s.UrlToFileMap.Delete(shareUUID)
  179. s.FileToUrlMap.Delete(pathhash)
  180. s.Database.Delete("share", shareUUID)
  181. } else {
  182. return errors.New("Share with given pathhash not exists. Given: " + pathhash)
  183. }
  184. return nil
  185. }
  186. func (s *ShareEntryTable) RemoveShareByUUID(uuid string) error {
  187. so, ok := s.UrlToFileMap.Load(uuid)
  188. if ok {
  189. shareOption := so.(*ShareOption)
  190. s.FileToUrlMap.Delete(shareOption.PathHash)
  191. s.UrlToFileMap.Delete(uuid)
  192. s.Database.Delete("share", uuid)
  193. } else {
  194. return errors.New("Share with given uuid not exists")
  195. }
  196. return nil
  197. }
  198. func (s *ShareEntryTable) ResolveShareOptionFromShareSubpath(subpath string) (*ShareOption, error) {
  199. subpathElements := strings.Split(filepath.ToSlash(filepath.Clean(subpath))[1:], "/")
  200. if len(subpathElements) >= 1 {
  201. shareObject := s.GetShareObjectFromUUID(subpathElements[0])
  202. if shareObject == nil {
  203. return nil, errors.New("Invalid subpath")
  204. } else {
  205. return shareObject, nil
  206. }
  207. } else {
  208. return nil, errors.New("Invalid subpath")
  209. }
  210. }
  211. func GetPathHash(fsh *filesystem.FileSystemHandler, vpath string, username string) (string, error) {
  212. return fsh.GetUniquePathHash(vpath, username)
  213. }