1
0
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:
wdvxdr 2021-07-29 15:18:50 +08:00
parent a3906a5d33
commit 86202d9d90
No known key found for this signature in database
GPG Key ID: 703F8C071DE7A1B6
8 changed files with 25 additions and 31 deletions

View File

@ -4,7 +4,6 @@ import (
"crypto/md5" "crypto/md5"
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"io/ioutil"
"math" "math"
"os" "os"
"path" "path"
@ -1001,7 +1000,7 @@ func (bot *CQBot) CQGetImage(file string) MSG {
if !global.PathExists(path.Join(global.ImagePath, file)) { if !global.PathExists(path.Join(global.ImagePath, file)) {
return Failed(100) return Failed(100)
} }
b, err := ioutil.ReadFile(path.Join(global.ImagePath, file)) b, err := os.ReadFile(path.Join(global.ImagePath, file))
if err == nil { if err == nil {
r := binary.NewReader(b) r := binary.NewReader(b)
r.ReadBytes(16) r.ReadBytes(16)

View File

@ -9,7 +9,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"math/rand" "math/rand"
"net/url" "net/url"
"os" "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) data, _ = global.FindFile(cover, cache, global.ImagePath)
} else { } else {
_ = global.ExtractCover(v.File, v.File+".jpg") _ = global.ExtractCover(v.File, v.File+".jpg")
data, _ = ioutil.ReadFile(v.File + ".jpg") data, _ = os.ReadFile(v.File + ".jpg")
} }
v.thumb = bytes.NewReader(data) v.thumb = bytes.NewReader(data)
video, _ := os.Open(v.File) 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") return nil, errors.New("invalid video")
} }
if path.Ext(rawPath) == ".video" { if path.Ext(rawPath) == ".video" {
b, _ := ioutil.ReadFile(rawPath) b, _ := os.ReadFile(rawPath)
r := binary.NewReader(b) r := binary.NewReader(b)
return &LocalVideoElement{ShortVideoElement: message.ShortVideoElement{ // todo 检查缓存是否有效 return &LocalVideoElement{ShortVideoElement: message.ShortVideoElement{ // todo 检查缓存是否有效
Md5: r.ReadBytes(16), Md5: r.ReadBytes(16),

View File

@ -2,7 +2,7 @@ package coolq
import ( import (
"encoding/hex" "encoding/hex"
"io/ioutil" "os"
"path" "path"
"strconv" "strconv"
"strings" "strings"
@ -545,7 +545,7 @@ func (bot *CQBot) checkMedia(e []message.IMessageElement) {
case *message.GroupImageElement: case *message.GroupImageElement:
filename := hex.EncodeToString(i.Md5) + ".image" filename := hex.EncodeToString(i.Md5) + ".image"
if !global.PathExists(path.Join(global.ImagePath, filename)) { 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.Write(i.Md5)
w.WriteUInt32(uint32(i.Size)) w.WriteUInt32(uint32(i.Size))
w.WriteString(i.ImageId) w.WriteString(i.ImageId)
@ -555,7 +555,7 @@ func (bot *CQBot) checkMedia(e []message.IMessageElement) {
case *message.FriendImageElement: case *message.FriendImageElement:
filename := hex.EncodeToString(i.Md5) + ".image" filename := hex.EncodeToString(i.Md5) + ".image"
if !global.PathExists(path.Join(global.ImagePath, filename)) { 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.Write(i.Md5)
w.WriteUInt32(uint32(i.Size)) w.WriteUInt32(uint32(i.Size))
w.WriteString(i.ImageId) w.WriteString(i.ImageId)
@ -571,12 +571,12 @@ func (bot *CQBot) checkMedia(e []message.IMessageElement) {
log.Warnf("语音文件 %v 下载失败: %v", i.Name, err) log.Warnf("语音文件 %v 下载失败: %v", i.Name, err)
continue continue
} }
_ = ioutil.WriteFile(path.Join(global.VoicePath, i.Name), b, 0o644) _ = os.WriteFile(path.Join(global.VoicePath, i.Name), b, 0o644)
} }
case *message.ShortVideoElement: case *message.ShortVideoElement:
filename := hex.EncodeToString(i.Md5) + ".video" filename := hex.EncodeToString(i.Md5) + ".video"
if !global.PathExists(path.Join(global.VideoPath, filename)) { 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.Md5)
w.Write(i.ThumbMd5) w.Write(i.ThumbMd5)
w.WriteUInt32(uint32(i.Size)) w.WriteUInt32(uint32(i.Size))

View File

@ -3,7 +3,7 @@ package global
import ( import (
"crypto/md5" "crypto/md5"
"fmt" "fmt"
"io/ioutil" "os"
"os/exec" "os/exec"
"path" "path"
@ -21,7 +21,7 @@ func EncoderSilk(data []byte) ([]byte, error) {
} }
tempName := fmt.Sprintf("%x", h.Sum(nil)) tempName := fmt.Sprintf("%x", h.Sum(nil))
if silkPath := path.Join("data/cache", tempName+".silk"); PathExists(silkPath) { 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) slk, err := codec.EncodeToSilk(data, tempName, true)
if err != nil { if err != nil {

View File

@ -6,7 +6,6 @@
package codec package codec
import ( import (
"io/ioutil"
"os" "os"
"os/exec" "os/exec"
"path" "path"
@ -21,7 +20,7 @@ const silkCachePath = "data/cache"
func EncodeToSilk(record []byte, tempName string, useCache bool) (silkWav []byte, err error) { func EncodeToSilk(record []byte, tempName string, useCache bool) (silkWav []byte, err error) {
// 1. 写入缓存文件 // 1. 写入缓存文件
rawPath := path.Join(silkCachePath, tempName+".wav") rawPath := path.Join(silkCachePath, tempName+".wav")
err = ioutil.WriteFile(rawPath, record, os.ModePerm) err = os.WriteFile(rawPath, record, os.ModePerm)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "write temp file error") 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) defer os.Remove(pcmPath)
// 3. 转silk // 3. 转silk
pcm, err := ioutil.ReadFile(pcmPath) pcm, err := os.ReadFile(pcmPath)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "read pcm file err") 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 { if useCache {
silkPath := path.Join(silkCachePath, tempName+".silk") silkPath := path.Join(silkCachePath, tempName+".silk")
err = ioutil.WriteFile(silkPath, silkWav, 0o666) err = os.WriteFile(silkPath, silkWav, 0o666)
} }
return return
} }

View File

@ -6,7 +6,6 @@ import (
"encoding/base64" "encoding/base64"
"encoding/hex" "encoding/hex"
"errors" "errors"
"io/ioutil"
"net" "net"
"net/url" "net/url"
"os" "os"
@ -51,7 +50,7 @@ func PathExists(path string) bool {
// ReadAllText 读取给定path对应文件无法读取时返回空值 // ReadAllText 读取给定path对应文件无法读取时返回空值
func ReadAllText(path string) string { func ReadAllText(path string) string {
b, err := ioutil.ReadFile(path) b, err := os.ReadFile(path)
if err != nil { if err != nil {
log.Error(err) log.Error(err)
return "" return ""
@ -61,7 +60,7 @@ func ReadAllText(path string) string {
// WriteAllText 将给定text写入给定path // WriteAllText 将给定text写入给定path
func WriteAllText(path, text string) error { 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 // Check 检测err是否为nil
@ -91,10 +90,10 @@ func FindFile(file, cache, p string) (data []byte, err error) {
hash := md5.Sum([]byte(file)) hash := md5.Sum([]byte(file))
cacheFile := path.Join(CachePath, hex.EncodeToString(hash[:])+".cache") cacheFile := path.Join(CachePath, hex.EncodeToString(hash[:])+".cache")
if PathExists(cacheFile) && cache == "1" { if PathExists(cacheFile) && cache == "1" {
return ioutil.ReadFile(cacheFile) return os.ReadFile(cacheFile)
} }
data, err = GetBytes(file) data, err = GetBytes(file)
_ = ioutil.WriteFile(cacheFile, data, 0o644) _ = os.WriteFile(cacheFile, data, 0o644)
if err != nil { if err != nil {
return nil, err 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` { if strings.HasPrefix(fu.Path, "/") && runtime.GOOS == `windows` {
fu.Path = fu.Path[1:] fu.Path = fu.Path[1:]
} }
data, err = ioutil.ReadFile(fu.Path) data, err = os.ReadFile(fu.Path)
if err != nil { if err != nil {
return nil, err return nil, err
} }
case PathExists(path.Join(p, file)): 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 { if err != nil {
return nil, err return nil, err
} }
@ -140,7 +139,7 @@ func DelFile(path string) bool {
// ReadAddrFile 从给定path中读取合法的IP地址与端口,每个IP地址以换行符"\n"作为分隔 // ReadAddrFile 从给定path中读取合法的IP地址与端口,每个IP地址以换行符"\n"作为分隔
func ReadAddrFile(path string) []*net.TCPAddr { func ReadAddrFile(path string) []*net.TCPAddr {
d, err := ioutil.ReadFile(path) d, err := os.ReadFile(path)
if err != nil { if err != nil {
return nil return nil
} }

View File

@ -3,7 +3,6 @@ package main
import ( import (
"bufio" "bufio"
"bytes" "bytes"
"io/ioutil"
"os" "os"
"strings" "strings"
"time" "time"
@ -63,7 +62,7 @@ func qrcodeLogin() error {
if err != nil { if err != nil {
return err return err
} }
_ = ioutil.WriteFile("qrcode.png", rsp.ImageData, 0o644) _ = os.WriteFile("qrcode.png", rsp.ImageData, 0o644)
defer func() { _ = os.Remove("qrcode.png") }() defer func() { _ = os.Remove("qrcode.png") }()
if cli.Uin != 0 { if cli.Uin != 0 {
log.Infof("请使用账号 %v 登录手机QQ扫描二维码 (qrcode.png) : ", cli.Uin) log.Infof("请使用账号 %v 登录手机QQ扫描二维码 (qrcode.png) : ", cli.Uin)
@ -139,7 +138,7 @@ func loginResponseProcessor(res *client.LoginResponse) error {
return qrcodeLogin() return qrcodeLogin()
case client.NeedCaptcha: case client.NeedCaptcha:
log.Warnf("登录需要验证码.") log.Warnf("登录需要验证码.")
_ = ioutil.WriteFile("captcha.jpg", res.CaptchaImage, 0o644) _ = os.WriteFile("captcha.jpg", res.CaptchaImage, 0o644)
log.Warnf("请输入验证码 (captcha.jpg) (Enter 提交)") log.Warnf("请输入验证码 (captcha.jpg) (Enter 提交)")
text = readLine() text = readLine()
global.DelFile("captcha.jpg") global.DelFile("captcha.jpg")

View File

@ -8,7 +8,6 @@ import (
"encoding/hex" "encoding/hex"
"flag" "flag"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"os/exec" "os/exec"
"path" "path"
@ -161,7 +160,7 @@ func main() {
if !global.PathExists("device.json") { if !global.PathExists("device.json") {
log.Warn("虚拟设备信息不存在, 将自动生成随机设备.") log.Warn("虚拟设备信息不存在, 将自动生成随机设备.")
client.GenRandomDevice() client.GenRandomDevice()
_ = ioutil.WriteFile("device.json", client.SystemDeviceInfo.ToJson(), 0o644) _ = os.WriteFile("device.json", client.SystemDeviceInfo.ToJson(), 0o644)
log.Info("已生成设备信息并保存到 device.json 文件.") log.Info("已生成设备信息并保存到 device.json 文件.")
} else { } else {
log.Info("将使用 device.json 内的设备信息运行Bot.") log.Info("将使用 device.json 内的设备信息运行Bot.")
@ -248,10 +247,10 @@ func main() {
isTokenLogin := false isTokenLogin := false
saveToken := func() { saveToken := func() {
AccountToken = cli.GenToken() AccountToken = cli.GenToken()
_ = ioutil.WriteFile("session.token", AccountToken, 0o644) _ = os.WriteFile("session.token", AccountToken, 0o644)
} }
if global.PathExists("session.token") { if global.PathExists("session.token") {
token, err := ioutil.ReadFile("session.token") token, err := os.ReadFile("session.token")
if err == nil { if err == nil {
if conf.Account.Uin != 0 { if conf.Account.Uin != 0 {
r := binary.NewReader(token) r := binary.NewReader(token)