//获得目录下所有文件信息 func getFileInfos(path string) []os.FileInfo { if path == "/" { infos := []os.FileInfo{} l := len(directorys) for i := 0; i < l; i++ { f, err := os.Stat(directorys[i]) if err != nil { log.LogError(logprefix, "获取文件信息时发生错误", "路径:", directorys[i], "错误:", err) } if f != nil { infos = append(infos, f) } } return infos } infos, err := ioutil.ReadDir(path) if err != nil { log.LogError(logprefix, "获取目录下文件信息是发生错误", "路径:", path, "错误:", err) } return infos }
//初始化文件管理模块 func initFileManager() { s := config.Value(filedir) if d, k := s.([]interface{}); k { for _, v := range d { if ss, ok := v.(string); ok { files.AddDirectory(ss) } } } else { log.LogError(logprefix, "获取文件管理目录失败") } route.AddHandlers(files.Rmux) }
//添加目录给该模块管理 func AddDirectory(dir ...string) { l := len(dir) for i := 0; i < l; i++ { if !fpath.IsExist(dir[i]) { fpath.MakeDir(dir[i]) } p := filepath.Clean(dir[i]) dirname = append(dirname, filepath.Base(p)) directorys = append(directorys, p) } if len(dirname) != len(directorys) { log.LogError(logprefix, "添加文件管理目录时错误", "文件夹名长度:", len(dirname), "目录长度:", len(directorys), "不相等") } }
//从管理的目录中删除特定目录 func RemoveDirectory(dir ...string) { l := len(dir) if l > 1 { for i := 0; i < l; i++ { RemoveDirectory(dir[i]) } return } s := dir[0] l = len(directorys) for i := 0; i < l; i++ { if s == directorys[i] { directorys = append(directorys[:i], directorys[i+1:]...) dirname = append(dirname[:i], dirname[i+1:]...) return } } if len(dirname) != len(directorys) { log.LogError(logprefix, "删除文件管理目录时错误", "文件夹名长度:", len(dirname), "目录长度:", len(directorys), "不相等") } }