diff --git a/go.mod b/go.mod index 979c4540..33d53136 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.21 require ( github.com/dlclark/regexp2 v1.11.4 + github.com/dustin/go-humanize v1.0.0 github.com/gin-gonic/gin v1.10.0 github.com/go-resty/resty/v2 v2.14.0 github.com/go-sql-driver/mysql v1.8.1 diff --git a/lib/file/file_linux.go b/lib/file/file_linux.go index b38053b2..4d934618 100644 --- a/lib/file/file_linux.go +++ b/lib/file/file_linux.go @@ -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(), } diff --git a/lib/file/file_windows.go b/lib/file/file_windows.go index fbcf9b3c..81ba749a 100644 --- a/lib/file/file_windows.go +++ b/lib/file/file_windows.go @@ -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(), }