feat: 添加对本地文件的操作接口
This commit is contained in:
@@ -16,7 +16,7 @@ type FileListRow struct {
|
||||
LinkCount int64 `json:"linkCount"` // 硬链接数目
|
||||
Owner string `json:"owner"` // 所属用户
|
||||
Group string `json:"group"` // 所属组
|
||||
Size string `json:"size"` // 文件的大小
|
||||
Size int64 `json:"size"` // 文件的大小
|
||||
ModifiedTime int64 `json:"modifiedTime"` // 最后修改时间,单位为秒
|
||||
FileName string `json:"fileName"` // 文件的名称
|
||||
}
|
||||
@@ -34,10 +34,10 @@ func FileList(sshClient *ConnSSH, path, search string) ([]FileListRow, error) {
|
||||
if search != "" {
|
||||
searchStr = search + searchStr
|
||||
}
|
||||
// cd /var/log && find. -maxdepth 1 -name'mme*' -exec ls -lthd --time-style=+%s {} +
|
||||
cmdStr := fmt.Sprintf("cd %s && find . -maxdepth 1 -name '%s' -exec ls -lthd --time-style=+%%s {} +", path, searchStr)
|
||||
// cd /var/log && ls -lthd --time-style=+%s mme*
|
||||
// cmdStr := fmt.Sprintf("cd %s && ls -lthd --time-style=+%%s %s", path, searchStr)
|
||||
// cd /var/log && find. -maxdepth 1 -name'mme*' -exec ls -ltd --time-style=+%s {} +
|
||||
cmdStr := fmt.Sprintf("cd %s && find . -maxdepth 1 -name '%s' -exec ls -ltd --time-style=+%%s {} +", path, searchStr)
|
||||
// cd /var/log && ls -ltd --time-style=+%s mme*
|
||||
// cmdStr := fmt.Sprintf("cd %s && ls -ltd --time-style=+%%s %s", path, searchStr)
|
||||
|
||||
// 是否远程客户端读取
|
||||
if sshClient == nil {
|
||||
@@ -94,7 +94,7 @@ func FileList(sshClient *ConnSSH, path, search string) ([]FileListRow, error) {
|
||||
LinkCount: parse.Number(fields[1]),
|
||||
Owner: fields[2],
|
||||
Group: fields[3],
|
||||
Size: fields[4],
|
||||
Size: parse.Number(fields[4]),
|
||||
ModifiedTime: parse.Number(fields[5]),
|
||||
FileName: fileName,
|
||||
})
|
||||
|
||||
78
src/framework/utils/file/files.go
Normal file
78
src/framework/utils/file/files.go
Normal file
@@ -0,0 +1,78 @@
|
||||
package file
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// FileListRow 文件列表行数据
|
||||
type FileListRow struct {
|
||||
FileType string `json:"fileType"` // 文件类型 dir, file, symlink
|
||||
FileMode string `json:"fileMode"` // 文件的权限
|
||||
LinkCount int64 `json:"linkCount"` // 硬链接数目
|
||||
Owner string `json:"owner"` // 所属用户
|
||||
Group string `json:"group"` // 所属组
|
||||
Size int64 `json:"size"` // 文件的大小
|
||||
ModifiedTime int64 `json:"modifiedTime"` // 最后修改时间,单位为秒
|
||||
FileName string `json:"fileName"` // 文件的名称
|
||||
}
|
||||
|
||||
// 文件列表
|
||||
// search 文件名后模糊*
|
||||
//
|
||||
// return 行记录,异常
|
||||
func FileList(path, search string) ([]FileListRow, error) {
|
||||
var rows []FileListRow
|
||||
|
||||
// 构建搜索模式
|
||||
pattern := "*"
|
||||
if search != "" {
|
||||
pattern = search + pattern
|
||||
}
|
||||
|
||||
// 读取目录内容
|
||||
entries, err := os.ReadDir(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 遍历目录项
|
||||
for _, entry := range entries {
|
||||
// 匹配文件名
|
||||
matched, err := filepath.Match(pattern, entry.Name())
|
||||
if err != nil || !matched {
|
||||
continue
|
||||
}
|
||||
|
||||
// 获取文件详细信息
|
||||
info, err := entry.Info()
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
// 确定文件类型
|
||||
fileType := "file"
|
||||
if info.IsDir() {
|
||||
fileType = "dir"
|
||||
} else if info.Mode()&os.ModeSymlink != 0 {
|
||||
fileType = "symlink"
|
||||
}
|
||||
|
||||
// 获取系统特定的文件信息
|
||||
linkCount, owner, group := getFileInfo(info)
|
||||
|
||||
// 组装文件信息
|
||||
rows = append(rows, FileListRow{
|
||||
FileMode: info.Mode().String(),
|
||||
FileType: fileType,
|
||||
LinkCount: linkCount,
|
||||
Owner: owner,
|
||||
Group: group,
|
||||
Size: info.Size(),
|
||||
ModifiedTime: info.ModTime().UnixMilli(),
|
||||
FileName: entry.Name(),
|
||||
})
|
||||
}
|
||||
|
||||
return rows, nil
|
||||
}
|
||||
36
src/framework/utils/file/files_unix.go
Normal file
36
src/framework/utils/file/files_unix.go
Normal file
@@ -0,0 +1,36 @@
|
||||
//go:build !windows
|
||||
// +build !windows
|
||||
|
||||
package file
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/user"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
// getFileInfo 获取系统特定的文件信息s
|
||||
func getFileInfo(info os.FileInfo) (linkCount int64, owner, group string) {
|
||||
// Unix-like 系统 (Linux, macOS)
|
||||
if stat, ok := info.Sys().(*syscall.Stat_t); ok {
|
||||
// 获取用户名
|
||||
ownerName := "root"
|
||||
if stat.Uid != 0 {
|
||||
if u, err := user.LookupId(fmt.Sprint(stat.Uid)); err == nil {
|
||||
ownerName = u.Username
|
||||
}
|
||||
}
|
||||
|
||||
// 获取组名
|
||||
groupName := "root"
|
||||
if stat.Gid != 0 {
|
||||
if g, err := user.LookupGroupId(fmt.Sprint(stat.Gid)); err == nil {
|
||||
groupName = g.Name
|
||||
}
|
||||
}
|
||||
|
||||
return int64(stat.Nlink), ownerName, groupName
|
||||
}
|
||||
return 1, "", ""
|
||||
}
|
||||
13
src/framework/utils/file/files_windows.go
Normal file
13
src/framework/utils/file/files_windows.go
Normal file
@@ -0,0 +1,13 @@
|
||||
//go:build windows
|
||||
// +build windows
|
||||
|
||||
package file
|
||||
|
||||
import (
|
||||
"os"
|
||||
)
|
||||
|
||||
// getFileInfo 获取系统特定的文件信息
|
||||
func getFileInfo(_ os.FileInfo) (linkCount int64, owner, group string) {
|
||||
return 1, "Administrator", "Administrators"
|
||||
}
|
||||
Reference in New Issue
Block a user