bokodir.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package bokofile
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "path/filepath"
  7. "strings"
  8. "golang.org/x/net/webdav"
  9. )
  10. /*
  11. bokodir.go
  12. The bokodir implements a disk based file system from the webdav.FileSystem interface
  13. A file in this implementation corrisponding to a real file on disk
  14. */
  15. type RouterDir struct {
  16. Prefix string //Path prefix to trim, usually is the root path of the worker
  17. DiskPath string //Disk path to create a file system from
  18. ReadOnly bool //Label this worker as read only
  19. /* Private Properties */
  20. dir webdav.Dir
  21. }
  22. // Create a routerdir from a directory
  23. func CreateRouterFromDir(dir string, prefix string, readonly bool) (*RouterDir, error) {
  24. if _, err := os.Stat(dir); os.IsNotExist(err) {
  25. return nil, err
  26. }
  27. //Initiate the dir
  28. fs := webdav.Dir(dir)
  29. return &RouterDir{
  30. Prefix: prefix,
  31. DiskPath: dir,
  32. ReadOnly: readonly,
  33. dir: fs,
  34. }, nil
  35. }
  36. func (r *RouterDir) CleanPrefix(name string) string {
  37. name = filepath.ToSlash(filepath.Clean(name)) + "/"
  38. fmt.Println("[Bokodir]", r.Prefix, name, strings.TrimPrefix(name, r.Prefix))
  39. return strings.TrimPrefix(name, r.Prefix)
  40. }
  41. func (r *RouterDir) Mkdir(ctx context.Context, name string, perm os.FileMode) error {
  42. // Implement the Mkdir method
  43. name = r.CleanPrefix(name)
  44. fmt.Println("[Bokodir]", "Mkdir called to "+name)
  45. return r.dir.Mkdir(ctx, name, perm)
  46. }
  47. func (r *RouterDir) OpenFile(ctx context.Context, name string, flag int, perm os.FileMode) (webdav.File, error) {
  48. // Implement the OpenFile method
  49. name = r.CleanPrefix(name)
  50. fmt.Println("[Bokodir]", "OpenFile called to "+name)
  51. return r.dir.OpenFile(ctx, name, flag, perm)
  52. }
  53. func (r *RouterDir) RemoveAll(ctx context.Context, name string) error {
  54. // Implement the RemoveAll method
  55. name = r.CleanPrefix(name)
  56. fmt.Println("[Bokodir]", "RemoveAll called to "+name)
  57. return r.dir.RemoveAll(ctx, name)
  58. }
  59. func (r *RouterDir) Rename(ctx context.Context, oldName, newName string) error {
  60. // Implement the Rename method
  61. oldName = r.CleanPrefix(oldName)
  62. newName = r.CleanPrefix(newName)
  63. fmt.Println("[Bokodir]", "Rename called from "+oldName+" to "+newName)
  64. return r.dir.Rename(ctx, oldName, newName)
  65. }
  66. func (r *RouterDir) Stat(ctx context.Context, name string) (os.FileInfo, error) {
  67. // Implement the Stat method
  68. name = r.CleanPrefix(name)
  69. fmt.Println("[Bokodir]", "Stat called to "+name)
  70. return r.dir.Stat(ctx, name)
  71. }
  72. // Ensure RouterDir implements the FileSystem interface
  73. var _ webdav.FileSystem = (*RouterDir)(nil)