2021-10-25 18:53:59 +08:00
|
|
|
|
package utils
|
|
|
|
|
|
|
|
|
|
import (
|
2021-11-15 19:20:39 +08:00
|
|
|
|
"bytes"
|
2021-10-25 18:53:59 +08:00
|
|
|
|
"encoding/json"
|
2021-10-26 22:28:37 +08:00
|
|
|
|
"github.com/Xhofe/alist/conf"
|
2021-10-25 18:53:59 +08:00
|
|
|
|
log "github.com/sirupsen/logrus"
|
2021-11-15 19:20:39 +08:00
|
|
|
|
"golang.org/x/text/encoding/simplifiedchinese"
|
|
|
|
|
"golang.org/x/text/transform"
|
2021-10-25 18:53:59 +08:00
|
|
|
|
"io/ioutil"
|
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
2021-10-28 22:50:09 +08:00
|
|
|
|
"strings"
|
2021-10-25 18:53:59 +08: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 22:28:37 +08: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-10-30 19:26:23 +08:00
|
|
|
|
ext = strings.ToLower(strings.TrimLeft(ext,"."))
|
2021-10-30 00:35:29 +08:00
|
|
|
|
if IsContain(conf.OfficeTypes, ext) {
|
2021-10-26 22:28:37 +08:00
|
|
|
|
return conf.OFFICE
|
|
|
|
|
}
|
2021-10-30 00:35:29 +08:00
|
|
|
|
if IsContain(conf.AudioTypes, ext) {
|
2021-10-26 22:28:37 +08:00
|
|
|
|
return conf.AUDIO
|
|
|
|
|
}
|
2021-10-30 00:35:29 +08:00
|
|
|
|
if IsContain(conf.VideoTypes, ext) {
|
2021-10-26 22:28:37 +08:00
|
|
|
|
return conf.VIDEO
|
|
|
|
|
}
|
2021-10-30 00:35:29 +08:00
|
|
|
|
if IsContain(conf.TextTypes, ext) {
|
2021-10-26 22:28:37 +08:00
|
|
|
|
return conf.TEXT
|
|
|
|
|
}
|
2021-10-30 00:35:29 +08:00
|
|
|
|
if IsContain(conf.ImageTypes, ext) {
|
|
|
|
|
return conf.IMAGE
|
|
|
|
|
}
|
2021-10-26 22:28:37 +08:00
|
|
|
|
return conf.UNKNOWN
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-25 18:53:59 +08: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-30 00:35:29 +08:00
|
|
|
|
data, err := json.MarshalIndent(conf, "", " ")
|
2021-10-25 18:53:59 +08: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 22:50:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ParsePath(path string) string {
|
|
|
|
|
path = strings.TrimRight(path, "/")
|
|
|
|
|
if !strings.HasPrefix(path, "/") {
|
|
|
|
|
path = "/" + path
|
|
|
|
|
}
|
|
|
|
|
return path
|
2021-10-30 00:35:29 +08:00
|
|
|
|
}
|
2021-11-15 19:20:39 +08:00
|
|
|
|
|
|
|
|
|
func IsGBK(data []byte) bool {
|
|
|
|
|
length := len(data)
|
|
|
|
|
var i int = 0
|
|
|
|
|
for i < length {
|
|
|
|
|
//fmt.Printf("for %x\n", data[i])
|
|
|
|
|
if data[i] <= 0xff {
|
|
|
|
|
//编码小于等于127,只有一个字节的编码,兼容ASCII吗
|
|
|
|
|
i++
|
|
|
|
|
continue
|
|
|
|
|
} else {
|
|
|
|
|
//大于127的使用双字节编码
|
|
|
|
|
if data[i] >= 0x81 &&
|
|
|
|
|
data[i] <= 0xfe &&
|
|
|
|
|
data[i + 1] >= 0x40 &&
|
|
|
|
|
data[i + 1] <= 0xfe &&
|
|
|
|
|
data[i + 1] != 0xf7 {
|
|
|
|
|
i += 2
|
|
|
|
|
continue
|
|
|
|
|
} else {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GbkToUtf8(s []byte) ([]byte, error) {
|
|
|
|
|
reader := transform.NewReader(bytes.NewReader(s), simplifiedchinese.GBK.NewDecoder())
|
|
|
|
|
d, e := ioutil.ReadAll(reader)
|
|
|
|
|
if e != nil {
|
|
|
|
|
return nil, e
|
|
|
|
|
}
|
|
|
|
|
return d, nil
|
|
|
|
|
}
|