package routes import ( "fmt" "io/ioutil" "path" "regexp" "time" "git.trj.tw/golang/go-file-serve/module/config" "git.trj.tw/golang/go-file-serve/module/context" ) type responseFS struct { Name string `json:"name"` Path string `json:"path"` Size int64 `json:"size"` Mtime time.Time `json:"mtime"` } // FSList - func FSList(c *context.Context) { p := c.Param("path") conf := config.GetConf() basePath := conf.FilePath _ = basePath regex := regexp.MustCompile("^/") p = regex.ReplaceAllString(p, "") regex = regexp.MustCompile("\\.\\./") p = regex.ReplaceAllString(p, "/") fullPath := path.Join(basePath, p) info, err := ioutil.ReadDir(fullPath) if err != nil { c.ServerError() return } dirs := make([]responseFS, 0) files := make([]responseFS, 0) for _, v := range info { f := responseFS{ Name: v.Name(), Path: path.Join(p, v.Name()), Size: v.Size(), Mtime: v.ModTime(), } if v.IsDir() { f.Name += "/" dirs = append(dirs, f) } else { files = append(files, f) } } list := make([]responseFS, 0, len(dirs)+len(files)) list = append(list, dirs...) list = append(list, files...) dirs = nil files = nil c.HTML("list.tmpl", map[string]interface{}{ "title": fmt.Sprintf("Path: /%s", p), "path": fmt.Sprintf("/%s", p), "list": list, }) // c.Success(map[string]interface{}{ // "directory": dirs, // "file": files, // }) }