OpenFile implements interface webdav.Filesystem.
(ctx context.Context, name string, flag int, perm os.FileMode)
| 14 | |
| 15 | // OpenFile implements interface webdav.Filesystem. |
| 16 | func (dfs *FS) OpenFile(ctx context.Context, name string, flag int, perm os.FileMode) (webdav.File, error) { |
| 17 | _, isStaticRoot := dfs.trimStaticRoot(name) |
| 18 | if !isStaticRoot && !shared.IsRoot(name) { |
| 19 | // Show a folder with no children to represent the requested child. In |
| 20 | // practice, the children of this folder are never read, we just need |
| 21 | // to give webdav a file here which it uses to call file.Stat(). So, |
| 22 | // even though the Child may in fact have its own children, it doesn't |
| 23 | // matter here. |
| 24 | return &shared.DirFile{ |
| 25 | Info: shared.ReadOnlyDirInfo(name, dfs.now()), |
| 26 | LoadChildren: func() ([]fs.FileInfo, error) { |
| 27 | return nil, nil |
| 28 | }, |
| 29 | }, nil |
| 30 | } |
| 31 | |
| 32 | di, err := dfs.Stat(ctx, name) |
| 33 | if err != nil { |
| 34 | return nil, err |
| 35 | } |
| 36 | |
| 37 | if dfs.StaticRoot != "" && !isStaticRoot { |
| 38 | // Show a folder with a single subfolder that is the static root. |
| 39 | return &shared.DirFile{ |
| 40 | Info: di, |
| 41 | LoadChildren: func() ([]fs.FileInfo, error) { |
| 42 | return []fs.FileInfo{ |
| 43 | shared.ReadOnlyDirInfo(dfs.StaticRoot, dfs.now()), |
| 44 | }, nil |
| 45 | }, |
| 46 | }, nil |
| 47 | } |
| 48 | |
| 49 | // Show a folder with one subfolder for each Child of this FS. |
| 50 | return &shared.DirFile{ |
| 51 | Info: di, |
| 52 | LoadChildren: func() ([]fs.FileInfo, error) { |
| 53 | childInfos := make([]fs.FileInfo, 0, len(dfs.Children)) |
| 54 | for _, c := range dfs.Children { |
| 55 | if c.isAvailable() { |
| 56 | childInfo := shared.ReadOnlyDirInfo(c.Name, dfs.now()) |
| 57 | childInfos = append(childInfos, childInfo) |
| 58 | } |
| 59 | } |
| 60 | return childInfos, nil |
| 61 | }, |
| 62 | }, nil |
| 63 | } |