shareEntry.go 6.7 KB

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