Skip to content

Commit 5c4567d

Browse files
committed
test: update test
1 parent 3249a5b commit 5c4567d

File tree

1 file changed

+13
-53
lines changed

1 file changed

+13
-53
lines changed

main_test.go

+13-53
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,25 @@
11
package main
22

33
import (
4-
"fmt"
5-
"github.com/IUnlimit/telegram-bot-disrecall/internal/tool"
6-
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
7-
"os"
8-
"regexp"
9-
"sync"
10-
"testing"
11-
"time"
12-
134
global "github.com/IUnlimit/telegram-bot-disrecall/internal"
145
"github.com/IUnlimit/telegram-bot-disrecall/internal/bot"
156
"github.com/IUnlimit/telegram-bot-disrecall/internal/conf"
167
"github.com/IUnlimit/telegram-bot-disrecall/internal/db"
178
"github.com/IUnlimit/telegram-bot-disrecall/internal/logger"
189
"github.com/IUnlimit/telegram-bot-disrecall/internal/model"
10+
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
1911
log "github.com/sirupsen/logrus"
12+
"sync"
13+
"testing"
2014
)
2115

2216
// go test -test.run TestMain
2317
func TestMain(m *testing.M) {
2418
conf.Init()
2519
logger.Init()
2620
db.Init()
27-
//updateDB()
28-
fetchFile()
21+
updateDB()
22+
//fetchFile()
2923
}
3024

3125
func fetchFile() {
@@ -52,57 +46,23 @@ func updateDB() {
5246
var wg sync.WaitGroup
5347
var fileModels []*model.FileModel
5448
// .Where("file_size = ?", 0)
55-
db.Instance.Where("file_type IN ('Video', 'Photo')").Find(&fileModels)
49+
db.Instance.Where("file_type IN ('Video', 'Photo') AND file_path = ''").Find(&fileModels)
5650
log.Infof("Start %d tasks", len(fileModels))
5751
for _, fileModel := range fileModels {
5852
wg.Add(1)
5953
go func(fileModel *model.FileModel) {
6054
defer wg.Done()
6155
testDownload(fileModel, basic)
62-
// db.Instance.Model(fileModel).Updates(model.FileModel{FileSize: int64(file.FileSize), FilePath: file.FilePath})
56+
6357
}(fileModel)
64-
break
58+
//break
6559
}
6660
wg.Wait()
6761
}
6862

69-
func testDownload(fileModel *model.FileModel, b *bot.BasicTGBot) {
70-
fileConfig := tgbotapi.FileConfig{FileID: fileModel.FileID}
71-
// TODO 文件过大时 http 超时, 无法获取 Info ?
72-
file, err := b.API.GetFile(fileConfig)
73-
if err != nil {
74-
log.Errorf("Fetch file info failed: %v", err)
75-
return
76-
}
77-
78-
//// 判断是否为本地服务器
79-
//if strings.HasPrefix(file.FilePath, "/") {
80-
// // 将绝对路径转换为相对路径
81-
// index := strings.Index(file.FilePath, b.API.Token)
82-
// file.FilePath = fmt.Sprintf(".%s", file.FilePath[index+len(b.API.Token):])
83-
//}
84-
85-
fileDirectURL := file.Link(b.API.Token)
86-
rootDir := global.Config.RootDir
87-
date := time.Now().Format("2006-01-02")
88-
fromUserID := fileModel.Json.Data().From.ID
89-
filePath := fmt.Sprintf("%s/%s/%d/%s", rootDir, date, fromUserID, file.FilePath)
90-
log.Debugf("FileDirectURL: %s", fileDirectURL)
91-
92-
// 替换文件名
93-
re := regexp.MustCompile(`([^/]+)\.([^.]+)$`)
94-
filePath = re.ReplaceAllString(filePath, file.FileUniqueID+".$2")
95-
96-
// 下载文件
97-
filePath, err = tool.DownloadFile(fileDirectURL, filePath)
98-
if err != nil {
99-
log.Errorf("File %s download failed: %v", fileDirectURL, err)
100-
_ = os.Remove(filePath)
101-
return
102-
}
103-
104-
log.Infof("File successfully download to '%s'", filePath)
105-
log.Infof("文件成功下载到: %s", filePath)
106-
107-
db.Instance.Model(&model.FileModel{}).Where("id = ?", fileModel.ID).Update("file_path", filePath)
63+
func testDownload(fileModel *model.FileModel, basic *bot.BasicTGBot) {
64+
basic.DownloadFile(fileModel.FileID, fileModel.Json.Data(), func(filePath string, fileSize int64) {
65+
fileModel.FilePath = filePath
66+
db.Instance.Model(fileModel).Updates(model.FileModel{FileSize: fileSize, FilePath: filePath})
67+
})
10868
}

0 commit comments

Comments
 (0)