forked from EverythingSuckz/TG-FileStreamBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload.go
More file actions
59 lines (49 loc) · 1.74 KB
/
upload.go
File metadata and controls
59 lines (49 loc) · 1.74 KB
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
package uploader
import (
"EverythingSuckz/fsb/config"
"EverythingSuckz/fsb/internal/bot"
"EverythingSuckz/fsb/internal/utils"
"EverythingSuckz/fsb/pkg/drive115"
"fmt"
"github.com/celestix/gotgproto/dispatcher"
"github.com/celestix/gotgproto/dispatcher/handlers"
"github.com/celestix/gotgproto/ext"
)
import "go.uber.org/zap"
func Load(log *zap.Logger, dispatcher dispatcher.Dispatcher) {
log = log.Named("uploader")
defer log.Info("Initialized uploader command handlers")
dispatcher.AddHandler(
handlers.NewCommand("upload", upload),
)
}
func upload(ctx *ext.Context, u *ext.Update) error {
if u.EffectiveMessage.ReplyToMessage == nil {
ctx.Reply(u, "Reply to a message to upload.", nil)
return dispatcher.EndGroups
}
driveClient := drive115.NewClient(config.ValueOf.Drive115Cookie)
uploadInfo, err := driveClient.GetUploadInfo()
if err != nil {
ctx.Reply(u, fmt.Sprintf("Failed to get upload info: %s", err), nil)
return dispatcher.EndGroups
}
worker := bot.GetNextWorker()
file, err := utils.FileFromMessage(ctx, worker.Client, u.EffectiveMessage.ReplyToMessage.GetID())
if err != nil {
ctx.Reply(u, fmt.Sprintf("Failed to get file from message: %s", err), nil)
return dispatcher.EndGroups
}
reader, err := utils.NewTelegramReader(ctx, worker.Client, file.Location, 0, file.FileSize-1, file.FileSize)
if err != nil {
ctx.Reply(u, fmt.Sprintf("Failed to create telegram reader: %s", err), nil)
return dispatcher.EndGroups
}
result, err := driveClient.Upload(reader, uploadInfo.UploadURL)
if err != nil {
ctx.Reply(u, fmt.Sprintf("Failed to upload file: %s", err), nil)
return dispatcher.EndGroups
}
ctx.Reply(u, fmt.Sprintf("File uploaded successfully. File ID: %s", result.FileID), nil)
return dispatcher.EndGroups
}