router.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package bokofs
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "path/filepath"
  7. "strings"
  8. "time"
  9. "golang.org/x/net/webdav"
  10. "imuslab.com/bokofs/bokofsd/mod/bokofs/bokoworker"
  11. )
  12. type RootRouter struct {
  13. parent *Server
  14. }
  15. // InitiateRootRouter create and prepare a virtual root file system for
  16. // this bokoFS instance
  17. func (s *Server) InitiateRootRouter() error {
  18. s.RootRouter = &RootRouter{
  19. parent: s,
  20. }
  21. return nil
  22. }
  23. func (r *RootRouter) GetRootDir(name string) string {
  24. if name == "" {
  25. return "/"
  26. }
  27. name = filepath.ToSlash(filepath.Clean(name))
  28. pathChunks := strings.Split(name, "/")
  29. reqRootPath := "/" + pathChunks[1]
  30. fmt.Println("Requesting Root Path: ", reqRootPath)
  31. reqRootPath = strings.TrimSuffix(reqRootPath, "/")
  32. return reqRootPath
  33. }
  34. func (r *RootRouter) GetWorkerByPath(name string) (*bokoworker.Worker, error) {
  35. reqRootPath := r.GetRootDir(name)
  36. targetWorker, ok := r.parent.LoadedWorkers.Load(reqRootPath)
  37. if !ok {
  38. return nil, os.ErrNotExist
  39. }
  40. return targetWorker.(*bokoworker.Worker), nil
  41. }
  42. func (r *RootRouter) Mkdir(ctx context.Context, name string, perm os.FileMode) error {
  43. // Implement the Mkdir method
  44. fmt.Println("Mkdir called to " + name)
  45. return nil
  46. }
  47. func (r *RootRouter) OpenFile(ctx context.Context, name string, flag int, perm os.FileMode) (webdav.File, error) {
  48. // Implement the OpenFile method
  49. fmt.Println("OpenFile called to " + name)
  50. if filepath.ToSlash(filepath.Base(name)) == "/" {
  51. //Request to the vObject base path
  52. thisVirtualObject := r.newVirtualObject(&vObjectProperties{
  53. name: name,
  54. size: 0,
  55. mode: os.ModeDir,
  56. modTime: time.Now(),
  57. isDir: true,
  58. })
  59. return thisVirtualObject, nil
  60. }
  61. targetWorker, err := r.GetWorkerByPath(name)
  62. if err != nil {
  63. return nil, err
  64. }
  65. return targetWorker.Filesystem.OpenFile(ctx, name, flag, perm)
  66. }
  67. func (r *RootRouter) RemoveAll(ctx context.Context, name string) error {
  68. // Implement the RemoveAll method
  69. fmt.Println("RemoveAll called to " + name)
  70. return nil
  71. }
  72. func (r *RootRouter) Rename(ctx context.Context, oldName, newName string) error {
  73. // Implement the Rename method
  74. fmt.Println("Rename called from " + oldName + " to " + newName)
  75. return nil
  76. }
  77. func (r *RootRouter) Stat(ctx context.Context, name string) (os.FileInfo, error) {
  78. // Implement the Stat method
  79. fmt.Println("Stat called to " + name)
  80. if filepath.ToSlash(filepath.Base(name)) == "/" {
  81. thisVirtualObject := r.newVirtualObject(&vObjectProperties{
  82. name: name,
  83. size: 0,
  84. mode: os.ModeDir,
  85. modTime: time.Now(),
  86. isDir: true,
  87. })
  88. thisVirtualObjectFileInfo := thisVirtualObject.GetFileInfo()
  89. return thisVirtualObjectFileInfo, nil
  90. }
  91. targetWorker, err := r.GetWorkerByPath(name)
  92. if err != nil {
  93. return nil, err
  94. }
  95. return targetWorker.Filesystem.Stat(ctx, name)
  96. }
  97. // Ensure RootRouter implements the FileSystem interface
  98. var _ webdav.FileSystem = (*RootRouter)(nil)