fshAdapter.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package webdav
  2. import (
  3. "context"
  4. "errors"
  5. "log"
  6. "os"
  7. "path/filepath"
  8. "imuslab.com/arozos/mod/filesystem"
  9. "imuslab.com/arozos/mod/network/webdav"
  10. )
  11. type FshWebDAVAdapter struct {
  12. fsh *filesystem.FileSystemHandler
  13. username string
  14. }
  15. func NewFshWebDAVAdapter(fsh *filesystem.FileSystemHandler, username string) *FshWebDAVAdapter {
  16. return &FshWebDAVAdapter{
  17. fsh,
  18. username,
  19. }
  20. }
  21. func (a *FshWebDAVAdapter) requestPathToRealPath(name string) (string, error) {
  22. fullVpath := a.fsh.UUID + ":" + name
  23. realRequestPath, err := a.fsh.FileSystemAbstraction.VirtualPathToRealPath(fullVpath, a.username)
  24. if err != nil {
  25. return "", err
  26. }
  27. return filepath.ToSlash(realRequestPath), nil
  28. }
  29. func (a *FshWebDAVAdapter) Mkdir(ctx context.Context, name string, perm os.FileMode) error {
  30. realRequestPath, err := a.requestPathToRealPath(name)
  31. if err != nil {
  32. return err
  33. }
  34. return a.fsh.FileSystemAbstraction.Mkdir(realRequestPath, perm)
  35. }
  36. func (a *FshWebDAVAdapter) OpenFile(ctx context.Context, name string, flag int, perm os.FileMode) (webdav.File, error) {
  37. //The name come in as the relative path of the request vpath (e.g. user:/Video/test.mp4 will get requested as /Video/test.mp4)
  38. //Merge it into a proper vpath and perform abstraction path translation
  39. realRequestPath, err := a.requestPathToRealPath(name)
  40. if err != nil {
  41. log.Println(err)
  42. return nil, err
  43. }
  44. if a.fsh.RequireBuffer {
  45. //Buffer the remote content to local for access
  46. //WIP
  47. return nil, errors.New("work in progress")
  48. } else {
  49. return a.fsh.FileSystemAbstraction.OpenFile(realRequestPath, flag, perm)
  50. }
  51. }
  52. func (a *FshWebDAVAdapter) RemoveAll(ctx context.Context, name string) error {
  53. realRequestPath, err := a.requestPathToRealPath(name)
  54. if err != nil {
  55. return err
  56. }
  57. return a.fsh.FileSystemAbstraction.RemoveAll(realRequestPath)
  58. }
  59. func (a *FshWebDAVAdapter) Rename(ctx context.Context, oldName, newName string) error {
  60. realOldname, err := a.requestPathToRealPath(oldName)
  61. if err != nil {
  62. return err
  63. }
  64. realNewname, err := a.requestPathToRealPath(newName)
  65. if err != nil {
  66. return err
  67. }
  68. return a.fsh.FileSystemAbstraction.Rename(realOldname, realNewname)
  69. }
  70. func (a *FshWebDAVAdapter) Stat(ctx context.Context, name string) (os.FileInfo, error) {
  71. realRequestPath, err := a.requestPathToRealPath(name)
  72. if err != nil {
  73. return nil, err
  74. }
  75. return a.fsh.FileSystemAbstraction.Stat(realRequestPath)
  76. }