7
7
"ManyACG/storage"
8
8
"ManyACG/types"
9
9
"bytes"
10
+ "runtime"
10
11
"time"
11
12
12
13
. "ManyACG/logger"
@@ -25,29 +26,11 @@ func PostArtwork(bot *telego.Bot, artwork *types.Artwork, storage storage.Storag
25
26
return nil , errors .ErrNilArtwork
26
27
}
27
28
28
- inputMediaPhotos := make ([]telego.InputMedia , len (artwork .Pictures ))
29
- for i , picture := range artwork .Pictures {
30
- fileBytes , err := storage .GetFile (picture .StorageInfo )
31
- if err != nil {
32
- Logger .Errorf ("failed to get file: %s" , err )
33
- return nil , err
34
- }
35
- fileBytes , err = common .CompressImage (fileBytes , 10 , 2560 )
29
+ if len (artwork .Pictures ) <= 10 {
30
+ inputMediaPhotos , err := getInputMediaPhotos (artwork , storage , 0 , len (artwork .Pictures ))
36
31
if err != nil {
37
- Logger .Errorf ("failed to compress image: %s" , err )
38
32
return nil , err
39
33
}
40
- photo := telegoutil .MediaPhoto (telegoutil .File (telegoutil .NameReader (bytes .NewReader (fileBytes ), picture .StorageInfo .Path )))
41
- if i == 0 {
42
- photo = photo .WithCaption (GetArtworkHTMLCaption (artwork )).WithParseMode (telego .ModeHTML )
43
- }
44
- if artwork .R18 {
45
- photo = photo .WithHasSpoiler ()
46
- }
47
- inputMediaPhotos [i ] = photo
48
- }
49
-
50
- if len (inputMediaPhotos ) <= 10 {
51
34
return bot .SendMediaGroup (
52
35
telegoutil .MediaGroup (
53
36
ChannelChatID ,
@@ -56,16 +39,17 @@ func PostArtwork(bot *telego.Bot, artwork *types.Artwork, storage storage.Storag
56
39
)
57
40
}
58
41
59
- allMessages := make ([]telego.Message , len (inputMediaPhotos ))
60
- for i := 0 ; i < len (inputMediaPhotos ); i += 10 {
42
+ allMessages := make ([]telego.Message , len (artwork . Pictures ))
43
+ for i := 0 ; i < len (artwork . Pictures ); i += 10 {
61
44
end := i + 10
62
- if end > len (inputMediaPhotos ) {
63
- end = len (inputMediaPhotos )
45
+ if end > len (artwork . Pictures ) {
46
+ end = len (artwork . Pictures )
64
47
}
65
- mediaGroup := telegoutil .MediaGroup (
66
- ChannelChatID ,
67
- inputMediaPhotos [i :end ]... ,
68
- )
48
+ inputMediaPhotos , err := getInputMediaPhotos (artwork , storage , i , end )
49
+ if err != nil {
50
+ return nil , err
51
+ }
52
+ mediaGroup := telegoutil .MediaGroup (ChannelChatID , inputMediaPhotos ... )
69
53
if i > 0 {
70
54
mediaGroup = mediaGroup .WithReplyParameters (& telego.ReplyParameters {
71
55
ChatID : ChannelChatID ,
@@ -77,7 +61,36 @@ func PostArtwork(bot *telego.Bot, artwork *types.Artwork, storage storage.Storag
77
61
return nil , err
78
62
}
79
63
copy (allMessages [i :], messages )
80
- time .Sleep (time .Duration (int (config .Cfg .Telegram .Sleep )* len (inputMediaPhotos [ i : end ] )) * time .Second )
64
+ time .Sleep (time .Duration (int (config .Cfg .Telegram .Sleep )* len (inputMediaPhotos )) * time .Second )
81
65
}
82
66
return allMessages , nil
83
67
}
68
+
69
+ // start from 0
70
+ func getInputMediaPhotos (artwork * types.Artwork , storage storage.Storage , start , end int ) ([]telego.InputMedia , error ) {
71
+ inputMediaPhotos := make ([]telego.InputMedia , end - start )
72
+ for i := start ; i < end ; i ++ {
73
+ picture := artwork .Pictures [i ]
74
+ fileBytes , err := storage .GetFile (picture .StorageInfo )
75
+ if err != nil {
76
+ Logger .Errorf ("failed to get file: %s" , err )
77
+ return nil , err
78
+ }
79
+ fileBytes , err = common .CompressImage (fileBytes , 10 , 2560 )
80
+ if err != nil {
81
+ Logger .Errorf ("failed to compress image: %s" , err )
82
+ return nil , err
83
+ }
84
+ photo := telegoutil .MediaPhoto (telegoutil .File (telegoutil .NameReader (bytes .NewReader (fileBytes ), picture .StorageInfo .Path )))
85
+ if i == 0 {
86
+ photo = photo .WithCaption (GetArtworkHTMLCaption (artwork )).WithParseMode (telego .ModeHTML )
87
+ }
88
+ if artwork .R18 {
89
+ photo = photo .WithHasSpoiler ()
90
+ }
91
+ inputMediaPhotos [i - start ] = photo
92
+ fileBytes = nil
93
+ }
94
+ runtime .GC ()
95
+ return inputMediaPhotos , nil
96
+ }
0 commit comments