1
1
package main
2
2
3
3
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
-
13
4
global "github.com/IUnlimit/telegram-bot-disrecall/internal"
14
5
"github.com/IUnlimit/telegram-bot-disrecall/internal/bot"
15
6
"github.com/IUnlimit/telegram-bot-disrecall/internal/conf"
16
7
"github.com/IUnlimit/telegram-bot-disrecall/internal/db"
17
8
"github.com/IUnlimit/telegram-bot-disrecall/internal/logger"
18
9
"github.com/IUnlimit/telegram-bot-disrecall/internal/model"
10
+ tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
19
11
log "github.com/sirupsen/logrus"
12
+ "sync"
13
+ "testing"
20
14
)
21
15
22
16
// go test -test.run TestMain
23
17
func TestMain (m * testing.M ) {
24
18
conf .Init ()
25
19
logger .Init ()
26
20
db .Init ()
27
- // updateDB()
28
- fetchFile ()
21
+ updateDB ()
22
+ // fetchFile()
29
23
}
30
24
31
25
func fetchFile () {
@@ -52,57 +46,23 @@ func updateDB() {
52
46
var wg sync.WaitGroup
53
47
var fileModels []* model.FileModel
54
48
// .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 )
56
50
log .Infof ("Start %d tasks" , len (fileModels ))
57
51
for _ , fileModel := range fileModels {
58
52
wg .Add (1 )
59
53
go func (fileModel * model.FileModel ) {
60
54
defer wg .Done ()
61
55
testDownload (fileModel , basic )
62
- // db.Instance.Model(fileModel).Updates(model.FileModel{FileSize: int64(file.FileSize), FilePath: file.FilePath})
56
+
63
57
}(fileModel )
64
- break
58
+ // break
65
59
}
66
60
wg .Wait ()
67
61
}
68
62
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
+ })
108
68
}
0 commit comments