marge: 合并11.2版本

This commit is contained in:
TsMask
2024-11-22 10:06:51 +08:00
parent 86ba2fb4a6
commit 1bdb13a2ab
67 changed files with 3536 additions and 3765 deletions

View File

@@ -1,161 +1,27 @@
package file
import (
"fmt"
"net/http"
"os"
"path/filepath"
)
// const (
// //经过测试linux下延时需要大于100ms
// TIME_DELAY_AFTER_WRITE = 200
// )
func GetFileAndDirCount(dir string) (int, int, error) {
var fileCount, dirCount int
// type Response struct {
// Data []string `json:"data"`
// }
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
})
// type MMLRequest struct {
// MML []string `json:"mml"`
// }
// func GetFile(w http.ResponseWriter, r *http.Request) {
// log.Debug("PostMMLToNF processing... ")
// vars := mux.Vars(r)
// neType := vars["elementTypeValue"]
// params := r.URL.Query()
// neId := params["ne_id"]
// log.Debug("neType:", neType, "neId", neId)
// neInfo := new(dborm.NeInfo)
// var err error
// if len(neId) == 0 {
// log.Error("ne_id NOT FOUND")
// services.ResponseBadRequest400WrongParamValue(w)
// return
// }
// neInfo, err = dborm.XormGetNeInfo(neType, neId[0])
// if err != nil {
// log.Error("dborm.XormGetNeInfo is failed:", err)
// services.ResponseInternalServerError500DatabaseOperationFailed(w)
// return
// }
// var buf [8192]byte
// var n int
// var mmlResult []string
// if neInfo != nil {
// hostMML := fmt.Sprintf("%s:%d", neInfo.Ip, config.GetYamlConfig().MML.Port)
// conn, err := net.Dial("tcp", hostMML)
// if err != nil {
// errMsg := fmt.Sprintf("Failed to dial %s: %v", hostMML, err)
// log.Error(errMsg)
// mmlResult = append(mmlResult, errMsg)
// response := Response{mmlResult}
// services.ResponseWithJson(w, http.StatusOK, response)
// return
// }
// loginStr := fmt.Sprintf("%s\n%s\n", config.GetYamlConfig().MML.User, config.GetYamlConfig().MML.Password)
// n, err = conn.Write([]byte(loginStr))
// if err != nil {
// log.Errorf("Error: %s", err.Error())
// return
// }
// time.Sleep(time.Millisecond * TIME_DELAY_AFTER_WRITE)
// n, err = conn.Read(buf[0:])
// if err != nil {
// log.Errorf("Error: %s", err.Error())
// return
// }
// log.Debug(string(buf[0:n]))
// body, err := io.ReadAll(io.LimitReader(r.Body, global.RequestBodyMaxLen))
// if err != nil {
// log.Error("io.ReadAll is failed:", err)
// services.ResponseNotFound404UriNotExist(w, r)
// return
// }
// log.Debug("Body:", string(body))
// mmlRequest := new(MMLRequest)
// _ = json.Unmarshal(body, mmlRequest)
// for _, mml := range mmlRequest.MML {
// mmlCommand := fmt.Sprintf("%s\n", mml)
// log.Debug("mml command:", mmlCommand)
// n, err = conn.Write([]byte(mmlCommand))
// if err != nil {
// log.Errorf("Error: %s", err.Error())
// return
// }
// time.Sleep(time.Millisecond * TIME_DELAY_AFTER_WRITE)
// n, err = conn.Read(buf[0:])
// if err != nil {
// log.Errorf("Error: %s", err.Error())
// return
// }
// log.Debug(string(buf[0 : n-len(neType)-2]))
// mmlResult = append(mmlResult, string(buf[0:n-len(neType)-2]))
// }
// }
// response := Response{mmlResult}
// services.ResponseWithJson(w, http.StatusOK, response)
// }
// 格式文件大小单位
func FormatFileSize(fileSize float64) (size string) {
if fileSize < 1024 {
return fmt.Sprintf("%.2fB", fileSize/float64(1))
} else if fileSize < (1024 * 1024) {
return fmt.Sprintf("%.2fKB", fileSize/float64(1024))
} else if fileSize < (1024 * 1024 * 1024) {
return fmt.Sprintf("%.2fMB", fileSize/float64(1024*1024))
} else if fileSize < (1024 * 1024 * 1024 * 1024) {
return fmt.Sprintf("%.2fGB", fileSize/float64(1024*1024*1024))
} else if fileSize < (1024 * 1024 * 1024 * 1024 * 1024) {
return fmt.Sprintf("%.2fTB", fileSize/float64(1024*1024*1024*1024))
} else {
return fmt.Sprintf("%.2fEB", fileSize/float64(1024*1024*1024*1024*1024))
}
}
func IsSymlink(mode os.FileMode) bool {
return mode&os.ModeSymlink != 0
}
const dotCharacter = 46
func IsHidden(path string) bool {
return path[0] == dotCharacter
}
func GetMimeType(path string) string {
file, err := os.Open(path)
if err != nil {
return ""
}
defer file.Close()
buffer := make([]byte, 512)
_, err = file.Read(buffer)
if err != nil {
return ""
}
mimeType := http.DetectContentType(buffer)
return mimeType
}
func GetSymlink(path string) string {
linkPath, err := os.Readlink(path)
if err != nil {
return ""
}
return linkPath
return fileCount, dirCount, err
}

