FindNames accepts a "http.FileSystem" and a root name and returns the list containing its file names.
(fileSystem http.FileSystem, name string)
| 135 | // FindNames accepts a "http.FileSystem" and a root name and returns |
| 136 | // the list containing its file names. |
| 137 | func FindNames(fileSystem http.FileSystem, name string) ([]string, error) { |
| 138 | if strings.Contains(name, "..") { |
| 139 | return nil, fmt.Errorf("invalid root name") |
| 140 | } |
| 141 | |
| 142 | f, err := fileSystem.Open(name) // it's the root dir. |
| 143 | if err != nil { |
| 144 | return nil, err |
| 145 | } |
| 146 | defer f.Close() |
| 147 | |
| 148 | fi, err := f.Stat() |
| 149 | if err != nil { |
| 150 | return nil, err |
| 151 | } |
| 152 | |
| 153 | if !fi.IsDir() { |
| 154 | return []string{name}, nil |
| 155 | } |
| 156 | |
| 157 | fileinfos, err := f.Readdir(-1) |
| 158 | if err != nil { |
| 159 | return nil, err |
| 160 | } |
| 161 | |
| 162 | files := make([]string, 0) |
| 163 | |
| 164 | for _, info := range fileinfos { |
| 165 | // Note: |
| 166 | // go-bindata has absolute names with os.Separator, |
| 167 | // http.Dir the basename. |
| 168 | baseFilename := toBaseName(info.Name()) |
| 169 | fullname := path.Join(name, baseFilename) |
| 170 | if fullname == name { // prevent looping through itself. |
| 171 | continue |
| 172 | } |
| 173 | rfiles, err := FindNames(fileSystem, fullname) |
| 174 | if err != nil { |
| 175 | return nil, err |
| 176 | } |
| 177 | |
| 178 | files = append(files, rfiles...) |
| 179 | } |
| 180 | |
| 181 | return files, nil |
| 182 | } |
| 183 | |
| 184 | // Instead of path.Base(filepath.ToSlash(s)) |
| 185 | // let's do something like that, it is faster |