diff --git a/plugin_acgimage/classify.go b/plugin_acgimage/classify.go index 27094dcc..0c060f92 100644 --- a/plugin_acgimage/classify.go +++ b/plugin_acgimage/classify.go @@ -3,7 +3,6 @@ package acgimage import ( "net/url" - "os" "strconv" "strings" @@ -13,6 +12,7 @@ import ( "github.com/wdvxdr1123/ZeroBot/message" "github.com/FloatTech/ZeroBot-Plugin/control" + "github.com/FloatTech/ZeroBot-Plugin/utils/file" ) const ( @@ -21,9 +21,8 @@ const ( ) var ( - botpath, _ = os.Getwd() - datapath = botpath + "/data/acgimage/" - cacheuri = "file:///" + datapath + "cache" + datapath = file.BOT_PATH + "/data/acgimage/" + cacheuri = "file:///" + datapath + "cache" // r18有一定保护,一般不会发出图片 randapi = "&loli=true&r18=true" msgof = make(map[int64]int64) diff --git a/plugin_hs/run.go b/plugin_hs/run.go index 9b82cd1b..be839055 100644 --- a/plugin_hs/run.go +++ b/plugin_hs/run.go @@ -17,8 +17,7 @@ import ( "github.com/FloatTech/ZeroBot-Plugin/utils/file" ) -var botpath, _ = os.Getwd() -var cachedir = botpath + "/data/hs/" +var cachedir = file.BOT_PATH + "/data/hs/" var header = req.Header{ "user-agent": `Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Mobile Safari/537.36`, diff --git a/plugin_saucenao/searcher.go b/plugin_saucenao/searcher.go index 12704704..651415fe 100644 --- a/plugin_saucenao/searcher.go +++ b/plugin_saucenao/searcher.go @@ -19,8 +19,7 @@ import ( ) var ( - botpath, _ = os.Getwd() - datapath = botpath + "/data/saucenao/" + datapath = file.BOT_PATH + "/data/saucenao/" ) func init() { // 插件主体 diff --git a/plugin_setutime/setu_geter.go b/plugin_setutime/setu_geter.go index d92c4067..68e31cd9 100644 --- a/plugin_setutime/setu_geter.go +++ b/plugin_setutime/setu_geter.go @@ -18,6 +18,7 @@ import ( "github.com/wdvxdr1123/ZeroBot/message" "github.com/FloatTech/ZeroBot-Plugin/control" + fileutil "github.com/FloatTech/ZeroBot-Plugin/utils/file" "github.com/FloatTech/ZeroBot-Plugin/utils/math" "github.com/FloatTech/ZeroBot-Plugin/utils/sql" ) @@ -249,8 +250,7 @@ func (p *imgpool) pop(imgtype string) (illust *pixiv.Illust) { func file(i *pixiv.Illust) string { filename := fmt.Sprint(i.Pid) - pwd, _ := os.Getwd() - filepath := pwd + `/` + pool.Path + filename + filepath := fileutil.BOT_PATH + `/` + pool.Path + filename if _, err := os.Stat(filepath + ".jpg"); err == nil || os.IsExist(err) { return `file:///` + filepath + ".jpg" } diff --git a/utils/file/dir_nowin.go b/utils/file/dir_nowin.go new file mode 100644 index 00000000..a4523c40 --- /dev/null +++ b/utils/file/dir_nowin.go @@ -0,0 +1,11 @@ +//go:build !windows +// +build !windows + +package file + +import "os" + +func Pwd() (path string) { + path, _ = os.Getwd() + return +} diff --git a/utils/file/dir_win.go b/utils/file/dir_win.go new file mode 100644 index 00000000..a1dd4708 --- /dev/null +++ b/utils/file/dir_win.go @@ -0,0 +1,14 @@ +//go:build windows +// +build windows + +package file + +import ( + "os" + "strings" +) + +func Pwd() string { + path, _ := os.Getwd() + return strings.ReplaceAll(path, "\\", "/") +} diff --git a/utils/file/dl.go b/utils/file/dl.go index 8b204d1e..1caea7e6 100644 --- a/utils/file/dl.go +++ b/utils/file/dl.go @@ -21,15 +21,3 @@ func DownloadTo(url, file string) error { } return err } - -// IsExist 文件/路径存在 -func IsExist(path string) bool { - _, err := os.Stat(path) - return err == nil || os.IsExist(err) -} - -// IsExist 文件/路径不存在 -func IsNotExist(path string) bool { - _, err := os.Stat(path) - return err != nil && os.IsNotExist(err) -} diff --git a/utils/file/f.go b/utils/file/f.go new file mode 100644 index 00000000..c38b0069 --- /dev/null +++ b/utils/file/f.go @@ -0,0 +1,17 @@ +package file + +import "os" + +var BOT_PATH = Pwd() + +// IsExist 文件/路径存在 +func IsExist(path string) bool { + _, err := os.Stat(path) + return err == nil || os.IsExist(err) +} + +// IsExist 文件/路径不存在 +func IsNotExist(path string) bool { + _, err := os.Stat(path) + return err != nil && os.IsNotExist(err) +}