Skip to content

Commit 8165320

Browse files
committed
Create interface for file data.
1 parent 000cb2e commit 8165320

File tree

7 files changed

+266
-207
lines changed

7 files changed

+266
-207
lines changed

bot.go

Lines changed: 14 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
package tgbotapi
44

55
import (
6-
"bytes"
76
"encoding/json"
87
"errors"
98
"fmt"
@@ -12,7 +11,6 @@ import (
1211
"mime/multipart"
1312
"net/http"
1413
"net/url"
15-
"os"
1614
"strings"
1715
"time"
1816
)
@@ -180,54 +178,37 @@ func (bot *BotAPI) UploadFiles(endpoint string, params Params, files []RequestFi
180178
}
181179

182180
for _, file := range files {
183-
switch f := file.File.(type) {
184-
case string:
185-
fileHandle, err := os.Open(f)
181+
if file.Data.NeedsUpload() {
182+
name, reader, err := file.Data.UploadData()
186183
if err != nil {
187184
w.CloseWithError(err)
188185
return
189186
}
190-
defer fileHandle.Close()
191187

192-
part, err := m.CreateFormFile(file.Name, fileHandle.Name())
188+
part, err := m.CreateFormFile(file.Name, name)
193189
if err != nil {
194190
w.CloseWithError(err)
195191
return
196192
}
197193

198-
io.Copy(part, fileHandle)
199-
case FileBytes:
200-
part, err := m.CreateFormFile(file.Name, f.Name)
201-
if err != nil {
194+
if _, err := io.Copy(part, reader); err != nil {
202195
w.CloseWithError(err)
203196
return
204197
}
205198

206-
buf := bytes.NewBuffer(f.Bytes)
207-
io.Copy(part, buf)
208-
case FileReader:
209-
part, err := m.CreateFormFile(file.Name, f.Name)
210-
if err != nil {
211-
w.CloseWithError(err)
212-
return
199+
if closer, ok := reader.(io.ReadCloser); ok {
200+
if err = closer.Close(); err != nil {
201+
w.CloseWithError(err)
202+
return
203+
}
213204
}
205+
} else {
206+
value := file.Data.SendData()
214207

215-
io.Copy(part, f.Reader)
216-
case FileURL:
217-
val := string(f)
218-
if err := m.WriteField(file.Name, val); err != nil {
219-
w.CloseWithError(err)
220-
return
221-
}
222-
case FileID:
223-
val := string(f)
224-
if err := m.WriteField(file.Name, val); err != nil {
208+
if err := m.WriteField(file.Name, value); err != nil {
225209
w.CloseWithError(err)
226210
return
227211
}
228-
default:
229-
w.CloseWithError(errors.New(ErrBadFileType))
230-
return
231212
}
232213
}
233214
}()
@@ -316,8 +297,7 @@ func (bot *BotAPI) IsMessageToMe(message Message) bool {
316297

317298
func hasFilesNeedingUpload(files []RequestFile) bool {
318299
for _, file := range files {
319-
switch file.File.(type) {
320-
case string, FileBytes, FileReader:
300+
if file.Data.NeedsUpload() {
321301
return true
322302
}
323303
}
@@ -344,20 +324,7 @@ func (bot *BotAPI) Request(c Chattable) (*APIResponse, error) {
344324
// However, if there are no files to be uploaded, there's likely things
345325
// that need to be turned into params instead.
346326
for _, file := range files {
347-
var s string
348-
349-
switch f := file.File.(type) {
350-
case string:
351-
s = f
352-
case FileID:
353-
s = string(f)
354-
case FileURL:
355-
s = string(f)
356-
default:
357-
return nil, errors.New(ErrBadFileType)
358-
}
359-
360-
params[file.Name] = s
327+
params[file.Name] = file.Data.SendData()
361328
}
362329
}
363330

bot_test.go

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func TestCopyMessage(t *testing.T) {
127127
func TestSendWithNewPhoto(t *testing.T) {
128128
bot, _ := getBot(t)
129129

130-
msg := NewPhoto(ChatID, "tests/image.jpg")
130+
msg := NewPhoto(ChatID, FilePath("tests/image.jpg"))
131131
msg.Caption = "Test"
132132
_, err := bot.Send(msg)
133133

@@ -169,7 +169,7 @@ func TestSendWithNewPhotoWithFileReader(t *testing.T) {
169169
func TestSendWithNewPhotoReply(t *testing.T) {
170170
bot, _ := getBot(t)
171171

172-
msg := NewPhoto(ChatID, "tests/image.jpg")
172+
msg := NewPhoto(ChatID, FilePath("tests/image.jpg"))
173173
msg.ReplyToMessageID = ReplyToMessageID
174174

175175
_, err := bot.Send(msg)
@@ -182,7 +182,7 @@ func TestSendWithNewPhotoReply(t *testing.T) {
182182
func TestSendNewPhotoToChannel(t *testing.T) {
183183
bot, _ := getBot(t)
184184

185-
msg := NewPhotoToChannel(Channel, "tests/image.jpg")
185+
msg := NewPhotoToChannel(Channel, FilePath("tests/image.jpg"))
186186
msg.Caption = "Test"
187187
_, err := bot.Send(msg)
188188

@@ -239,7 +239,7 @@ func TestSendWithExistingPhoto(t *testing.T) {
239239
func TestSendWithNewDocument(t *testing.T) {
240240
bot, _ := getBot(t)
241241

242-
msg := NewDocument(ChatID, "tests/image.jpg")
242+
msg := NewDocument(ChatID, FilePath("tests/image.jpg"))
243243
_, err := bot.Send(msg)
244244

245245
if err != nil {
@@ -250,8 +250,8 @@ func TestSendWithNewDocument(t *testing.T) {
250250
func TestSendWithNewDocumentAndThumb(t *testing.T) {
251251
bot, _ := getBot(t)
252252

253-
msg := NewDocument(ChatID, "tests/voice.ogg")
254-
msg.Thumb = "tests/image.jpg"
253+
msg := NewDocument(ChatID, FilePath("tests/voice.ogg"))
254+
msg.Thumb = FilePath("tests/image.jpg")
255255
_, err := bot.Send(msg)
256256

257257
if err != nil {
@@ -273,7 +273,7 @@ func TestSendWithExistingDocument(t *testing.T) {
273273
func TestSendWithNewAudio(t *testing.T) {
274274
bot, _ := getBot(t)
275275

276-
msg := NewAudio(ChatID, "tests/audio.mp3")
276+
msg := NewAudio(ChatID, FilePath("tests/audio.mp3"))
277277
msg.Title = "TEST"
278278
msg.Duration = 10
279279
msg.Performer = "TEST"
@@ -302,7 +302,7 @@ func TestSendWithExistingAudio(t *testing.T) {
302302
func TestSendWithNewVoice(t *testing.T) {
303303
bot, _ := getBot(t)
304304

305-
msg := NewVoice(ChatID, "tests/voice.ogg")
305+
msg := NewVoice(ChatID, FilePath("tests/voice.ogg"))
306306
msg.Duration = 10
307307
_, err := bot.Send(msg)
308308

@@ -356,7 +356,7 @@ func TestSendWithVenue(t *testing.T) {
356356
func TestSendWithNewVideo(t *testing.T) {
357357
bot, _ := getBot(t)
358358

359-
msg := NewVideo(ChatID, "tests/video.mp4")
359+
msg := NewVideo(ChatID, FilePath("tests/video.mp4"))
360360
msg.Duration = 10
361361
msg.Caption = "TEST"
362362

@@ -384,7 +384,7 @@ func TestSendWithExistingVideo(t *testing.T) {
384384
func TestSendWithNewVideoNote(t *testing.T) {
385385
bot, _ := getBot(t)
386386

387-
msg := NewVideoNote(ChatID, 240, "tests/videonote.mp4")
387+
msg := NewVideoNote(ChatID, 240, FilePath("tests/videonote.mp4"))
388388
msg.Duration = 10
389389

390390
_, err := bot.Send(msg)
@@ -410,7 +410,7 @@ func TestSendWithExistingVideoNote(t *testing.T) {
410410
func TestSendWithNewSticker(t *testing.T) {
411411
bot, _ := getBot(t)
412412

413-
msg := NewSticker(ChatID, "tests/image.jpg")
413+
msg := NewSticker(ChatID, FilePath("tests/image.jpg"))
414414

415415
_, err := bot.Send(msg)
416416

@@ -434,7 +434,7 @@ func TestSendWithExistingSticker(t *testing.T) {
434434
func TestSendWithNewStickerAndKeyboardHide(t *testing.T) {
435435
bot, _ := getBot(t)
436436

437-
msg := NewSticker(ChatID, "tests/image.jpg")
437+
msg := NewSticker(ChatID, FilePath("tests/image.jpg"))
438438
msg.ReplyMarkup = ReplyKeyboardRemove{
439439
RemoveKeyboard: true,
440440
Selective: false,
@@ -550,7 +550,7 @@ func TestSetWebhookWithCert(t *testing.T) {
550550

551551
bot.Request(DeleteWebhookConfig{})
552552

553-
wh, err := NewWebhookWithCert("https://example.com/tgbotapi-test/"+bot.Token, "tests/cert.pem")
553+
wh, err := NewWebhookWithCert("https://example.com/tgbotapi-test/"+bot.Token, FilePath("tests/cert.pem"))
554554

555555
if err != nil {
556556
t.Error(err)
@@ -609,8 +609,8 @@ func TestSendWithMediaGroupPhotoVideo(t *testing.T) {
609609

610610
cfg := NewMediaGroup(ChatID, []interface{}{
611611
NewInputMediaPhoto(FileURL("https://github.com/go-telegram-bot-api/telegram-bot-api/raw/0a3a1c8716c4cd8d26a262af9f12dcbab7f3f28c/tests/image.jpg")),
612-
NewInputMediaPhoto("tests/image.jpg"),
613-
NewInputMediaVideo("tests/video.mp4"),
612+
NewInputMediaPhoto(FilePath("tests/image.jpg")),
613+
NewInputMediaVideo(FilePath("tests/video.mp4")),
614614
})
615615

616616
messages, err := bot.SendMediaGroup(cfg)
@@ -632,7 +632,7 @@ func TestSendWithMediaGroupDocument(t *testing.T) {
632632

633633
cfg := NewMediaGroup(ChatID, []interface{}{
634634
NewInputMediaDocument(FileURL("https://i.imgur.com/unQLJIb.jpg")),
635-
NewInputMediaDocument("tests/image.jpg"),
635+
NewInputMediaDocument(FilePath("tests/image.jpg")),
636636
})
637637

638638
messages, err := bot.SendMediaGroup(cfg)
@@ -653,8 +653,8 @@ func TestSendWithMediaGroupAudio(t *testing.T) {
653653
bot, _ := getBot(t)
654654

655655
cfg := NewMediaGroup(ChatID, []interface{}{
656-
NewInputMediaAudio("tests/audio.mp3"),
657-
NewInputMediaAudio("tests/audio.mp3"),
656+
NewInputMediaAudio(FilePath("tests/audio.mp3")),
657+
NewInputMediaAudio(FilePath("tests/audio.mp3")),
658658
})
659659

660660
messages, err := bot.SendMediaGroup(cfg)
@@ -715,7 +715,7 @@ func ExampleNewWebhook() {
715715

716716
log.Printf("Authorized on account %s", bot.Self.UserName)
717717

718-
wh, err := NewWebhookWithCert("https://www.google.com:8443/"+bot.Token, "cert.pem")
718+
wh, err := NewWebhookWithCert("https://www.google.com:8443/"+bot.Token, FilePath("cert.pem"))
719719

720720
if err != nil {
721721
panic(err)
@@ -755,7 +755,7 @@ func ExampleWebhookHandler() {
755755

756756
log.Printf("Authorized on account %s", bot.Self.UserName)
757757

758-
wh, err := NewWebhookWithCert("https://www.google.com:8443/"+bot.Token, "cert.pem")
758+
wh, err := NewWebhookWithCert("https://www.google.com:8443/"+bot.Token, FilePath("cert.pem"))
759759

760760
if err != nil {
761761
panic(err)
@@ -1004,7 +1004,7 @@ func TestCommands(t *testing.T) {
10041004
func TestEditMessageMedia(t *testing.T) {
10051005
bot, _ := getBot(t)
10061006

1007-
msg := NewPhoto(ChatID, "tests/image.jpg")
1007+
msg := NewPhoto(ChatID, FilePath("tests/image.jpg"))
10081008
msg.Caption = "Test"
10091009
m, err := bot.Send(msg)
10101010

@@ -1017,7 +1017,7 @@ func TestEditMessageMedia(t *testing.T) {
10171017
ChatID: ChatID,
10181018
MessageID: m.MessageID,
10191019
},
1020-
Media: NewInputMediaVideo("tests/video.mp4"),
1020+
Media: NewInputMediaVideo(FilePath("tests/video.mp4")),
10211021
}
10221022

10231023
_, err = bot.Request(edit)
@@ -1028,17 +1028,17 @@ func TestEditMessageMedia(t *testing.T) {
10281028

10291029
func TestPrepareInputMediaForParams(t *testing.T) {
10301030
media := []interface{}{
1031-
NewInputMediaPhoto("tests/image.jpg"),
1031+
NewInputMediaPhoto(FilePath("tests/image.jpg")),
10321032
NewInputMediaVideo(FileID("test")),
10331033
}
10341034

10351035
prepared := prepareInputMediaForParams(media)
10361036

1037-
if media[0].(InputMediaPhoto).Media != "tests/image.jpg" {
1037+
if media[0].(InputMediaPhoto).Media != FilePath("tests/image.jpg") {
10381038
t.Error("Original media was changed")
10391039
}
10401040

1041-
if prepared[0].(InputMediaPhoto).Media != "attach://file-0" {
1041+
if prepared[0].(InputMediaPhoto).Media != FileID("attach://file-0") {
10421042
t.Error("New media was not replaced")
10431043
}
10441044

0 commit comments

Comments
 (0)