mirror of
https://github.com/Mrs4s/go-cqhttp.git
synced 2025-05-04 19:17:37 +08:00
style: clean up io/ioutil.
rf ' ex { import "io/ioutil"; import "os"; ioutil.WriteFile -> os.WriteFile ioutil.ReadFile -> os.ReadFile } '
This commit is contained in:
parent
a3906a5d33
commit
86202d9d90
@ -4,7 +4,6 @@ import (
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"math"
|
||||
"os"
|
||||
"path"
|
||||
@ -1001,7 +1000,7 @@ func (bot *CQBot) CQGetImage(file string) MSG {
|
||||
if !global.PathExists(path.Join(global.ImagePath, file)) {
|
||||
return Failed(100)
|
||||
}
|
||||
b, err := ioutil.ReadFile(path.Join(global.ImagePath, file))
|
||||
b, err := os.ReadFile(path.Join(global.ImagePath, file))
|
||||
if err == nil {
|
||||
r := binary.NewReader(b)
|
||||
r.ReadBytes(16)
|
||||
|
@ -9,7 +9,6 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"math/rand"
|
||||
"net/url"
|
||||
"os"
|
||||
@ -897,7 +896,7 @@ func (bot *CQBot) ToElement(t string, d map[string]string, isGroup bool) (m inte
|
||||
data, _ = global.FindFile(cover, cache, global.ImagePath)
|
||||
} else {
|
||||
_ = global.ExtractCover(v.File, v.File+".jpg")
|
||||
data, _ = ioutil.ReadFile(v.File + ".jpg")
|
||||
data, _ = os.ReadFile(v.File + ".jpg")
|
||||
}
|
||||
v.thumb = bytes.NewReader(data)
|
||||
video, _ := os.Open(v.File)
|
||||
@ -1081,7 +1080,7 @@ func (bot *CQBot) makeImageOrVideoElem(d map[string]string, video, group bool) (
|
||||
return nil, errors.New("invalid video")
|
||||
}
|
||||
if path.Ext(rawPath) == ".video" {
|
||||
b, _ := ioutil.ReadFile(rawPath)
|
||||
b, _ := os.ReadFile(rawPath)
|
||||
r := binary.NewReader(b)
|
||||
return &LocalVideoElement{ShortVideoElement: message.ShortVideoElement{ // todo 检查缓存是否有效
|
||||
Md5: r.ReadBytes(16),
|
||||
|
@ -2,7 +2,7 @@ package coolq
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"strconv"
|
||||
"strings"
|
||||
@ -545,7 +545,7 @@ func (bot *CQBot) checkMedia(e []message.IMessageElement) {
|
||||
case *message.GroupImageElement:
|
||||
filename := hex.EncodeToString(i.Md5) + ".image"
|
||||
if !global.PathExists(path.Join(global.ImagePath, filename)) {
|
||||
_ = ioutil.WriteFile(path.Join(global.ImagePath, filename), binary.NewWriterF(func(w *binary.Writer) {
|
||||
_ = os.WriteFile(path.Join(global.ImagePath, filename), binary.NewWriterF(func(w *binary.Writer) {
|
||||
w.Write(i.Md5)
|
||||
w.WriteUInt32(uint32(i.Size))
|
||||
w.WriteString(i.ImageId)
|
||||
@ -555,7 +555,7 @@ func (bot *CQBot) checkMedia(e []message.IMessageElement) {
|
||||
case *message.FriendImageElement:
|
||||
filename := hex.EncodeToString(i.Md5) + ".image"
|
||||
if !global.PathExists(path.Join(global.ImagePath, filename)) {
|
||||
_ = ioutil.WriteFile(path.Join(global.ImagePath, filename), binary.NewWriterF(func(w *binary.Writer) {
|
||||
_ = os.WriteFile(path.Join(global.ImagePath, filename), binary.NewWriterF(func(w *binary.Writer) {
|
||||
w.Write(i.Md5)
|
||||
w.WriteUInt32(uint32(i.Size))
|
||||
w.WriteString(i.ImageId)
|
||||
@ -571,12 +571,12 @@ func (bot *CQBot) checkMedia(e []message.IMessageElement) {
|
||||
log.Warnf("语音文件 %v 下载失败: %v", i.Name, err)
|
||||
continue
|
||||
}
|
||||
_ = ioutil.WriteFile(path.Join(global.VoicePath, i.Name), b, 0o644)
|
||||
_ = os.WriteFile(path.Join(global.VoicePath, i.Name), b, 0o644)
|
||||
}
|
||||
case *message.ShortVideoElement:
|
||||
filename := hex.EncodeToString(i.Md5) + ".video"
|
||||
if !global.PathExists(path.Join(global.VideoPath, filename)) {
|
||||
_ = ioutil.WriteFile(path.Join(global.VideoPath, filename), binary.NewWriterF(func(w *binary.Writer) {
|
||||
_ = os.WriteFile(path.Join(global.VideoPath, filename), binary.NewWriterF(func(w *binary.Writer) {
|
||||
w.Write(i.Md5)
|
||||
w.Write(i.ThumbMd5)
|
||||
w.WriteUInt32(uint32(i.Size))
|
||||
|
@ -3,7 +3,7 @@ package global
|
||||
import (
|
||||
"crypto/md5"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
|
||||
@ -21,7 +21,7 @@ func EncoderSilk(data []byte) ([]byte, error) {
|
||||
}
|
||||
tempName := fmt.Sprintf("%x", h.Sum(nil))
|
||||
if silkPath := path.Join("data/cache", tempName+".silk"); PathExists(silkPath) {
|
||||
return ioutil.ReadFile(silkPath)
|
||||
return os.ReadFile(silkPath)
|
||||
}
|
||||
slk, err := codec.EncodeToSilk(data, tempName, true)
|
||||
if err != nil {
|
||||
|
@ -6,7 +6,6 @@
|
||||
package codec
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
@ -21,7 +20,7 @@ const silkCachePath = "data/cache"
|
||||
func EncodeToSilk(record []byte, tempName string, useCache bool) (silkWav []byte, err error) {
|
||||
// 1. 写入缓存文件
|
||||
rawPath := path.Join(silkCachePath, tempName+".wav")
|
||||
err = ioutil.WriteFile(rawPath, record, os.ModePerm)
|
||||
err = os.WriteFile(rawPath, record, os.ModePerm)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "write temp file error")
|
||||
}
|
||||
@ -36,7 +35,7 @@ func EncodeToSilk(record []byte, tempName string, useCache bool) (silkWav []byte
|
||||
defer os.Remove(pcmPath)
|
||||
|
||||
// 3. 转silk
|
||||
pcm, err := ioutil.ReadFile(pcmPath)
|
||||
pcm, err := os.ReadFile(pcmPath)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "read pcm file err")
|
||||
}
|
||||
@ -46,7 +45,7 @@ func EncodeToSilk(record []byte, tempName string, useCache bool) (silkWav []byte
|
||||
}
|
||||
if useCache {
|
||||
silkPath := path.Join(silkCachePath, tempName+".silk")
|
||||
err = ioutil.WriteFile(silkPath, silkWav, 0o666)
|
||||
err = os.WriteFile(silkPath, silkWav, 0o666)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
15
global/fs.go
15
global/fs.go
@ -6,7 +6,6 @@ import (
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"net/url"
|
||||
"os"
|
||||
@ -51,7 +50,7 @@ func PathExists(path string) bool {
|
||||
|
||||
// ReadAllText 读取给定path对应文件,无法读取时返回空值
|
||||
func ReadAllText(path string) string {
|
||||
b, err := ioutil.ReadFile(path)
|
||||
b, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return ""
|
||||
@ -61,7 +60,7 @@ func ReadAllText(path string) string {
|
||||
|
||||
// WriteAllText 将给定text写入给定path
|
||||
func WriteAllText(path, text string) error {
|
||||
return ioutil.WriteFile(path, utils.S2B(text), 0o644)
|
||||
return os.WriteFile(path, utils.S2B(text), 0o644)
|
||||
}
|
||||
|
||||
// Check 检测err是否为nil
|
||||
@ -91,10 +90,10 @@ func FindFile(file, cache, p string) (data []byte, err error) {
|
||||
hash := md5.Sum([]byte(file))
|
||||
cacheFile := path.Join(CachePath, hex.EncodeToString(hash[:])+".cache")
|
||||
if PathExists(cacheFile) && cache == "1" {
|
||||
return ioutil.ReadFile(cacheFile)
|
||||
return os.ReadFile(cacheFile)
|
||||
}
|
||||
data, err = GetBytes(file)
|
||||
_ = ioutil.WriteFile(cacheFile, data, 0o644)
|
||||
_ = os.WriteFile(cacheFile, data, 0o644)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -112,12 +111,12 @@ func FindFile(file, cache, p string) (data []byte, err error) {
|
||||
if strings.HasPrefix(fu.Path, "/") && runtime.GOOS == `windows` {
|
||||
fu.Path = fu.Path[1:]
|
||||
}
|
||||
data, err = ioutil.ReadFile(fu.Path)
|
||||
data, err = os.ReadFile(fu.Path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
case PathExists(path.Join(p, file)):
|
||||
data, err = ioutil.ReadFile(path.Join(p, file))
|
||||
data, err = os.ReadFile(path.Join(p, file))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -140,7 +139,7 @@ func DelFile(path string) bool {
|
||||
|
||||
// ReadAddrFile 从给定path中读取合法的IP地址与端口,每个IP地址以换行符"\n"作为分隔
|
||||
func ReadAddrFile(path string) []*net.TCPAddr {
|
||||
d, err := ioutil.ReadFile(path)
|
||||
d, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
5
login.go
5
login.go
@ -3,7 +3,6 @@ package main
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
@ -63,7 +62,7 @@ func qrcodeLogin() error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_ = ioutil.WriteFile("qrcode.png", rsp.ImageData, 0o644)
|
||||
_ = os.WriteFile("qrcode.png", rsp.ImageData, 0o644)
|
||||
defer func() { _ = os.Remove("qrcode.png") }()
|
||||
if cli.Uin != 0 {
|
||||
log.Infof("请使用账号 %v 登录手机QQ扫描二维码 (qrcode.png) : ", cli.Uin)
|
||||
@ -139,7 +138,7 @@ func loginResponseProcessor(res *client.LoginResponse) error {
|
||||
return qrcodeLogin()
|
||||
case client.NeedCaptcha:
|
||||
log.Warnf("登录需要验证码.")
|
||||
_ = ioutil.WriteFile("captcha.jpg", res.CaptchaImage, 0o644)
|
||||
_ = os.WriteFile("captcha.jpg", res.CaptchaImage, 0o644)
|
||||
log.Warnf("请输入验证码 (captcha.jpg): (Enter 提交)")
|
||||
text = readLine()
|
||||
global.DelFile("captcha.jpg")
|
||||
|
7
main.go
7
main.go
@ -8,7 +8,6 @@ import (
|
||||
"encoding/hex"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
@ -161,7 +160,7 @@ func main() {
|
||||
if !global.PathExists("device.json") {
|
||||
log.Warn("虚拟设备信息不存在, 将自动生成随机设备.")
|
||||
client.GenRandomDevice()
|
||||
_ = ioutil.WriteFile("device.json", client.SystemDeviceInfo.ToJson(), 0o644)
|
||||
_ = os.WriteFile("device.json", client.SystemDeviceInfo.ToJson(), 0o644)
|
||||
log.Info("已生成设备信息并保存到 device.json 文件.")
|
||||
} else {
|
||||
log.Info("将使用 device.json 内的设备信息运行Bot.")
|
||||
@ -248,10 +247,10 @@ func main() {
|
||||
isTokenLogin := false
|
||||
saveToken := func() {
|
||||
AccountToken = cli.GenToken()
|
||||
_ = ioutil.WriteFile("session.token", AccountToken, 0o644)
|
||||
_ = os.WriteFile("session.token", AccountToken, 0o644)
|
||||
}
|
||||
if global.PathExists("session.token") {
|
||||
token, err := ioutil.ReadFile("session.token")
|
||||
token, err := os.ReadFile("session.token")
|
||||
if err == nil {
|
||||
if conf.Account.Uin != 0 {
|
||||
r := binary.NewReader(token)
|
||||
|
Loading…
x
Reference in New Issue
Block a user