2022-08-24 11:10:50 +08:00
|
|
|
package files
|
|
|
|
|
|
|
|
import (
|
2023-08-25 23:42:14 +08:00
|
|
|
"bufio"
|
2022-09-09 18:10:41 +08:00
|
|
|
"github.com/spf13/afero"
|
2023-04-27 21:31:52 +08:00
|
|
|
"net/http"
|
2022-08-24 11:10:50 +08:00
|
|
|
"os"
|
2023-04-26 22:18:16 +08:00
|
|
|
"os/user"
|
2022-09-09 18:10:41 +08:00
|
|
|
"path/filepath"
|
2023-04-26 22:18:16 +08:00
|
|
|
"strconv"
|
2022-09-09 18:10:41 +08:00
|
|
|
"sync"
|
2022-08-24 11:10:50 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func IsSymlink(mode os.FileMode) bool {
|
|
|
|
return mode&os.ModeSymlink != 0
|
|
|
|
}
|
|
|
|
|
2022-08-30 17:59:59 +08:00
|
|
|
func GetMimeType(path string) string {
|
2023-04-27 21:31:52 +08:00
|
|
|
file, err := os.Open(path)
|
2022-08-30 17:59:59 +08:00
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
2023-04-27 21:31:52 +08: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 17:59:59 +08:00
|
|
|
}
|
2022-08-31 16:00:51 +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
|
|
|
|
2023-04-26 22:18:16 +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 18:10:41 +08:00
|
|
|
func ScanDir(fs afero.Fs, path string, dirMap *sync.Map, wg *sync.WaitGroup) {
|
|
|
|
afs := &afero.Afero{Fs: fs}
|
|
|
|
files, _ := afs.ReadDir(path)
|
|
|
|
for _, f := range files {
|
|
|
|
if f.IsDir() {
|
|
|
|
wg.Add(1)
|
|
|
|
go ScanDir(fs, filepath.Join(path, f.Name()), dirMap, wg)
|
|
|
|
} else {
|
|
|
|
if f.Size() > 0 {
|
|
|
|
dirMap.Store(filepath.Join(path, f.Name()), float64(f.Size()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
defer wg.Done()
|
|
|
|
}
|
|
|
|
|
2022-09-09 11:20:02 +08:00
|
|
|
const dotCharacter = 46
|
|
|
|
|
|
|
|
func IsHidden(path string) bool {
|
|
|
|
return path[0] == dotCharacter
|
|
|
|
}
|
2023-08-25 23:42:14 +08:00
|
|
|
|
|
|
|
func ReadFileByLine(filename string, page, pageSize int) ([]string, bool, error) {
|
2023-11-23 11:00:08 +08:00
|
|
|
if !NewFileOp().Stat(filename) {
|
|
|
|
return nil, true, nil
|
|
|
|
}
|
2023-08-25 23:42:14 +08:00
|
|
|
file, err := os.Open(filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(file)
|
|
|
|
|
|
|
|
var lines []string
|
|
|
|
currentLine := 0
|
|
|
|
startLine := (page - 1) * pageSize
|
|
|
|
endLine := startLine + pageSize
|
|
|
|
|
|
|
|
for scanner.Scan() {
|
|
|
|
if currentLine >= startLine && currentLine < endLine {
|
|
|
|
lines = append(lines, scanner.Text())
|
|
|
|
}
|
|
|
|
currentLine++
|
|
|
|
if currentLine >= endLine {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
isEndOfFile := currentLine < endLine
|
|
|
|
|
|
|
|
return lines, isEndOfFile, nil
|
|
|
|
}
|