80
lib/file/file_linux.go Normal file
View File

@@ -0,0 +1,80 @@
//go:build linux
// +build linux
package file
import (
"fmt"
"os"
"os/user"
"path/filepath"
"strings"
"syscall"
"github.com/dustin/go-humanize"
)
type FileInfo struct {
FileType string `json:"fileType"` // 文件类型
FileMode string `json:"fileMode"` // 文件的权限
LinkCount int64 `json:"linkCount"` // 硬链接数目
Owner string `json:"owner"` // 所属用户
Group string `json:"group"` // 所属组
Size string `json:"size"` // 文件的大小
ModifiedTime int64 `json:"modifiedTime"` // 最后修改时间,单位为秒
FileName string `json:"fileName"` // 文件的名称
}
func GetFileInfo(dir, suffix string) ([]FileInfo, error) {
var files []FileInfo
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if path == dir {
return nil // 跳过当前目录
}
fileType := "file"
if info.IsDir() {
fileType = "directory"
} else if info.Mode()&os.ModeSymlink != 0 {
fileType = "symlink"
}
// check if match suffix
if (suffix != "" && filepath.Ext(path) == suffix) || suffix == "" {
stat, ok := info.Sys().(*syscall.Stat_t)
if !ok {
return fmt.Errorf("not a syscall.Stat_t")
}
userInfo, err := user.LookupId(fmt.Sprint(stat.Uid))
if err != nil {
return err
}
groupInfo, err := user.LookupGroupId(fmt.Sprint(stat.Gid))
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: strings.ToUpper(humanReadableSize),
ModifiedTime: info.ModTime().Unix(),
FileName: info.Name(),
}
files = append(files, fileInfo)
}
return nil
})
if err != nil {
return nil, err
}
return files, nil
}

63
lib/file/file_windows.go Normal file
View File

@@ -0,0 +1,63 @@
//go:build windows
// +build windows
package file
import (
"os"
"path/filepath"
)
type FileInfo struct {
FileType string `json:"fileType"` // file type: file/directory
FileMode string `json:"fileMode"` // file mode
LinkCount int64 `json:"linkCount"` // link count
Owner string `json:"owner"` // owner
Group string `json:"group"` // group
Size int64 `json:"size"` // size: xx byte
ModifiedTime int64 `json:"modifiedTime"` // last modified time:seconds
FilePath string `json:"filePath"` // file path
FileName string `json:"fileName"` // file name
}
func GetFileInfo(dir, suffix string) ([]FileInfo, error) {
var files []FileInfo
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if path == dir {
return nil // 跳过当前目录
}
fileType := "file"
if info.IsDir() {
fileType = "directory"
} else if info.Mode()&os.ModeSymlink != 0 {
fileType = "symlink"
}
// check if match suffix
if (suffix != "" && filepath.Ext(path) == suffix) || suffix == "" {
fileInfo := FileInfo{
FileType: fileType,
FileMode: info.Mode().String(),
LinkCount: 0,
Owner: "-",
Group: "-",
Size: info.Size(),
ModifiedTime: info.ModTime().Unix(),
FilePath: dir,
FileName: info.Name(),
}
files = append(files, fileInfo)
}
return nil
})
if err != nil {
return nil, err
}
return files, nil
}