✏️ 尝试解决win下路径异常

This commit is contained in:
fumiama 2021-10-24 18:13:12 +08:00
parent 3a5e391191
commit a3685e2e83
8 changed files with 49 additions and 22 deletions

View File

@ -3,7 +3,6 @@ package acgimage
import ( import (
"net/url" "net/url"
"os"
"strconv" "strconv"
"strings" "strings"
@ -13,6 +12,7 @@ import (
"github.com/wdvxdr1123/ZeroBot/message" "github.com/wdvxdr1123/ZeroBot/message"
"github.com/FloatTech/ZeroBot-Plugin/control" "github.com/FloatTech/ZeroBot-Plugin/control"
"github.com/FloatTech/ZeroBot-Plugin/utils/file"
) )
const ( const (
@ -21,9 +21,8 @@ const (
) )
var ( var (
botpath, _ = os.Getwd() datapath = file.BOT_PATH + "/data/acgimage/"
datapath = botpath + "/data/acgimage/" cacheuri = "file:///" + datapath + "cache"
cacheuri = "file:///" + datapath + "cache"
// r18有一定保护一般不会发出图片 // r18有一定保护一般不会发出图片
randapi = "&loli=true&r18=true" randapi = "&loli=true&r18=true"
msgof = make(map[int64]int64) msgof = make(map[int64]int64)

View File

@ -17,8 +17,7 @@ import (
"github.com/FloatTech/ZeroBot-Plugin/utils/file" "github.com/FloatTech/ZeroBot-Plugin/utils/file"
) )
var botpath, _ = os.Getwd() var cachedir = file.BOT_PATH + "/data/hs/"
var cachedir = botpath + "/data/hs/"
var header = req.Header{ 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`, "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`,

View File

@ -19,8 +19,7 @@ import (
) )
var ( var (
botpath, _ = os.Getwd() datapath = file.BOT_PATH + "/data/saucenao/"
datapath = botpath + "/data/saucenao/"
) )
func init() { // 插件主体 func init() { // 插件主体

View File

@ -18,6 +18,7 @@ import (
"github.com/wdvxdr1123/ZeroBot/message" "github.com/wdvxdr1123/ZeroBot/message"
"github.com/FloatTech/ZeroBot-Plugin/control" "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/math"
"github.com/FloatTech/ZeroBot-Plugin/utils/sql" "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 { func file(i *pixiv.Illust) string {
filename := fmt.Sprint(i.Pid) filename := fmt.Sprint(i.Pid)
pwd, _ := os.Getwd() filepath := fileutil.BOT_PATH + `/` + pool.Path + filename
filepath := pwd + `/` + pool.Path + filename
if _, err := os.Stat(filepath + ".jpg"); err == nil || os.IsExist(err) { if _, err := os.Stat(filepath + ".jpg"); err == nil || os.IsExist(err) {
return `file:///` + filepath + ".jpg" return `file:///` + filepath + ".jpg"
} }

11
utils/file/dir_nowin.go Normal file
View File

@ -0,0 +1,11 @@
//go:build !windows
// +build !windows
package file
import "os"
func Pwd() (path string) {
path, _ = os.Getwd()
return
}

14
utils/file/dir_win.go Normal file
View File

@ -0,0 +1,14 @@
//go:build windows
// +build windows
package file
import (
"os"
"strings"
)
func Pwd() string {
path, _ := os.Getwd()
return strings.ReplaceAll(path, "\\", "/")
}

View File

@ -21,15 +21,3 @@ func DownloadTo(url, file string) error {
} }
return err 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)
}

17
utils/file/f.go Normal file
View File

@ -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)
}