2022-08-24 03:10:50 +00:00
|
|
|
package files
|
|
|
|
|
|
|
|
import (
|
2022-08-30 09:59:59 +00:00
|
|
|
"github.com/gabriel-vasile/mimetype"
|
2022-09-09 10:10:41 +00:00
|
|
|
"github.com/spf13/afero"
|
2022-08-24 03:10:50 +00:00
|
|
|
"os"
|
2022-09-09 10:10:41 +00:00
|
|
|
"path/filepath"
|
|
|
|
"sync"
|
2022-08-24 03:10:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func IsSymlink(mode os.FileMode) bool {
|
|
|
|
return mode&os.ModeSymlink != 0
|
|
|
|
}
|
|
|
|
|
2022-08-30 09:59:59 +00:00
|
|
|
func GetMimeType(path string) string {
|
|
|
|
mime, err := mimetype.DetectFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return mime.String()
|
|
|
|
}
|
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
|
|
|
|
2022-09-09 10:10:41 +00: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 03:20:02 +00:00
|
|
|
const dotCharacter = 46
|
|
|
|
|
|
|
|
func IsHidden(path string) bool {
|
|
|
|
return path[0] == dotCharacter
|
|
|
|
}
|