mirror of
https://github.com/tgdrive/teldrive.git
synced 2024-09-20 08:15:55 +08:00
start bots in background
This commit is contained in:
parent
7b3220732e
commit
3683c17c3c
|
@ -138,29 +138,20 @@ func rangedParts(parts []types.Part, start, end int64) []types.Part {
|
|||
func GetChannelById(ctx context.Context, client *telegram.Client, channelID int64, userID string) (*tg.InputChannel, error) {
|
||||
|
||||
channel := &tg.InputChannel{}
|
||||
|
||||
key := kv.Key("channels", strconv.FormatInt(channelID, 10), userID)
|
||||
|
||||
err := kv.GetValue(database.KV, key, channel)
|
||||
inputChannel := &tg.InputChannel{
|
||||
ChannelID: channelID,
|
||||
}
|
||||
channels, err := client.API().ChannelsGetChannels(ctx, []tg.InputChannelClass{inputChannel})
|
||||
|
||||
if err != nil {
|
||||
inputChannel := &tg.InputChannel{
|
||||
ChannelID: channelID,
|
||||
}
|
||||
channels, err := client.API().ChannelsGetChannels(ctx, []tg.InputChannelClass{inputChannel})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(channels.GetChats()) == 0 {
|
||||
return nil, errors.New("no channels found")
|
||||
}
|
||||
|
||||
channel = channels.GetChats()[0].(*tg.Channel).AsInput()
|
||||
|
||||
kv.SetValue(database.KV, key, channel)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(channels.GetChats()) == 0 {
|
||||
return nil, errors.New("no channels found")
|
||||
}
|
||||
|
||||
channel = channels.GetChats()[0].(*tg.Channel).AsInput()
|
||||
return channel, nil
|
||||
}
|
||||
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
@ -21,7 +20,6 @@ import (
|
|||
"github.com/divyam234/teldrive/utils/md5"
|
||||
"github.com/divyam234/teldrive/utils/reader"
|
||||
"github.com/divyam234/teldrive/utils/tgc"
|
||||
"github.com/gotd/td/telegram"
|
||||
|
||||
"github.com/divyam234/teldrive/types"
|
||||
|
||||
|
@ -413,32 +411,37 @@ func (fs *FileService) GetFileStream(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
var client *telegram.Client
|
||||
if len(tokens) == 0 {
|
||||
http.Error(w, "bots not found", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
var token string
|
||||
var channelUser string
|
||||
|
||||
if len(tokens) == 0 {
|
||||
client, _ = tgc.UserLogin(jwtUser.TgSession)
|
||||
channelUser = jwtUser.Subject
|
||||
} else {
|
||||
tgc.Workers.Set(tokens)
|
||||
token = tgc.Workers.Next()
|
||||
client, _ = tgc.BotLogin(token)
|
||||
channelUser = strings.Split(token, ":")[0]
|
||||
limit := utils.Min(len(tokens), 10)
|
||||
|
||||
tgc.StreamWorkers.Set(tokens[:limit])
|
||||
|
||||
client, err := tgc.StreamWorkers.Next()
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
channelUser = strings.Split(token, ":")[0]
|
||||
|
||||
if r.Method != "HEAD" {
|
||||
tgc.RunWithAuth(c, client, token, func(ctx context.Context) error {
|
||||
parts, err := getParts(c, client, file, channelUser)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
parts = rangedParts(parts, start, end)
|
||||
r, _ := reader.NewLinearReader(c, client, parts)
|
||||
io.CopyN(w, r, contentLength)
|
||||
return nil
|
||||
})
|
||||
|
||||
parts, err := getParts(c, client.Tg, file, channelUser)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
parts = rangedParts(parts, start, end)
|
||||
r, _ := reader.NewLinearReader(c, client.Tg, parts)
|
||||
io.CopyN(w, r, contentLength)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -21,6 +21,13 @@ func Max[T constraints.Ordered](a, b T) T {
|
|||
return b
|
||||
}
|
||||
|
||||
func Min[T constraints.Ordered](a, b T) T {
|
||||
if a > b {
|
||||
return b
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
func CamelToPascalCase(input string) string {
|
||||
var result strings.Builder
|
||||
upperNext := true
|
||||
|
|
|
@ -2,6 +2,9 @@ package tgc
|
|||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/gotd/contrib/bg"
|
||||
"github.com/gotd/td/telegram"
|
||||
)
|
||||
|
||||
type BotWorkers struct {
|
||||
|
@ -27,3 +30,45 @@ func (w *BotWorkers) Next() string {
|
|||
}
|
||||
|
||||
var Workers = &BotWorkers{}
|
||||
|
||||
type Client struct {
|
||||
Tg *telegram.Client
|
||||
Stop bg.StopFunc
|
||||
Status string
|
||||
}
|
||||
|
||||
type streamWorkers struct {
|
||||
sync.Mutex
|
||||
bots []string
|
||||
clients []*Client
|
||||
index int
|
||||
}
|
||||
|
||||
func (w *streamWorkers) Set(bots []string) {
|
||||
w.Lock()
|
||||
defer w.Unlock()
|
||||
if len(w.clients) == 0 {
|
||||
w.bots = bots
|
||||
for _, token := range bots {
|
||||
client, _ := BotLogin(token)
|
||||
w.clients = append(w.clients, &Client{Tg: client, Status: "idle"})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (w *streamWorkers) Next() (*Client, error) {
|
||||
w.Lock()
|
||||
defer w.Unlock()
|
||||
w.index = (w.index + 1) % len(w.clients)
|
||||
if w.clients[w.index].Status == "idle" {
|
||||
stop, err := bg.Connect(w.clients[w.index].Tg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
w.clients[w.index].Stop = stop
|
||||
w.clients[w.index].Status = "running"
|
||||
}
|
||||
return w.clients[w.index], nil
|
||||
}
|
||||
|
||||
var StreamWorkers = &streamWorkers{}
|
||||
|
|
Loading…
Reference in a new issue