From c80842087d2bc0b96ca111900b84615d28d58869 Mon Sep 17 00:00:00 2001 From: simonzhangsz Date: Wed, 28 Aug 2024 14:35:44 +0800 Subject: [PATCH] add: file export for table --- features/lm/file_export/controller.go | 27 ++++++++++++++++++++++----- features/lm/file_export/model.go | 17 +++++++---------- lib/file/file.go | 27 +++++++++++++++++++++++++++ lib/file/file_linux.go | 21 --------------------- lib/file/file_windows.go | 21 --------------------- lib/services/response.go | 13 +++++++++++-- 6 files changed, 67 insertions(+), 59 deletions(-) create mode 100644 lib/file/file.go diff --git a/features/lm/file_export/controller.go b/features/lm/file_export/controller.go index 18648420..22374b5e 100644 --- a/features/lm/file_export/controller.go +++ b/features/lm/file_export/controller.go @@ -64,16 +64,33 @@ func (m *SysJob) GetFileExportTable(c *gin.Context) { } func (m *FileExport) GetFileList(c *gin.Context) { - dir := c.Query("path") - suffix := c.Query("suffix") + var querys FileExportQuery - files, err := file.GetFileInfo(dir, suffix) + if err := c.ShouldBindQuery(&querys); err != nil { + c.JSON(http.StatusBadRequest, services.ErrResp(err.Error())) + return + } + + files, err := file.GetFileInfo(querys.Path, querys.Suffix) if err != nil { log.Error(err) c.JSON(http.StatusInternalServerError, services.ErrResp(err.Error())) } - total := int64(len(files)) - c.JSON(http.StatusOK, services.TotalDataResp(files, total)) + + // split files list + lenNum := int64(len(files)) + start := (querys.PageNum - 1) * querys.PageSize + end := start + querys.PageSize + var splitList []file.FileInfo + if start >= lenNum { + splitList = []file.FileInfo{} + } else if end >= lenNum { + splitList = files[start:] + } else { + splitList = files[start:end] + } + total := len(files) + c.JSON(http.StatusOK, services.TotalDataResp(splitList, total)) } func (m *FileExport) Total(c *gin.Context) { diff --git a/features/lm/file_export/model.go b/features/lm/file_export/model.go index 83613c29..bb9eda9e 100644 --- a/features/lm/file_export/model.go +++ b/features/lm/file_export/model.go @@ -1,6 +1,7 @@ package file_export import ( + "be.ems/lib/file" "gorm.io/datatypes" ) @@ -19,16 +20,12 @@ func (m *SysJob) TableName() string { } type FileExport struct { - FileType string `json:"fileType"` // 文件类型 - 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"` // 文件的名称 + file.FileInfo } -func (m *FileExport) TableName() string { - return "file_export" +type FileExportQuery struct { + Path string `form:"path" binding:"required"` + Suffix string `form:"suffix"` + PageNum int64 `form:"pageNum" binding:"required"` + PageSize int64 `form:"pageSize" binding:"required"` } diff --git a/lib/file/file.go b/lib/file/file.go new file mode 100644 index 00000000..7bc2a57e --- /dev/null +++ b/lib/file/file.go @@ -0,0 +1,27 @@ +package file + +import ( + "os" + "path/filepath" +) + +func GetFileAndDirCount(dir string) (int, int, error) { + var fileCount, dirCount int + + err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + if path == dir { + return nil // 跳过当前目录 + } + if info.IsDir() { + dirCount++ + } else { + fileCount++ + } + return nil + }) + + return fileCount, dirCount, err +} diff --git a/lib/file/file_linux.go b/lib/file/file_linux.go index 6e5f4640..9e459ef5 100644 --- a/lib/file/file_linux.go +++ b/lib/file/file_linux.go @@ -61,24 +61,3 @@ func GetFileInfo(dir, suffix string) ([]FileInfo, error) { } return files, nil } - -func GetFileAndDirCount(dir string) (int, int, error) { - var fileCount, dirCount int - - err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { - if err != nil { - return err - } - if path == dir { - return nil // 跳过当前目录 - } - if info.IsDir() { - dirCount++ - } else { - fileCount++ - } - return nil - }) - - return fileCount, dirCount, err -} diff --git a/lib/file/file_windows.go b/lib/file/file_windows.go index b19e37a3..5371d44e 100644 --- a/lib/file/file_windows.go +++ b/lib/file/file_windows.go @@ -59,24 +59,3 @@ func GetFileInfo(dir, suffix string) ([]FileInfo, error) { } return files, nil } - -func GetFileAndDirCount(dir string) (int, int, error) { - var fileCount, dirCount int - - err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { - if err != nil { - return err - } - if path == dir { - return nil // 跳过当前目录 - } - if info.IsDir() { - dirCount++ - } else { - fileCount++ - } - return nil - }) - - return fileCount, dirCount, err -} diff --git a/lib/services/response.go b/lib/services/response.go index 5b683e9d..93f24425 100644 --- a/lib/services/response.go +++ b/lib/services/response.go @@ -13,7 +13,7 @@ func DataResp(data any) map[string]any { return map[string]any{"code": CODE_SUCC, "data": data} } -func SuccResp() map[string]any { +func SuccMessageResp() map[string]any { return map[string]any{"code": CODE_SUCC, "message": "success"} } @@ -21,6 +21,15 @@ func TotalResp(total int64) map[string]any { return map[string]any{"code": CODE_SUCC, "total": total} } -func TotalDataResp(data any, total int64) map[string]any { +func TotalDataResp(data any, total any) map[string]any { return map[string]any{"code": CODE_SUCC, "data": data, "total": total} } + +func SuccResp(va map[string]any) map[string]any { + resp := make(map[string]any) + resp["code"] = CODE_SUCC + for k, v := range va { + resp[k] = v + } + return resp +}