From a3685e2e8323ee9862d9b8d188a6a7396d175f9d Mon Sep 17 00:00:00 2001 From: fumiama Date: Sun, 24 Oct 2021 18:13:12 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=8F=EF=B8=8F=20=20=E5=B0=9D=E8=AF=95?= =?UTF-8?q?=E8=A7=A3=E5=86=B3win=E4=B8=8B=E8=B7=AF=E5=BE=84=E5=BC=82?= =?UTF-8?q?=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugin_acgimage/classify.go | 7 +++---- plugin_hs/run.go | 3 +-- plugin_saucenao/searcher.go | 3 +-- plugin_setutime/setu_geter.go | 4 ++-- utils/file/dir_nowin.go | 11 +++++++++++ utils/file/dir_win.go | 14 ++++++++++++++ utils/file/dl.go | 12 ------------ utils/file/f.go | 17 +++++++++++++++++ 8 files changed, 49 insertions(+), 22 deletions(-) create mode 100644 utils/file/dir_nowin.go create mode 100644 utils/file/dir_win.go create mode 100644 utils/file/f.go 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) +}