tpl: Make readDir use the WorkingDir fs
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Thu, 31 Mar 2016 21:06:51 +0000 (23:06 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Thu, 31 Mar 2016 21:06:51 +0000 (23:06 +0200)
Fixes #2010

tpl/template_funcs.go
tpl/template_resources.go

index 8abd70b8cc4c962cf432be42dd5793087282ca83..e9cc26869f5808d3d3ad3f1fdf8ab6ad9133dfab 100644 (file)
@@ -1501,6 +1501,21 @@ func readFileFromWorkingDir(i interface{}) (string, error) {
        return readFile(hugofs.WorkingDir(), cast.ToString(i))
 }
 
+// readDirFromWorkingDir listst the directory content relative to the
+// configured WorkingDir.
+func readDirFromWorkingDir(i interface{}) ([]os.FileInfo, error) {
+
+       path := cast.ToString(i)
+
+       list, err := afero.ReadDir(hugofs.WorkingDir(), path)
+
+       if err != nil {
+               return nil, fmt.Errorf("Failed to read Directory %s with error message %s", path, err)
+       }
+
+       return list, nil
+}
+
 // safeHTMLAttr returns a given string as html/template HTMLAttr content.
 //
 // safeHTMLAttr is currently disabled, pending further discussion
@@ -1722,7 +1737,7 @@ func init() {
                "partial":      partial,
                "plainify":     plainify,
                "pluralize":    pluralize,
-               "readDir":      readDir,
+               "readDir":      readDirFromWorkingDir,
                "readFile":     readFileFromWorkingDir,
                "ref":          ref,
                "relURL":       func(a string) template.HTML { return template.HTML(helpers.RelURL(a)) },
index 0d0ea833282237370a59645bd1cf81db7932a2aa..76a83a87f31ccf688f3ca3a22b42b01f050a2bb2 100644 (file)
@@ -21,7 +21,6 @@ import (
        "io/ioutil"
        "net/http"
        "net/url"
-       "os"
        "path/filepath"
        "strings"
        "sync"
@@ -259,25 +258,3 @@ func getCSV(sep string, urlParts ...string) [][]string {
        }
        return d
 }
-
-func readDir(path string) []os.FileInfo {
-       wd := ""
-       p := ""
-       if viper.GetString("WorkingDir") != "" {
-               wd = viper.GetString("WorkingDir")
-       }
-       if strings.Contains(path, "..") {
-               jww.ERROR.Printf("Path %s contains parent directory marker", path)
-               return nil
-       }
-
-       p = filepath.Clean(path)
-       p = filepath.Join(wd, p)
-
-       list, err := ioutil.ReadDir(p)
-       if err != nil {
-               jww.ERROR.Printf("Failed to read Directory %s with error message %s", path, err)
-               return nil
-       }
-       return list
-}