支持读取目录文件列表
This commit is contained in:
parent
1d8e8d546b
commit
223d5ab374
26
define/file.go
Normal file
26
define/file.go
Normal 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
66
file.go
@ -11,8 +11,11 @@ import (
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"git.zhangdeman.cn/zhangdeman/util/define"
|
||||
|
||||
"github.com/go-ini/ini"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
@ -179,3 +182,66 @@ func (f *file) GetFileMIMEType(filePath string) (string, error) {
|
||||
contentType := http.DetectContentType(buffer)
|
||||
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
20
file_test.go
Normal 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)
|
||||
}
|
Loading…
Reference in New Issue
Block a user