-
Notifications
You must be signed in to change notification settings - Fork 0
/
getfile.go
165 lines (143 loc) · 3.71 KB
/
getfile.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
package getfile
import (
"io"
"os"
"path"
"path/filepath"
"strings"
"github.com/Brawl345/gobot/logger"
"github.com/Brawl345/gobot/model"
"github.com/Brawl345/gobot/plugin"
"github.com/Brawl345/gobot/utils"
"gopkg.in/telebot.v3"
)
var log = logger.New("getfile")
type (
Plugin struct {
fileService Service
dir string
}
Service interface {
Create(uniqueID, fileName, mediaType string) error
Exists(uniqueID string) (bool, error)
}
)
func New(credentialService model.CredentialService, fileService Service) *Plugin {
dir, err := credentialService.GetKey("getfile_dir")
if err != nil {
dir = "tmp"
}
return &Plugin{
fileService: fileService,
dir: dir,
}
}
func (*Plugin) Name() string {
return "getfile"
}
func (p *Plugin) Commands() []telebot.Command {
return nil
}
func (p *Plugin) Handlers(*telebot.User) []plugin.Handler {
return []plugin.Handler{
&plugin.CommandHandler{
Trigger: telebot.OnMedia,
HandlerFunc: p.OnMedia,
HandleEdits: true,
},
}
}
func (p *Plugin) OnMedia(c plugin.GobotContext) error {
var fileID string
var uniqueID string
var subFolder string
var fileSize int64
if c.Message().Media() != nil {
fileID = c.Message().Media().MediaFile().FileID
fileSize = c.Message().Media().MediaFile().FileSize
uniqueID = c.Message().Media().MediaFile().UniqueID
subFolder = c.Message().Media().MediaType()
} else {
fileID = c.Message().Sticker.FileID
fileSize = c.Message().Sticker.FileSize
uniqueID = c.Message().Sticker.UniqueID
subFolder = c.Message().Sticker.MediaType()
}
if fileSize > utils.MaxFilesizeDownload {
log.Warn().Msgf("File is too big: %d", fileSize)
return nil
}
exists, err := p.fileService.Exists(uniqueID)
if err != nil {
log.Err(err).Msg("Error checking if file exists")
return nil
}
if exists {
log.Info().Msgf("File already exists: %s", uniqueID)
return nil
}
savePath := filepath.Join(p.dir, subFolder)
err = os.MkdirAll(savePath, 0770)
if err != nil {
log.Err(err).Msgf("Could not create directory: %s", savePath)
return nil
}
file := &telebot.File{FileID: fileID}
reader, err := c.Bot().File(file)
if err != nil {
return err
}
defer func(reader io.ReadCloser) {
err := reader.Close()
if err != nil {
log.Err(err).Msg("Error closing file")
}
}(reader)
var fileName string
if c.Message().Animation != nil && c.Message().Animation.FileName != "" {
fileName = uniqueID + "_" + c.Message().Animation.FileName
} else if c.Message().Audio != nil && c.Message().Audio.FileName != "" {
fileName = uniqueID + "_" + c.Message().Audio.FileName
} else if c.Message().Document != nil && c.Message().Document.FileName != "" {
fileName = uniqueID + "_" + c.Message().Document.FileName
} else if c.Message().Video != nil && c.Message().Video.FileName != "" {
fileName = uniqueID + "_" + c.Message().Video.FileName
} else {
fileName = path.Base(file.FilePath)
}
// Fix file endings
if c.Message().Sticker != nil &&
!c.Message().Sticker.Animated {
if !strings.HasSuffix(fileName, ".webp") && !strings.HasSuffix(fileName, ".webm") {
fileName += ".webp"
}
}
if c.Message().Voice != nil &&
!strings.HasSuffix(fileName, ".oga") {
fileName += ".oga"
}
if c.Message().VideoNote != nil &&
!strings.HasSuffix(fileName, ".mp4") {
fileName += ".mp4"
}
out, err := os.Create(filepath.Join(savePath, fileName))
if err != nil {
return err
}
defer func(out *os.File) {
err := out.Close()
if err != nil {
log.Err(err).Msg("Error closing file")
}
}(out)
_, err = io.Copy(out, reader)
if err != nil {
return err
}
log.Info().Msgf("Saved as: %s", filepath.Join(savePath, fileName))
err = p.fileService.Create(uniqueID, fileName, subFolder)
if err != nil {
return err
}
return nil
}