diff options
Diffstat (limited to 'contrib/fs/dir.go')
-rw-r--r-- | contrib/fs/dir.go | 89 |
1 files changed, 6 insertions, 83 deletions
diff --git a/contrib/fs/dir.go b/contrib/fs/dir.go index b00fe5c..e43a375 100644 --- a/contrib/fs/dir.go +++ b/contrib/fs/dir.go @@ -3,7 +3,7 @@ package fs import ( "bytes" "io" - "io/fs" + "os" "sort" "strings" "text/template" @@ -11,83 +11,6 @@ import ( sr "tildegit.org/tjp/sliderule" ) -// ResolveDirectory opens the directory corresponding to a request path. -// -// The string is the full path to the directory. If the returned ReadDirFile -// is not nil, it will be open and must be closed by the caller. -func ResolveDirectory( - request *sr.Request, - fileSystem fs.FS, -) (string, fs.ReadDirFile, error) { - path := strings.Trim(request.Path, "/") - if path == "" { - path = "." - } - - if isPrivate(path) { - return "", nil, nil - } - - file, err := fileSystem.Open(path) - if isNotFound(err) { - return "", nil, nil - } - if err != nil { - return "", nil, err - } - - isDir, err := fileIsDir(file) - if err != nil { - _ = file.Close() - return "", nil, err - } - - if !isDir { - _ = file.Close() - return "", nil, nil - } - - dirFile, ok := file.(fs.ReadDirFile) - if !ok { - _ = file.Close() - return "", nil, nil - } - - return path, dirFile, nil -} - -// ResolveDirectoryDefault finds any of the provided filenames within a directory. -// -// If it does not find any of the filenames it returns "", nil, nil. -func ResolveDirectoryDefault( - fileSystem fs.FS, - dirPath string, - dir fs.ReadDirFile, - filenames []string, -) (string, fs.File, error) { - entries, err := dir.ReadDir(0) - if err != nil { - return "", nil, err - } - sort.Slice(entries, func(a, b int) bool { - return entries[a].Name() < entries[b].Name() - }) - - for _, filename := range filenames { - idx := sort.Search(len(entries), func(i int) bool { - return entries[i].Name() >= filename - }) - - if idx < len(entries) && entries[idx].Name() == filename { - path := strings.TrimLeft(dirPath+"/"+filename, "./") - file, err := fileSystem.Open(path) - return path, file, err - } - } - - return "", nil, nil -} - // RenderDirectoryListing provides an io.Reader with the output of a directory listing template. // // The template is provided the following namespace: @@ -104,13 +27,13 @@ func ResolveDirectoryDefault( // - .Info is a method returning (fs.FileInfo, error) func RenderDirectoryListing( path string, - dir fs.ReadDirFile, + requestpath string, template *template.Template, server sr.Server, ) (io.Reader, error) { buf := &bytes.Buffer{} - environ, err := dirlistNamespace(path, dir, server) + environ, err := dirlistNamespace(path, requestpath, server) if err != nil { return nil, err } @@ -122,8 +45,8 @@ func RenderDirectoryListing( return buf, nil } -func dirlistNamespace(path string, dirFile fs.ReadDirFile, server sr.Server) (map[string]any, error) { - entries, err := dirFile.ReadDir(0) +func dirlistNamespace(path, requestpath string, server sr.Server) (map[string]any, error) { + entries, err := os.ReadDir(path) if err != nil { return nil, err } @@ -140,7 +63,7 @@ func dirlistNamespace(path string, dirFile fs.ReadDirFile, server sr.Server) (ma }) var dirname string - if path == "." { + if requestpath == "" { dirname = "(root)" } else { dirname = path[strings.LastIndex(path, "/")+1:] |