mirror of
https://github.com/stashapp/stash.git
synced 2025-12-17 12:24:38 +03:00
Moved everything out of internal
This commit is contained in:
66
utils/file.go
Normal file
66
utils/file.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"github.com/h2non/filetype"
|
||||
"github.com/h2non/filetype/types"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func FileType(filePath string) (types.Type, error) {
|
||||
file, _ := os.Open(filePath)
|
||||
|
||||
// We only have to pass the file header = first 261 bytes
|
||||
head := make([]byte, 261)
|
||||
_, _ = file.Read(head)
|
||||
|
||||
return filetype.Match(head)
|
||||
}
|
||||
|
||||
func FileExists(path string) (bool, error) {
|
||||
_, err := os.Stat(path)
|
||||
if err == nil {
|
||||
return true, nil
|
||||
} else if os.IsNotExist(err) {
|
||||
return false, err
|
||||
} else {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func EnsureDir(path string) error {
|
||||
exists, err := FileExists(path)
|
||||
if !exists {
|
||||
err = os.Mkdir(path, 0755)
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// TODO test
|
||||
func RemoveDir(path string) error {
|
||||
return os.RemoveAll(path)
|
||||
}
|
||||
|
||||
// TODO test
|
||||
func EmptyDir(path string) error {
|
||||
d, err := os.Open(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer d.Close()
|
||||
|
||||
names, err := d.Readdirnames(-1)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, name := range names {
|
||||
err = os.RemoveAll(filepath.Join(path, name))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user