mirror of
https://github.com/crawlab-team/crawlab.git
synced 2026-01-29 18:00:51 +01:00
加入可配置爬虫
This commit is contained in:
@@ -3,11 +3,15 @@ package utils
|
||||
import (
|
||||
"archive/zip"
|
||||
"bufio"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/apex/log"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime/debug"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// 删除文件
|
||||
@@ -71,6 +75,16 @@ func IsDir(path string) bool {
|
||||
return s.IsDir()
|
||||
}
|
||||
|
||||
func ListDir(path string) []os.FileInfo {
|
||||
list, err := ioutil.ReadDir(path)
|
||||
if err != nil {
|
||||
log.Errorf(err.Error())
|
||||
debug.PrintStack()
|
||||
return nil
|
||||
}
|
||||
return list
|
||||
}
|
||||
|
||||
// 判断所给路径是否为文件
|
||||
func IsFile(path string) bool {
|
||||
return !IsDir(path)
|
||||
@@ -239,3 +253,117 @@ func _Compress(file *os.File, prefix string, zw *zip.Writer) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
/**
|
||||
* 拷贝文件夹,同时拷贝文件夹中的文件
|
||||
* @param srcPath 需要拷贝的文件夹路径: D:/test
|
||||
* @param destPath 拷贝到的位置: D:/backup/
|
||||
*/
|
||||
func CopyDir(srcPath string, destPath string) error {
|
||||
// 检测目录正确性
|
||||
if srcInfo, err := os.Stat(srcPath); err != nil {
|
||||
fmt.Println(err.Error())
|
||||
return err
|
||||
} else {
|
||||
if !srcInfo.IsDir() {
|
||||
e := errors.New("srcPath不是一个正确的目录!")
|
||||
fmt.Println(e.Error())
|
||||
return e
|
||||
}
|
||||
}
|
||||
if destInfo, err := os.Stat(destPath); err != nil {
|
||||
fmt.Println(err.Error())
|
||||
return err
|
||||
} else {
|
||||
if !destInfo.IsDir() {
|
||||
e := errors.New("destInfo不是一个正确的目录!")
|
||||
fmt.Println(e.Error())
|
||||
return e
|
||||
}
|
||||
}
|
||||
|
||||
err := filepath.Walk(srcPath, func(path string, f os.FileInfo, err error) error {
|
||||
if f == nil {
|
||||
return err
|
||||
}
|
||||
if !f.IsDir() {
|
||||
path := strings.Replace(path, "\\", "/", -1)
|
||||
destNewPath := strings.Replace(path, srcPath, destPath, -1)
|
||||
_, _ = CopyFile(path, destNewPath)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
fmt.Printf(err.Error())
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// 生成目录并拷贝文件
|
||||
func CopyFile(src, dest string) (w int64, err error) {
|
||||
srcFile, err := os.Open(src)
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
return
|
||||
}
|
||||
defer srcFile.Close()
|
||||
// 分割path目录
|
||||
destSplitPathDirs := strings.Split(dest, "/")
|
||||
|
||||
// 检测时候存在目录
|
||||
destSplitPath := ""
|
||||
for index, dir := range destSplitPathDirs {
|
||||
if index < len(destSplitPathDirs)-1 {
|
||||
destSplitPath = destSplitPath + dir + "/"
|
||||
if !Exists(destSplitPath) {
|
||||
//创建目录
|
||||
err := os.Mkdir(destSplitPath, os.ModePerm)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
dstFile, err := os.Create(dest)
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
return
|
||||
}
|
||||
defer dstFile.Close()
|
||||
|
||||
return io.Copy(dstFile, srcFile)
|
||||
}
|
||||
|
||||
// 设置文件变量值
|
||||
// 可以理解为将文件中的变量占位符替换为想要设置的值
|
||||
func SetFileVariable(filePath string, key string, value string) error {
|
||||
// 占位符标识
|
||||
sep := "###"
|
||||
|
||||
// 读取文件到字节
|
||||
contentBytes, err := ioutil.ReadFile(filePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 将字节转化为文本
|
||||
content := string(contentBytes)
|
||||
|
||||
// 替换文本
|
||||
content = strings.ReplaceAll(content, fmt.Sprintf("%s%s%s", sep, key, sep), value)
|
||||
|
||||
// 打开文件
|
||||
f, err := os.OpenFile(filePath, os.O_WRONLY, 0777)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 将替换后的内容写入文件
|
||||
if _, err := f.Write([]byte(content)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
f.Close()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user