2018-01-26 17:17:38 +08:00
|
|
|
|
package filetil
|
|
|
|
|
|
|
|
|
|
import (
|
2024-07-05 15:31:34 +08:00
|
|
|
|
"bytes"
|
|
|
|
|
"fmt"
|
|
|
|
|
"io"
|
|
|
|
|
"io/ioutil"
|
|
|
|
|
"math"
|
2018-01-26 17:17:38 +08:00
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
//==================================
|
|
|
|
|
//更多文件和目录的操作,使用filepath包和os包
|
|
|
|
|
//==================================
|
|
|
|
|
|
2024-07-05 15:31:34 +08:00
|
|
|
|
type FileTypeStrategy interface {
|
|
|
|
|
GetFilePath(filePath, fileName, ext string) string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type ImageStrategy struct{}
|
|
|
|
|
|
|
|
|
|
func (i ImageStrategy) GetFilePath(filePath, fileName, ext string) string {
|
|
|
|
|
return filepath.Join(filePath, "images", fileName+ext)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type VideoStrategy struct{}
|
|
|
|
|
|
|
|
|
|
func (v VideoStrategy) GetFilePath(filePath, fileName, ext string) string {
|
|
|
|
|
return filepath.Join(filePath, "videos", fileName+ext)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type DefaultStrategy struct{}
|
|
|
|
|
|
|
|
|
|
func (d DefaultStrategy) GetFilePath(filePath, fileName, ext string) string {
|
|
|
|
|
return filepath.Join(filePath, "files", fileName+ext)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 返回的目录扫描结果
|
2018-01-26 17:17:38 +08:00
|
|
|
|
type FileList struct {
|
|
|
|
|
IsDir bool //是否是目录
|
|
|
|
|
Path string //文件路径
|
|
|
|
|
Ext string //文件扩展名
|
|
|
|
|
Name string //文件名
|
|
|
|
|
Size int64 //文件大小
|
|
|
|
|
ModTime int64 //文件修改时间戳
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-05 15:31:34 +08:00
|
|
|
|
// 目录扫描
|
|
|
|
|
// @param dir 需要扫描的目录
|
|
|
|
|
// @return fl 文件列表
|
|
|
|
|
// @return err 错误
|
2018-01-26 17:17:38 +08:00
|
|
|
|
func ScanFiles(dir string) (fl []FileList, err error) {
|
|
|
|
|
err = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
|
|
|
|
|
if err == nil {
|
|
|
|
|
path = strings.Replace(path, "\\", "/", -1) //文件路径处理
|
|
|
|
|
fl = append(fl, FileList{
|
|
|
|
|
IsDir: info.IsDir(),
|
|
|
|
|
Path: path,
|
|
|
|
|
Ext: strings.ToLower(filepath.Ext(path)),
|
|
|
|
|
Name: info.Name(),
|
|
|
|
|
Size: info.Size(),
|
|
|
|
|
ModTime: info.ModTime().Unix(),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
return err
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
2018-03-22 14:27:23 +08:00
|
|
|
|
|
2024-07-05 15:31:34 +08:00
|
|
|
|
// 拷贝文件
|
2018-03-22 14:27:23 +08:00
|
|
|
|
func CopyFile(source string, dst string) (err error) {
|
|
|
|
|
sourceFile, err := os.Open(source)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
defer sourceFile.Close()
|
|
|
|
|
|
2024-07-05 15:31:34 +08:00
|
|
|
|
_, err = os.Stat(filepath.Dir(dst))
|
2018-03-22 14:27:23 +08:00
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
if os.IsNotExist(err) {
|
2024-07-05 15:31:34 +08:00
|
|
|
|
os.MkdirAll(filepath.Dir(dst), 0766)
|
|
|
|
|
} else {
|
2018-03-22 14:27:23 +08:00
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
destFile, err := os.Create(dst)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
defer destFile.Close()
|
|
|
|
|
|
|
|
|
|
_, err = io.Copy(destFile, sourceFile)
|
|
|
|
|
if err == nil {
|
|
|
|
|
sourceInfo, err := os.Stat(source)
|
|
|
|
|
if err != nil {
|
|
|
|
|
err = os.Chmod(dst, sourceInfo.Mode())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-05 15:31:34 +08:00
|
|
|
|
// 拷贝目录
|
2018-03-22 14:27:23 +08:00
|
|
|
|
func CopyDir(source string, dest string) (err error) {
|
|
|
|
|
|
|
|
|
|
// get properties of source dir
|
|
|
|
|
sourceInfo, err := os.Stat(source)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// create dest dir
|
|
|
|
|
err = os.MkdirAll(dest, sourceInfo.Mode())
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
directory, _ := os.Open(source)
|
|
|
|
|
|
|
|
|
|
objects, err := directory.Readdir(-1)
|
|
|
|
|
|
|
|
|
|
for _, obj := range objects {
|
|
|
|
|
|
2024-07-05 15:31:34 +08:00
|
|
|
|
sourceFilePointer := filepath.Join(source, obj.Name())
|
2018-03-22 14:27:23 +08:00
|
|
|
|
|
|
|
|
|
destinationFilePointer := filepath.Join(dest, obj.Name())
|
|
|
|
|
|
|
|
|
|
if obj.IsDir() {
|
|
|
|
|
// create sub-directories - recursively
|
|
|
|
|
err = CopyDir(sourceFilePointer, destinationFilePointer)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println(err)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// perform copy
|
|
|
|
|
err = CopyFile(sourceFilePointer, destinationFilePointer)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func RemoveDir(dir string) error {
|
|
|
|
|
return os.RemoveAll(dir)
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-24 17:24:02 +08:00
|
|
|
|
func AbsolutePath(p string) (string, error) {
|
|
|
|
|
|
|
|
|
|
if strings.HasPrefix(p, "~") {
|
|
|
|
|
home := os.Getenv("HOME")
|
|
|
|
|
if home == "" {
|
|
|
|
|
panic(fmt.Sprintf("can not found HOME in envs, '%s' AbsPh Failed!", p))
|
|
|
|
|
}
|
|
|
|
|
p = fmt.Sprint(home, string(p[1:]))
|
|
|
|
|
}
|
|
|
|
|
s, err := filepath.Abs(p)
|
|
|
|
|
|
|
|
|
|
if nil != err {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
return s, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// FileExists reports whether the named file or directory exists.
|
2018-07-23 09:54:00 +08:00
|
|
|
|
func FileExists(path string) bool {
|
|
|
|
|
_, err := os.Stat(path)
|
|
|
|
|
if err == nil {
|
2018-03-25 14:59:42 +08:00
|
|
|
|
return true
|
2018-03-24 17:24:02 +08:00
|
|
|
|
}
|
2018-07-23 09:54:00 +08:00
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false
|
2018-03-24 17:24:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func FormatBytes(size int64) string {
|
|
|
|
|
units := []string{" B", " KB", " MB", " GB", " TB"}
|
|
|
|
|
|
|
|
|
|
s := float64(size)
|
|
|
|
|
|
|
|
|
|
i := 0
|
|
|
|
|
|
|
|
|
|
for ; s >= 1024 && i < 4; i++ {
|
|
|
|
|
s /= 1024
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fmt.Sprintf("%.2f%s", s, units[i])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Round(val float64, places int) float64 {
|
|
|
|
|
var t float64
|
|
|
|
|
f := math.Pow10(places)
|
|
|
|
|
x := val * f
|
|
|
|
|
if math.IsInf(x, 0) || math.IsNaN(x) {
|
|
|
|
|
return val
|
|
|
|
|
}
|
|
|
|
|
if x >= 0.0 {
|
|
|
|
|
t = math.Ceil(x)
|
|
|
|
|
if (t - x) > 0.50000000001 {
|
|
|
|
|
t -= 1.0
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
t = math.Ceil(-x)
|
|
|
|
|
if (t + x) > 0.50000000001 {
|
|
|
|
|
t -= 1.0
|
|
|
|
|
}
|
|
|
|
|
t = -t
|
|
|
|
|
}
|
|
|
|
|
x = t / f
|
|
|
|
|
|
|
|
|
|
if !math.IsInf(x, 0) {
|
|
|
|
|
return x
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return t
|
2018-03-24 22:36:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-07-05 15:31:34 +08:00
|
|
|
|
// 判断指定目录下是否存在指定后缀的文件
|
|
|
|
|
func HasFileOfExt(path string, exts []string) bool {
|
2018-03-24 22:36:35 +08:00
|
|
|
|
err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
|
|
|
|
|
if !info.IsDir() {
|
|
|
|
|
|
|
|
|
|
ext := filepath.Ext(info.Name())
|
|
|
|
|
|
2024-07-05 15:31:34 +08:00
|
|
|
|
for _, item := range exts {
|
|
|
|
|
if strings.EqualFold(ext, item) {
|
2018-03-24 22:36:35 +08:00
|
|
|
|
return os.ErrExist
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return err == os.ErrExist
|
2018-03-26 19:26:01 +08:00
|
|
|
|
}
|
2024-07-05 15:31:34 +08:00
|
|
|
|
|
2019-02-27 17:32:06 +08:00
|
|
|
|
// IsImageExt 判断是否是图片后缀
|
|
|
|
|
func IsImageExt(filename string) bool {
|
|
|
|
|
ext := filepath.Ext(filename)
|
|
|
|
|
|
|
|
|
|
return strings.EqualFold(ext, ".jpg") ||
|
|
|
|
|
strings.EqualFold(ext, ".jpeg") ||
|
|
|
|
|
strings.EqualFold(ext, ".png") ||
|
|
|
|
|
strings.EqualFold(ext, ".gif") ||
|
2024-07-05 15:31:34 +08:00
|
|
|
|
strings.EqualFold(ext, ".svg") ||
|
|
|
|
|
strings.EqualFold(ext, ".bmp") ||
|
|
|
|
|
strings.EqualFold(ext, ".webp")
|
2019-02-27 17:32:06 +08:00
|
|
|
|
}
|
2018-03-26 19:26:01 +08:00
|
|
|
|
|
2024-07-05 15:31:34 +08:00
|
|
|
|
// IsImageExt 判断是否是视频后缀
|
|
|
|
|
func IsVideoExt(filename string) bool {
|
|
|
|
|
ext := filepath.Ext(filename)
|
|
|
|
|
|
|
|
|
|
return strings.EqualFold(ext, ".mp4") ||
|
|
|
|
|
strings.EqualFold(ext, ".webm") ||
|
|
|
|
|
strings.EqualFold(ext, ".ogg") ||
|
|
|
|
|
strings.EqualFold(ext, ".avi") ||
|
|
|
|
|
strings.EqualFold(ext, ".flv") ||
|
|
|
|
|
strings.EqualFold(ext, ".mov")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 忽略字符串中的BOM头
|
|
|
|
|
func ReadFileAndIgnoreUTF8BOM(filename string) ([]byte, error) {
|
|
|
|
|
|
|
|
|
|
data, err := ioutil.ReadFile(filename)
|
2018-03-26 19:26:01 +08:00
|
|
|
|
if err != nil {
|
2024-07-05 15:31:34 +08:00
|
|
|
|
return nil, err
|
2018-03-26 19:26:01 +08:00
|
|
|
|
}
|
|
|
|
|
if data == nil {
|
2024-07-05 15:31:34 +08:00
|
|
|
|
return nil, nil
|
2018-03-26 19:26:01 +08:00
|
|
|
|
}
|
2024-07-05 15:31:34 +08:00
|
|
|
|
data = bytes.Replace(data, []byte("\r"), []byte(""), -1)
|
2018-03-26 19:26:01 +08:00
|
|
|
|
if len(data) >= 3 && data[0] == 0xef && data[1] == 0xbb && data[2] == 0xbf {
|
2024-07-05 15:31:34 +08:00
|
|
|
|
return data[3:], err
|
2018-03-26 19:26:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-07-05 15:31:34 +08:00
|
|
|
|
return data, nil
|
2018-08-14 15:57:52 +08:00
|
|
|
|
}
|