Skip to content

Commit e9888df

Browse files
feat(bot): add support for images (#163)
2 parents a8dceaf + e90279b commit e9888df

File tree

6 files changed

+60
-19
lines changed

6 files changed

+60
-19
lines changed

internal/cache/cache.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ func InitCache(log *zap.Logger) {
2323
log = log.Named("cache")
2424
gob.Register(types.File{})
2525
gob.Register(tg.InputDocumentFileLocation{})
26+
gob.Register(tg.InputPhotoFileLocation{})
2627
defer log.Sugar().Info("Initialized")
2728
cache = &Cache{cache: freecache.NewCache(10 * 1024 * 1024), log: log}
2829
}

internal/commands/stream.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func supportedMediaFilter(m *types.Message) (bool, error) {
3232
case *tg.MessageMediaDocument:
3333
return true, nil
3434
case *tg.MessageMediaPhoto:
35-
return false, nil
35+
return true, nil
3636
case tg.MessageMediaClass:
3737
return false, dispatcher.EndGroups
3838
default:
@@ -65,11 +65,6 @@ func sendLink(ctx *ext.Context, u *ext.Update) error {
6565
return dispatcher.EndGroups
6666
}
6767
messageID := update.Updates[0].(*tg.UpdateMessageID).ID
68-
if err != nil {
69-
utils.Logger.Sugar().Error(err)
70-
ctx.Reply(u, fmt.Sprintf("Error - %s", err.Error()), nil)
71-
return dispatcher.EndGroups
72-
}
7368
doc := update.Updates[1].(*tg.UpdateNewChannelMessage).Message.(*tg.Message).Media
7469
file, err := utils.FileFromMedia(doc)
7570
if err != nil {

internal/routes/stream.go

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"net/http"
99
"strconv"
1010

11+
"github.com/gotd/td/tg"
1112
range_parser "github.com/quantumsheep/range-parser"
1213
"go.uber.org/zap"
1314

@@ -39,10 +40,6 @@ func getStreamRoute(ctx *gin.Context) {
3940
return
4041
}
4142

42-
ctx.Header("Accept-Ranges", "bytes")
43-
var start, end int64
44-
rangeHeader := r.Header.Get("Range")
45-
4643
worker := bot.GetNextWorker()
4744

4845
file, err := utils.FileFromMessage(ctx, worker.Client, messageID)
@@ -62,6 +59,34 @@ func getStreamRoute(ctx *gin.Context) {
6259
return
6360
}
6461

62+
// for photo messages
63+
if file.FileSize == 0 {
64+
res, err := worker.Client.API().UploadGetFile(ctx, &tg.UploadGetFileRequest{
65+
Location: file.Location,
66+
Offset: 0,
67+
Limit: 1024 * 1024,
68+
})
69+
if err != nil {
70+
http.Error(w, err.Error(), http.StatusInternalServerError)
71+
return
72+
}
73+
result, ok := res.(*tg.UploadFile)
74+
if !ok {
75+
http.Error(w, "unexpected response", http.StatusInternalServerError)
76+
return
77+
}
78+
fileBytes := result.GetBytes()
79+
ctx.Header("Content-Disposition", fmt.Sprintf("inline; filename=\"%s\"", file.FileName))
80+
if r.Method != "HEAD" {
81+
ctx.Data(http.StatusOK, file.MimeType, fileBytes)
82+
}
83+
return
84+
}
85+
86+
ctx.Header("Accept-Ranges", "bytes")
87+
var start, end int64
88+
rangeHeader := r.Header.Get("Range")
89+
6590
if rangeHeader == "" {
6691
start = 0
6792
end = file.FileSize - 1
@@ -98,10 +123,6 @@ func getStreamRoute(ctx *gin.Context) {
98123
ctx.Header("Content-Disposition", fmt.Sprintf("%s; filename=\"%s\"", disposition, file.FileName))
99124

100125
if r.Method != "HEAD" {
101-
if err != nil {
102-
http.Error(w, err.Error(), http.StatusInternalServerError)
103-
return
104-
}
105126
lr, _ := utils.NewTelegramReader(ctx, worker.Client, file.Location, start, end, contentLength)
106127
if _, err := io.CopyN(w, lr, contentLength); err != nil {
107128
log.Error("Error while copying stream", zap.Error(err))

internal/types/file.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
)
1111

1212
type File struct {
13-
Location *tg.InputDocumentFileLocation
13+
Location tg.InputFileLocationClass
1414
FileSize int64
1515
FileName string
1616
MimeType string

internal/utils/helpers.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,32 @@ func FileFromMedia(media tg.MessageMediaClass) (*types.File, error) {
6767
MimeType: document.MimeType,
6868
ID: document.ID,
6969
}, nil
70-
// TODO: add photo support
70+
case *tg.MessageMediaPhoto:
71+
photo, ok := media.Photo.AsNotEmpty()
72+
if !ok {
73+
return nil, fmt.Errorf("unexpected type %T", media)
74+
}
75+
sizes := photo.Sizes
76+
if len(sizes) == 0 {
77+
return nil, errors.New("photo has no sizes")
78+
}
79+
photoSize := sizes[len(sizes)-1]
80+
size, ok := photoSize.AsNotEmpty()
81+
if !ok {
82+
return nil, errors.New("photo size is empty")
83+
}
84+
location := new(tg.InputPhotoFileLocation)
85+
location.ID = photo.GetID()
86+
location.AccessHash = photo.GetAccessHash()
87+
location.FileReference = photo.GetFileReference()
88+
location.ThumbSize = size.GetType()
89+
return &types.File{
90+
Location: location,
91+
FileSize: 0, // caller should judge if this is a photo or not
92+
FileName: fmt.Sprintf("photo_%d.jpg", photo.GetID()),
93+
MimeType: "image/jpeg",
94+
ID: photo.GetID(),
95+
}, nil
7196
}
7297
return nil, fmt.Errorf("unexpected type %T", media)
7398
}
@@ -99,7 +124,6 @@ func FileFromMessage(ctx context.Context, client *gotgproto.Client, messageID in
99124
return nil, err
100125
}
101126
return file, nil
102-
// TODO: add photo support
103127
}
104128

105129
func GetLogChannelPeer(ctx context.Context, api *tg.Client, peerStorage *storage.PeerStorage) (*tg.InputChannel, error) {

internal/utils/reader.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ type telegramReader struct {
1414
ctx context.Context
1515
log *zap.Logger
1616
client *gotgproto.Client
17-
location *tg.InputDocumentFileLocation
17+
location tg.InputFileLocationClass
1818
start int64
1919
end int64
2020
next func() ([]byte, error)
@@ -32,7 +32,7 @@ func (*telegramReader) Close() error {
3232
func NewTelegramReader(
3333
ctx context.Context,
3434
client *gotgproto.Client,
35-
location *tg.InputDocumentFileLocation,
35+
location tg.InputFileLocationClass,
3636
start int64,
3737
end int64,
3838
contentLength int64,

0 commit comments

Comments
 (0)