fshAdapter.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. buffDir string //The folder to put buffer files
  15. }
  16. func NewFshWebDAVAdapter(fsh *filesystem.FileSystemHandler, username string, buffDir string) *FshWebDAVAdapter {
  17. return &FshWebDAVAdapter{
  18. fsh,
  19. username,
  20. buffDir,
  21. }
  22. }
  23. func (a *FshWebDAVAdapter) requestPathToRealPath(name string) (string, error) {
  24. fullVpath := a.fsh.UUID + ":" + name
  25. realRequestPath, err := a.fsh.FileSystemAbstraction.VirtualPathToRealPath(fullVpath, a.username)
  26. if err != nil {
  27. return "", err
  28. }
  29. return filepath.ToSlash(realRequestPath), nil
  30. }
  31. func (a *FshWebDAVAdapter) Mkdir(ctx context.Context, name string, perm os.FileMode) error {
  32. realRequestPath, err := a.requestPathToRealPath(name)
  33. if err != nil {
  34. return err
  35. }
  36. return a.fsh.FileSystemAbstraction.Mkdir(realRequestPath, perm)
  37. }
  38. func (a *FshWebDAVAdapter) OpenFile(ctx context.Context, name string, flag int, perm os.FileMode) (webdav.File, error) {
  39. //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)
  40. //Merge it into a proper vpath and perform abstraction path translation
  41. realRequestPath, err := a.requestPathToRealPath(name)
  42. if err != nil {
  43. log.Println(err)
  44. return nil, err
  45. }
  46. if a.fsh.RequireBuffer {
  47. //Buffer the remote content to local for access
  48. //WIP
  49. return nil, errors.New("work in progress")
  50. } else {
  51. return a.fsh.FileSystemAbstraction.OpenFile(realRequestPath, flag, perm)
  52. }
  53. }
  54. func (a *FshWebDAVAdapter) RemoveAll(ctx context.Context, name string) error {
  55. realRequestPath, err := a.requestPathToRealPath(name)
  56. if err != nil {
  57. return err
  58. }
  59. return a.fsh.FileSystemAbstraction.RemoveAll(realRequestPath)
  60. }
  61. func (a *FshWebDAVAdapter) Rename(ctx context.Context, oldName, newName string) error {
  62. realOldname, err := a.requestPathToRealPath(oldName)
  63. if err != nil {
  64. return err
  65. }
  66. realNewname, err := a.requestPathToRealPath(newName)
  67. if err != nil {
  68. return err
  69. }
  70. return a.fsh.FileSystemAbstraction.Rename(realOldname, realNewname)
  71. }
  72. func (a *FshWebDAVAdapter) Stat(ctx context.Context, name string) (os.FileInfo, error) {
  73. realRequestPath, err := a.requestPathToRealPath(name)
  74. if err != nil {
  75. return nil, err
  76. }
  77. return a.fsh.FileSystemAbstraction.Stat(realRequestPath)
  78. }