alist/pkg/utils/file.go

31 lines
568 B
Go
Raw Normal View History

2022-06-06 13:48:53 +00:00
package utils
import (
log "github.com/sirupsen/logrus"
"os"
"path/filepath"
)
// Exists determine whether the file exists
func Exists(name string) bool {
if _, err := os.Stat(name); err != nil {
if os.IsNotExist(err) {
return false
}
}
return true
}
// CreatNestedFile create nested file
func CreatNestedFile(path string) (*os.File, error) {
basePath := filepath.Dir(path)
if !Exists(basePath) {
err := os.MkdirAll(basePath, 0700)
if err != nil {
log.Errorf("can't create foler%s", err)
return nil, err
}
}
return os.Create(path)
}