1Panel/backend/utils/files/utils.go

170 lines
2.9 KiB
Go
Raw Normal View History

2022-08-24 11:10:50 +08:00
package files
import (
"bufio"
"fmt"
"io"
"net/http"
2022-08-24 11:10:50 +08:00
"os"
"os/user"
"path/filepath"
"strconv"
"strings"
2022-08-24 11:10:50 +08:00
)
func IsSymlink(mode os.FileMode) bool {
return mode&os.ModeSymlink != 0
}
func IsBlockDevice(mode os.FileMode) bool {
return mode&os.ModeDevice != 0 && mode&os.ModeCharDevice == 0
}
2022-08-30 17:59:59 +08:00
func GetMimeType(path string) string {
file, err := os.Open(path)
2022-08-30 17:59:59 +08:00
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
2022-08-30 17:59:59 +08:00
}
func GetSymlink(path string) string {
linkPath, err := os.Readlink(path)
if err != nil {
return ""
}
return linkPath
}
2022-09-09 11:20:02 +08:00
func GetUsername(uid uint32) string {
usr, err := user.LookupId(strconv.Itoa(int(uid)))
if err != nil {
return ""
}
return usr.Username
}
func GetGroup(gid uint32) string {
usr, err := user.LookupGroupId(strconv.Itoa(int(gid)))
if err != nil {
return ""
}
return usr.Name
}
2022-09-09 11:20:02 +08:00
const dotCharacter = 46
func IsHidden(path string) bool {
return path[0] == dotCharacter
}
func countLines(path string) (int, error) {
file, err := os.Open(path)
if err != nil {
return 0, err
}
defer file.Close()
reader := bufio.NewReader(file)
count := 0
for {
_, err := reader.ReadString('\n')
if err != nil {
if err == io.EOF {
if count > 0 {
count++
}
return count, nil
}
return count, err
}
count++
}
}
func ReadFileByLine(filename string, page, pageSize int, latest bool) (lines []string, isEndOfFile bool, total int, err error) {
2023-11-23 11:00:08 +08:00
if !NewFileOp().Stat(filename) {
return
2023-11-23 11:00:08 +08:00
}
file, err := os.Open(filename)
if err != nil {
return
}
defer file.Close()
totalLines, err := countLines(filename)
if err != nil {
return
}
total = (totalLines + pageSize - 1) / pageSize
reader := bufio.NewReaderSize(file, 8192)
if latest {
page = total
}
currentLine := 0
startLine := (page - 1) * pageSize
endLine := startLine + pageSize
for {
line, _, err := reader.ReadLine()
if err == io.EOF {
break
}
if currentLine >= startLine && currentLine < endLine {
lines = append(lines, string(line))
}
currentLine++
if currentLine >= endLine {
break
}
}
isEndOfFile = currentLine < endLine
return
}
func GetParentMode(path string) (os.FileMode, error) {
absPath, err := filepath.Abs(path)
if err != nil {
return 0, err
}
for {
fileInfo, err := os.Stat(absPath)
if err == nil {
return fileInfo.Mode() & os.ModePerm, nil
}
if !os.IsNotExist(err) {
return 0, err
}
parentDir := filepath.Dir(absPath)
if parentDir == absPath {
return 0, fmt.Errorf("no existing directory found in the path: %s", path)
}
absPath = parentDir
}
}
func IsInvalidChar(name string) bool {
return strings.Contains(name, "&")
}
func IsEmptyDir(dir string) bool {
f, err := os.Open(dir)
if err != nil {
return false
}
defer f.Close()
_, err = f.Readdirnames(1)
return err == io.EOF
}