file_system.share.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package main
  2. /*
  3. share:/ - files shared with the user
  4. Mounts the drive of mod/filesystem/abstractions/sharefs into the base
  5. storage pool, so every user has a share:/ root listing what other users
  6. shared with them (worked out by the share manager, mod/share/drive.go).
  7. The File Manager shows it as "Shared with me" above the trash bin rather
  8. than among the storage devices, and web apps open its files by path like
  9. those of any other drive.
  10. The drive is read only, which the file operation handlers already check.
  11. What a share allows beyond reading - taking a copy - is checked here by
  12. sharedDriveCopyDenied for the operations that would; the File Manager
  13. does not offer them for items that disallow it in the first place.
  14. */
  15. import (
  16. "encoding/base64"
  17. "errors"
  18. "path/filepath"
  19. "time"
  20. fs "imuslab.com/arozos/mod/filesystem"
  21. "imuslab.com/arozos/mod/filesystem/abstractions/sharefs"
  22. "imuslab.com/arozos/mod/filesystem/arozfs"
  23. "imuslab.com/arozos/mod/filesystem/metadata"
  24. "imuslab.com/arozos/mod/share"
  25. "imuslab.com/arozos/mod/user"
  26. )
  27. // sharedDriveResolver hands the drive the share manager's view of what is
  28. // shared with each user. The drive is mounted with the storage pools, before
  29. // FileSystemInit creates the share manager, so it is looked up per call.
  30. type sharedDriveResolver struct{}
  31. func (sharedDriveResolver) Items(username string) []*sharefs.Item {
  32. if shareManager == nil {
  33. return nil
  34. }
  35. return shareManager.Items(username)
  36. }
  37. // sharedDriveThumbnail renders the thumbnail of a shared file on its owner's
  38. // drive, where the owner's own thumbnail cache is kept
  39. func sharedDriveThumbnail(fsh *fs.FileSystemHandler, rpath string) ([]byte, error) {
  40. if thumbRenderHandler == nil || !metadata.ThumbnailSupported(rpath) {
  41. return nil, arozfs.ErrNoThumbnail
  42. }
  43. encoded, err := thumbRenderHandler.LoadCache(fsh, rpath, false)
  44. if err != nil {
  45. return nil, err
  46. }
  47. data, err := base64.StdEncoding.DecodeString(encoded)
  48. if err != nil || len(data) == 0 {
  49. return nil, arozfs.ErrNoThumbnail
  50. }
  51. return data, nil
  52. }
  53. func newSharedDriveHandler() *fs.FileSystemHandler {
  54. abs := sharefs.New(share.SharedDriveUUID, sharedDriveResolver{}, sharefs.Options{
  55. Thumbnail: sharedDriveThumbnail,
  56. BufferDir: filepath.Join(*tmp_directory, "sharefs"),
  57. })
  58. return &fs.FileSystemHandler{
  59. Name: "Shared with me",
  60. UUID: share.SharedDriveUUID,
  61. Path: share.SharedDriveUUID + ":/",
  62. Hierarchy: "public",
  63. HierarchyConfig: fs.DefaultEmptyHierarchySpecificConfig,
  64. ReadOnly: true,
  65. RequireBuffer: false,
  66. InitiationTime: time.Now().Unix(),
  67. FileSystemAbstraction: abs,
  68. Filesystem: "share",
  69. StartOptions: fs.FileSystemOption{
  70. Name: "Shared with me",
  71. Uuid: share.SharedDriveUUID,
  72. Path: share.SharedDriveUUID + ":/",
  73. Access: "readonly",
  74. Hierarchy: "public",
  75. Filesystem: "share",
  76. },
  77. RuntimePersistenceConfig: fs.RuntimePersistenceConfig{
  78. LocalBufferPath: *tmp_directory,
  79. },
  80. }
  81. }
  82. // sharedDriveAttach adds the drive to the handlers of the base storage pool,
  83. // unless a configured storage already uses its id
  84. func sharedDriveAttach(handlers []*fs.FileSystemHandler) []*fs.FileSystemHandler {
  85. for _, fsh := range handlers {
  86. if fsh.UUID == share.SharedDriveUUID {
  87. systemWideLogger.PrintAndLog("Storage", "A storage is mounted as "+share.SharedDriveUUID+
  88. ":/, where files shared with each user would appear. Give it another id to enable Shared with me.", nil)
  89. return handlers
  90. }
  91. }
  92. return append(handlers, newSharedDriveHandler())
  93. }
  94. // sharedDriveOwnerOf returns who shared a file on the share:/ drive with the
  95. // user, or "" for anything else (another drive, the drive root, a path that
  96. // is not shared). The file ownership records only know a user's own home.
  97. func sharedDriveOwnerOf(userinfo *user.User, fsh *fs.FileSystemHandler, vpath string) string {
  98. if !share.IsSharedDrive(fsh) {
  99. return ""
  100. }
  101. item, err := fsh.FileSystemAbstraction.(*sharefs.ShareFileSystem).ItemOf(vpath, userinfo.Username)
  102. if err != nil || item == nil {
  103. return ""
  104. }
  105. return item.Owner
  106. }
  107. // sharedDriveCopyDenied returns why a copy of one of the given files cannot be
  108. // taken, or nil: a file shared with the user whose owner does not allow
  109. // downloading it, or the drive root itself.
  110. func sharedDriveCopyDenied(userinfo *user.User, vpaths []string) error {
  111. for _, vpath := range vpaths {
  112. fsh, _, err := GetFSHandlerSubpathFromVpath(vpath)
  113. if err != nil || !share.IsSharedDrive(fsh) {
  114. continue
  115. }
  116. drive := fsh.FileSystemAbstraction.(*sharefs.ShareFileSystem)
  117. item, err := drive.ItemOf(vpath, userinfo.Username)
  118. if err != nil {
  119. return errors.New("Source file not exists")
  120. }
  121. if item == nil {
  122. return errors.New("Shared with me cannot be copied as a whole")
  123. }
  124. if !item.CanDownload {
  125. return errors.New("The owner of " + item.Name + " does not allow downloading it")
  126. }
  127. }
  128. return nil
  129. }