shareEntry.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. AllowLivePreview 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. AllowLivePreview: true,
  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) GetShareUUIDFromPathHash(pathhash string) string {
  118. shareObject := s.GetShareObjectFromPathHash(pathhash)
  119. if shareObject == nil {
  120. return ""
  121. } else {
  122. return shareObject.UUID
  123. }
  124. }
  125. func (s *ShareEntryTable) GetShareObjectFromPathHash(pathhash string) *ShareOption {
  126. var targetShareOption *ShareOption = nil
  127. s.FileToUrlMap.Range(func(k, v interface{}) bool {
  128. thisPathhash := k.(string)
  129. shareObject := v.(*ShareOption)
  130. if thisPathhash == pathhash {
  131. targetShareOption = shareObject
  132. }
  133. return true
  134. })
  135. return targetShareOption
  136. }
  137. func (s *ShareEntryTable) GetShareObjectFromUUID(uuid string) *ShareOption {
  138. var targetShareOption *ShareOption
  139. s.UrlToFileMap.Range(func(k, v interface{}) bool {
  140. thisUuid := k.(string)
  141. shareObject := v.(*ShareOption)
  142. if thisUuid == uuid {
  143. targetShareOption = shareObject
  144. }
  145. return true
  146. })
  147. return targetShareOption
  148. }
  149. func (s *ShareEntryTable) FileIsShared(pathhash string) bool {
  150. shareUUID := s.GetShareUUIDFromPathHash(pathhash)
  151. return shareUUID != ""
  152. }
  153. func (s *ShareEntryTable) RemoveShareByPathHash(pathhash string) error {
  154. so, ok := s.FileToUrlMap.Load(pathhash)
  155. if ok {
  156. shareUUID := so.(*ShareOption).UUID
  157. s.UrlToFileMap.Delete(shareUUID)
  158. s.FileToUrlMap.Delete(pathhash)
  159. s.Database.Delete("share", shareUUID)
  160. } else {
  161. return errors.New("Share with given realpath not exists")
  162. }
  163. return nil
  164. }
  165. func (s *ShareEntryTable) RemoveShareByUUID(uuid string) error {
  166. so, ok := s.UrlToFileMap.Load(uuid)
  167. if ok {
  168. shareOption := so.(*ShareOption)
  169. s.FileToUrlMap.Delete(shareOption.PathHash)
  170. s.UrlToFileMap.Delete(uuid)
  171. s.Database.Delete("share", uuid)
  172. } else {
  173. return errors.New("Share with given uuid not exists")
  174. }
  175. return nil
  176. }
  177. func (s *ShareEntryTable) ResolveShareOptionFromShareSubpath(subpath string) (*ShareOption, error) {
  178. subpathElements := strings.Split(filepath.ToSlash(filepath.Clean(subpath))[1:], "/")
  179. if len(subpathElements) >= 1 {
  180. shareObject := s.GetShareObjectFromUUID(subpathElements[0])
  181. if shareObject == nil {
  182. return nil, errors.New("Invalid subpath")
  183. } else {
  184. return shareObject, nil
  185. }
  186. } else {
  187. return nil, errors.New("Invalid subpath")
  188. }
  189. }
  190. /*
  191. func (s *ShareEntryTable) ResolveShareVrootPath(subpath string, username string, usergroup []string) (string, error) {
  192. //Get a list of accessible files from this user
  193. subpathElements := strings.Split(filepath.ToSlash(filepath.Clean(subpath))[1:], "/")
  194. if len(subpathElements) == 0 {
  195. //Requesting root folder.
  196. return "", errors.New("This virtual file system router do not support root listing")
  197. }
  198. //Analysis the subpath elements
  199. if len(subpathElements) == 1 {
  200. return "", errors.New("Redirect: parent")
  201. } else if len(subpathElements) == 2 {
  202. //ID only or ID with the target filename
  203. shareObject := s.GetShareObjectFromUUID(subpathElements[0])
  204. if shareObject == nil {
  205. return "", errors.New("Share file not found")
  206. }
  207. return shareObject.FileRealPath, nil
  208. } else if len(subpathElements) > 2 {
  209. //Loading folder / files inside folder type shares
  210. shareObject := s.GetShareObjectFromUUID(subpathElements[0])
  211. folderSubpaths := append([]string{shareObject.FileRealPath}, subpathElements[2:]...)
  212. targetFolder := filepath.Join(folderSubpaths...)
  213. return targetFolder, nil
  214. }
  215. return "", errors.New("Not implemented")
  216. }
  217. */
  218. func GetPathHash(fsh *filesystem.FileSystemHandler, vpath string, username string) string {
  219. fshAbs := fsh.FileSystemAbstraction
  220. rpath := ""
  221. if strings.Contains(vpath, ":/") {
  222. rpath, _ = fshAbs.VirtualPathToRealPath(vpath, username)
  223. rpath = filepath.ToSlash(rpath)
  224. } else {
  225. //Passed in realpath as vpath.
  226. rpath = vpath
  227. }
  228. hash := md5.Sum([]byte(fsh.UUID + "_" + rpath))
  229. return hex.EncodeToString(hash[:])
  230. }