update: file size humanize show

This commit is contained in:
2024-09-06 17:05:51 +08:00
parent 9760e95ee1
commit 292bbeab89
3 changed files with 13 additions and 4 deletions

View File

@@ -8,7 +8,10 @@ import (
"os"
"os/user"
"path/filepath"
"strings"
"syscall"
"github.com/dustin/go-humanize"
)
type FileInfo struct {
@@ -17,7 +20,7 @@ type FileInfo struct {
LinkCount int64 `json:"linkCount"` // 硬链接数目
Owner string `json:"owner"` // 所属用户
Group string `json:"group"` // 所属组
Size int64 `json:"size"` // 文件的大小
Size string `json:"size"` // 文件的大小
ModifiedTime int64 `json:"modifiedTime"` // 最后修改时间,单位为秒
FileName string `json:"fileName"` // 文件的名称
}
@@ -55,13 +58,14 @@ func GetFileInfo(dir, suffix string) ([]FileInfo, error) {
if err != nil {
return err
}
humanReadableSize := humanize.Bytes(uint64(info.Size()))
fileInfo := FileInfo{
FileType: fileType,
FileMode: info.Mode().String(),
LinkCount: int64(info.Sys().(*syscall.Stat_t).Nlink),
Owner: userInfo.Username,
Group: groupInfo.Name,
Size: info.Size(),
Size: strings.ToUpper(humanReadableSize),
ModifiedTime: info.ModTime().Unix(),
FileName: info.Name(),
}

View File

@@ -6,6 +6,9 @@ package file
import (
"os"
"path/filepath"
"strings"
"github.com/dustin/go-humanize"
)
type FileInfo struct {
@@ -14,7 +17,7 @@ type FileInfo struct {
LinkCount int64 `json:"linkCount"` // 硬链接数目
Owner string `json:"owner"` // 所属用户
Group string `json:"group"` // 所属组
Size int64 `json:"size"` // 文件的大小
Size string `json:"size"` // 文件的大小
ModifiedTime int64 `json:"modifiedTime"` // 最后修改时间,单位为秒
FileName string `json:"fileName"` // 文件的名称
}
@@ -40,13 +43,14 @@ func GetFileInfo(dir, suffix string) ([]FileInfo, error) {
// check if match suffix
if (suffix != "" && filepath.Ext(path) == suffix) || suffix == "" {
humanReadableSize := humanize.Bytes(uint64(info.Size()))
fileInfo := FileInfo{
FileType: fileType,
FileMode: info.Mode().String(),
LinkCount: 0,
Owner: "-",
Group: "-",
Size: info.Size(),
Size: strings.ToUpper(humanReadableSize),
ModifiedTime: info.ModTime().Unix(),
FileName: info.Name(),
}