2022-08-24 03:10:50 +00:00
|
|
|
package files
|
|
|
|
|
|
|
|
import (
|
2023-08-25 15:42:14 +00:00
|
|
|
"bufio"
|
2024-04-09 08:36:09 +00:00
|
|
|
"fmt"
|
2024-03-06 03:56:07 +00:00
|
|
|
"io"
|
2023-04-27 13:31:52 +00:00
|
|
|
"net/http"
|
2022-08-24 03:10:50 +00:00
|
|
|
"os"
|
2023-04-26 14:18:16 +00:00
|
|
|
"os/user"
|
2022-09-09 10:10:41 +00:00
|
|
|
"path/filepath"
|
2023-04-26 14:18:16 +00:00
|
|
|
"strconv"
|
2024-04-11 10:32:11 +00:00
|
|
|
"strings"
|
2022-08-24 03:10:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func IsSymlink(mode os.FileMode) bool {
|
|
|
|
return mode&os.ModeSymlink != 0
|
|
|
|
}
|
|
|
|
|
2024-04-09 06:28:09 +00:00
|
|
|
func IsBlockDevice(mode os.FileMode) bool {
|
|
|
|
return mode&os.ModeDevice != 0 && mode&os.ModeCharDevice == 0
|
|
|
|
}
|
|
|
|
|
2022-08-30 09:59:59 +00:00
|
|
|
func GetMimeType(path string) string {
|
2023-04-27 13:31:52 +00:00
|
|
|
file, err := os.Open(path)
|
2022-08-30 09:59:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
2023-04-27 13:31:52 +00:00
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
buffer := make([]byte, 512)
|
|
|
|
_, err = file.Read(buffer)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
mimeType := http.DetectContentType(buffer)
|
|
|
|
return mimeType
|
2022-08-30 09:59:59 +00:00
|
|
|
}
|
2022-08-31 08:00:51 +00:00
|
|
|
|
|
|
|
func GetSymlink(path string) string {
|
|
|
|
linkPath, err := os.Readlink(path)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return linkPath
|
|
|
|
}
|
2022-09-09 03:20:02 +00:00
|
|
|
|
2023-04-26 14:18:16 +00: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 03:20:02 +00:00
|
|
|
const dotCharacter = 46
|
|
|
|
|
|
|
|
func IsHidden(path string) bool {
|
|
|
|
return path[0] == dotCharacter
|
|
|
|
}
|
2023-08-25 15:42:14 +00:00
|
|
|
|
2024-06-17 14:24:50 +00:00
|
|
|
func countLines(path string) (int, error) {
|
|
|
|
file, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(file)
|
|
|
|
lineCount := 0
|
|
|
|
for scanner.Scan() {
|
|
|
|
lineCount++
|
|
|
|
}
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return lineCount, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ReadFileByLine(filename string, page, pageSize int, latest bool) (lines []string, isEndOfFile bool, total int, err error) {
|
2023-11-23 03:00:08 +00:00
|
|
|
if !NewFileOp().Stat(filename) {
|
2024-06-17 14:24:50 +00:00
|
|
|
return
|
2023-11-23 03:00:08 +00:00
|
|
|
}
|
2023-08-25 15:42:14 +00:00
|
|
|
file, err := os.Open(filename)
|
|
|
|
if err != nil {
|
2024-06-17 14:24:50 +00:00
|
|
|
return
|
2023-08-25 15:42:14 +00:00
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
2024-06-17 14:24:50 +00:00
|
|
|
totalLines, err := countLines(filename)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
total = (totalLines + pageSize - 1) / pageSize
|
2024-03-06 03:56:07 +00:00
|
|
|
reader := bufio.NewReaderSize(file, 8192)
|
2023-08-25 15:42:14 +00:00
|
|
|
|
2024-06-17 14:24:50 +00:00
|
|
|
if latest {
|
|
|
|
page = total
|
|
|
|
}
|
2023-08-25 15:42:14 +00:00
|
|
|
currentLine := 0
|
|
|
|
startLine := (page - 1) * pageSize
|
|
|
|
endLine := startLine + pageSize
|
|
|
|
|
2024-03-06 03:56:07 +00:00
|
|
|
for {
|
|
|
|
line, _, err := reader.ReadLine()
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
2023-08-25 15:42:14 +00:00
|
|
|
if currentLine >= startLine && currentLine < endLine {
|
2024-03-06 03:56:07 +00:00
|
|
|
lines = append(lines, string(line))
|
2023-08-25 15:42:14 +00:00
|
|
|
}
|
|
|
|
currentLine++
|
|
|
|
if currentLine >= endLine {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-17 14:24:50 +00:00
|
|
|
isEndOfFile = currentLine < endLine
|
|
|
|
return
|
2023-08-25 15:42:14 +00:00
|
|
|
}
|
2024-04-09 08:36:09 +00:00
|
|
|
|
|
|
|
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 {
|
2024-04-18 03:18:07 +00:00
|
|
|
return fileInfo.Mode() & os.ModePerm, nil
|
2024-04-09 08:36:09 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2024-04-11 10:32:11 +00:00
|
|
|
|
|
|
|
func IsInvalidChar(name string) bool {
|
2024-04-26 08:13:53 +00:00
|
|
|
return strings.Contains(name, "&")
|
2024-04-11 10:32:11 +00:00
|
|
|
}
|
2024-05-29 07:49:10 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|