Skip to content

Commit

Permalink
internal/download: move download logic to this package
Browse files Browse the repository at this point in the history
also disable http2 when downloading, may fix some issue

For #1563
  • Loading branch information
wdvxdr1123 committed Oct 13, 2022
1 parent 1dd12df commit f4117bf
Show file tree
Hide file tree
Showing 8 changed files with 328 additions and 307 deletions.
5 changes: 2 additions & 3 deletions cmd/gocq/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import (
"github.com/mattn/go-colorable"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/tidwall/gjson"

"github.com/Mrs4s/go-cqhttp/global"
"github.com/Mrs4s/go-cqhttp/internal/download"
)

var console = bufio.NewReader(os.Stdin)
Expand Down Expand Up @@ -243,12 +243,11 @@ func getTicket(u string) (str string) {
}

func fetchCaptcha(id string) string {
data, err := global.GetBytes("https://captcha.go-cqhttp.org/captcha/ticket?id=" + id)
g, err := download.Request{URL: "https://captcha.go-cqhttp.org/captcha/ticket?id=" + id}.JSON()
if err != nil {
log.Warnf("获取 Ticket 时出现错误: %v", err)
return ""
}
g := gjson.ParseBytes(data)
if g.Get("ticket").Exists() {
return g.Get("ticket").String()
}
Expand Down
12 changes: 5 additions & 7 deletions coolq/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/Mrs4s/go-cqhttp/global"
"github.com/Mrs4s/go-cqhttp/internal/base"
"github.com/Mrs4s/go-cqhttp/internal/cache"
"github.com/Mrs4s/go-cqhttp/internal/download"
"github.com/Mrs4s/go-cqhttp/internal/param"
"github.com/Mrs4s/go-cqhttp/modules/filter"
)
Expand Down Expand Up @@ -1545,12 +1546,8 @@ func (bot *CQBot) CQGetImage(file string) global.MSG {
}
local := path.Join(global.CachePath, file+path.Ext(msg["filename"].(string)))
if !global.PathExists(local) {
if body, err := global.HTTPGetReadCloser(msg["url"].(string)); err == nil {
f, _ := os.OpenFile(local, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o0644)
_, _ = f.ReadFrom(body)
_ = body.Close()
_ = f.Close()
} else {
r := download.Request{URL: msg["url"].(string)}
if err := r.WriteToFile(local); err != nil {
log.Warnf("下载图片 %v 时出现错误: %v", msg["url"], err)
return Failed(100, "DOWNLOAD_IMAGE_ERROR", err.Error())
}
Expand Down Expand Up @@ -1593,7 +1590,8 @@ func (bot *CQBot) CQDownloadFile(url string, headers gjson.Result, threadCount i
return Failed(100, "DELETE_FILE_ERROR", err.Error())
}
}
if err := global.DownloadFileMultiThreading(url, file, 0, threadCount, h); err != nil {
r := download.Request{URL: url, Header: h}
if err := r.WriteToFileMultiThreading(file, threadCount); err != nil {
log.Warnf("下载链接 %v 时出现错误: %v", url, err)
return Failed(100, "DOWNLOAD_FILE_ERROR", err.Error())
}
Expand Down
12 changes: 8 additions & 4 deletions coolq/cqcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/Mrs4s/go-cqhttp/global"
"github.com/Mrs4s/go-cqhttp/internal/base"
"github.com/Mrs4s/go-cqhttp/internal/cache"
"github.com/Mrs4s/go-cqhttp/internal/download"
"github.com/Mrs4s/go-cqhttp/internal/mime"
"github.com/Mrs4s/go-cqhttp/internal/param"
)
Expand Down Expand Up @@ -895,9 +896,9 @@ func (bot *CQBot) ToElement(t string, d map[string]string, sourceType message.So
name := info.Get("track_info.name").Str
mid := info.Get("track_info.mid").Str
albumMid := info.Get("track_info.album.mid").Str
pinfo, _ := global.GetBytes("http://u.y.qq.com/cgi-bin/musicu.fcg?g_tk=2034008533&uin=0&format=json&data={\"comm\":{\"ct\":23,\"cv\":0},\"url_mid\":{\"module\":\"vkey.GetVkeyServer\",\"method\":\"CgiGetVkey\",\"param\":{\"guid\":\"4311206557\",\"songmid\":[\"" + mid + "\"],\"songtype\":[0],\"uin\":\"0\",\"loginflag\":1,\"platform\":\"23\"}}}&_=1599039471576")
pinfo, _ := download.Request{URL: "http://u.y.qq.com/cgi-bin/musicu.fcg?g_tk=2034008533&uin=0&format=json&data={\"comm\":{\"ct\":23,\"cv\":0},\"url_mid\":{\"module\":\"vkey.GetVkeyServer\",\"method\":\"CgiGetVkey\",\"param\":{\"guid\":\"4311206557\",\"songmid\":[\"" + mid + "\"],\"songtype\":[0],\"uin\":\"0\",\"loginflag\":1,\"platform\":\"23\"}}}&_=1599039471576"}.JSON()
jumpURL := "https://i.y.qq.com/v8/playsong.html?platform=11&appshare=android_qq&appversion=10030010&hosteuin=oKnlNenz7i-s7c**&songmid=" + mid + "&type=0&appsongtype=1&_wv=1&source=qq&ADTAG=qfshare"
purl := gjson.ParseBytes(pinfo).Get("url_mid.data.midurlinfo.0.purl").Str
purl := pinfo.Get("url_mid.data.midurlinfo.0.purl").Str
preview := "http://y.gtimg.cn/music/photo_new/T002R180x180M000" + albumMid + ".jpg"
content := info.Get("track_info.singer.0.name").Str
if d["content"] != "" {
Expand Down Expand Up @@ -1089,8 +1090,11 @@ func (bot *CQBot) makeImageOrVideoElem(d map[string]string, video bool, sourceTy
if exist {
_ = os.Remove(cacheFile)
}
if err := global.DownloadFileMultiThreading(f, cacheFile, maxSize, thread, nil); err != nil {
return nil, err
{
r := download.Request{URL: f, Limit: maxSize}
if err := r.WriteToFileMultiThreading(cacheFile, thread); err != nil {
return nil, err
}
}
useCacheFile:
if video {
Expand Down
8 changes: 4 additions & 4 deletions coolq/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"os"
"path"
"strconv"
"strings"
Expand All @@ -18,6 +17,7 @@ import (
"github.com/Mrs4s/go-cqhttp/global"
"github.com/Mrs4s/go-cqhttp/internal/base"
"github.com/Mrs4s/go-cqhttp/internal/cache"
"github.com/Mrs4s/go-cqhttp/internal/download"
)

// ToFormattedMessage 将给定[]message.IMessageElement转换为通过coolq.SetMessageFormat所定义的消息上报格式
Expand Down Expand Up @@ -666,7 +666,8 @@ func (bot *CQBot) checkMedia(e []message.IMessageElement, sourceID int64) {
filename := hex.EncodeToString(i.Md5) + ".image"
cache.Image.Insert(i.Md5, data)
if i.Url != "" && !global.PathExists(path.Join(global.ImagePath, "guild-images", filename)) {
if err := global.DownloadFile(i.Url, path.Join(global.ImagePath, "guild-images", filename), -1, nil); err != nil {
r := download.Request{URL: i.Url}
if err := r.WriteToFile(path.Join(global.ImagePath, "guild-images", filename)); err != nil {
log.Warnf("下载频道图片时出现错误: %v", err)
}
}
Expand All @@ -684,12 +685,11 @@ func (bot *CQBot) checkMedia(e []message.IMessageElement, sourceID int64) {
i.Name = strings.ReplaceAll(i.Name, "{", "")
i.Name = strings.ReplaceAll(i.Name, "}", "")
if !global.PathExists(path.Join(global.VoicePath, i.Name)) {
b, err := global.GetBytes(i.Url)
err := download.Request{URL: i.Url}.WriteToFile(path.Join(global.VoicePath, i.Name))
if err != nil {
log.Warnf("语音文件 %v 下载失败: %v", i.Name, err)
continue
}
_ = os.WriteFile(path.Join(global.VoicePath, i.Name), b, 0o644)
}
case *message.ShortVideoElement:
data := binary.NewWriterF(func(w *binary.Writer) {
Expand Down
5 changes: 3 additions & 2 deletions global/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
b14 "github.com/fumiama/go-base16384"
"github.com/segmentio/asm/base64"
log "github.com/sirupsen/logrus"

"github.com/Mrs4s/go-cqhttp/internal/download"
)

const (
Expand Down Expand Up @@ -82,8 +84,7 @@ func FindFile(file, cache, p string) (data []byte, err error) {
if (cache == "" || cache == "1") && PathExists(cacheFile) {
return os.ReadFile(cacheFile)
}
data, err = GetBytes(file)
_ = os.WriteFile(cacheFile, data, 0o644)
err = download.Request{URL: file}.WriteToFile(cacheFile)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit f4117bf

Please sign in to comment.