-
Notifications
You must be signed in to change notification settings - Fork 14
/
cache.go
170 lines (141 loc) · 4.04 KB
/
cache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package main
import (
"crypto/md5"
"database/sql"
"fmt"
tgbotapi "github.com/krol44/telegram-bot-api"
log "github.com/sirupsen/logrus"
"os"
"path"
"strings"
"time"
)
type Cache struct {
Task *Task
}
func (c Cache) Add(tgFileId string, tgFileSize int, nativeFilePath string) {
var md5Sum string
if file, err := os.ReadFile(nativeFilePath); err == nil {
md5Sum = fmt.Sprintf("%x", md5.Sum(file))
}
caption := strings.TrimSuffix(path.Base(nativeFilePath), path.Ext(path.Base(nativeFilePath)))
var urlHttp string
if c.Task.DescriptionUrl != "" {
urlHttp = "\n" + c.Task.DescriptionUrl
}
if _, isSlice := c.Task.GetTimeSlice(); isSlice {
nativeFilePath = ""
md5Sum = ""
c.Task.UrlIDForCache = "no"
}
_, err := Postgres.Exec(`INSERT INTO cache
(caption, native_path_file, native_md5_sum, video_url_id, tg_from_id, tg_file_id, tg_file_size, date_create)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)`,
caption+urlHttp, nativeFilePath, md5Sum, c.Task.UrlIDForCache, c.Task.Message.From.ID,
tgFileId, tgFileSize, time.Now())
if err != nil {
log.Error(err)
}
}
func (c Cache) TrySend(typeSome string, pathway string) bool {
var row CacheRow
err := Postgres.Get(&row,
"SELECT caption, tg_file_id, native_path_file FROM cache WHERE native_path_file = $1 ORDER BY id DESC",
pathway)
if err != nil {
return false
}
if typeSome == "video" {
sob := tgbotapi.NewVideo(c.Task.Message.Chat.ID, tgbotapi.FileID(row.TgFileID))
sob.Caption = row.Caption + signAdvt
if strings.Contains(row.NativePathFile, "torrent-client") {
sob.ProtectContent = true
}
_, err := c.Task.App.Bot.Send(sob)
if err != nil {
return false
}
c.Task.App.SendLogToChannel(c.Task.Message.From, "mess",
"video sent from cache - "+row.Caption)
}
if typeSome == "doc" {
sob := tgbotapi.NewDocument(c.Task.Message.Chat.ID, tgbotapi.FileID(row.TgFileID))
sob.Caption = row.Caption + signAdvt
if strings.Contains(row.NativePathFile, "torrent-client") {
sob.ProtectContent = true
}
_, err := c.Task.App.Bot.Send(sob)
if err != nil {
return false
}
c.Task.App.SendLogToChannel(c.Task.Message.From, "mess",
"doc sent from cache - "+row.Caption)
}
return true
}
func (c Cache) GetFileIdThroughMd5(NativeFilePath string) string {
var md5Sum string
if file, err := os.ReadFile(NativeFilePath); err == nil {
md5Sum = fmt.Sprintf("%x", md5.Sum(file))
}
if md5Sum == "" {
return ""
}
var row CacheRow
err := Postgres.Get(&row,
"SELECT tg_file_id FROM cache WHERE native_md5_sum = $1 ORDER BY id DESC", md5Sum)
if err != nil {
return ""
}
return row.TgFileID
}
func (c Cache) TrySendThroughMd5(NativeFilePath string) bool {
var md5Sum string
if file, err := os.ReadFile(NativeFilePath); err == nil {
md5Sum = fmt.Sprintf("%x", md5.Sum(file))
}
if md5Sum == "" {
return false
}
var row CacheRow
err := Postgres.Get(&row,
"SELECT caption, tg_file_id, native_path_file FROM cache WHERE native_md5_sum = $1 ORDER BY id DESC",
md5Sum)
if err != nil {
return false
}
sob := tgbotapi.NewVideo(c.Task.Message.Chat.ID, tgbotapi.FileID(row.TgFileID))
sob.Caption = row.Caption + signAdvt
if strings.Contains(row.NativePathFile, "torrent-client") {
sob.ProtectContent = true
}
_, err = c.Task.App.Bot.Send(sob)
if err != nil {
return false
}
c.Task.App.SendLogToChannel(c.Task.Message.From, "video",
"video sent from cache md5 - "+row.Caption, row.TgFileID)
return true
}
func (c Cache) TrySendThroughID() bool {
var row CacheRow
err := Postgres.Get(&row,
"SELECT caption, tg_file_id FROM cache WHERE video_url_id = $1 ORDER BY id DESC", c.Task.UrlIDForCache)
if err != nil && err != sql.ErrNoRows {
log.Error(err)
return false
}
if row.TgFileID == "" {
return false
}
sob := tgbotapi.NewVideo(c.Task.Message.Chat.ID, tgbotapi.FileID(row.TgFileID))
sob.Caption = row.Caption + signAdvt
_, err = c.Task.App.Bot.Send(sob)
if err != nil {
log.Error(err)
return false
}
c.Task.App.SendLogToChannel(c.Task.Message.From, "video",
"video sent from cache video url id - "+row.Caption, row.TgFileID)
return true
}