package bokofs import ( "context" "fmt" "os" "path/filepath" "strings" "time" "golang.org/x/net/webdav" "imuslab.com/bokofs/bokofsd/mod/bokofs/bokoworker" ) type RootRouter struct { parent *Server } // InitiateRootRouter create and prepare a virtual root file system for // this bokoFS instance func (s *Server) InitiateRootRouter() error { s.RootRouter = &RootRouter{ parent: s, } return nil } func (r *RootRouter) GetRootDir(name string) string { if name == "" { return "/" } name = filepath.ToSlash(filepath.Clean(name)) pathChunks := strings.Split(name, "/") reqRootPath := "/" + pathChunks[1] fmt.Println("Requesting Root Path: ", reqRootPath) reqRootPath = strings.TrimSuffix(reqRootPath, "/") return reqRootPath } func (r *RootRouter) GetWorkerByPath(name string) (*bokoworker.Worker, error) { reqRootPath := r.GetRootDir(name) targetWorker, ok := r.parent.LoadedWorkers.Load(reqRootPath) if !ok { return nil, os.ErrNotExist } return targetWorker.(*bokoworker.Worker), nil } func (r *RootRouter) Mkdir(ctx context.Context, name string, perm os.FileMode) error { // Implement the Mkdir method fmt.Println("Mkdir called to " + name) return nil } func (r *RootRouter) OpenFile(ctx context.Context, name string, flag int, perm os.FileMode) (webdav.File, error) { // Implement the OpenFile method fmt.Println("OpenFile called to " + name) if filepath.ToSlash(filepath.Base(name)) == "/" { //Request to the vObject base path thisVirtualObject := r.newVirtualObject(&vObjectProperties{ name: name, size: 0, mode: os.ModeDir, modTime: time.Now(), isDir: true, }) return thisVirtualObject, nil } targetWorker, err := r.GetWorkerByPath(name) if err != nil { return nil, err } return targetWorker.Filesystem.OpenFile(ctx, name, flag, perm) } func (r *RootRouter) RemoveAll(ctx context.Context, name string) error { // Implement the RemoveAll method fmt.Println("RemoveAll called to " + name) return nil } func (r *RootRouter) Rename(ctx context.Context, oldName, newName string) error { // Implement the Rename method fmt.Println("Rename called from " + oldName + " to " + newName) return nil } func (r *RootRouter) Stat(ctx context.Context, name string) (os.FileInfo, error) { // Implement the Stat method fmt.Println("Stat called to " + name) if filepath.ToSlash(filepath.Base(name)) == "/" { thisVirtualObject := r.newVirtualObject(&vObjectProperties{ name: name, size: 0, mode: os.ModeDir, modTime: time.Now(), isDir: true, }) thisVirtualObjectFileInfo := thisVirtualObject.GetFileInfo() return thisVirtualObjectFileInfo, nil } targetWorker, err := r.GetWorkerByPath(name) if err != nil { return nil, err } return targetWorker.Filesystem.Stat(ctx, name) } // Ensure RootRouter implements the FileSystem interface var _ webdav.FileSystem = (*RootRouter)(nil)