2021-10-25 10:53:59 +00:00
|
|
|
|
package utils
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
2021-10-26 14:28:37 +00:00
|
|
|
|
"github.com/Xhofe/alist/conf"
|
2021-10-25 10:53:59 +00:00
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
"io/ioutil"
|
|
|
|
|
"os"
|
2021-12-28 12:34:45 +00:00
|
|
|
|
"path"
|
2021-10-25 10:53:59 +00:00
|
|
|
|
"path/filepath"
|
2021-10-28 14:50:09 +00:00
|
|
|
|
"strings"
|
2021-10-25 10:53:59 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-26 14:28:37 +00:00
|
|
|
|
// IsDir determine whether the file is dir
|
|
|
|
|
func IsDir(path string) bool {
|
|
|
|
|
s, err := os.Stat(path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
return s.IsDir()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetFileType get file type
|
|
|
|
|
func GetFileType(ext string) int {
|
|
|
|
|
if ext == "" {
|
|
|
|
|
return conf.UNKNOWN
|
|
|
|
|
}
|
2021-12-28 12:34:45 +00:00
|
|
|
|
ext = strings.ToLower(strings.TrimLeft(ext, "."))
|
2021-10-29 16:35:29 +00:00
|
|
|
|
if IsContain(conf.OfficeTypes, ext) {
|
2021-10-26 14:28:37 +00:00
|
|
|
|
return conf.OFFICE
|
|
|
|
|
}
|
2021-10-29 16:35:29 +00:00
|
|
|
|
if IsContain(conf.AudioTypes, ext) {
|
2021-10-26 14:28:37 +00:00
|
|
|
|
return conf.AUDIO
|
|
|
|
|
}
|
2021-10-29 16:35:29 +00:00
|
|
|
|
if IsContain(conf.VideoTypes, ext) {
|
2021-10-26 14:28:37 +00:00
|
|
|
|
return conf.VIDEO
|
|
|
|
|
}
|
2021-10-29 16:35:29 +00:00
|
|
|
|
if IsContain(conf.TextTypes, ext) {
|
2021-10-26 14:28:37 +00:00
|
|
|
|
return conf.TEXT
|
|
|
|
|
}
|
2021-10-29 16:35:29 +00:00
|
|
|
|
if IsContain(conf.ImageTypes, ext) {
|
|
|
|
|
return conf.IMAGE
|
|
|
|
|
}
|
2021-10-26 14:28:37 +00:00
|
|
|
|
return conf.UNKNOWN
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-25 10:53:59 +00:00
|
|
|
|
// 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)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WriteToJson write struct to json file
|
|
|
|
|
func WriteToJson(src string, conf interface{}) bool {
|
2021-10-29 16:35:29 +00:00
|
|
|
|
data, err := json.MarshalIndent(conf, "", " ")
|
2021-10-25 10:53:59 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
log.Errorf("failed convert Conf to []byte:%s", err.Error())
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
err = ioutil.WriteFile(src, data, 0777)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Errorf("failed to write json file:%s", err.Error())
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
return true
|
2021-10-28 14:50:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ParsePath(path string) string {
|
|
|
|
|
path = strings.TrimRight(path, "/")
|
|
|
|
|
if !strings.HasPrefix(path, "/") {
|
|
|
|
|
path = "/" + path
|
|
|
|
|
}
|
|
|
|
|
return path
|
2021-10-29 16:35:29 +00:00
|
|
|
|
}
|
2021-11-15 11:20:39 +00:00
|
|
|
|
|
2021-11-27 16:12:04 +00:00
|
|
|
|
func RemoveLastSlash(path string) string {
|
|
|
|
|
if len(path) > 1 {
|
|
|
|
|
return strings.TrimSuffix(path, "/")
|
|
|
|
|
}
|
|
|
|
|
return path
|
2021-11-29 13:09:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Dir(path string) string {
|
|
|
|
|
idx := strings.LastIndex(path, "/")
|
2021-11-29 13:30:52 +00:00
|
|
|
|
if idx == 0 {
|
|
|
|
|
return "/"
|
|
|
|
|
}
|
|
|
|
|
if idx == -1 {
|
|
|
|
|
return path
|
|
|
|
|
}
|
2021-11-29 13:09:16 +00:00
|
|
|
|
return path[:idx]
|
2021-12-02 10:57:29 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Base(path string) string {
|
|
|
|
|
idx := strings.LastIndex(path, "/")
|
|
|
|
|
if idx == -1 {
|
|
|
|
|
return path
|
|
|
|
|
}
|
|
|
|
|
return path[idx+1:]
|
2021-12-16 10:03:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Join(elem ...string) string {
|
2021-12-28 12:34:45 +00:00
|
|
|
|
res := path.Join(elem...)
|
2021-12-16 10:03:58 +00:00
|
|
|
|
if res == "\\" {
|
|
|
|
|
res = "/"
|
|
|
|
|
}
|
|
|
|
|
return res
|
2021-12-28 12:34:45 +00:00
|
|
|
|
}
|
2022-02-02 09:32:11 +00:00
|
|
|
|
|
|
|
|
|
func Split(p string) (string, string) {
|
|
|
|
|
return path.Split(p)
|
|
|
|
|
}
|