支持读取目录文件列表

This commit is contained in:
白茶清欢 2023-07-31 15:39:02 +08:00
parent 1d8e8d546b
commit 223d5ab374
3 changed files with 112 additions and 0 deletions

26
define/file.go Normal file
View File

@ -0,0 +1,26 @@
// Package define ...
//
// Description : define ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2023-07-31 14:52
package define
import "io/fs"
// FileInfo 文件信息
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:53 2023/7/31
type FileInfo struct {
IsDir bool `json:"is_dir"` // 是否目录
Name string `json:"name"` // 文件名
AbsolutePath string `json:"absolute_path"` // 绝对路径
Format string `json:"format"` // 文件格式
Size int64 `json:"size"` // 文件大小
ModifyTime int64 `json:"modify_time"` // 修改时间, 秒级时间戳
Mode fs.FileMode `json:"mode"` // 文件权限
FileList []*FileInfo `json:"file_list"` // 递归读取, 且当前文件为目录
}

66
file.go
View File

@ -11,8 +11,11 @@ import (
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"os" "os"
"path/filepath"
"strings" "strings"
"git.zhangdeman.cn/zhangdeman/util/define"
"github.com/go-ini/ini" "github.com/go-ini/ini"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -179,3 +182,66 @@ func (f *file) GetFileMIMEType(filePath string) (string, error) {
contentType := http.DetectContentType(buffer) contentType := http.DetectContentType(buffer)
return contentType, nil return contentType, nil
} }
// ReadDirFileList 读取目录下的文件列表, 支持递归
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:42 2023/7/31
func (f *file) ReadDirFileList(dirPath string, ignoreHiddenFile bool, isRecurve bool) ([]*define.FileInfo, error) {
var (
err error
fileList []os.DirEntry
itemFileInfo os.FileInfo
)
result := make([]*define.FileInfo, 0)
if fileList, err = os.ReadDir(dirPath); nil != err {
return result, err
}
for _, itemFile := range fileList {
if ignoreHiddenFile && strings.HasPrefix(itemFile.Name(), ".") {
// 或略隐藏文件、文件夹
continue
}
if itemFileInfo, err = itemFile.Info(); nil != err {
return result, err
}
fileInfo := &define.FileInfo{
IsDir: itemFile.IsDir(),
Name: itemFile.Name(),
AbsolutePath: filepath.Join(dirPath, itemFile.Name()),
Format: "",
Size: itemFileInfo.Size(),
ModifyTime: itemFileInfo.ModTime().Unix(),
Mode: itemFileInfo.Mode(),
FileList: make([]*define.FileInfo, 0),
}
if !fileInfo.IsDir {
fileArr := strings.Split(fileInfo.Name, ".")
if len(fileArr) >= 2 {
fileInfo.Format = fileArr[len(fileArr)-1]
} else {
fileInfo.Format = "unknown"
}
} else {
fileInfo.Format = "dir"
}
if fileInfo.IsDir && isRecurve {
if fileInfo.FileList, err = f.ReadDirFileList(fileInfo.AbsolutePath, ignoreHiddenFile, isRecurve); nil != err {
return result, err
}
}
result = append(result, fileInfo)
}
return result, nil
}
// ReadFileListRecurve 递归读取文件内容
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:26 2023/7/31
func (f *file) ReadFileListRecurve(rootDir string) ([]*define.FileInfo, error) {
return nil, nil
}

20
file_test.go Normal file
View File

@ -0,0 +1,20 @@
// Package util ...
//
// Description : util ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2023-07-31 15:15
package util
import (
"testing"
)
func Test_file_ReadDirFileList(t *testing.T) {
res, err := File.ReadDirFileList("./", false, true)
if nil != err {
panic(err.Error())
}
JSON.ConsoleOutput(res)
}