update: file size humanize show
This commit is contained in:
1
go.mod
1
go.mod
@@ -4,6 +4,7 @@ go 1.21
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/dlclark/regexp2 v1.11.4
|
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/gin-gonic/gin v1.10.0
|
||||||
github.com/go-resty/resty/v2 v2.14.0
|
github.com/go-resty/resty/v2 v2.14.0
|
||||||
github.com/go-sql-driver/mysql v1.8.1
|
github.com/go-sql-driver/mysql v1.8.1
|
||||||
|
|||||||
@@ -8,7 +8,10 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"os/user"
|
"os/user"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
|
"github.com/dustin/go-humanize"
|
||||||
)
|
)
|
||||||
|
|
||||||
type FileInfo struct {
|
type FileInfo struct {
|
||||||
@@ -17,7 +20,7 @@ type FileInfo struct {
|
|||||||
LinkCount int64 `json:"linkCount"` // 硬链接数目
|
LinkCount int64 `json:"linkCount"` // 硬链接数目
|
||||||
Owner string `json:"owner"` // 所属用户
|
Owner string `json:"owner"` // 所属用户
|
||||||
Group string `json:"group"` // 所属组
|
Group string `json:"group"` // 所属组
|
||||||
Size int64 `json:"size"` // 文件的大小
|
Size string `json:"size"` // 文件的大小
|
||||||
ModifiedTime int64 `json:"modifiedTime"` // 最后修改时间,单位为秒
|
ModifiedTime int64 `json:"modifiedTime"` // 最后修改时间,单位为秒
|
||||||
FileName string `json:"fileName"` // 文件的名称
|
FileName string `json:"fileName"` // 文件的名称
|
||||||
}
|
}
|
||||||
@@ -55,13 +58,14 @@ func GetFileInfo(dir, suffix string) ([]FileInfo, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
humanReadableSize := humanize.Bytes(uint64(info.Size()))
|
||||||
fileInfo := FileInfo{
|
fileInfo := FileInfo{
|
||||||
FileType: fileType,
|
FileType: fileType,
|
||||||
FileMode: info.Mode().String(),
|
FileMode: info.Mode().String(),
|
||||||
LinkCount: int64(info.Sys().(*syscall.Stat_t).Nlink),
|
LinkCount: int64(info.Sys().(*syscall.Stat_t).Nlink),
|
||||||
Owner: userInfo.Username,
|
Owner: userInfo.Username,
|
||||||
Group: groupInfo.Name,
|
Group: groupInfo.Name,
|
||||||
Size: info.Size(),
|
Size: strings.ToUpper(humanReadableSize),
|
||||||
ModifiedTime: info.ModTime().Unix(),
|
ModifiedTime: info.ModTime().Unix(),
|
||||||
FileName: info.Name(),
|
FileName: info.Name(),
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,9 @@ package file
|
|||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/dustin/go-humanize"
|
||||||
)
|
)
|
||||||
|
|
||||||
type FileInfo struct {
|
type FileInfo struct {
|
||||||
@@ -14,7 +17,7 @@ type FileInfo struct {
|
|||||||
LinkCount int64 `json:"linkCount"` // 硬链接数目
|
LinkCount int64 `json:"linkCount"` // 硬链接数目
|
||||||
Owner string `json:"owner"` // 所属用户
|
Owner string `json:"owner"` // 所属用户
|
||||||
Group string `json:"group"` // 所属组
|
Group string `json:"group"` // 所属组
|
||||||
Size int64 `json:"size"` // 文件的大小
|
Size string `json:"size"` // 文件的大小
|
||||||
ModifiedTime int64 `json:"modifiedTime"` // 最后修改时间,单位为秒
|
ModifiedTime int64 `json:"modifiedTime"` // 最后修改时间,单位为秒
|
||||||
FileName string `json:"fileName"` // 文件的名称
|
FileName string `json:"fileName"` // 文件的名称
|
||||||
}
|
}
|
||||||
@@ -40,13 +43,14 @@ func GetFileInfo(dir, suffix string) ([]FileInfo, error) {
|
|||||||
|
|
||||||
// check if match suffix
|
// check if match suffix
|
||||||
if (suffix != "" && filepath.Ext(path) == suffix) || suffix == "" {
|
if (suffix != "" && filepath.Ext(path) == suffix) || suffix == "" {
|
||||||
|
humanReadableSize := humanize.Bytes(uint64(info.Size()))
|
||||||
fileInfo := FileInfo{
|
fileInfo := FileInfo{
|
||||||
FileType: fileType,
|
FileType: fileType,
|
||||||
FileMode: info.Mode().String(),
|
FileMode: info.Mode().String(),
|
||||||
LinkCount: 0,
|
LinkCount: 0,
|
||||||
Owner: "-",
|
Owner: "-",
|
||||||
Group: "-",
|
Group: "-",
|
||||||
Size: info.Size(),
|
Size: strings.ToUpper(humanReadableSize),
|
||||||
ModifiedTime: info.ModTime().Unix(),
|
ModifiedTime: info.ModTime().Unix(),
|
||||||
FileName: info.Name(),
|
FileName: info.Name(),
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user